当前位置: 首页 > news >正文

【Three.js基础学习】19.Custom models with Blender

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

blender模型资源:【blender】一个汉堡包-CSDN博客

一、代码

import './style.css'
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js'
import * as dat from 'dat.gui'/*** Base*/
// Debug
const gui = new dat.GUI()// Canvas
const canvas = document.querySelector('canvas.webgl')// Scene
const scene = new THREE.Scene()/*** Models*/
const dracoLoader = new DRACOLoader()
dracoLoader.setDecoderPath('/draco/')const gltfLoader = new GLTFLoader()
gltfLoader.setDRACOLoader(dracoLoader)let mixer = nullgltfLoader.load('/models/hamburger.glb',(gltf) =>{scene.add(gltf.scene)}
)/*** Floor*/
const floor = new THREE.Mesh(new THREE.PlaneBufferGeometry(50, 50),new THREE.MeshStandardMaterial({color: '#444444',metalness: 0,roughness: 0.5})
)
floor.receiveShadow = true
floor.rotation.x = - Math.PI * 0.5
scene.add(floor)/*** Lights*/
const ambientLight = new THREE.AmbientLight(0xffffff, 0.8)
scene.add(ambientLight)const directionalLight = new THREE.DirectionalLight(0xffffff, 0.6)
directionalLight.castShadow = true
directionalLight.shadow.mapSize.set(1024, 1024)
directionalLight.shadow.camera.far = 15
directionalLight.shadow.camera.left = - 7
directionalLight.shadow.camera.top = 7
directionalLight.shadow.camera.right = 7
directionalLight.shadow.camera.bottom = - 7
directionalLight.position.set(5, 5, 5)
scene.add(directionalLight)/*** Sizes*/
const sizes = {width: window.innerWidth,height: window.innerHeight
}window.addEventListener('resize', () =>
{// Update sizessizes.width = window.innerWidthsizes.height = window.innerHeight// Update cameracamera.aspect = sizes.width / sizes.heightcamera.updateProjectionMatrix()// Update rendererrenderer.setSize(sizes.width, sizes.height)renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
})/*** Camera*/
// Base camera
const camera = new THREE.PerspectiveCamera(75, sizes.width / sizes.height, 0.1, 100)
camera.position.set(- 8, 4, 8)
scene.add(camera)// Controls
const controls = new OrbitControls(camera, canvas)
controls.target.set(0, 1, 0)
controls.enableDamping = true/*** Renderer*/
const renderer = new THREE.WebGLRenderer({canvas: canvas
})
renderer.shadowMap.enabled = true
renderer.shadowMap.type = THREE.PCFSoftShadowMap
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))/*** Animate*/
const clock = new THREE.Clock()
let previousTime = 0const tick = () =>
{const elapsedTime = clock.getElapsedTime()const deltaTime = elapsedTime - previousTimepreviousTime = elapsedTimeif(mixer){mixer.update(deltaTime)}// Update controlscontrols.update()// Renderrenderer.render(scene, camera)// Call tick again on the next framewindow.requestAnimationFrame(tick)
}tick()


总结

想学建模,但是没时间!想学three.js,但是忘得差不多了!不想工作,这可怎么办呢。


http://www.mrgr.cn/news/5407.html

相关文章:

  • SonarQube前后端代码质量分析实战
  • 【计算机三级-数据库技术】数据库后台编程技术
  • 【Linux网络】NAT技术
  • 腾讯无界微前端框架介绍
  • 黑神话 悟空 配置 Mac玩游戏
  • 仿Muduo库实现高并发服务器——TcpServer模块
  • 【python】PyQt5的窗口界面的各种交互逻辑实现,轻松掌控图形化界面程序
  • C语言贪吃蛇之BUG满天飞
  • 设计师具备怎样的专业素养,才能走得更远?
  • Nginx 从入门到精通
  • H5护肤品化妆品公司响应式静态HTML网站模板源码
  • object.defineProperty用法
  • Avue实现动态查询与数据展示(附Demo)
  • 教你使用win10实现电脑的定时任务执行
  • vue3项目路由跳转,地址改变了但是页面并没有变怎么解决
  • 高内聚低耦合的理解
  • 电机知识汇总
  • 用Python实现9大回归算法详解——08. 随机森林回归算法
  • 第二百零九节 Java格式 - Java数字格式类
  • Vue3+Vite 解决“找不到模块“@/components/xxx.vue”或其相应的类型声明 ts(2307)”