使用threeBSP方法后,创建的球体不平滑

时间:2017-12-29 02:03:58

标签: three.js csg

image

我正在尝试更改球体的片段数量,但如果达到45限制,我就无法切割它,并且没有其他方法可以使球体更平滑。

ACH

1 个答案:

答案 0 :(得分:1)

computeVertexNormals()应该做你想做的事:

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

请参阅代码段:

对于正确的网格computeVertexNormals()被调用,而左网格则没有被调用。

(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://threejs.org/build/three.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script src="https://rawgit.com/Wilt/ThreeCSG/develop/ThreeCSG.js"></script>

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