Three.js湖边小屋,包含gltf渲染、天空和水纹、光照阴影、运动的点光源、相机位置和文字切屏、粒子效果等

前期准备

使用vue3+vite+three.js+gsap 开发

npm install three gsap

代码

<script setup>
// 导入three.js
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 { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader'
// 引入官方水效果
import { Water } from 'three/examples/jsm/objects/Water2.js';
// 导入补间动画库
import gsap from 'gsap';
import { ref, onMounted } from 'vue';
// 创建场景
const scene = new THREE.Scene();
// 初始化相机
const camera = new THREE.PerspectiveCamera(
    45, // 视角
    window.innerWidth / window.innerHeight, // 宽高比
    0.1, // 近平面
    1000 // 远平面
);
camera.position.set(-3.23,2.98,4.06);
camera.updateProjectionMatrix();
// 初始化渲染器
const renderer = new THREE.WebGLRenderer(
  //设置抗锯齿
  { antialias: true } 
);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 设置色调映射
renderer.outputEncoding = THREE.sRGBEncoding
renderer.toneMapping = THREE.ACESFilmicToneMapping
renderer.toneMappingExposure = 0.5
renderer.shadowMap.enabled = true
// 调节亮度
renderer.toneMappingExposure = 1

// 初始化控制器
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;

// 初始化loader
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/v1/decoders/');
// 初始化gltfLoader
const gltfLoader = new GLTFLoader();
gltfLoader.setDRACOLoader(dracoLoader);
// 加载环境纹理
let rgbeLoader = new RGBELoader()
rgbeLoader.load('./textures/sky.hdr',(texture)=>{
  texture.mapping = THREE.EquiretangularReflectiionMapping;
  scene.background = texture
  scene.environment = texture
})
gltfLoader.load("./model/scene.glb",(gltf)=>{
  const model = gltf.scene;
  model.traverse((child)=>{
    if(child.name = "Plane"){
     child.visible = false
    }
    if(child.isMesh){
      child.castShadow = true
      child.receiveShadow = true
    }
  })
  scene.add(model)
})

// 添加水效果
const waterGeometry = new THREE.CircleGemoetry(300,32)
const water = new Water(
  waterGeometry,
  {
    // 数值越大效果越明显
    textureWidth: 2048,
    textureHeight: 2048,
    color:0xeeeeff,
    flowDirection: new THREE.Vector2(1,1),
    scale:2,
  }
)
// 设置水的旋转,否则导致房子浸泡在水中
water.rotation.x = -Math.PI / 2
// 设置水的位置
water.position.y = -1
scene.add(water)

// 加载模型
gltfLoader.load(
  'models/scene.gltf',
  (gltf) => {
    scene.add(gltf.scene);
    // 设置模型位置
    gltf.scene.position.set(0,0,0);
    // 设置模型大小
    gltf.scene.scale.set(0.01,0.01,0.01);
    // 设置模型旋转
    gltf.scene.rotation.set(0,0,0);
  },
  (xhr) => {
    console.log((xhr.loaded / xhr.total * 100) + '% loaded');
  },
  (error) => {
    console.log('error!', error);
  }
);

// 添加平行光
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(0, 50, 0);
scene.add(light);
// 添加点光源
const pointLight = new THREE.PointLight(0xffffff, 100);
pointLight.position.set(0.1,2.4, 0);
pointLight.castShadow = true;
scene.add(pointLight);
// 创建点光源组
const pointLightGroup = new THREE.Group();
pointLightGroup.position.set(-8,2.5,-1.5);
// 球绕圆的半径
let radius = 3;
let pointLightArr = [];
for(let i = 0; i < 3; i++){
  // 创建点光源
  const sphereGeometry = new THREE.SphereGeometry(0.1, 32, 32);
  const sphereMaterial = new THREE.MeshStandardMaterial({
    color: 0xffffff,
    emissive: 0xffffff,
    emissiveIntensity: 10,
    roughness: 0.5,
    metalness: 0.5
  })
  const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
  sphere.position.set(radius * Math.cos(i * 120 * Math.PI / 180), Math.cos((i * 120 * Math.PI) / 180), radius * Math.sin(i * 120 * Math.PI / 180));

  // 添加到点光源组
  pointLightGroup.add(sphere);

  const pointLight = new THREE.PointLight(0xffffff, 1);
  sphere.add(pointLight);
    // 将点光源添加到点光源组
  pointLightGroup.add(sphere);
}
scene.add(pointLightGroup);
function render(){
  requestAnimationFrame(render);
  renderer.render(scene, camera);
  controls.update();
}

// 使用补间动画,从0到2pi,使灯泡旋转
let options = {
  angle:0
}
gsap.to(options,{
  angle:Math.PI * 2,
  duration:10,
  repeat:-1,
  ease:"linear",
  onUpdate:()=>{
    pointLightGroup.rotation.y = options.angle
    pointLightArr.forEach((item,index)=>{
      item.position.set(radius * Math.cos((index * 120) * Math.PI / 180), Math.cos(((index * 120) * Math.PI) / 180), radius * Math.sin((index * 120) * Math.PI / 180));
    })
  }
})
render();

// 使用补间动画,移动相机
let timeLine1 = gsap.timeline();
let timeline2 = gsap.timeline();

// 定义相机移动函数
function translateCamera(position,target){
  timeLine1.to(camera.position,{
    x:position.x,
    y:position.y,
    z:position.z,
    duration:2,
    ease:"power2.inOut"
  })

  timeline2.to(camera.position,{
    x:target.x,
    y:target.y,
    z:target.z,
    duration:2,
    ease:"power2.inOut"
  })
}

let words = [
  {
    text:"Attendre et espérer !",
    callback:()=>{
      new THREE.Vector3(-3.23,2.98,4.06),
      new THREE.Vector3(-8, 2, 0)
    },
  },
  {
    text:"Attendre et espérer !!",
    callback:()=>{
      new THREE.Vector3(7,0,23),
      new THREE.Vector3(0,0,0)
    },
  },
  {
    text:"Attendre et espérer !!!",
    callback:()=>{
      new THREE.Vector3(10,3,0),
      new THREE.Vector3(5,2,0)
    },
  },
  {
    text:"Attendre et espérer !!!!",
    callback:()=>{
      new THREE.Vector3(7,0,23),
      new THREE.Vector3(0,0,0)
    },
  },
  {
    text:"Attendre et espérer !!!!!",
    callback:()=>{
      new THREE.Vector3(-20,1.3,6.6),
      new THREE.Vector3(5,2,0)
      // 调用画爱心函数
      makeHeart()
    },
  },
]

let index = ref(0)
let isAnimate = false
// 监听鼠标滚轮事件
window.addEventListener('wheel',(e)=>{
  // 操作节流
  if(isAnimate){
    return
  }
  if(e.deltaY > 0){
    index.value++
    if(index.value > words.length - 1){
      index.value = 0
    }
  }
  words[index.value].callback()
  setTimeout(() => {
    isAnimate = false
  }, 1000);
})

// 实例化星星
let starsInstance = new THREE.InstanceMesh(
  new THREE.SphereGeometry(0.1,32,32),
  new THREE.MeshStandardMaterial({
    color:0xffffff,
    emissive:0xffffff,
    emissiveIntensity:10,
    roughness:0.5,
    metalness:0.5
  }),
  100
);

// 随机分布星星
let starsArr = []
let endArr = []
for(let i = 0; i < 100; i++){
  let x = Math.random() * 100 - 50
  let y = Math.random() * 100 - 50
  let z = Math.random() * 100 - 50
  starsArr.push(new THREE.Vector3(x,y,z))
  // 矩阵修改位移
  let matrix = new THREE.Matrix4()
  matrix.setPosition(x,y,z)
  starsInstance.setMatrixAt(i,matrix)
}
scene.add(starsInstance)

// 使用贝塞尔曲线,实现星星移动
let heartShape = new THREE.Shape()
heartShape.moveTo(25,25)
heartShape.bezierCurveTo(25,25,20,0,0,0)
heartShape.bezierCurveTo(-30,0,-30,35,-30,35)
heartShape.bezierCurveTo(-30,55,-10,77,25,95)
heartShape.bezierCurveTo(60,77,80,55,80,35)
heartShape.bezierCurveTo(80,35,80,0,50,0)
heartShape.bezierCurveTo(35,0,25,25,25,25)

//根据路径获取点
for(let i = 0; i < 100; i++){
  let point = heartShape.getPointAt(i / 100)
  endArr.push(new THREE.Vector3(point.x,point.y,point.z))
}

// 创建动画
function makeHeart(){
  let params = {
    time:0
  }

  gsap.to(params,{
    time:1,
    duration:10,
    ease:"linear",
    onUpdate:()=>{
      for(let i = 0; i < 100; i++){
        let x = THREE.MathUtils.lerp(starsArr[i].x,endArr[i].x,params.time)
        let y = THREE.MathUtils.lerp(starsArr[i].y,endArr[i].y,params.time)
        let z = THREE.MathUtils.lerp(starsArr[i].z,endArr[i].z,params.time)
        let matrix = new THREE.Matrix4()
        matrix.setPosition(x,y,z)
        starsInstance.setMatrixAt(i,matrix)
      }
      starsInstance.instanceMatrix.needsUpdate = true
    }
  })
}
</script>
<template>
<div class="words" style="position:fixed;left: 0;top:0;z-index:10;pointer-events: none;transition: all 1s;" :style="{
    transform:`translate(${index.value * -100}vw,0)`
  }"> 
  <div v-for="item in words" style="width: 100vw;height: 100vh;">
    <h1 style="padding: 100px 50px;font-size:50px;color:#fff">{{item.text}}</h1>
  </div>
</div>
</template>
<style scoped>
*{
  margin:0;
  padding:0;
}
canvas{
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
}
</style>

效果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用three.js加载glTF模型非常简单,你可以按照以下步骤进行操作: 1. 引入three.jsGLTFLoader.js文件。 ```html <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/build/three.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/loaders/GLTFLoader.js"></script> ``` 2. 创建一个场景、相机渲染器。 ```javascript 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); ``` 3. 加载glTF模型。 ```javascript const loader = new THREE.GLTFLoader(); loader.load('path/to/model.gltf', function(gltf) { scene.add(gltf.scene); }, undefined, function(error) { console.error(error); }); ``` 4. 渲染场景。 ```javascript function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); } animate(); ``` 完整的代码如下所示: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Load glTF Model with Three.js</title> <style> body { margin: 0; overflow: hidden; } </style> </head> <body> <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/build/three.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/loaders/GLTFLoader.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 loader = new THREE.GLTFLoader(); loader.load('path/to/model.gltf', function(gltf) { scene.add(gltf.scene); }, undefined, function(error) { console.error(error); }); function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); } animate(); </script> </body> </html> ``` 请注意,上述代码中的路径"path/to/model.gltf"应该替换为你自己的glTF模型的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值