美文网首页
场景材质覆盖

场景材质覆盖

作者: Codifier | 来源:发表于2019-11-07 18:32 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Override  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();
        scene.fog = new THREE.FogExp2(0xEA7EF7, 0.01);
        // If not null, it will force everything in the scene to be rendered with that material.
        // Default is null.
        scene.overrideMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});

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

        // add a plane
        let planeGeometry = new THREE.PlaneGeometry(60, 40, 1, 1);
        let planeMaterial = new THREE.MeshLambertMaterial({
            color: 0xffffff
        });
        let plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.receiveShadow = true;
        plane.rotation.x = -0.5 * Math.PI;
        plane.position.x = 0;
        plane.position.y = 0;
        plane.position.z = 0;
        scene.add(plane);

        // add a ambient light
        let ambientLight = new THREE.AmbientLight(0x3c3c3c);
        scene.add(ambientLight);

        // add a spot light
        let spotLight = new THREE.SpotLight(0xffffff, 1.2, 150, 120);
        spotLight.position.set(-40, 60, -10);
        spotLight.castShadow = true;
        scene.add(spotLight);

        // attributes which can be modified in GUI
        const controls = {
            "rotationSpeed" : 0.02,
            "numberOfObjects" : scene.children.length,
            "removeCube" : function(){
                let allChildren = scene.children;
                let lastObject = allChildren[allChildren.length - 1];
                if (lastObject instanceof THREE.Mesh) {
                    scene.remove(lastObject);
                    this.numberOfObjects = scene.children.length;
                }
            },
            "addCube" : function(){
                let cubeSize = Math.ceil((Math.random() * 3));
                let cubeGeometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
                let cubeMaterial = new THREE.MeshLambertMaterial({
                    color: Math.random() * 0xffffff
                });
                let cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
                cube.castShadow = true;
                cube.name = "cube-" + scene.children.length;

                cube.position.x = -30 + Math.round((Math.random() * planeGeometry.parameters.width));
                cube.position.y = Math.round((Math.random() * 5));
                cube.position.z = -20 + Math.round((Math.random() * planeGeometry.parameters.height));

                scene.add(cube);
                this.numberOfObjects = scene.children.length;
            },
            "outputObjects" : function(){
                console.log(scene.children);
            }
        };

        // init GUI
        initGUI(controls);

        renderScene();

        function initGUI(controls){
            let gui = new dat.GUI();
            gui.add(controls, 'rotationSpeed', 0, 0.5);
            gui.add(controls, 'addCube');
            gui.add(controls, 'removeCube');
            gui.add(controls, 'outputObjects');
            gui.add(controls, 'numberOfObjects').listen();
        }

        function onResize() {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
        }

        function renderScene(){
            trackballControls.update(clock.getDelta());
            stats.update();

            // rotate cube
            scene.traverse(function (e) {
                if (e instanceof THREE.Mesh && e != plane) {
                    e.rotation.x += controls.rotationSpeed;
                    e.rotation.y += controls.rotationSpeed;
                    e.rotation.z += controls.rotationSpeed;
                }
            });

            requestAnimationFrame(renderScene);
            renderer.render(scene, camera);
        }
    }
</script>
</body>
</html>

运行结果:



总结:
scene.overrideMaterial的作用是让场景中的所有物体都具有相同的材质

相关文章

  • 场景材质覆盖

    运行结果: 总结:scene.overrideMaterial的作用是让场景中的所有物体都具有相同的材质

  • 2020-03-17 修改模型颜色

    场景:客户可以自行修改物体的颜色,材质常见材质包括: 1、基础材质 MeshBasicMaterial : 对光照...

  • 如何给unity立方体(3d-Cube)设置颜色

    场景中右键创建一个立方体, Project窗口中创建一个材质 选中材质, 在In平sector窗口中设置材质的颜色...

  • Marmoset Toolbag3翻译(搬运)

    一,菜单相关 二,场景相关 三,材质相关 四,其他

  • CORONA 渲染器学习笔记之四:材质-7

    【前言】 上一篇研究了LIGHT MATERIAL 灯光材质,虽然是材质,但它能使物体发光并能照亮场景。今...

  • 影驰名人堂1060全覆盖显卡冷头

    影驰名人堂1060全覆盖显卡冷头,采用进口亚克力材质做上盖,高纯度紫铜底板,全面覆盖GPU显存及供电模块,散热更彻...

  • 代码覆盖率解决方案

    在测试的世界里,覆盖包含代码级覆盖、流程分支级覆盖、业务场景级覆盖等,覆盖是衡量测试完整性的重要指标,我们只有覆...

  • 长短视频

    短视频会覆盖长视频场景吗? 关于视频产品的思考:短视频会覆盖长视频场景吗? 刘飞 http://www.niaog...

  • 07 RayData cs 渲染参数和面板

    1.打开工具面板选择颜色和材质选项 2.弹出以下子层级 3.不透明度、基础材质、自发光材质(不受场景中灯光的影响并...

  • Unity场景打包测试记录

    测试1:将多个场景共用的Prefab\FBX\材质\贴图等打包到AssetBundle中,场景打包出的AssetB...

网友评论

      本文标题:场景材质覆盖

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