73 - three.js 笔记 - 设置折射贴图比率 refractionRatio 加环境贴图实现反射效果

通过给物体设置环境贴图envMap可以实现虚假的反射效果,通过设置CubeCamera可以实现动态的反光,设置材质的refractionRatio 属性可以实现透明反射的效果。

1、示例

示例
http://ithanmang.com/threeJs/home/201809/20180907/03-refraction-texture.html
效果
这里写图片描述

2、实现步骤

2.1、refractionRatio

refractionRatio:是物体对环境贴图的折射率,值越接近0的话,对贴图的反射越强烈,越接近1则月接近透明状态,需要配合THREE.CubeRefractionMappingTHREE.EquirectangularRefractionMapping一起使用。

2.2、设置环境贴图和映射方式
let cubeTexture = new THREE.CubeTextureLoader().setPath('../../textures/cube/Bridge2/')
            .load(
                [
                    'posx.jpg',
                    'negx.jpg',
                    'posy.jpg',
                    'negy.jpg',
                    'posz.jpg',
                    'negz.jpg'
                ]
            );
cubeTexture.mapping = THREE.CubeRefractionMapping;
scene.background = cubeTexture;
2.3、设置材质的映射率
envMap: cubeTexture, 
refractionRatio:0.98, 
reflectivity:0.9

首先,需要设置环境贴图,然后设置设置率,设置贴图的反射率

3、示例代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="../../../three.png">
    <title>通过 Reflector 创建反光镜</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>

</head>
<body>
<p id="loading">loading......</p>
<script>

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

    /* 场景 */
    function initScene() {

        scene = new THREE.Scene();

    }

    /* 相机 */
    function initCamera() {

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

    }

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

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

        document.body.appendChild(renderer.domElement);

    }

    /* 灯光 */
    function initLight() {

        scene.add(new THREE.AmbientLight(0x0c0c0c));

        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();


    }

    /* 场景中的内容 */
    let cube;
    function initContent() {

        let cubeTexture = new THREE.CubeTextureLoader().setPath('../../textures/cube/Bridge2/')
            .load(
                [
                    'posx.jpg',
                    'negx.jpg',
                    'posy.jpg',
                    'negy.jpg',
                    'posz.jpg',
                    'negz.jpg'
                ]
            );
        cubeTexture.mapping = THREE.CubeRefractionMapping;
        scene.background = cubeTexture;

        let cubeMaterial1 = new THREE.MeshPhongMaterial({color: 0xccddff, envMap: cubeTexture, refractionRatio:0.98, reflectivity:0.9});
        let cubeMaterial2 = new THREE.MeshPhongMaterial({color: 0xccfffd, envMap: cubeTexture, refractionRatio:0.98, reflectivity:0.9});

        cube = new THREE.Mesh(new THREE.BoxGeometry(4, 4, 4), cubeMaterial1);
        cube.material.color = new THREE.Color('0xffffff');
        scene.add(cube);

        let loader = new THREE.JSONLoader();
        loader.load('../../models/json/Female02_slim.json', function (geometry) {

            geometry.applyMatrix(new THREE.Matrix4().makeTranslation(0, -100, 0));

            let mesh = new THREE.Mesh(geometry, cubeMaterial1);
            mesh.translateX(7);
            mesh.scale.set(0.1, 0.1, 0.1);
            scene.add(mesh);

        });
        loader.load('../../models/json/Male02_dds.json', function (geometry) {

            geometry.applyMatrix(new THREE.Matrix4().makeTranslation(0, -100, 0));

            let mesh = new THREE.Mesh(geometry, cubeMaterial2);
            mesh.translateX(-7);
            mesh.scale.set(0.1, 0.1, 0.1);
            scene.add(mesh);

            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();

        if (cube) {

            cube.rotateX(0.01);
            cube.rotateY(0.01);
            cube.rotateZ(0.01);

        }

    }

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

        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>

猜你喜欢

转载自blog.csdn.net/ithanmang/article/details/82500654