美文网首页
点云效果

点云效果

作者: Codifier | 来源:发表于2019-11-15 11:34 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Point Cloud</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.set(20, 0, 150);
            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 cloud;
    
            // attributes which can be modified in GUI
            const controls = {
                "size" : 4,
                "transparent" : true,
                "opacity" : 0.6,
                "vertexColors" : true,
                "color" : 0xffffff,
                "vertexColor" : 0x00ff00,
                "sizeAttenuation" : true,
                "rotate" : true,
                "redraw" : function(){
                    if(scene.getObjectByName("particles")){
                        scene.remove(scene.getObjectByName("particles"));
                    }
                    createParticles();
                }
            };
            // init GUI
            initGUI();
    
            createParticles();
    
            renderScene();
    
            function initGUI(){
                let gui = new dat.GUI();
                gui.add(controls, 'size', 0, 10).onChange(controls.redraw);
                gui.add(controls, 'transparent').onChange(controls.redraw);
                gui.add(controls, 'opacity', 0, 1).onChange(controls.redraw);
                gui.add(controls, 'vertexColors').onChange(controls.redraw);
                gui.addColor(controls, 'color').onChange(controls.redraw);
                gui.addColor(controls, 'vertexColor').onChange(controls.redraw);
                gui.add(controls, 'sizeAttenuation').onChange(controls.redraw);
                gui.add(controls, 'rotate');
            }
    
            function onResize() {
                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();
                renderer.setSize(window.innerWidth, window.innerHeight);
            }
    
            function createParticles() {
                let geom = new THREE.Geometry();
                let material = new THREE.PointsMaterial({
                    size: controls.size,
                    transparent: controls.transparent,
                    opacity: controls.opacity,
                    vertexColors: controls.vertexColors,
                    sizeAttenuation: controls.sizeAttenuation,
                    color: new THREE.Color(controls.color)
                });
    
                let range = 500;
                for (let i = 0; i < 15000; i++) {
                    let particle = new THREE.Vector3(
                        Math.random() * range - range / 2,
                        Math.random() * range - range / 2,
                        Math.random() * range - range / 2
                    );
                    geom.vertices.push(particle);
                    let color = new THREE.Color(controls.vertexColorValue);
                    let asHSL = {};
                    color.getHSL(asHSL);
                    color.setHSL(asHSL.h, asHSL.s, asHSL.l * Math.random());
                    geom.colors.push(color);
                }
    
                cloud = new THREE.Points(geom, material);
                cloud.name = "particles";
                scene.add(cloud);
            }
    
            function renderScene(){
                trackballControls.update(clock.getDelta());
                stats.update();
                if (controls.rotate) {
                    cloud.rotation.x += 0.01;
                    cloud.rotation.z += 0.01;
                }
                requestAnimationFrame(renderScene);
                renderer.render(scene, camera);
            }
        }
    </script>
    </body>
    </html>
    

    运行结果:


    相关文章

      网友评论

          本文标题:点云效果

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