美文网首页
网格面材质

网格面材质

作者: Codifier | 来源:发表于2019-11-12 15:09 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Mesh Face Material</title>
        <script src="../../three-part/threejs/three.js"></script>
        <script src="../../three-part/utils/stats.min.js"></script>
        <script src="../../three-part/utils/dat.gui.min.js"></script>
        <script src="../controls/TrackballControls.js"></script>
        <script src="../util/util.js"></script>
        <style>
            body {
                margin: 0;
                overflow: hidden;
            }
        </style>
    </head>
    <body>
    <div id="container"></div>
    <script type="text/javascript">
        init();
        function init() {
            // show FPS
            let stats = initStats();
            // resize
            window.addEventListener('resize', onResize, false);
    
            let scene = new THREE.Scene();
            let camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
            camera.position.x = -30;
            camera.position.y = 40;
            camera.position.z = 30;
            camera.lookAt(scene.position);
    
            let renderer = new THREE.WebGLRenderer();
            renderer.setClearColor(new THREE.Color(0x000000));
            renderer.setSize(window.innerWidth, window.innerHeight);
            renderer.shadowMap.enabled = true;
            document.getElementById("container").appendChild(renderer.domElement);
    
            // init trackball control
            let trackballControls = initTrackballControls(camera, renderer);
            let clock = new THREE.Clock();
    
            let spotLight = new THREE.SpotLight(0xffffff);
            spotLight.position.set(-40, 60, -10);
            spotLight.castShadow = true;
            scene.add(spotLight);
    
            let group = new THREE.Mesh();
            let mats = [];
            mats.push(new THREE.MeshBasicMaterial({
                color: 0x009e60
            }));
            mats.push(new THREE.MeshBasicMaterial({
                color: 0x0051ba
            }));
            mats.push(new THREE.MeshBasicMaterial({
                color: 0xffd500
            }));
            mats.push(new THREE.MeshBasicMaterial({
                color: 0xff5800
            }));
            mats.push(new THREE.MeshBasicMaterial({
                color: 0xC41E3A
            }));
            mats.push(new THREE.MeshBasicMaterial({
                color: 0xffffff
            }));
    
            for (let x = 0; x < 3; x++) {
                for (let y = 0; y < 3; y++) {
                    for (let z = 0; z < 3; z++) {
                        let cubeGeom = new THREE.BoxGeometry(2.9, 2.9, 2.9);
                        let cube = new THREE.Mesh(cubeGeom, mats);
                        cube.position.set(x * 3 - 3, y * 3 - 3, z * 3 - 3);
                        group.add(cube);
                    }
                }
            }
    
            group.scale.copy(new THREE.Vector3(2,2,2));
            scene.add(group);
    
            // attributes which can be modified in GUI
            const controls = {
    
            };
            // init GUI
            initGUI();
    
            renderScene();
    
            function initGUI(){
                let gui = new dat.GUI();
            }
    
            function onResize() {
                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();
                renderer.setSize(window.innerWidth, window.innerHeight);
            }
    
            function renderScene(){
                trackballControls.update(clock.getDelta());
                stats.update();
                group.rotation.y += 0.02;
                group.rotation.z -= 0.02;
                group.rotation.x += 0.02;
                requestAnimationFrame(renderScene);
                renderer.render(scene, camera);
            }
        }
    </script>
    </body>
    </html>
    

    运行结果:


    相关文章

      网友评论

          本文标题:网格面材质

          本文链接:https://www.haomeiwen.com/subject/ogcqictx.html