环境光

2019-11-11  本文已影响0人  Codifier
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ambient Light</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 = -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 ambient light
        /**
         * AmbientLight( color : Integer, intensity : Float )
         * color - (optional) Numeric value of the RGB component of the color. Default is 0xffffff.
         * intensity - (optional) Numeric value of the light's strength/intensity. Default is 1.
         */
        let ambientLight = new THREE.AmbientLight("#606008", 1);
        scene.add(ambientLight);

        // add spotlight
        let spotLight = new THREE.SpotLight(0xffffff, 1, 180, Math.PI / 4);
        spotLight.shadow.mapSize.set(2048, 2048);
        spotLight.position.set(-30, 40, -10);
        spotLight.castShadow = true;
        scene.add(spotLight);

        // 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;
        scene.add(plane);

        // add a sphere
        let sphereGeometry = new THREE.SphereGeometry(4, 20, 20);
        let sphereMatrial = new THREE.MeshLambertMaterial({
            color : 0x7777ff
        });
        let sphere = new THREE.Mesh(sphereGeometry, sphereMatrial);
        sphere.position.set(0, 4, 2);
        sphere.castShadow = true;
        scene.add(sphere);

        // attributes which can be modified in GUI
        const controls = {
            "intensity" : ambientLight.intensity,
            "ambientColor" : ambientLight.color.getStyle(),
            "disableSpotlight" : false
        };
        // init GUI
        initGUI();

        renderScene();

        function initGUI(){
            let gui = new dat.GUI();
            gui.add(controls, 'intensity', 0, 3, 0.1).onChange(function (e) {
                ambientLight.color = new THREE.Color(controls.ambientColor);
                ambientLight.intensity = controls.intensity;
            });
            gui.addColor(controls, 'ambientColor').onChange(function (e) {
                ambientLight.color = new THREE.Color(controls.ambientColor);
                ambientLight.intensity = controls.intensity;
            });
            gui.add(controls, 'disableSpotlight').onChange(function (e) {
                spotLight.visible = !e;
            });
        }

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

        function renderScene(){
            trackballControls.update(clock.getDelta());
            stats.update();
            requestAnimationFrame(renderScene);
            renderer.render(scene, camera);
        }
    }
</script>
</body>
</html>

运行结果:

  1. 打开聚光灯效果:


  2. 关闭聚光灯效果:


上一篇 下一篇

猜你喜欢

热点阅读