在使用threejs 的裁剪,手动旋转时正常,写代码旋转时,裁剪异常了。主要代码如下:
const camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 1, 1000);
const renderer = new THREE.WebGLRenderer({antialias: true});
renderer.localClippingEnabled = true;
const scene = new THREE.Scene();
scene.autoUpdate = true;
const arcballControl = new ArcballControls(camera, renderer.domElement,scene);
arcballControl.setGizmosVisible(false);
const holderLight = new THREE.DirectionalLight( 0xffffff, 5 );
scene.add( holderLight );
const meshGroup = new THREE.Group();
const mesh = new THREE.Mesh(geometry,material);
const mesh2 = new THREE.Mesh(geometry2,material2);
meshGroup.add(mesh);
meshGroup.add(mesh2);
scene.add(meshGroup);
const animate = function () {
holderLight.position.copy(camera.position);
renderer.render(scene, camera);
requestAnimationFrame(animate);
};
animate();
有一个按纽控制裁剪,执行:
const changeView = () =>{
mesh.material.clippingPlanes = [new THREE.Plane(new THREE.Vector3(1, 0, 0), 1)];
mesh.material.clipIntersection = true;
mesh2.material.clippingPlanes = [new THREE.Plane(new THREE.Vector3(-1, 0, 0), 1)];
mesh2.material.clipIntersection = true;
}
此时没有什么问题,鼠标手动旋转正常。
再写一个按纽,让其自动旋转1圈:
const rotatePosition = () =>{
var pi = Math.PI*2
const i = pi/360
timer = setInterval(function(){
if(pi > 0){
meshGroup.rotation.y -= pi>i?i:pi;
pi -= i;
} else {
clearInterval(timer);
}
}, 20);
}
旋转正常,但是裁剪的显示明显的异常,就是mesh 物体在转,在裁剪的位置一直固定在那,没有跟随着转
感觉裁剪的内容一直在变化。
如何才能让自动旋转正常呢?