前言
加油努力,做大做强,今天就不写基础代码了,直接上核心代码
导入环境贴图
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";
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); light.intensity = 10; scene.add(light); const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5); scene.add(directionalLight);
|
注意:如果遇到打包缺失图片,则需要在打包生成的dist文件下把缺失的图片从官网demo中拿下来