The spheres produced by the threeBSP method exhibit a lack of smoothness

Is there a way to smooth out the sphere when I reach the segment limit of 45? Despite trying to change the number of segments, if I hit the cap of 45, cutting it becomes difficult and other methods seem unavailable for creating a smoother sphere.

var result;
var sphereG = new THREE.SphereGeometry( 115, 45, 45 );
var sphereM = new THREE.MeshPhongMaterial({color:"#fff"} );
var sphere = new THREE.Mesh(sphereG,sphereM);
var polyhedronG = new THREE.Geometry();
polyhedronG.vertices.push(
    new THREE.Vector3(-200,200,-200),   //A 0
    new THREE.Vector3(-200,-200,200),   //B 1
    new THREE.Vector3(200,-200,-200),   //D 2
    new THREE.Vector3(-1,-1,-1)         //O 3
);
polyhedronG.faces.push( 
   new THREE.Face3( 0, 1, 2 ),
   new THREE.Face3( 0, 2, 3 ),
   new THREE.Face3( 0, 3, 1 ),
   new THREE.Face3( 3, 2, 1 )
);
var polyhedronM = new THREE.MeshPhongMaterial( {
    color:"#E8FBFF",
    side:THREE.DoubleSide,
    transparent:true,
    opacity:0.1
});
var polyhedron  = new THREE.Mesh(polyhedronG,polyhedronM);
var boxBSP = new ThreeBSP(polyhedron);
var sphereBSP = new ThreeBSP(sphere);
var resultBSP = sphereBSP.subtract(boxBSP);              
result = resultBSP.toMesh();
result.material=new THREE.MeshPhongMaterial({color:'#fff'});

Answer №1

computeVertexNormals() function can achieve the desired outcome:

result = resultBSP.toMesh();
result.material=new THREE.MeshPhongMaterial({color:'#fff'});
result.geometry.computeVertexNormals();

Refer to the following code snippet:

On the right mesh, computeVertexNormals() was invoked while on the left mesh it was not.

https://i.sstatic.net/K89Mz.png

(function onLoad() {
  var container, camera, scene, renderer, controls;
  
  init();
  animate();

  function createModel() {

    var sphereG = new THREE.SphereGeometry( 115, 45, 45 );
    var sphere = new THREE.Mesh(sphereG);
    var polyhedronG = new THREE.Geometry();
    polyhedronG.vertices.push(
        new THREE.Vector3(-200,200,-200),   //A 0
        new THREE.Vector3(-200,-200,200),   //B 1
        new THREE.Vector3(200,-200,-200),   //D 2
        new THREE.Vector3(-1,-1,-1)         //O 3
    );
    polyhedronG.faces.push( 
      new THREE.Face3( 0, 1, 2 ),
      new THREE.Face3( 0, 2, 3 ),
      new THREE.Face3( 0, 3, 1 ),
      new THREE.Face3( 3, 2, 1 )
    );
    var polyhedronM = new THREE.MeshPhongMaterial( {
        color:"#E8FBFF",
        side:THREE.DoubleSide,
        transparent:true,
        opacity:0.1
    });
    var polyhedron  = new THREE.Mesh(polyhedronG,polyhedronM);
    var boxBSP = new ThreeBSP(polyhedron);
    var sphereBSP = new ThreeBSP(sphere);
    
    var resultBSP1 = sphereBSP.subtract(boxBSP);
    var resultMesh1 = resultBSP1.toMesh();
    resultMesh1.material=new THREE.MeshPhongMaterial({color:'#ff8080'});
    resultMesh1.position.x = 100

    var resultBSP2 = sphereBSP.subtract(boxBSP); 
    var resultMesh2 = resultBSP2.toMesh();
    resultMesh2.material=new THREE.MeshPhongMaterial({color:'#ff8080'});
    resultMesh2.position.x = -100
    resultMesh2.geometry.computeVertexNormals();
    
    scene.add(resultMesh1);
    scene.add(resultMesh2);
  }


  function init() {
    container = document.getElementById('container');
    
    renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMap.enabled = true;
    container.appendChild(renderer.domElement);

    scene = new THREE.Scene();
    scene.background = new THREE.Color(0xffffff);
    
    camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.set(0, -400, -150);
    scene.add(camera);
    resize();
    window.onresize = resize;
    
    var ambientLight = new THREE.AmbientLight(0x404040);
    scene.add(ambientLight);

    var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
    directionalLight.position.x = -1;
    directionalLight.position.y = 0;
    directionalLight.position.z = -2;
    scene.add( directionalLight );

    controls = new THREE.OrbitControls(camera, renderer.domElement);
        
    createModel();
  }

  function resize() {
    renderer.setSize(window.innerWidth, window.innerHeight);
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
  }

  function animate() {
    requestAnimationFrame(animate);
    render();
  }

  function render() {
    renderer.render(scene, camera);
  }
})();
<script src="https://rawcdn.githack.com/mrdoob/three.js/r124/build/three.js"></script>
<script src="https://rawcdn.githack.com/mrdoob/three.js/r124/examples/js/controls/OrbitControls.js"></script>
<script src="https://rawgit.com/Wilt/ThreeCSG/develop/ThreeCSG.js"></script>

<div id="container"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to Retrieve a Variable from the Parent Component in a Child Component using Angular (1.5) JS

