【threeJS】常用技术细节

//包围球
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值