<!DOCTYPE html> <html> <head> <title>Example 04.05 - Mesh face material</title> <script src="https://cdn.bootcss.com/three.js/r67/three.js"></script> <script src="https://cdn.bootcss.com/stats.js/r10/Stats.min.js"></script> <script type="text/javascript" src="https://cdn.bootcss.com/dat-gui/0.7.3/dat.gui.js"></script> <style> body { margin: 0; overflow: hidden; } </style> </head> <body> <div id="Stats-output"> </div> <div id="WebGL-output"> </div> <script type="text/javascript"> // 初始化 function init() { var stats = initStats(); // 创建一个场景 var scene = new THREE.Scene(); // 创建一个相机 var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000); // 创建一个渲染器 var renderer = new THREE.WebGLRenderer(); renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0)); renderer.setSize(window.innerWidth, window.innerHeight); renderer.shadowMapEnabled = false; // 创建一个地面 var planeGeometry = new THREE.PlaneGeometry(60, 40, 1, 1); var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff}); var plane = new THREE.Mesh(planeGeometry, planeMaterial); plane.receiveShadow = true; // 旋转这个地面 plane.rotation.x = -0.5 * Math.PI; plane.position.x = 0; plane.position.y = -2; plane.position.z = 0; // 把地面添加到场景中去 scene.add(plane); // 相机的坐标 和 朝向 camera.position.x = -40; camera.position.y = 40; camera.position.z = 40; camera.lookAt(scene.position); // add subtle ambient lighting // var ambientLight = new THREE.AmbientLight(0x0c0c0c); // scene.add(ambientLight); // 添加聚光灯 var spotLight = new THREE.SpotLight(0xffffff); spotLight.position.set(-40, 60, -10); spotLight.castShadow = true; scene.add(spotLight); // 把渲染后的结果放到DOM元素中去 document.getElementById("WebGL-output").appendChild(renderer.domElement); var group = new THREE.Mesh(); // 设置每个面的颜色 var mats = []; mats.push(new THREE.MeshBasicMaterial({color: 0x009e60})); mats.push(new THREE.MeshBasicMaterial({color: 0x009e60})); mats.push(new THREE.MeshBasicMaterial({color: 0x0051ba})); mats.push(new THREE.MeshBasicMaterial({color: 0x0051ba})); mats.push(new THREE.MeshBasicMaterial({color: 0xffd500})); mats.push(new THREE.MeshBasicMaterial({color: 0xffd500})); mats.push(new THREE.MeshBasicMaterial({color: 0xff5800})); mats.push(new THREE.MeshBasicMaterial({color: 0xff5800})); mats.push(new THREE.MeshBasicMaterial({color: 0xC41E3A})); mats.push(new THREE.MeshBasicMaterial({color: 0xC41E3A})); mats.push(new THREE.MeshBasicMaterial({color: 0xffffff})); mats.push(new THREE.MeshBasicMaterial({color: 0xffffff})); var faceMaterial = new THREE.MeshFaceMaterial(mats); for (var x = 0; x < 3; x++) { for (var y = 0; y < 3; y++) { for (var z = 0; z < 3; z++) { var cubeGeom = new THREE.BoxGeometry(2.9, 2.9, 2.9); var cube = new THREE.Mesh(cubeGeom, faceMaterial); cube.position.set(x * 3 - 3, y * 3, z * 3 - 3); group.add(cube); } } } scene.add(group); var step = 0; var controls = new function () { this.rotationSpeed = 0.02; this.numberOfObjects = scene.children.length; }; var gui = new dat.GUI(); gui.add(controls, 'rotationSpeed', 0, 0.5); render(); function render() { stats.update(); group.rotation.y = step += controls.rotationSpeed; requestAnimationFrame(render); renderer.render(scene, camera); } function initStats() { var stats = new Stats(); stats.setMode(0); // 0: fps, 1: ms stats.domElement.style.position = 'absolute'; stats.domElement.style.left = '0px'; stats.domElement.style.top = '0px'; document.getElementById("Stats-output").appendChild(stats.domElement); return stats; } } window.onload = init; </script> </body> </html>