美文网首页
加载GLTF模型

加载GLTF模型

作者: Codifier | 来源:发表于2019-11-13 16:53 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>GLTF Loader</title>
        <script src="../../three-part/threejs/three.js"></script>
        <script src="../../three-part/threejs/GLTFLoader.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, 100000);
            camera.position.x = 50;
            camera.position.y = 50;
            camera.position.z = 30;
            camera.lookAt(new THREE.Vector3(0, 15, 0));
    
            let renderer = new THREE.WebGLRenderer();
            renderer.setClearColor(new THREE.Color(0x000000));
            renderer.setSize(window.innerWidth, window.innerHeight);
            renderer.shadowMap.enabled = true;
            renderer.antialias = true;
            document.getElementById("container").appendChild(renderer.domElement);
    
            // init trackball control
            let trackballControls = initTrackballControls(camera, renderer);
            let clock = new THREE.Clock();
    
            let loader = new THREE.GLTFLoader();
            console.time();
            loader.load('../assets/models/gltf/hzxihuData/hzxihu_lvhua.gltf',
                // onLoad callback
                function (gltf) {
                    let axes = new THREE.AxesHelper(20);
                    let gltfScene = gltf.scene || gltf.scenes[0];
                    gltfScene.position.set(0, 10, 0);
                    gltf.scene.scale.set(0.1, 0.1, 0.1);
                    gltfScene.add(axes);
                    scene.add(gltfScene);
                    console.timeEnd();
                },
                // onProgress callback
                function (xhr) {
                    console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
                },
                // onError callback
                function (err) {
                    console.error('An error happened');
                }
            );
    
            // add ambient light
            let ambientLight = new THREE.AmbientLight({
                color : 0xffffff
            });
            scene.add(ambientLight);
    
            let directionalLight = new THREE.DirectionalLight({
                color : 0xffffff,
                intensity : 10
            });
            directionalLight.position.set(0, 50, 0);
            scene.add(directionalLight);
    
            // add a plane
            let planeGeometry = new THREE.PlaneGeometry(100, 100, 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);
    
            // attributes which can be modified in GUI
            const controls = {
    
            };
            // init GUI
            initGUI();
    
            function generateSprite() {
    
                let canvas = document.createElement('canvas');
                canvas.width = 16;
                canvas.height = 16;
                let context = canvas.getContext('2d');
    
                // draw the sprites
                let gradient = context.createRadialGradient(canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2);
                gradient.addColorStop(0, 'rgba(255,255,255,1)');
                gradient.addColorStop(0.2, 'rgba(0,255,255,1)');
                gradient.addColorStop(0.4, 'rgba(0,0,64,1)');
                gradient.addColorStop(1, 'rgba(0,0,0,1)');
                context.fillStyle = gradient;
                context.fillRect(0, 0, canvas.width, canvas.height);
    
                // create the texture
                let texture = new THREE.Texture(canvas);
                texture.needsUpdate = true;
                return texture;
            }
    
            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();
                requestAnimationFrame(renderScene);
                renderer.render(scene, camera);
            }
        }
    </script>
    </body>
    </html>
    

    运行结果:



    总结:

    1. 要使用GLTFLoader,需要先加入GLTFLoader.js文件;
    2. 在GLTFLoader加载的回调函数中,模型文件从gltf.scene中获取。

    相关文章

      网友评论

          本文标题:加载GLTF模型

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