-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathshader-replacement.ts
218 lines (188 loc) · 5.61 KB
/
shader-replacement.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @title Shader Replacement
* @category Material
* @thumbnail https://mdn.alipayobjects.com/merchant_appfe/afts/img/A*p9uTTKU3vtwAAAAAAAAAAAAADiR2AQ/original
*/
import { OrbitControl } from "@galacean/engine-toolkit-controls";
import * as dat from "dat.gui";
import {
AmbientLight,
AssetType,
BackgroundMode,
Camera,
DirectLight,
GLTFResource,
Logger,
PrimitiveMesh,
Shader,
SkyBoxMaterial,
Texture2D,
WebGLEngine,
} from "@galacean/engine";
/**
* Main function.
*/
async function main() {
Logger.enable();
initShader();
// Create engine
const engine = await WebGLEngine.create({ canvas: "canvas" });
engine.canvas.resizeByClientSize();
// Get scene and create root entity
const scene = engine.sceneManager.activeScene;
const rootEntity = scene.createRootEntity();
// Create directional light
const directLightEntity = rootEntity.createChild("Directional Light");
directLightEntity.addComponent(DirectLight);
directLightEntity.transform.setRotation(30, 0, 0);
const directLightEntity2 = rootEntity.createChild("Directional Light2");
directLightEntity2.addComponent(DirectLight);
directLightEntity2.transform.setRotation(-30, 180, 0);
// Create camera
const cameraEntity = rootEntity.createChild("Camera");
cameraEntity.transform.setPosition(0, 0, 5);
const camera = cameraEntity.addComponent(Camera);
cameraEntity.addComponent(OrbitControl);
// Create sky
const background = scene.background;
background.mode = BackgroundMode.Sky;
const sky = background.sky;
const skyMaterial = new SkyBoxMaterial(engine);
sky.material = skyMaterial;
sky.mesh = PrimitiveMesh.createCuboid(engine, 1, 1, 1);
engine.resourceManager
.load([
{
type: AssetType.Env,
url: "https://gw.alipayobjects.com/os/bmw-prod/f369110c-0e33-47eb-8296-756e9c80f254.bin",
},
{
url: "https://gw.alipayobjects.com/os/bmw-prod/150e44f6-7810-4c45-8029-3575d36aff30.gltf",
type: AssetType.GLTF,
},
{
url: "https://gw.alipayobjects.com/os/OasisHub/267000040/9994/%25E5%25BD%2592%25E6%25A1%25A3.gltf",
type: AssetType.GLTF,
},
{
url: "https://mdn.alipayobjects.com/huamei_b4l2if/afts/img/A*Q60vQ40ZERsAAAAAAAAAAAAADil6AQ/original",
type: AssetType.Texture2D,
},
])
.then((resources) => {
// Add ambient light
const ambientLight = <AmbientLight>resources[0];
scene.ambientLight = ambientLight;
skyMaterial.texture = ambientLight.specularTexture;
skyMaterial.textureDecodeRGBM = true;
// Add helmet model
const glTFResourceHelmet = <GLTFResource>resources[1];
const helmetEntity = glTFResourceHelmet.defaultSceneRoot;
helmetEntity.transform.position.set(-1, 0, 0);
rootEntity.addChild(helmetEntity);
// Add duck model
const glTFResourceDuck = <GLTFResource>resources[2];
const duckEntity = glTFResourceDuck.defaultSceneRoot;
duckEntity.transform.position.set(1, -1, 0);
rootEntity.addChild(duckEntity);
// Apply uv check texture
const uvCheckTexture = <Texture2D>resources[3];
for (const material of glTFResourceHelmet.materials!) {
material.shaderData.setTexture("u_UVCheckTexture", uvCheckTexture);
}
for (const material of glTFResourceDuck.materials!) {
material.shaderData.setTexture("u_UVCheckTexture", uvCheckTexture);
}
// Run engine
engine.run();
// Add debug GUI to switch replacement shader
addDebugGUI(camera);
});
}
main();
/**
* Init replacement shader.
*/
function initShader() {
const normalVS = `
#include <common>
#include <common_vert>
#include <blendShape_input>
#include <normal_share>
void main() {
#include <begin_position_vert>
#include <begin_normal_vert>
#include <blendShape_vert>
#include <skinning_vert>
#include <normal_vert>
#include <position_vert>
}`;
const normalFS = `
#include <common>
#include <normal_share>
void main() {
gl_FragColor = vec4(v_normal,1.0);
}
`;
const uvCheckVS = `
#include <common>
#include <common_vert>
#include <blendShape_input>
#include <uv_share>
void main() {
#include <begin_position_vert>
#include <blendShape_vert>
#include <skinning_vert>
#include <uv_vert>
#include <position_vert>
}`;
const uvCheckFS = `
#include <common>
#include <uv_share>
uniform sampler2D u_UVCheckTexture;
void main() {
vec4 textureColor = texture2D(u_UVCheckTexture, v_uv);
gl_FragColor = textureColor;
}
`;
// Create normal shader
Shader.create("NormalShader", normalVS, normalFS);
// Create uv check shader
Shader.create("UVCheckShader", uvCheckVS, uvCheckFS);
}
/**
* Add debug GUI to switch replacement shader.
* @param camera - Camera to render scene
*/
function addDebugGUI(camera: Camera): void {
enum RenderMode {
PBR = "PBR Shader(No replacement)",
Normal = "Replacement normal Shader",
UVCheck = "Replacement UVCheck Shader",
}
const debugGUI = new dat.GUI({
width: 350,
});
const state = {
"Render Mode": RenderMode.PBR,
};
debugGUI
.add(state, "Render Mode", [
RenderMode.PBR,
RenderMode.Normal,
RenderMode.UVCheck,
])
.onChange((v: string) => {
switch (v) {
case RenderMode.PBR:
camera.resetReplacementShader();
break;
case RenderMode.Normal:
camera.setReplacementShader(Shader.find("NormalShader"));
break;
case RenderMode.UVCheck:
camera.setReplacementShader(Shader.find("UVCheckShader"));
break;
}
});
}