three.js学习笔记四:加载本地.gltf模型

效果

three.js学习笔记四:加载本地.gltf模型

代码


<html>
  <head>
    <meta charset="utf-8" />
    <title>My first three.js app</title>
    <style>
      body {
        margin: 0;
      }
    </style>
  </head>
  <body>
    <script type="module">
      import * as THREE from "./three/build/three.module.js";
      import { GLTFLoader } from "./three/examples/jsm/loaders/GLTFLoader.js";
      // 场景
      const scene = new THREE.Scene();
      // 相机
      const camera = new THREE.PerspectiveCamera(
        35,
        window.innerWidth / window.innerHeight,
        1,
        10
      );

      // 添加灯光
      const light = new THREE.AmbientLight("#ffffff"); // soft white light
      scene.add(light);

      const renderer = new THREE.WebGLRenderer();
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);

      camera.position.z = 5;

      const loader = new GLTFLoader();

      loader.load(
        "gril/scene.gltf",
        function (gltf) {
          scene.add(gltf.scene);
          console.log("加载完成");
        },
        (e) => {
          console.log("加载中", e);
        },
        function (error) {
          console.error(error);
        }
      );

      function animate() {
        requestAnimationFrame(animate);
        renderer.render(scene, camera);
      }

      animate();
    </script>
  </body>
</html>

上一篇:cesium试使用


下一篇:Ubuntu换源官方指引