forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.light.shadow.ts
351 lines (275 loc) · 9.22 KB
/
benchmark.light.shadow.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { Mat4, Vec3 } from "kiwi.matrix";
import { GTexture, IPerformance, PipeGL, TAttribute, TUniform } from "../../src";
import { cubeElements, cubePositions } from "../util/createCube";
import { createNormals } from "../util/createNormals";
//https://www.chinedufn.com/webgl-shadow-mapping-tutorial/
const RADIUS = 700;
const SHDOW_RADIUS = 512;
const LIGHT_POSITION0 = [0.1, 2.5, 2.5];
const CAMERA_POSTION0 = [5, 2.5, 5];
const VIEW: Mat4 = new Mat4()
.lookAt(
new Vec3().set(CAMERA_POSTION0[0], CAMERA_POSTION0[1], CAMERA_POSTION0[2]),
new Vec3().set(0, 0, 0),
new Vec3().set(0, 1, 0)
).invert();
const VIEW_LIGHT = new Mat4()
.lookAt(
new Vec3().set(LIGHT_POSITION0[0], LIGHT_POSITION0[1], LIGHT_POSITION0[2]),
new Vec3().set(0, 0, 0),
new Vec3().set(0, 1, 0)
).invert();
const PROJECTION = Mat4.perspective(Math.PI / 4, RADIUS / RADIUS, 0.1, 100);
const MODEL = new Mat4().identity();
const pipegl0 = new PipeGL({
width: RADIUS,
height: RADIUS
});
interface LightUniform extends TUniform {
projection: number[];
view: number[];
position: number[];
}
const light0 = pipegl0.compile<TAttribute, LightUniform>({
vert: `precision mediump float;
uniform vec3 position;
uniform mat4 view, projection;
void main(){
gl_PointSize = 10.0;
gl_Position = projection * view *vec4(position, 1.0);
}`,
frag: `precision mediump float;
void main(){
gl_FragColor = vec4(1,1,1,1);
}`,
attributes: {
},
uniforms: {
position: LIGHT_POSITION0,
projection: PROJECTION.value,
view: VIEW.value
},
primitive: "POINTS",
count: 1,
});
interface ShadowAttribute extends TAttribute {
position: number[][];
}
interface ShadowUniform extends TUniform {
projection: number[];
view: number[];
model: { (performance: IPerformance, batchId: number): number[] };
}
const SHADOW_TEXTURE = pipegl0.texture2D(new Uint8Array(SHDOW_RADIUS * SHDOW_RADIUS * 4), SHDOW_RADIUS, SHDOW_RADIUS, 4);
const SHADOW_FBO = pipegl0.framebuffer({
colors: [SHADOW_TEXTURE]
});
const shadow0 = pipegl0.compile<ShadowAttribute, ShadowUniform>({
vert: `precision mediump float;
attribute vec3 position;
uniform mat4 projection, view, model;
void main(){
gl_Position = projection*view*model*vec4(position,1.0);
}`,
frag: `precision mediump float;
vec4 pack(float depth){
const vec4 bitShift = vec4(1.0, 256.0, 256.0 * 256.0, 256.0 * 256.0 * 256.0);
const vec4 bitMask = vec4(1.0/256.0, 1.0/256.0, 1.0/256.0, 0.0);
//返回片元坐标小数部分
vec4 rgbaDepth = fract(depth*bitShift); //取z值
rgbaDepth -= rgbaDepth.gbaa*bitMask; //裁剪不符合8位的位置数据
return rgbaDepth;
}
void main(){
gl_FragColor = pack(gl_FragCoord.z);
}`,
attributes: {
position: cubePositions,
},
uniforms: {
projection: PROJECTION.value,
view: VIEW_LIGHT.value,
// view:VIEW.value,
model: (performance: IPerformance, batchId: number): number[] => {
return MODEL.rotateY(0.003).rotateX(0.002).value;
},
},
elements: cubeElements,
framebuffer: {
framebuffer: SHADOW_FBO,
clear: {
color: [1, 1, 1, 1],
depth: true,
}
},
status: {
viewport: [0, 0, SHDOW_RADIUS, SHDOW_RADIUS]
}
});
interface CubeAttribute extends TAttribute {
position: number[][];
normal: number[][];
}
interface CubeUniform extends TUniform {
projection: number[];
view: number[];
model: { (performance: IPerformance, batchId: number): number[] };
ambient: number; //light相关:环境光
lightColor: number[]; //light相关:光颜色
lightPosition: number[]; //light相关:光源位置
specular: number; //light相关:镜面反射率
viewPosition: number[]; //light相关:摄像头位置
}
const cube0 = pipegl0.compile<CubeAttribute, CubeUniform>({
vert: `precision mediump float;
attribute vec3 position;
attribute vec3 normal;
uniform mat4 projection, view, model;
varying vec3 vNormal; //物体法线
varying vec3 vPosition; //世界物体位置
void main(){
float scale = 1.0;
vPosition = (model*vec4(position,1.0)).xyz; //应用position后世界坐标
vNormal = mat3(model)*normal; //应用model后的法线
gl_Position = projection*view*model*vec4(scale*position, 1.0);
}`,
frag: `precision mediump float;
uniform float ambient; //环境光
uniform float specular; //镜面光
uniform vec3 lightColor;
uniform vec3 lightPosition;
uniform vec3 viewPosition;
varying vec3 vNormal;
varying vec3 vPosition; //世界物体位置
void main(){
//计算环境光结果
vec3 ambient0=ambient*lightColor;
//计算漫反射
vec3 lightDir=normalize(lightPosition-vPosition);
float diff=max(dot(vNormal,lightDir),0.0);
vec3 diffuse0=diff*lightColor;
//镜面反射
vec3 viewDir=normalize(viewPosition-vPosition);
vec3 reflectDir=reflect(-lightDir,vNormal);
float spec=max(dot(viewDir,reflectDir),0.0);
float spec32=pow(spec,32.0); //注意这里pow(float, float), 参数必须是float类型
vec3 specular0=specular*spec32*lightColor;
//颜色汇总
gl_FragColor=vec4(ambient0+diffuse0+specular0,1.0);
}
`,
attributes: {
position: cubePositions,
normal: createNormals(cubeElements, cubePositions),
},
uniforms: {
projection: PROJECTION.value,
view: VIEW.value,
model: (performance: IPerformance, batchId: number): number[] => {
return MODEL.rotateY(0.003).rotateX(0.002).value;
},
viewPosition: CAMERA_POSTION0, //视角位置
specular: 0.5, //镜面反射率
ambient: 0.1, //环境光
lightColor: [1, 1, 1], //白光
lightPosition: LIGHT_POSITION0, //光源位置
},
elements: cubeElements,
status: {
DEPTH_TEST: true,
CULL_FACE: true,
cullFace: [0x0404],
frontFace: [0x0900]
}
});
interface PlaneAttribute extends TAttribute {
position: number[][];
}
interface PlaneUinform extends TUniform {
projection: number[];
view: number[];
viewLight: number[];
'lights[0].color': number[];
'lights[0].position': number[];
texture: GTexture;
}
//Memory bandwidth is often the biggest performance issue on GPUs.(http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/)
//参考教程:
//https://www.chinedufn.com/webgl-shadow-mapping-tutorial/
const plane0 = pipegl0.compile<PlaneAttribute, PlaneUinform>({
vert: `precision mediump float;
attribute vec3 position;
attribute vec2 uv;
uniform mat4 projection,view,viewLight;
varying vec4 vPositionLight; //存储深度坐标值
// varying vec2 vUv; //纹理坐标
void main(){
// vUv = uv
vPositionLight = projection * viewLight * vec4(position,1.0);
gl_Position = projection * view * vec4(position,1.0);
}`,
frag: `precision mediump float;
struct Light{
vec3 color;
vec3 position;
};
uniform sampler2D texture;
uniform Light lights[1];
varying vec4 vPositionLight;
// varying vec2 vUv;
float unpack(const in vec4 rgbaDepth){
const vec4 bitShift = vec4(1.0, 1.0/256.0, 1.0/(256.0*256.0), 1.0/(256.0*256.0*256.0));
float depth = dot(rgbaDepth, bitShift);
return depth;
}
void main(){
//采样算法
float texelSize = ${1.0 / SHDOW_RADIUS};
float amountInLight = 0.0;
vec3 uv = (vPositionLight.xyz/vPositionLight.w)/2.0 + 0.5;
//采样(3x3)区域
for(float x=-1.5;x<=1.5;x++)
for(float y=-1.5;y<=1.5;y++){
float texelDepth = unpack(texture2D(texture, uv.xy+vec2(x,y)*texelSize));
if(uv.z-0.001>texelDepth)
amountInLight +=1.0;
}
//颜色压缩
amountInLight = 1.0-amountInLight/12.0;
gl_FragColor=vec4(amountInLight*vec3(1,1,1), 1.0);
}`,
attributes: {
position: [
[-2.5, -2, -2.5],
[-2.5, -2, 2.5],
[2.5, -2, 2.5],
[2.5, -2, -2.5],
]
},
uniforms: {
projection: PROJECTION.value,
view: VIEW.value,
viewLight: VIEW_LIGHT.value,
'lights[0].color': [1.0, 1.0, 1.0],
'lights[0].position': LIGHT_POSITION0,
texture: SHADOW_TEXTURE,
},
elements: [0, 1, 2, 0, 2, 3],
status: {
DEPTH_TEST: true,
stencilMask: [0],
stencilOp: [0x1E00, 0x1E00, 0x1E01],
}
});
const anim = () => {
pipegl0.clear({
color: [0, 0, 0, 1],
depth: true,
})
light0.draw();
shadow0.draw();
cube0.draw();
plane0.draw();
requestAnimationFrame(anim);
}
anim();