//包围球
function show(scene,sphere){//显示包围球
var m = new Mesh(
new SphereGeometry( 1, 8, 8 ),
new MeshBasicMaterial( { color: 0x0faf0f } )
);
m.position.set(sphere.center.x,sphere.center.y,sphere.center.z)
m.scale.set(sphere.radius,sphere.radius,sphere.radius)
scene.add(m)
}
function getBoxes(instanceMesh){//获取实例化对象对包围球
instanceMesh.spheres=[]
instanceMesh.geometry.computeBoundingSphere()
for(var j=0;j<instanceMesh.spheres_mat.length;j++){
var sphere = instanceMesh.geometry.boundingSphere.clone()
sphere.applyMatrix4(instanceMesh.spheres_mat[j])
sphere.applyMatrix4(instanceMesh.matrixWorld)
instanceMesh.spheres.push(sphere)
show(scene,sphere)
}
}
//获取模型着色器代码
mesh.material.onBeforeCompile=function(shader,render){
console.log(
"onBeforeCompile",
mesh.name,
shader.fragmentShader,
shader.vertexShader
);
shader.vertexShader=shader.vertexShader.replace("","")
};
//线框模式
material.wireframe = true
//单面、双面渲染
material.side=0
material.side=2
//视点拾取
const raycaster = new Raycaster();
const mouse = new Vector2();
window.addEventListener( 'mousemove', event=>{//鼠标移动事件
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1 ;
}, false );
window.addEventListener('click',()=>{
raycaster.setFromCamera( mouse, camera )
raycaster.intersectObjects( arr )
},false)
//判断是否为mesh对象
glb.scene.traverse(o=>{
if(o instanceof THREE.Mesh){}
})
//渲染到场景中的三角面个数
renderer.info.render.triangles
//计算帧率
var frameIndex = 0, frameIndexPre = 0;
frameIndexUpdate();
setInterval(computeFPS, 1000);
function frameIndexUpdate() {
frameIndex++;
requestAnimationFrame(frameIndexUpdate);
}
function computeFPS() {
console.log(frameIndex - frameIndexPre);
frameIndexPre = frameIndex;
}
【threeJS】常用技术细节
于 2021-10-13 21:31:00 首次发布