zoukankan      html  css  js  c++  java
  • 24-THREE.JS 镜面高光材质

    <!DOCTYPE html>
    
    <html>
    
    <head>
        <title>Example 04.07 - Mesh Phong 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>
        <script type="text/javascript" src="../libs/CanvasRenderer.js"></script>
        <script type="text/javascript" src="../libs/Projector.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;
            var webGLRenderer = new THREE.WebGLRenderer();
            webGLRenderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
            webGLRenderer.setSize(window.innerWidth, window.innerHeight);
            webGLRenderer.shadowMapEnabled = true;
    
            var canvasRenderer = new THREE.CanvasRenderer();
            canvasRenderer.setSize(window.innerWidth, window.innerHeight);
            renderer = webGLRenderer;
    
            var groundGeom = new THREE.PlaneGeometry(100, 100, 4, 4);
            var groundMesh = new THREE.Mesh(groundGeom, new THREE.MeshBasicMaterial({color: 0x555555}));
            groundMesh.rotation.x = -Math.PI / 2;
            groundMesh.position.y = -20;
            scene.add(groundMesh);
    
            var sphereGeometry = new THREE.SphereGeometry(14, 20, 20);
            var cubeGeometry = new THREE.BoxGeometry(15, 15, 15);
            var planeGeometry = new THREE.PlaneGeometry(14, 14, 4, 4);
    
    
            var meshMaterial = new THREE.MeshPhongMaterial({color: 0x7777ff});
            var sphere = new THREE.Mesh(sphereGeometry, meshMaterial);
            var cube = new THREE.Mesh(cubeGeometry, meshMaterial);
            var plane = new THREE.Mesh(planeGeometry, meshMaterial);
    
            
            sphere.position.x = 0;
            sphere.position.y = 3;
            sphere.position.z = 2;
    
    
            cube.position = sphere.position;
            plane.position = sphere.position;
    
    
            // 把正方体添加到场景中去
            scene.add(cube);
    
            // 设置相机的坐标
            camera.position.x = -20;
            camera.position.y = 30;
            camera.position.z = 40;
            camera.lookAt(new THREE.Vector3(10, 0, 0));
    
            // add subtle ambient lighting
    //        var ambientLight = new THREE.AmbientLight(0x0c0c0c);
    //        scene.add(ambientLight);
    
            // 添加聚光灯
            var spotLight = new THREE.SpotLight(0xffffff);
            spotLight.position.set(-0, 30, 60);
            spotLight.castShadow = true;
            spotLight.intensity = 0.6;
            scene.add(spotLight);
    
            // 把渲染后的结果放到DOM元素中去
            document.getElementById("WebGL-output").appendChild(renderer.domElement);
    
            
            var step = 0;
    
            var controls = new function () {
                this.rotationSpeed = 0.02;
                this.bouncingSpeed = 0.03;
    
                this.opacity = meshMaterial.opacity;
                this.transparent = meshMaterial.transparent;
                this.overdraw = meshMaterial.overdraw;
                this.visible = meshMaterial.visible;
                this.emissive = meshMaterial.emissive.getHex();
                this.ambient = meshMaterial.ambient.getHex();
                this.specular = meshMaterial.specular.getHex();
                this.shininess = meshMaterial.shininess;
                this.side = "front";
    
                this.color = meshMaterial.color.getStyle();
    
                this.metal = false;
                this.wrapAround = false;
                this.wrapR = 1;
                this.wrapG = 1;
                this.wrapB = 1;
    
    
                this.selectedMesh = "cube";
    
            };
    
            var gui = new dat.GUI();
    
    
            var spGui = gui.addFolder("Mesh");
            spGui.add(controls, 'opacity', 0, 1).onChange(function (e) {
                meshMaterial.opacity = e
            });
            spGui.add(controls, 'transparent').onChange(function (e) {
                meshMaterial.transparent = e
            });
            spGui.add(controls, 'visible').onChange(function (e) {
                meshMaterial.visible = e
            });
            spGui.addColor(controls, 'ambient').onChange(function (e) {
                meshMaterial.ambient = new THREE.Color(e)
            });
            spGui.addColor(controls, 'emissive').onChange(function (e) {
                meshMaterial.emissive = new THREE.Color(e)
            });
            spGui.addColor(controls, 'specular').onChange(function (e) {
                meshMaterial.specular = new THREE.Color(e)
            });
            spGui.add(controls, 'shininess', 0, 200).onChange(function (e) {
                meshMaterial.shininess = e
            });
            spGui.add(controls, 'side', ["front", "back", "double"]).onChange(function (e) {
                console.log(e);
                switch (e) {
                    case "front":
                        meshMaterial.side = THREE.FrontSide;
                        break;
                    case "back":
                        meshMaterial.side = THREE.BackSide;
                        break;
                    case "double":
                        meshMaterial.side = THREE.DoubleSide;
                        break;
                }
                meshMaterial.needsUpdate = true;
                console.log(meshMaterial);
            });
            spGui.addColor(controls, 'color').onChange(function (e) {
                meshMaterial.color.setStyle(e)
            });
            spGui.add(controls, 'selectedMesh', ["cube", "sphere", "plane"]).onChange(function (e) {
    
                scene.remove(plane);
                scene.remove(cube);
                scene.remove(sphere);
    
                switch (e) {
                    case "cube":
                        scene.add(cube);
                        break;
                    case "sphere":
                        scene.add(sphere);
                        break;
                    case "plane":
                        scene.add(plane);
                        break;
    
                }
    
                scene.add(e);
            });
    
            spGui.add(controls, 'metal').onChange(function (e) {
    
                meshMaterial.metal = e;
                meshMaterial.needsUpdate = true;
            });
    
            spGui.add(controls, 'wrapAround').onChange(function (e) {
    
                meshMaterial.wrapAround = e;
                meshMaterial.needsUpdate = true;
            });
    
            spGui.add(controls, 'wrapR', 0, 1).step(0.01).onChange(function (e) {
                meshMaterial.wrapRGB.x = e;
            });
    
            spGui.add(controls, 'wrapG', 0, 1).step(0.01).onChange(function (e) {
                meshMaterial.wrapRGB.y = e;
            });
    
            spGui.add(controls, 'wrapB', 0, 1).step(0.01).onChange(function (e) {
                meshMaterial.wrapRGB.z = e;
    
            });
    
            render();
    
            function render() {
                stats.update();
    
                cube.rotation.y = step += 0.01;
                plane.rotation.y = step;
                sphere.rotation.y = step;
                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>
    

      

  • 相关阅读:
    Struts2+Spring3+Mybatis3开发环境搭建
    spring+struts2+mybatis
    【LeetCode】Populating Next Right Pointers in Each Node
    【LeetCode】Remove Duplicates from Sorted Array
    【LeetCode】Remove Duplicates from Sorted Array II
    【LeetCode】Binary Tree Inorder Traversal
    【LeetCode】Merge Two Sorted Lists
    【LeetCode】Reverse Integer
    【LeetCode】Same Tree
    【LeetCode】Maximum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/shuaihan/p/9881306.html
Copyright © 2011-2022 走看看