CDN模型下使用three.js 加载glb模型

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="importmap">
        {
          "imports": {
            "three": "https://cdn.jsdelivr.net/npm/three@v0.167.1/build/three.module.js",
            "three/addons/": "https://cdn.jsdelivr.net/npm/three@v0.167.1/examples/jsm/"
          }
        }
    </script>
    <script type="module" src="/main.js"></script>
</head>
<body>
<div id="mymodel" style="width:500px; height: 600px;margin:100px auto;"> </div>
</body>
</html>
javascript 复制代码
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';



const elem = document.getElementById("mymodel");

const camera = new THREE.PerspectiveCamera(50,
    elem.clientWidth / elem.clientHeight,1, 1000);

const scene = new THREE.Scene();
//const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );

const renderer = new THREE.WebGLRenderer();
renderer.setSize(elem.clientWidth, elem.clientHeight);

renderer.setPixelRatio(window.devicePixelRatio);
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1;
renderer.outputEncoding = THREE.sRGBEncoding;

elem.appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); // 环境光
scene.add(ambientLight);

window.addEventListener('resize', function () {
    renderer.setSize(elem.clientWidth, elem.clientHeight);
    camera.aspect = elem.clientWidth/elem.clientHeight;
    camera.updateProjectionMatrix();
}, false);


const loader = new GLTFLoader();
loader.load( '/generated_1723538759.glb', function ( gltf ) {
    console.log( 'loaded',gltf)
    camera.position.set(4, 0, 0);
    gltf.scene.position.y = 0.2
    scene.add( gltf.scene );

}, undefined, function ( error ) {

    console.error( error );

} );

const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // 启用 damping 给控制器带来重量感
controls.dampingFactor = 0.1;  //动态系数因子 越小越灵敏
controls.rotateSpeed = 0.1;
controls.enableZoom = true; // 是否可以缩放
controls.autoRotate = false; // 是否自动旋转
controls.autoRotateSpeed = 5; // 自动旋转速度

controls.minDistance = 2;
controls.maxDistance = 10;
controls.target.set(0, 0.5, - 0.2);
// 决定了初始化时 模型在页面视野中的角度




function animate() {
    controls.update();
    renderer.render( scene, camera );
 }
renderer.setAnimationLoop( animate );
相关推荐
workflower15 小时前
时序数据获取事件
开发语言·人工智能·python·深度学习·机器学习·结对编程
CoderYanger16 小时前
C.滑动窗口-求子数组个数-越长越合法——2799. 统计完全子数组的数目
java·c语言·开发语言·数据结构·算法·leetcode·职场和发展
C++业余爱好者16 小时前
Java 提供了8种基本数据类型及封装类型介绍
java·开发语言·python
林杜雨都16 小时前
Action和Func
开发语言·c#
皮卡龙16 小时前
Java常用的JSON
java·开发语言·spring boot·json
火山灿火山17 小时前
Qt常用控件(三)
开发语言·qt
拉不动的猪17 小时前
webpack编译中为什么不建议load替换ast中节点删除consolg.log
前端·javascript·webpack
利刃大大17 小时前
【JavaSE】十三、枚举类Enum && Lambda表达式 && 列表排序常见写法
java·开发语言·枚举·lambda·排序
float_六七17 小时前
Java反射:万能遥控器拆解编程
java·开发语言
han_hanker17 小时前
java 异常类——详解
java·开发语言