• 22-THREE.JS 面材质


    <!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>
    

      

  • 相关阅读:
    如何找按钮数组在布局中的横竖坐标位置?
    java中使用rmi进行远程方法调用
    测试视频集,各种测试用的视频文件
    如何彻底禁用VS 2008的智能感知功能
    JavaScript秘密花园 Type Casting,undefined,eval,setTimeout,Auto Semicolon Insertion
    JavaScript秘密花园 scope, namespace, constructor, equality and comparisons
    造成内存位置访问无效的一个原因
    depends在VS2008消失了
    12月10日晚的月全食照片
    JavaScript秘密花园 Array, Array Constructor, for in loop, typeof, instanceOf
  • 原文地址:https://www.cnblogs.com/shuaihan/p/9881157.html
Copyright © 2020-2023  润新知