,可以通过以下步骤实现:
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
var material1 = new THREE.MeshBasicMaterial({ color: 0xff0000 }); // 红色材质
var cube1 = new THREE.Mesh(cubeGeometry, material1);
cube1.position.x = -2;
scene.add(cube1);
var material2 = new THREE.MeshPhongMaterial({ color: 0x00ff00 }); // 绿色材质
var cube2 = new THREE.Mesh(cubeGeometry, material2);
cube2.position.x = 0;
scene.add(cube2);
var material3 = new THREE.MeshLambertMaterial({ color: 0x0000ff }); // 蓝色材质
var cube3 = new THREE.Mesh(cubeGeometry, material3);
cube3.position.x = 2;
scene.add(cube3);
var ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
var directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(0, 1, 1);
scene.add(directionalLight);
function animate() {
requestAnimationFrame(animate);
cube1.rotation.x += 0.01;
cube1.rotation.y += 0.01;
cube2.rotation.x += 0.01;
cube2.rotation.y += 0.01;
cube3.rotation.x += 0.01;
cube3.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
通过上述步骤,就可以在three.js中实现不同材质的多立方体。在这个例子中,我们创建了三个立方体,分别使用了不同的材质(红色、绿色和蓝色)。你可以根据需要选择不同的材质类型,例如基础材质(MeshBasicMaterial)、光照材质(MeshPhongMaterial)和兰伯特材质(MeshLambertMaterial)等。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云