Threejs实现数字孪生, 高速收费站, 汽车模型

threejs三大要素

场景 相机 渲染器

// 场景
const scene = new THREE.Scene();
scene.background = new THREE.Color("#cccccc")
scene.environment = "#cccccc"
// 相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 8, 45);
// 渲染器
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;

开启灯光

// 灯光
// 前灯光
const light1 = new THREE.DirectionalLight(0xffffff, 1)
light1.position.set(0, 0, 10)
scene.add(light1)
// 后灯光
const light2 = new THREE.DirectionalLight(0xffffff, 1)
light2.position.set(0, 0, -10)
scene.add(light2)
// 左灯光
const light3 = new THREE.DirectionalLight(0xffffff, 1)
light3.position.set(-10, 0, 0)
scene.add(light3)
// 右灯光
const light4 = new THREE.DirectionalLight(0xffffff, 1)
light4.position.set(10, 0, 0)
scene.add(light4)
// 上灯光
const light5 = new THREE.DirectionalLight(0xffffff, 1)
light5.position.set(0, 10, 0)
scene.add(light5)

加载模型

const loader = new GLTFLoader()
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath("/draco/gltf/");
loader.setDRACOLoader(dracoLoader);
//后续可以加载各种模型
// 警车
let policeCars;
loader.load('/public/shoufeizhan/警车.glb', gltf => {
  policeCars = gltf.scene.children[0]
  scene.add(policeCars)
})
// 客车
let coachCar;
loader.load('/public/shoufeizhan/客车.glb', gltf => {
  coachCar = gltf.scene
  scene.add(coachCar)
})

loader.load('/public/shoufeizhan/收费站.glb', gltf => {
  console.log(gltf);
  const model = gltf.scene
  model.traverse(child => {
    if (child.name.includes('通行杆')) {
      child.castShadow = true
      child.receiveShadow = true
      child.children.forEach(c => {
        c.isRotated = false
      })
      passagePoleList = child.children
    }

动画 Tweenjs

// 栏杆抬起放下的动画
function railingRotation(target) {
  return new Promise((res, rej) => {
    const targetRotation = { z: target.isRotated ? 0 : Math.PI / 2 };
    const rotationTween = new Tween(target.rotation).to(targetRotation, 1000).easing(TWEEN.Easing.Quadratic.InOut).start()
    tweens.push(rotationTween)
    rotationTween.onComplete(() => {
      target.isRotated = !target.isRotated
    })
  })
}

最终实现

高速收费站实现

在这里插入图片描述

由于demo涉及较多glb模型 gltf模型 占用内存较大 代码量也大 可以添加一下微信 备注threejs 发完整的项目zip包

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值