-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathscene-fog.ts
151 lines (130 loc) · 3.99 KB
/
scene-fog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @title Scene Fog
* @category Scene
* @thumbnail https://mdn.alipayobjects.com/merchant_appfe/afts/img/A*FEbDQLYuRNkAAAAAAAAAAAAADiR2AQ/original
*/
import {
AmbientLight,
AssetType,
Camera,
Color,
DirectLight,
FogMode,
GLTFResource,
Scene,
ShadowType,
Vector3,
WebGLEngine,
} from "@galacean/engine";
import { FreeControl } from "@galacean/engine-toolkit-controls";
import * as dat from "dat.gui";
async function main() {
const engine = await WebGLEngine.create({ canvas: "canvas" });
engine.canvas.resizeByClientSize();
const scene = engine.sceneManager.activeScene;
// Set background color to corn flower blue
const cornFlowerBlue = new Color(130 / 255, 163 / 255, 255 / 255);
scene.background.solidColor = cornFlowerBlue;
// Set fog
scene.fogMode = FogMode.ExponentialSquared;
scene.fogDensity = 0.015;
scene.fogEnd = 200;
scene.fogColor = cornFlowerBlue;
const rootEntity = scene.createRootEntity();
// Create camera entity and components
const cameraEntity = rootEntity.createChild("camera");
cameraEntity.transform.setPosition(-6, 2, -22);
cameraEntity.transform.rotate(new Vector3(0, -110, 0));
cameraEntity.addComponent(Camera);
cameraEntity.addComponent(FreeControl).floorMock = false;
// Create light entity and component
const lightEntity = rootEntity.createChild("light");
lightEntity.transform.setPosition(0, 0.7, 0.5);
lightEntity.transform.lookAt(new Vector3(0, 0, 0));
// Enable light cast shadow
const directLight = lightEntity.addComponent(DirectLight);
directLight.shadowType = ShadowType.SoftLow;
// Add ambient light
const ambientLight = await engine.resourceManager.load<AmbientLight>({
url: "https://gw.alipayobjects.com/os/bmw-prod/09904c03-0d23-4834-aa73-64e11e2287b0.bin",
type: AssetType.Env,
});
scene.ambientLight = ambientLight;
// Add model
const glTFResource = await engine.resourceManager.load<GLTFResource>(
"https://gw.alipayobjects.com/os/OasisHub/19748279-7b9b-4c17-abdf-2c84f93c54c8/oasis-file/1670226408346/low_poly_scene_forest_waterfall.gltf"
);
rootEntity.addChild(glTFResource.defaultSceneRoot);
engine.run();
// Add debug GUI for fog
addDebugGUI(scene);
}
function addDebugGUI(scene: Scene): void {
let fogStartItem;
let fogEndItem;
let fogDensityItem;
const gui = new dat.GUI();
let switchMode = (mode: FogMode) => {
switch (mode) {
case FogMode.None:
clearModeItems();
break;
case FogMode.Linear:
clearModeItems();
fogStartItem = gui.add(debugInfos, "fogStart", 0, 300).onChange((v) => {
scene.fogStart = v;
});
fogEndItem = gui.add(debugInfos, "fogEnd", 0, 300).onChange((v) => {
scene.fogStart = v;
});
break;
case FogMode.Exponential:
case FogMode.ExponentialSquared:
clearModeItems();
fogDensityItem = gui
.add(debugInfos, "fogDensity", 0.01, 0.1)
.onChange((v) => {
scene.fogDensity = v;
});
break;
}
scene.fogMode = mode;
};
let clearModeItems = () => {
if (fogStartItem) {
fogStartItem.remove();
fogStartItem = null;
}
if (fogEndItem) {
fogEndItem.remove();
fogEndItem = null;
}
if (fogDensityItem) {
fogDensityItem.remove();
fogDensityItem = null;
}
};
const fogColor = scene.fogColor;
const debugInfos = {
fogMode: scene.fogMode,
fogColor: [fogColor.r * 255, fogColor.g * 255, fogColor.b * 255],
fogStart: scene.fogStart,
fogEnd: scene.fogEnd,
fogDensity: scene.fogDensity,
};
gui
.add(debugInfos, "fogMode", {
None: FogMode.None,
Linear: FogMode.Linear,
Exponential: FogMode.Exponential,
ExponentialSquared: FogMode.ExponentialSquared,
})
.onChange((v) => {
switchMode(parseInt(v));
});
gui.addColor(debugInfos, "fogColor").onChange((v) => {
scene.fogColor.set(v[0] / 255, v[1] / 255, v[2] / 255, 1.0);
});
switchMode(scene.fogMode);
}
main();