1. 程式人生 > >70 - three.js 筆記 - 使用環境貼圖 建立虛假的反光效果

70 - three.js 筆記 - 使用環境貼圖 建立虛假的反光效果

計算環境反光會非常耗費CPU,而且通常會使用光線追蹤演算法,在three.js中通過環境貼圖envMap來偽裝反光,並且可以將環境貼圖用到指定的物件上。

1、示例

示例
http://ithanmang.com/threeJs/home/201809/20180905/07-envMap-texture.html
效果
這裡寫圖片描述

2、使用步驟

2.1、建立一個全景的貼圖
scene = new THREE.Scene();
        scene.background = new THREE.CubeTextureLoader().setPath('../../textures/cube/Park2/'
) .load( [ 'posx.jpg', 'negx.jpg', 'posy.jpg', 'negy.jpg', 'posz.jpg', 'negz.jpg' ] );
2.2、給場景中的物件的材質新增環境貼圖
let material = new THREE.MeshPhongMaterial();
        material.envMap = scene.background;

        let
sphereGeometry = new THREE.SphereGeometry(20, 60, 60); let boxGeometry = new THREE.BoxGeometry(30, 30, 30); let box = new THREE.Mesh(boxGeometry, material); let sphere = new THREE.Mesh(sphereGeometry, material); sphere.translateX(-45); box.translateX(45); let
loader = new THREE.GLTFLoader(); loader.load('../../models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', function (gltf) { gltf.scene.traverse(function (child) { if (child.isMesh) { child.scale.set(25, 25, 25); child.material.envMap = scene.background; } }); scene.add(gltf.scene); scene.add(box); scene.add(sphere); removeLoading(); });

示例中添加了三個物件,球體、立方體,載入了一個外部模型。
分別給各自的材質屬性的envMap屬性設定為scene.background

3、示例程式碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="../../../three.png">
    <title>使用環境貼圖建立虛假的反光效果</title>
    <style>
        body {
            margin: 0;
            overflow: hidden; /* 溢位隱藏 */
        }

        #loading {
            position: fixed;
            top: 50%;
            left: 50%;
            color: #FFFFFF;
            font-size: 20px;
            margin-top: -30px;
            margin-left: -40px;
        }
    </style>
    <script src="../../libs/build/three-r93.js"></script>
    <script src="../../libs/examples/js/Detector.js"></script>
    <script src="../../libs/examples/js/libs/dat.gui.min.js"></script>
    <script src="../../libs/examples/js/libs/stats.min.js"></script>
    <script src="../../libs/examples/js/controls/OrbitControls.js"></script>
    <script src="../../libs/examples/js/loaders/GLTFLoader.js"></script>
</head>
<body>
<p id="loading">loading......</p>
<script>

    let scene, camera, renderer, controls, guiControls;
    let stats = initStats();

    /* 場景 */
    function initScene() {

        scene = new THREE.Scene();
        scene.background = new THREE.CubeTextureLoader().setPath('../../textures/cube/Park2/')
            .load( [
                'posx.jpg',
                'negx.jpg',
                'posy.jpg',
                'negy.jpg',
                'posz.jpg',
                'negz.jpg'
            ] );

    }

    /* 相機 */
    function initCamera() {

        camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);
        camera.position.set(0, 10, 100);
        camera.lookAt(new THREE.Vector3(0, 0, 0));

    }

    /* 渲染器 */
    function initRender() {

        renderer = new THREE.WebGLRenderer({antialias: true});
        renderer.setSize(window.innerWidth, window.innerHeight);

        document.body.appendChild(renderer.domElement);

    }

    /* 燈光 */
    function initLight() {

        scene.add(new THREE.AmbientLight(0xffffff, 0.6));

        let spotLight1 = new THREE.SpotLight(0xffffff);
        spotLight1.position.set(-400, -400, -400);

        let spotLight2 = new THREE.SpotLight(0xffffff);
        spotLight2.position.set(400, 400, 400);

        scene.add(spotLight1);
        scene.add(spotLight2);

    }

    /* 控制器 */
    function initControls() {

        /* 地圖控制元件 */
        controls = new THREE.OrbitControls(camera, renderer.domElement);

        /* 屬性引數 */

    }

    /* 除錯外掛 */
    function initGui() {

        guiControls = new function () {


        };

        let gui = new dat.GUI();

    }

    /* 場景中的內容 */
    function initContent() {

        let material = new THREE.MeshPhongMaterial();
        material.envMap = scene.background;

        let sphereGeometry = new THREE.SphereGeometry(20, 60, 60);


        let boxGeometry = new THREE.BoxGeometry(30, 30, 30);
        let box = new THREE.Mesh(boxGeometry, material);

        let sphere = new THREE.Mesh(sphereGeometry, material);
        sphere.translateX(-45);
        box.translateX(45);

        let loader = new THREE.GLTFLoader();
        loader.load('../../models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', function (gltf) {

            gltf.scene.traverse(function (child) {

                if (child.isMesh) {
                    child.scale.set(25, 25, 25);
                    child.material.envMap = scene.background;
                }

            });

            scene.add(gltf.scene);
            scene.add(box);
            scene.add(sphere);

            removeLoading();

        });

    }

    /* 移除載入元素 */
    function removeLoading() {

        document.getElementById('loading').style.display = 'none';

    }

    /* 效能外掛 */
    function initStats() {

        let stats = new Stats();

        document.body.appendChild(stats.domElement);

        return stats;

    }

    /* 視窗變動觸發 */
    function onWindowResize() {

        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize(window.innerWidth, window.innerHeight);

    }

    /* 資料更新 */
    function update() {

        stats.update();

        controls.update();


    }

    /* 初始化 */
    function init() {

        if (!Detector.webgl) Detector.addGetWebGLMessage();

        initScene();
        initCamera();
        initRender();
        initLight();
        initControls();
        initContent();
        initGui();

        /* 監聽事件 */
        window.addEventListener('resize', onWindowResize, false);

    }

    /* 迴圈渲染 */
    function animate() {

        requestAnimationFrame(animate);
        renderer.render(scene, camera);
        update();

    }

    /* 初始載入 */
    (function () {
        console.log("three init start...");

        init();
        animate();

        console.log("three init end...");
    })();

</script>
</body>
</html>