Skip to content

Commit 1036d52

Browse files
authored
Merge branch 'master' into env-mapping-pass
2 parents f01217a + a9905c4 commit 1036d52

38 files changed

+4188
-87
lines changed

demos/gltf-renderer/gltfrenderer.ts

+49-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ForwardSceneRenderPass,
1414
Framebuffer,
1515
Geometry,
16+
GLTFAlphaMode,
1617
GLTFLoader,
1718
GLTFPbrMaterial,
1819
GLTFPrimitive,
@@ -74,6 +75,8 @@ export class GltfRenderer extends Renderer {
7475
protected _uRoughnessFactor: WebGLUniformLocation;
7576
protected _uEmissiveFactor: WebGLUniformLocation;
7677
protected _uNormalScale: WebGLUniformLocation;
78+
protected _uBlendMode: WebGLUniformLocation;
79+
protected _uBlendCutoff: WebGLUniformLocation;
7780

7881
protected _uSpecularEnvironment: WebGLUniformLocation;
7982
protected _uBRDFLookupTable: WebGLUniformLocation;
@@ -127,6 +130,8 @@ export class GltfRenderer extends Renderer {
127130
this._uRoughnessFactor = this._program.uniform('u_roughnessFactor');
128131
this._uEmissiveFactor = this._program.uniform('u_emissiveFactor');
129132
this._uNormalScale = this._program.uniform('u_normalScale');
133+
this._uBlendMode = this._program.uniform('u_blendMode');
134+
this._uBlendCutoff = this._program.uniform('u_blendCutoff');
130135

131136
this._uSpecularEnvironment = this._program.uniform('u_specularEnvironment');
132137
this._uBRDFLookupTable = this._program.uniform('u_brdfLUT');
@@ -164,20 +169,6 @@ export class GltfRenderer extends Renderer {
164169
this._forwardPass.updateViewProjectionTransform = (matrix: mat4) => {
165170
gl.uniformMatrix4fv(this._uViewProjection, false, matrix);
166171
};
167-
this._forwardPass.bindUniforms = () => {
168-
gl.uniform3fv(this._uEye, this._camera.eye);
169-
170-
gl.uniform1i(this._uBaseColor, 0);
171-
gl.uniform1i(this._uMetallicRoughness, 1);
172-
gl.uniform1i(this._uNormal, 2);
173-
gl.uniform1i(this._uOcclusion, 3);
174-
gl.uniform1i(this._uEmissive, 4);
175-
gl.uniform1i(this._uSpecularEnvironment, 5);
176-
gl.uniform1i(this._uBRDFLookupTable, 6);
177-
178-
this._specularEnvironment.bind(gl.TEXTURE5);
179-
this._brdfLUT.bind(gl.TEXTURE6);
180-
};
181172
this._forwardPass.bindGeometry = (geometry: Geometry) => {
182173
const primitive = geometry as GLTFPrimitive;
183174
gl.uniform1i(this._uGeometryFlags, primitive.flags);
@@ -245,6 +236,27 @@ export class GltfRenderer extends Renderer {
245236
gl.uniform1f(this._uRoughnessFactor, pbrMaterial.roughnessFactor);
246237
gl.uniform1f(this._uNormalScale, pbrMaterial.normalScale);
247238
gl.uniform1i(this._uPbrFlags, pbrMaterial.flags);
239+
240+
/**
241+
* Handle alpha modes
242+
*/
243+
if (pbrMaterial.alphaMode === GLTFAlphaMode.OPAQUE) {
244+
gl.disable(gl.BLEND);
245+
gl.uniform1i(this._uBlendMode, 0);
246+
} else if (pbrMaterial.alphaMode === GLTFAlphaMode.MASK) {
247+
gl.enable(gl.BLEND);
248+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
249+
gl.uniform1i(this._uBlendMode, 1);
250+
gl.uniform1f(this._uBlendCutoff, pbrMaterial.alphaCutoff);
251+
} else if (pbrMaterial.alphaMode === GLTFAlphaMode.BLEND) {
252+
gl.enable(gl.BLEND);
253+
// We premultiply in the shader
254+
gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
255+
256+
gl.uniform1i(this._uBlendMode, 2);
257+
} else {
258+
auxiliaries.log(auxiliaries.LogLevel.Warning, 'Unknown blend mode encountered.');
259+
}
248260
};
249261

250262
this.loadAsset();
@@ -311,6 +323,8 @@ export class GltfRenderer extends Renderer {
311323
return;
312324
}
313325

326+
this.bindUniforms();
327+
314328
const gl = this._context.gl;
315329

316330
// TODO: proper handling of transparent materials in the loader
@@ -323,6 +337,27 @@ export class GltfRenderer extends Renderer {
323337
protected onSwap(): void {
324338
}
325339

340+
protected bindUniforms(): void {
341+
const gl = this._context.gl;
342+
343+
this._program.bind();
344+
345+
gl.uniform3fv(this._uEye, this._camera.eye);
346+
347+
gl.uniform1i(this._uBaseColor, 0);
348+
gl.uniform1i(this._uMetallicRoughness, 1);
349+
gl.uniform1i(this._uNormal, 2);
350+
gl.uniform1i(this._uOcclusion, 3);
351+
gl.uniform1i(this._uEmissive, 4);
352+
gl.uniform1i(this._uSpecularEnvironment, 5);
353+
gl.uniform1i(this._uBRDFLookupTable, 6);
354+
355+
this._specularEnvironment.bind(gl.TEXTURE5);
356+
this._brdfLUT.bind(gl.TEXTURE6);
357+
358+
this._program.unbind();
359+
}
360+
326361
/**
327362
* Load asset from URI specified by the HTML select
328363
*/
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
import { vec3 } from 'gl-matrix';
3+
4+
// tslint:disable:max-classes-per-file
5+
6+
export class SphereLight {
7+
public center: vec3;
8+
public radius: number;
9+
public luminance: vec3;
10+
11+
constructor(center: vec3, radius: number, luminance: vec3) {
12+
this.center = center;
13+
this.radius = radius;
14+
this.luminance = luminance;
15+
}
16+
}
17+
18+
export class DiskLight {
19+
public center: vec3;
20+
public radius: number;
21+
public luminance: vec3;
22+
public direction: vec3;
23+
public fovy: number;
24+
25+
constructor(center: vec3, radius: number, luminance: vec3, direction: vec3, fovy: number) {
26+
this.center = center;
27+
this.radius = radius;
28+
this.luminance = luminance;
29+
this.direction = direction;
30+
this.fovy = fovy;
31+
}
32+
}
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
precision highp float;
2+
3+
// Adapted from https://github.com/KhronosGroup/glTF-WebGL-PBR
4+
5+
@import ../../../source/shaders/facade.vert;
6+
@import ../../../source/shaders/ndcoffset;
7+
8+
const int HAS_NORMALS = 1;
9+
const int HAS_TANGENTS = 1 << 1;
10+
const int HAS_UV = 1 << 2;
11+
const int HAS_COLORS = 1 << 3;
12+
13+
#if __VERSION__ == 100
14+
attribute vec4 a_position;
15+
attribute vec4 a_normal;
16+
attribute vec4 a_tangent;
17+
attribute vec2 a_texcoord_0;
18+
attribute vec2 a_texcoord_1;
19+
attribute vec2 a_texcoord_2;
20+
attribute vec4 a_joints;
21+
attribute vec4 a_weights;
22+
attribute vec4 a_color;
23+
#else
24+
layout (location = 0) in vec4 a_position;
25+
layout (location = 1) in vec3 a_normal;
26+
layout (location = 2) in vec4 a_tangent;
27+
layout (location = 3) in vec2 a_texcoord_0;
28+
layout (location = 4) in vec2 a_texcoord_1;
29+
layout (location = 5) in vec2 a_texcoord_2;
30+
layout (location = 6) in vec4 a_joints;
31+
layout (location = 7) in vec4 a_weights;
32+
layout (location = 8) in vec4 a_color;
33+
#endif
34+
35+
uniform mat4 u_model;
36+
uniform mat4 u_projection;
37+
uniform mat4 u_view;
38+
uniform mat3 u_normalMatrix;
39+
40+
uniform mediump int u_geometryFlags;
41+
42+
uniform vec2 u_ndcOffset;
43+
uniform vec2 u_cocPoint;
44+
45+
varying vec2 v_uv[3];
46+
varying vec4 v_color;
47+
varying vec3 v_position;
48+
49+
varying mat3 v_TBN;
50+
varying vec3 v_normal;
51+
52+
bool checkFlag(int flag) {
53+
return (u_geometryFlags & flag) == flag;
54+
}
55+
56+
vec4 depthOfField(mat4 modelView, vec4 worldPos, vec2 cocPoint, float focalDist)
57+
{
58+
vec4 viewVertex = modelView * worldPos;
59+
viewVertex.xy += cocPoint * (viewVertex.z + focalDist);
60+
return viewVertex;
61+
}
62+
63+
void main(void)
64+
{
65+
vec4 pos = u_model * a_position;
66+
v_position = vec3(pos.xyz) / pos.w;
67+
68+
if (checkFlag(HAS_NORMALS)) {
69+
if (checkFlag(HAS_TANGENTS)) {
70+
vec3 normalW = normalize(vec3(u_normalMatrix * a_normal));
71+
vec3 tangentW = normalize(vec3(u_model * vec4(a_tangent.xyz, 0.0)));
72+
vec3 bitangentW = cross(normalW, tangentW) * a_tangent.w;
73+
v_TBN = mat3(tangentW, bitangentW, normalW);
74+
} else { // HAS_TANGENTS != 1
75+
v_normal = normalize(vec3(u_model * vec4(a_normal.xyz, 0.0)));
76+
}
77+
}
78+
79+
if (checkFlag(HAS_UV)) {
80+
v_uv[0] = a_texcoord_0;
81+
v_uv[1] = a_texcoord_1;
82+
v_uv[2] = a_texcoord_2;
83+
} else {
84+
v_uv[0] = vec2(0., 0.);
85+
v_uv[1] = vec2(0., 0.);
86+
v_uv[2] = vec2(0., 0.);
87+
}
88+
89+
if (checkFlag(HAS_COLORS)) {
90+
v_color = a_color;
91+
} else {
92+
v_color = vec4(1.0);
93+
}
94+
95+
vec4 viewVertex = depthOfField(u_view * u_model, a_position, u_cocPoint, 8.0);
96+
gl_Position = u_projection * viewVertex;
97+
ndcOffset(gl_Position, u_ndcOffset);
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
#if __VERSION__ == 100
3+
4+
#ifdef GL_OES_standard_derivatives
5+
#extension GL_OES_standard_derivatives : enable
6+
#endif
7+
8+
#endif
9+
10+
precision highp float;
11+
12+
@import ../../../source/shaders/facade.frag;
13+
14+
15+
uniform vec2 u_cameraNearFar;
16+
uniform mat4 u_view;
17+
18+
19+
#if __VERSION__ == 100
20+
#define fragColor gl_FragColor
21+
#else
22+
layout(location = 0) out vec4 fragColor;
23+
#endif
24+
25+
const int HAS_NORMALS = 1;
26+
const int HAS_TANGENTS = 1 << 1;
27+
const int HAS_UV = 1 << 2;
28+
const int HAS_COLORS = 1 << 3;
29+
const int USE_IBL = 1 << 4;
30+
const int HAS_BASECOLORMAP = 1 << 5;
31+
const int HAS_NORMALMAP = 1 << 6;
32+
const int HAS_EMISSIVEMAP = 1 << 7;
33+
const int HAS_METALROUGHNESSMAP = 1 << 8;
34+
const int HAS_OCCLUSIONMAP = 1 << 9;
35+
const int USE_TEX_LOD = 1 << 10;
36+
37+
uniform mediump int u_geometryFlags;
38+
39+
varying vec3 v_position;
40+
varying mat3 v_TBN;
41+
varying vec3 v_normal;
42+
varying vec2 v_uv[3];
43+
44+
bool checkGeometryFlag(int flag) {
45+
return (u_geometryFlags & flag) == flag;
46+
}
47+
48+
// Find the normal for this fragment, pulling either from a predefined normal map
49+
// or from the interpolated mesh normal and tangent attributes.
50+
vec3 getNormal()
51+
{
52+
mat3 TBN;
53+
// Retrieve the tangent space matrix
54+
if (!checkGeometryFlag(HAS_TANGENTS)) {
55+
vec3 pos_dx = dFdx(v_position);
56+
vec3 pos_dy = dFdy(v_position);
57+
vec3 tex_dx = dFdx(vec3(v_uv[0], 0.0));
58+
vec3 tex_dy = dFdy(vec3(v_uv[0], 0.0));
59+
vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);
60+
61+
vec3 ng;
62+
if (checkGeometryFlag(HAS_NORMALS))
63+
ng = normalize(v_normal);
64+
else
65+
ng = cross(pos_dx, pos_dy);
66+
67+
t = normalize(t - ng * dot(ng, t));
68+
vec3 b = normalize(cross(ng, t));
69+
TBN = mat3(t, b, ng);
70+
}
71+
else { // HAS_TANGENTS
72+
TBN = v_TBN;
73+
}
74+
75+
// The tbn matrix is linearly interpolated, so we need to re-normalize
76+
vec3 n = normalize(TBN[2].xyz);
77+
78+
// reverse backface normals
79+
n *= (2.0 * float(gl_FrontFacing) - 1.0);
80+
81+
return n;
82+
}
83+
84+
void main(void)
85+
{
86+
vec4 viewPosition = u_view * vec4(v_position, 1.0);
87+
viewPosition /= viewPosition.w;
88+
89+
float depth = length(viewPosition.xyz);
90+
fragColor = vec4(getNormal(), depth);
91+
}

0 commit comments

Comments
 (0)