threeJS19-Loader-json

需要電子檔書籍或者源碼可以Q羣:828202939   希望可以和大家一起學習、一起進步!!

如有錯別字或有理解不到位的地方,可以留言或者加微信15250969798,博主會及時修改!!!!!

博主的案例並不難,只是爲了更好的給想入門threeJS的同學一點點經驗!!!!!

涉及的知識點博主已經從three源碼庫裏面摘要出來放在對應的註釋裏面!

今天學習three裏面json格式模型的加載!這種格式也是官方推薦的格式之一!

效果圖:

代碼:

<html>

<head>
    <title>threeJS19-Loader-json</title>
    <style>
        body {
            background-color: #000;
            margin: 0px;
            overflow: hidden;
        }

        #WebGL {
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            z-index: 999;
        }
    </style>
</head>

<body>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
    <script src="../../../build/three.js"></script>
    <script src="../../js/controls/OrbitControls.js"></script>
    <div id="WebGL"></div>
    <script>
        'use strict';
        var container, camera, scene, renderer, geometry, material, animate, controls; //常用變量
        var spotLight, mshStdBox; //自定義對象變量
        var target = new THREE.Vector3(0, 30, 0);
        var model, skeleton;
        var webGLW = $('#WebGL').width();
        var webGLH = $('#WebGL').height();
        var clock = new THREE.Clock();
        var mixer; //存放動畫數據
        init();
        animate();

        function init() {
            container = document.getElementById('WebGL');
            scene = new THREE.Scene();
            scene.background = new THREE.Color(0xcfcfcf);
            scene.fog = new THREE.Fog(0xa0a0a0, 200, 1000); //霧
            camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 0.1, 1000);
            camera.position.set(0, 100, -150);

            var ambient = new THREE.AmbientLight(0xffffff);
            scene.add(ambient);

            lights(); //燈光:聚光燈
            lightsHelper(spotLight); //燈光顯示助手
            plane() //// 地面
            Box(); //旋轉的立方體
            loadModel(); //添加人物模型
            rendererScene(); //場景渲染
            OrbitControls(camera, renderer); //OrbitControls控件模塊,90版本鼠標右鍵上下是移動,96版本之後右鍵上下是縮放
            window.addEventListener('resize', onWindowResize, false); //監聽屏幕變化

        }

        function plane() {
            // 地面
            let grid_mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(2000, 2000), new THREE.MeshPhongMaterial({
                color: 0x999999,
                depthWrite: false
            }));
            grid_mesh.rotation.x = -Math.PI / 2;
            grid_mesh.receiveShadow = true;
            scene.add(grid_mesh);
            var grid = new THREE.GridHelper(2000, 20, 0x000000, 0x000000);
            grid.material.opacity = 0.2;
            grid.material.transparent = true;
            scene.add(grid);
        }

        function Box() {
            // BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments )
            geometry = new THREE.BoxGeometry(80, 5, 80, 3, 3, 3);
            var matStdObjects = new THREE.MeshStandardMaterial({
                color: 0xA0cf00,
                roughness: 0,
                metalness: 0.6
            });
            mshStdBox = new THREE.Mesh(geometry, matStdObjects);
            mshStdBox.position.set(0, -5, 0);
            mshStdBox.rotation.set(0, Math.PI / 2.0, 0);
            mshStdBox.castShadow = true;
            mshStdBox.receiveShadow = true;
            scene.add(mshStdBox);
        }

        function lights() {
            //  SpotLight( color:顏色, intensity:強度, distance:發光距離, angle:角度, penumbra:邊緣範圍, decay:衰減 )
            spotLight = new THREE.SpotLight(0xffffff, 1);
            spotLight.position.set(10, 120, -30);
            spotLight.angle = Math.PI / 4;
            spotLight.penumbra = 0.05; //邊緣範圍,反比
            spotLight.decay = 2; //衰減係數,反比
            spotLight.distance = 400; //發光距離
            spotLight.castShadow = true; //陰影
            spotLight.shadow.mapSize.width = 1024;
            spotLight.shadow.mapSize.height = 1024;
            spotLight.shadow.camera.near = 10; //近截面
            spotLight.shadow.camera.far = 250;
            scene.add(spotLight);
        }

        function lightsHelper(lightsObject) {
            // 聚光燈顯示助手SpotLightHelper( light:燈光, color:顏色 )
            var lightHelper = new THREE.SpotLightHelper(lightsObject, 0xdfdfdf);
            scene.add(lightHelper);
            var mesh = new THREE.Mesh(new THREE.PlaneBufferGeometry(200, 200), new THREE.MeshPhongMaterial({
                color: 0x9cfcf99,
                depthWrite: false
            }));
            mesh.rotation.x = -Math.PI / 2;
            mesh.position.set(0, -20, 0)
            mesh.receiveShadow = true;
            scene.add(mesh);
        }

        function loadModel() { //加載模型
            // json
            var loader = new THREE.JSONLoader();
            loader.load('../../models/json/legacy/monster/monster.js', function (geometry, materials) {
                material = materials[0];
                material.morphTargets = true;
                material.color.setHex(0xffaaaa);
                var mesh = new THREE.Mesh(geometry, materials);
                mesh.scale.set(0.05, 0.05, 0.05);
                mesh.position.set(0,0,50);
                mesh.rotation.y = Math.PI /2;
                console.log('mesh', mesh);

                //動畫部分
                mixer = new THREE.AnimationMixer(mesh);
                var action = mixer.clipAction(geometry.animations[0]);
                action.play();
                scene.add(mesh);

            });


        }

        function onWindowResize() {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();

            renderer.setSize(window.innerWidth, window.innerHeight);

        }

        function rendererScene() {
            renderer = new THREE.WebGLRenderer({
                antialias: true
            });
            renderer.setPixelRatio(window.devicePixelRatio);
            renderer.setSize(window.innerWidth, window.innerHeight);
            renderer.shadowMap.enabled = true;
            renderer.shadowMap.type = THREE.PCFSoftShadowMap;
            renderer.gammaInput = true;
            renderer.gammaOutput = true;
            container.appendChild(renderer.domElement);
        }

        function OrbitControls(camera, renderer) {
            //OrbitControls控件操作模塊
            controls = new THREE.OrbitControls(camera, renderer.domElement);
            controls.target = target; //控制的target
            controls.autoRotate = true; //是否自動旋轉
            controls.autoRotateSpeed = 0.5; //自動旋轉速度,正比
        }

        function animate() {
            requestAnimationFrame(animate);
            if (controls) controls.update();
            mshStdBox.rotation.y += 0.01;
            var delta = clock.getDelta();
            if (mixer) mixer.update(delta);
            renderer.render(scene, camera);
        };
    </script>
</body>

</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章