Three.js 官网

CDN加载

three.js (vr128) - Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它在 web 中创建各种三维场景,包括了摄影机、光影、材质等各种对象。 | BootCDN - Bootstrap 中文网开源项目免费 CDN 加速服务
https://www.bootcdn.cn/three.js/

官网

https://threejs.org/

下载

https://github.com/mrdoob/three.js/archive/master.zip
图片.png
three.js使用 build 中的文件

Loader

src/loaders里面有各种loader
examples\js\loaders
图片.png
图片.png
本次案例使用的是图片.png

网上找gtlf类型的3d资源

搜索_免费_glb gltf素材网

http://glbxz.com/i/

本次使用的资源

餐具007免费_glb gltf素材网
http://glbxz.com/mobile/qingwen/1845.html

代码部分

index.html
加载three.js
加载脚本

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="stylesheet" href="style.css">
  8. <title>Document</title>
  9. </head>
  10. <body>
  11. <div class="scene">
  12. </div>
  13. <script src="./three.min.js"></script>
  14. <script src="./GLTFLoader.js"></script>
  15. <script src="./app.js"></script>
  16. </body>
  17. </html>

app.js

  1. 声明容器,照相机,渲染器,场景,展示对象变量
  2. 获取dom元素作为容器
  3. 使用THREE创建场景
  4. 声明视角变量fov,aspect比例,近点0.1,范围远点500
  5. 创建视角照相机
  6. 设置照相机位置
  7. 创建环境光 设置环境光位置,为场景添加环境光
  8. 创建webgl渲染器
  9. 设置渲染器大小为容器大小,比例为容器比例
  10. 设置渲染器比率为显示器比率
  11. 创建GITFloader
  12. 加载gltf模型
  13. 把gltf模型添加到场景中
  14. 为模型添加animation
  15. 场景渲染
  16. 监听window的resize事件
  17. 窗口比例变化重新设定照相机的视角比例 更新矩阵 设置大小 ```javascript //Variables for setup

let container; let camera; let renderer; let scene; let house;

function init() { container = document.querySelector(“.scene”);

//Create scene scene = new THREE.Scene();

const fov = 35; const aspect = container.clientWidth / container.clientHeight; const near = 0.1; const far = 500;

//camera setup

camera = new THREE.PerspectiveCamera(fov, aspect, near, far); camera.position.set(0.1, 0.2, 2.5);

const ambient = new THREE.AmbientLight(0xefefef, 0.5);

scene.add(ambient);

const light = new THREE.DirectionalLight(0xffffff, 0.7); light.position.set(10, 10, 10); scene.add(light);

//renderer setup renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); renderer.setSize(container.clientWidth, container.clientHeight); renderer.setPixelRatio(window.devicePixelRatio);

container.appendChild(renderer.domElement);

//load model let loader = new THREE.GLTFLoader(); loader.load(“./3d/glbxz.com-0007.gltf”, function (gltf) { scene.add(gltf.scene); console.log(gltf.scene); house = gltf.scene.children; // renderer.render(scene, camera); animate(); }); }

function animate() { requestAnimationFrame(animate); house.forEach((item) => { // item.rotation.z += 0.005; // item.rotation.x += 0.005; item.rotation.y += 0.05; });

renderer.render(scene, camera); }

function onWindowResize() { camera.aspect = container.clientWidth / container.clientHeight; camera.updateProjectionMatrix(); renderer.setSize(container.clientWidth, container.clientHeight); }

window.addEventListener(“resize”,onWindowResize) init();

  1. style.css
  2. ```javascript
  3. *{
  4. margin: 0;
  5. padding: 0;
  6. box-sizing: border-box;
  7. }
  8. body{
  9. overflow: hidden;
  10. width: 100vw;
  11. height: 100vh;
  12. background: #757f9a; /* fallback for old browsers */ background: -webkit-linear-gradient(to right, rgb(117, 127, 154), rgb(215, 221, 232)); /* Chrome 10-25, Safari 5.1-6 */ background: linear-gradient(to right, rgb(117, 127, 154), rgb(215, 221, 232)); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
  13. }
  14. .scene,canvas{
  15. position: absolute;
  16. width: 100%;
  17. height: 100%;
  18. }

实现效果

旋转
图片.png