20-THREE.JS 混合材质
<!DOCTYPE html> <html> <head> <title></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, 10, 130); // 场景一个渲染器 var renderer = new THREE.WebGLRenderer(); renderer.sortObjects = false; renderer.setClearColor(new THREE.Color(0x00000, 1.0)); renderer.setSize(window.innerWidth, window.innerHeight); renderer.shadowMapEnabled = true; // 设置相机的位置 最远最近距离 朝向 camera.position.x = -50; camera.position.y = 40; camera.position.z = 50; camera.near = 7; camera.far = 139; camera.lookAt(scene.position); // 把渲染后的效果放到DOM元素中去 document.getElementById("WebGL-output").appendChild(renderer.domElement); var step = 0; var controls = new function () { this.cameraNear = camera.near; this.cameraFar = camera.far; this.rotationSpeed = 0.02; this.numberOfObjects = scene.children.length; this.color = 0x00ff00; this.removeCube = function () { var allChildren = scene.children; var lastObject = allChildren[allChildren.length - 1]; if (lastObject instanceof THREE.Mesh) { scene.remove(lastObject); this.numberOfObjects = scene.children.length; } }; this.addCube = function () { var cubeSize = Math.ceil(3 + (Math.random() * 3)); var cubeGeometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize); //混合材质 var cubeMaterial = new THREE.MeshDepthMaterial(); var colorMaterial = new THREE.MeshBasicMaterial({ color: controls.color, transparent: true, blending: THREE.MultiplyBlending }); var cube = new THREE.SceneUtils.createMultiMaterialObject(cubeGeometry, [colorMaterial, cubeMaterial]); cube.children[1].scale.set(0.99, 0.99, 0.99); cube.castShadow = true; // 给正方体随机的位置 cube.position.x = -60 + Math.round((Math.random() * 100)); cube.position.y = Math.round((Math.random() * 10)); cube.position.z = -100 + Math.round((Math.random() * 150)); // 在场景中添加正方体 scene.add(cube); this.numberOfObjects = scene.children.length; }; this.outputObjects = function () { console.log(scene.children); } }; var gui = new dat.GUI(); gui.addColor(controls, 'color'); gui.add(controls, 'rotationSpeed', 0, 0.5); gui.add(controls, 'addCube'); gui.add(controls, 'removeCube'); gui.add(controls, 'cameraNear', 0, 50).onChange(function (e) { camera.near = e; }); gui.add(controls, 'cameraFar', 50, 200).onChange(function (e) { camera.far = e; }); var i = 0; while (i < 10) { controls.addCube(); i++; } render(); function render() { stats.update(); // 旋转所有正方体 scene.traverse(function (e) { if (e instanceof THREE.Mesh) { e.rotation.x += controls.rotationSpeed; e.rotation.y += controls.rotationSpeed; e.rotation.z += 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>