使用threeBSP方法后,创建的球体不光滑 [英] After using the threeBSP method, the created spheres are not smooth

查看:38
本文介绍了使用threeBSP方法后,创建的球体不光滑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

var 结果;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 0new THREE.Vector3(-200,-200,200),//B 1new THREE.Vector3(200,-200,-200),//D 2new THREE.Vector3(-1,-1,-1)//O 3);polyhedronG.faces.push(新三.Face3( 0, 1, 2 ),新三.Face3( 0, 2, 3 ),新三.Face3( 0, 3, 1 ),新三.Face3( 3, 2, 1 ));var polyhedronM = new THREE.MeshPhongMaterial( {颜色:#E8FBFF",侧面:三.双面,透明:真实,不透明度:0.1});var polyhedron = new THREE.Mesh(polyhedronG,polyhedronM);var boxBSP = new ThreeBSP(polyhedron);var sphereBSP = new ThreeBSP(sphere);var resultBSP = sphereBSP.subtract(boxBSP);结果 = resultBSP.toMesh();result.material=new THREE.MeshPhongMaterial({color:'#fff'});

解决方案

(function onLoad() {var 容器、相机、场景、渲染器、控件;在里面();动画();函数创建模型(){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 0new THREE.Vector3(-200,-200,200),//B 1new THREE.Vector3(200,-200,-200),//D 2new THREE.Vector3(-1,-1,-1)//O 3);polyhedronG.faces.push(新三.Face3( 0, 1, 2 ),新三.Face3( 0, 2, 3 ),新三.Face3( 0, 3, 1 ),新三.Face3( 3, 2, 1 ));var polyhedronM = new THREE.MeshPhongMaterial( {颜色:#E8FBFF",侧面:三.双面,透明:真实,不透明度: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 = 100var resultBSP2 = sphereBSP.subtract(boxBSP);var resultMesh2 = resultBSP2.toMesh();resultMesh2.material=new THREE.MeshPhongMaterial({color:'#ff8080'});resultMesh2.position.x = -100resultMesh2.geometry.computeVertexNormals();场景添加(resultMesh1);场景添加(resultMesh2);}函数初始化(){container = document.getElementById('container');渲染器 = 新的 THREE.WebGLRenderer({抗锯齿:真});renderer.setPixelRatio(window.devicePixelRatio);renderer.setSize(window.innerWidth, window.innerHeight);renderer.shadowMap.enabled = true;container.appendChild(renderer.domElement);场景 = 新的 THREE.Scene();场景背景 = 新三色(0xffffff);camera = new THREE.PerspectiveCamera(70, window.innerWidth/window.innerHeight, 1, 10000);相机位置设置(0,-400,-150);场景.添加(相机);调整大小();window.onresize = 调整大小;varambientLight = new THREE.AmbientLight(0x404040);场景.添加(环境光);var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );directionalLight.position.x = -1;方向光.position.y = 0;directionalLight.position.z = -2;场景添加(定向光);控件 = 新的 THREE.OrbitControls(camera, renderer.domElement);创建模型();}函数调整大小(){renderer.setSize(window.innerWidth, window.innerHeight);camera.aspect = window.innerWidth/window.innerHeight;相机.updateProjectionMatrix();}函数动画(){请求动画帧(动画);使成为();}函数渲染(){renderer.render(场景,相机);}})();

<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>

I'm trying to change the number of segments of the sphere, but I can't cut it if I have reached the 45 limit, and there's no other way to make the sphere a little smoother.

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'});

解决方案

computeVertexNormals() should do what you want:

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

See the code snippet:

For the right mesh computeVertexNormals() was called and for the left mesh it was not called.

(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>

这篇关于使用threeBSP方法后,创建的球体不光滑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