js 随机选取动画_【three.js练习程序】动画效果,100个方块随机运动

这篇博客介绍了如何使用Three.js库创建一个包含100个方块的3D场景,每个方块随机运动。通过设置方块的位置和颜色,结合WebGLRenderer渲染,实现了一种动态的随机选取动画效果。同时,博客还展示了如何限制方块的运动范围,确保它们不会超出设定的边界。

DOCTYPE html>

ceshititle>

script>

}

style>

head>

div>

varcube= newArray(100);varcamera, scene, renderer;varid= null;

init();functioninit()

{

scene= newTHREE.Scene();

camera= newTHREE.PerspectiveCamera(45,

window.innerWidth/window.innerHeight,0.1,1000);

camera.position.x= 100;

camera.position.y= 100;

camera.position.z= 100;

camera.lookAt(scene.position);//创建一个WebGL渲染器并设置其大小

renderer= newTHREE.WebGLRenderer();

renderer.setClearColor(newTHREE.Color(0xEEEEEE));

renderer.setSize(window.innerWidth, window.innerHeight);varaxes= newTHREE.AxisHelper(200);

scene.add(axes);for(vari=0;i<100;i++)

{vargeometry= newTHREE.BoxGeometry(5,5,5);for(varj= 0; j

{varhex=Math.random()* 0xffffff;

geometry.faces[ j ].color.setHex( hex );

geometry.faces[ j+ 1].color.setHex( hex );

}varmaterial= newTHREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors, overdraw:0.5} );

cube[i]= newTHREE.Mesh( geometry, material );

cube[i].position.x=Math.random()*100-50;

cube[i].position.y=Math.random()*100-50;

cube[i].position.z=Math.random()*100-50;

scene.add( cube[i] );

id=setInterval(render,33);

}functionrender()

{for(vari=0;i<100;i++)

{

cube[i].position.x+=Math.random()-0.5;

cube[i].position.y+=Math.random()-0.5;

cube[i].position.z+=Math.random()-0.5;if(Math.abs(cube[i].position.x)>100)

cube[i].position.x=0;if(Math.abs(cube[i].position.y)>100)

cube[i].position.y=0;if(Math.abs(cube[i].position.z)>100)

cube[i].position.z=0;

}

renderer.render( scene, camera );

}

document.getElementById("WebGL-output").appendChild(renderer.domElement);

}script>

body>

html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值