【可视化学习】16-利用THREE的水波纹
发表于:2023-06-05 |

前言

加油努力,做大做强,今天就不写基础代码了,直接上核心代码

导入环境贴图

1
2
3
4
5
6
7
8
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader";
// 加载场景背景
const rgbeLoader = new RGBELoader();
rgbeLoader.loadAsync("./assets/050.hdr").then((texture) => {
texture.mapping = THREE.EquirectangularReflectionMapping;
scene.background = texture;
scene.environment = texture;
});

效果图

导入浴缸

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";

// 导入water
import { Water } from "three/examples/jsm/objects/Water2";

// 加载浴缸
const gltfLoader = new GLTFLoader();
gltfLoader.load("./assets/model/yugang.glb", (gltf) => {
console.log(gltf);
const yugang = gltf.scene.children[0];
yugang.material.side = THREE.DoubleSide;

const waterGeometry = gltf.scene.children[1].geometry;
const water = new Water(waterGeometry, {
color: "#ffffff",
scale: 1,
flowDirection: new THREE.Vector2(1, 1),
textureHeight: 1024,
textureWidth: 1024,
});

scene.add(water);
scene.add(yugang);
});

添加光源

1
2
3
4
5
const light = new THREE.AmbientLight(0xffffff); // soft white light
light.intensity = 10;
scene.add(light);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
scene.add(directionalLight);

效果图

注意:如果遇到打包缺失图片,则需要在打包生成的dist文件下把缺失的图片从官网demo中拿下来

上一篇:
【可视化学习】17-利用Shader打造星系
下一篇:
【可视化学习】15-水波纹