I am currently working on abstracting the concept of a ticket list building into an angular application using 2 components. 1st component --> ("Smart Component") utilizes $http to fetch data and populate an array called populatedList within the parent ...

What is the best way to access the local Express server that is located in the same directory as the frontend project

This is the structure of my project - backend > node_modules > .env package-lock.json package.json server.js frontend > index.html style.css script.js server.js import dotenv from 'dotenv'; dotenv.config(); import express from &apo ...

PHP move_uploaded_file() function encountering an issue

As a newcomer to PHP, I am exploring the move_uploaded_file() function. I initiate an HTTP POST request from JavaScript/jQuery to a PHP file hosted on a Microsoft Azure server. The request includes an audio blob like so... mediaRecorder.ondataavailab ...

Extract the body.req object within a loop in a Node.js application

I'm looking to efficiently parse and save the body of a POST request using Mongoose in Node.js. Is there a way to use a for loop to accomplish this task, rather than manually saving every property? My ideal solution would involve something like: for ...

JavaScript - retrieve only the domain from the document.referrer

I am trying to extract only the domain from referrer URLs. Two examples of referrer URLs I encounter frequently are http://www.davidj.com/pages/flyer.asp and http://www.ronniej.com/linkdes.com/?adv=267&loc=897 Whenever I come across URLs like the ones ...

The createReadStream function cannot be found in the uploaded image

I am currently using node v14.17.0, "apollo-server-express": "^2.25.0", "graphql-upload": "^12.0.0" I'm facing an issue with uploading an image as I don't receive the createReadStream from the image that I upload via graphiql. Specifically, I am ...

List of random items:1. Red pen2

By using JavaScript, I want to generate a paragraph that contains a randomly ordered list of items (presented in paragraph format) for a basic website. The input would include: apples concrete a limited dose of contentment North Carolina The final ...

What causes the Invariant Violation: Invariant Violation: Maximum update depth exceeded error to occur when using setState()?

Looking for some help with this component: // here are the necessary imports export default class TabViewExample extends Component { state = { index: 0, routes: [ { key: 'first', title: 'Drop-Off', selected: true }, ...

Unable to employ pixelRatio for canvas within react three fiber

I'm working on a 3D model using react-three-fiber and I need to make it responsive. Unfortunately, the model appears blurry on smaller screens so I tried using pixelRatio = {window.devicePixelRatio}. However, I encountered an issue as the prop pixelRa ...

What are the steps to enable ajax communication with a database for posting, retrieving, and removing data?

Is there a way to utilize ajax for posting, deleting, and getting data from a database? I am looking to post content and then be able to delete it through the following link: (this is part of an assignment) However, I must use /ajax/addrecord.php and /a ...

Tips for sending an object from ng-repeat to a ng-include generated controller?

I am facing an issue with my ng-repeat and ng-include implementation. I want to include a separate controller inside the view loaded by ng-include, but I am struggling to access the item from ng-repeat in that controller. Here is what I have tried so far: ...

Vue.js navigation guards, restrict access to all unauthorized routes, grant entry to specific routes upon successful authentication

I'm currently working on implementing a navigation guard in Vue.js with a specific logic: I want to restrict access to all routes that require authentication and redirect users to the login page if they are not authenticated. The only exception is the ...

Is it necessary to reset the unordered list after adding it to the scrollbox?

I am facing an issue with my <ul> element that has overflow:auto; set. I want to dynamically add new <li> elements using the append(); method, but I can't seem to automatically scroll the box to the bottom after adding a new <li>: H ...

Replicating a website to use at a later time

I am brand new to the world of Javascript, currently diving into JavaScript and jQuery for an issue I'm facing. Here's what I'm trying to accomplish: I have a selection of words ("foo", "bar") Retrieve the current webpage's content ( ...

React - Issue with state not being updated accurately

My current project involves using the <Select> component from Material-ui to create a drop-down menu. I need to update the state of the <Select> component after a selection has been made. To achieve this, I have set the value property of the & ...

Component encounters issue with undefined props being passed

Encountering an issue where dummy data is not being passed to a component, resulting in undefined props. Even console.log() statements within the same page are not functioning properly. Can anyone identify what might be going wrong here? import AllPosts fr ...

Scripting issues detected in Safari and Microsoft Edge browsers

I recently finished developing an AngularJs Application that works flawlessly on Google Chrome and Mozilla Firefox. However, upon testing it on Safari and IE-11, I encountered errors in the console. One particular piece of code that causes issues is used ...

Having trouble with loading images from the assets folder, keep encountering a 304 error

While attempting to load a PNG file from the assets folder, I encountered a 304 error. My goal is to load images from the assets folder. const path = require('path'); const express = require('express'); const webpack = require('we ...

"Need to refresh localStorage in VueJS after navigating back with this.$router.go(-1)? Here's how

Below is my Login.vue code: mounted() { if (localStorage.login) this.$router.go(-1); }, methods: { axios.post(ApiUrl + "/login") { ... } then(response => { ... localStorage.login = true; this.$router.go(0); ...

Having difficulty refreshing the JGrid with updated content

I am currently utilizing JQuery and JGrid in conjunction with Java. I have a grid set up <script> jQuery("#table1").jqGrid({ .............. ............ }); Additionally, I have another grid function abc { va ...