美文网首页
动态环境贴图

动态环境贴图

作者: Codifier | 来源:发表于2019-11-20 10:43 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DynamicEnvMap</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 = 0;
        camera.position.y = 20;
        camera.position.z = 40;
        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 cubeTextureLoader = new THREE.CubeTextureLoader();
        let textureLoader = new THREE.TextureLoader();

        const urls = [
            '../assets/textures/cubemap/flowers/right.png',
            '../assets/textures/cubemap/flowers/left.png',
            '../assets/textures/cubemap/flowers/top.png',
            '../assets/textures/cubemap/flowers/bottom.png',
            '../assets/textures/cubemap/flowers/front.png',
            '../assets/textures/cubemap/flowers/back.png'
        ];

        scene.background = cubeTextureLoader.load(urls);

        let ambientLight = new THREE.AmbientLight(0x343434);
        scene.add(ambientLight);

        let spotLight = new THREE.SpotLight(0xffffff);
        spotLight.position.set(-10, 30, 40);
        spotLight.shadow.mapSize.width = 2048;
        spotLight.shadow.mapSize.height = 2048;
        spotLight.shadow.camera.fov = 15;
        spotLight.castShadow = true;
        spotLight.decay = 2;
        spotLight.penumbra = 0.05;
        scene.add(spotLight);

        /**
         * CubeCamera( near : Number, far : Number, cubeResolution : Number, options : Object )
         * near -- The near clipping distance.
         * far -- The far clipping distance
         * cubeResolution -- Sets the length of the cube's edges.
         * options -- (optional) object that holds texture parameters passed to the auto-generated WebGLRenderTargetCube.
         * @type {CubeCamera}
         */
        let cubeCamera = new THREE.CubeCamera(0.1, 300, 512);
        scene.add(cubeCamera);

        let cubeMaterial = new THREE.MeshStandardMaterial({
            envMap: scene.background,
            color: 0xffffff,
            metalness: 1,
            roughness: 0,
        });

        let sphereMaterial = cubeMaterial.clone();
        sphereMaterial.normalMap = textureLoader.load("../assets/textures/engraved/Engraved_Metal_003_NORM.jpg");
        sphereMaterial.aoMap = textureLoader.load("../assets/textures/engraved/Engraved_Metal_003_OCC.jpg");
        sphereMaterial.roughnessMap = textureLoader.load("../assets/textures/engraved/Engraved_Metal_003_ROUGH.jpg");

        let cubeGeometry = new THREE.BoxGeometry(20, 20, 20);
        let cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
        cube.position.set(-10, 10, 0);
        scene.add(cube);

        let sphereGeometry = new THREE.SphereGeometry(7, 50, 50);
        let sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
        sphere.position.set(20, 10, 0);
        scene.add(sphere);

        cubeCamera.position.copy(cube.position);
        cubeMaterial.envMap = cubeCamera.renderTarget.texture;

        // 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();
            cubeCamera.update(renderer, scene);
            requestAnimationFrame(renderScene);
            renderer.render(scene, camera);
            cube.rotation.y += 0.01;
            sphere.rotation.y -= 0.01;
        }
    }
</script>
</body>
</html>

运行结果:


相关文章

  • 动态环境贴图

    运行结果:

  • 2022-10-18 实现有倒影的水

    一、原理1.实现环境贴图和漫反射2.利用法线贴图对环境贴图进行干扰,让环境贴图有扭曲的效果3.利用环境贴图代替漫反...

  • 18.opengl高级-立方体贴图

    一、原理 立方体贴图在《视觉计算基础》一书中,第14章的环境贴图中有讲到,常见的环境贴图有立方体环境贴图和球体环境...

  • 环境光插值

    Babybus-u3d技术交流-环境光插值 上次写的动态混合光照贴图 [http://10.1.1.12/u3...

  • Kingdom-water

    基本思路:根据环境(风,光,基础运动)动态生成一张置换贴图用于倒影变化 Flash版本关键代码如下:声明置换滤镜 ...

  • 《Android 美颜类相机开发汇总》第六章 Android O

    美妆介绍 美颜类相机中一般会有彩妆功能,彩妆基本上都是贴图实现,这跟动态贴纸的贴图又不一样,动态贴纸的贴图一般是通...

  • Agisoft PhotoScan-相关应用-使用xNormal

    使用xNormal烘焙环境遮挡纹理贴图 本教程演示如何使用xNormal为纹理模型计算环境遮挡贴图。 模型必须纹理...

  • 静态环境贴图

    运行结果:

  • 动态混合光照贴图

    Babybus-u3d技术交流-动态混合光照贴图 爱说话的奇奇项目中的开窗和关窗各有一张光照贴图,开窗和关窗如果直...

  • iOS实现SpriteAnimation

    研究了一下抖音的动态贴图,抓包抖音的资源发现,其动态贴图资源包(一个zip压缩包)包括一张包含所有帧的大图,一个标...

网友评论

      本文标题:动态环境贴图

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