CDN模子下利用three.js 加载glb模子

[复制链接]
发表于 2026-2-6 08:19:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Title</title>
  6.     <script type="importmap">
  7.         {
  8.           "imports": {
  9.             "three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
  10.             "three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/"
  11.           }
  12.         }
  13.     </script>
  14.     <script type="module" src="/main.js"></script>
  15. </head>
  16. <body>
  17. <div id="mymodel" style="width:500px; height: 600px;margin:100px auto;"> </div>
  18. </body>
  19. </html>
复制代码
  1. import * as THREE from 'three';
  2. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  3. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  4. const elem = document.getElementById("mymodel");
  5. const camera = new THREE.PerspectiveCamera(50,
  6.     elem.clientWidth / elem.clientHeight,1, 1000);
  7. const scene = new THREE.Scene();
  8. //const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
  9. const renderer = new THREE.WebGLRenderer();
  10. renderer.setSize(elem.clientWidth, elem.clientHeight);
  11. renderer.setPixelRatio(window.devicePixelRatio);
  12. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  13. renderer.toneMappingExposure = 1;
  14. renderer.outputEncoding = THREE.sRGBEncoding;
  15. elem.appendChild(renderer.domElement);
  16. const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); // 环境光
  17. scene.add(ambientLight);
  18. window.addEventListener('resize', function () {
  19.     renderer.setSize(elem.clientWidth, elem.clientHeight);
  20.     camera.aspect = elem.clientWidth/elem.clientHeight;
  21.     camera.updateProjectionMatrix();
  22. }, false);
  23. const loader = new GLTFLoader();
  24. loader.load( '/generated_1723538759.glb', function ( gltf ) {
  25.     console.log( 'loaded',gltf)
  26.     camera.position.set(4, 0, 0);
  27.     gltf.scene.position.y = 0.2
  28.     scene.add( gltf.scene );
  29. }, undefined, function ( error ) {
  30.     console.error( error );
  31. } );
  32. const controls = new OrbitControls(camera, renderer.domElement);
  33. controls.enableDamping = true; // 启用 damping 给控制器带来重量感
  34. controls.dampingFactor = 0.1;  //动态系数因子 越小越灵敏
  35. controls.rotateSpeed = 0.1;
  36. controls.enableZoom = true; // 是否可以缩放
  37. controls.autoRotate = false; // 是否自动旋转
  38. controls.autoRotateSpeed = 5; // 自动旋转速度
  39. controls.minDistance = 2;
  40. controls.maxDistance = 10;
  41. controls.target.set(0, 0.5, - 0.2);
  42. // 决定了初始化时 模型在页面视野中的角度
  43. function animate() {
  44.     controls.update();
  45.     renderer.render( scene, camera );
  46. }
  47. renderer.setAnimationLoop( animate );
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表