简书链接:threejs3D静态导入的三种方式
文章字数:416,阅读全文大约需要1分钟
参考
https://threejs.org/docs/index.html#manual/zh/introduction/Creating-a-scene
方式1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <html>
<head> <script type="importmap"> { "imports": { "three": "https://unpkg.com/[email protected]/build/three.module.js" } } </script> <script type="module">
import * as THREE from 'three';
// 创建一个场景 const scene = new THREE.Scene();
</script> </head> <body> </body> </html>
|
方式2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <html>
<head> <script type="module"> import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js';
// 创建一个场景 const scene = new THREE.Scene(); </script> </head> <body> </body> </html>
|
方式3 本地导入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <html>
<head> <script type="module"> import * as THREE from './three0.148.0.js';//这个文件从cdn下载的
// 创建一个场景 const scene = new THREE.Scene(); </script> </head> <body> </body> </html>
|
157 的加载方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| <html>
<head> <script type="importmap"> { "imports": { "three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js", "three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/" } }
</script> <!-- { "imports": { "three": "../build/three.module.js", "three/addons/": "./jsm/" } } -->
<script type="module"> import * as THREE from "three"; // 导入轨道控制器 // import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js'; import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; // console.log(THREE);
// 目标:使用控制器查看3d物体
// 1、创建场景 const scene = new THREE.Scene();
// 2、创建相机 const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
// 设置相机位置 camera.position.set(0, 0, 10); scene.add(camera);
// 添加物体 // 创建几何体 const cubeGeometry = new THREE.BoxGeometry(1, 1, 1); const cubeMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 }); // 根据几何体和材质创建物体 const cube = new THREE.Mesh(cubeGeometry, cubeMaterial); // 将几何体添加到场景中 scene.add(cube);
// 初始化渲染器 const renderer = new THREE.WebGLRenderer(); // 设置渲染的尺寸大小 renderer.setSize(window.innerWidth, window.innerHeight); // console.log(renderer); // 将webgl渲染的canvas内容添加到body document.body.appendChild(renderer.domElement);
// // 使用渲染器,通过相机将场景渲染进来 // renderer.render(scene, camera);
// 创建轨道控制器 const controls = new OrbitControls(camera, renderer.domElement);
// 添加坐标轴辅助器 const axesHelper = new THREE.AxesHelper(5); scene.add(axesHelper);
function render() { renderer.render(scene, camera); // 渲染下一帧的时候就会调用render函数 requestAnimationFrame(render); }
render(); </script> </head>
<body>
</body>
</html>
|
某老版本131 的加载方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Three.js OrbitControls Example</title> <style> body { margin: 0; } </style> </head> <body> <script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<script> // 创建场景、相机和渲染器 const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement);
// 创建一个立方体并将其添加到场景 const geometry = new THREE.BoxGeometry(); const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 }); const cube = new THREE.Mesh(geometry, material); scene.add(cube);
// 设置相机位置和初始视角 camera.position.z = 5;
// 创建 OrbitControls 实例并将其附加到相机 const controls = new THREE.OrbitControls(camera, renderer.domElement);
// 渲染场景 function animate() { requestAnimationFrame(animate); cube.rotation.x += 0.01; cube.rotation.y += 0.01; controls.update(); // 更新OrbitControls renderer.render(scene, camera); } animate(); </script> </body> </html>
|