Skip to content

Commit 73d9e0a

Browse files
authored
Merge pull request #230 from p-otto/env-mapping-pass
Environment rendering pass
2 parents 91cc4d1 + 4239cbc commit 73d9e0a

File tree

70 files changed

+379
-120
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+379
-120
lines changed

examples/data/cube-map-nx.jpg

5.23 KB

examples/data/cube-map-ny.jpg

5.43 KB

examples/data/cube-map-nz.jpg

4.6 KB

examples/data/cube-map-px.jpg

3.52 KB

examples/data/cube-map-py.jpg

9.96 KB

examples/data/cube-map-pz.jpg

3.94 KB

examples/data/imagebasedlighting.frag

-3
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ void main(void)
7171
normal = normalize(TBN * normal);
7272

7373
float roughness = texture(u_roughnessTexture, v_uv).r;
74-
roughness = pow(roughness, GAMMA);
75-
7674
float metallic = texture(u_metallicTexture, v_uv).r;
77-
metallic = pow(metallic, GAMMA);
7875

7976
const vec3 f0 = vec3(0.04);
8077
vec3 diffuseColor = albedoColor * (vec3(1.0) - f0) * (1.0 - metallic);
2.79 MB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/envprojections-example.ts

+29-80
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import {
88
Canvas,
99
Context,
1010
DefaultFramebuffer,
11+
EnvironmentRenderingPass,
12+
EnvironmentTextureType,
1113
EventProvider,
1214
Framebuffer,
1315
Invalidate,
1416
Navigation,
15-
NdcFillingTriangle,
16-
Program,
1717
Renderer,
18-
Shader,
1918
Texture2D,
2019
TextureCube,
2120
Wizard,
@@ -29,8 +28,7 @@ class EnvironmentProjectionRenderer extends Renderer {
2928

3029
protected _defaultFBO: Framebuffer;
3130

32-
protected _ndcTriangle: NdcFillingTriangle;
33-
protected _program: Program;
31+
protected _environmentRenderingPass: EnvironmentRenderingPass;
3432

3533
protected _cubeMap: TextureCube;
3634
protected _equiRectangularMap: Texture2D;
@@ -40,12 +38,6 @@ class EnvironmentProjectionRenderer extends Renderer {
4038
protected _camera: Camera;
4139
protected _navigation: Navigation;
4240

43-
protected _uViewProjection: WebGLUniformLocation;
44-
protected _uViewProjectionInverse: WebGLUniformLocation;
45-
46-
protected _uViewport: WebGLUniformLocation;
47-
protected _uTime: WebGLUniformLocation;
48-
protected _uMode: WebGLUniformLocation;
4941

5042
protected onInitialize(context: Context,
5143
callback: Invalidate,
@@ -60,38 +52,8 @@ class EnvironmentProjectionRenderer extends Renderer {
6052
this._defaultFBO.initialize();
6153
this._defaultFBO.bind();
6254

63-
const gl = this._context.gl;
64-
65-
this._ndcTriangle = new NdcFillingTriangle(this._context, 'NdcFillingTriangle');
66-
this._ndcTriangle.initialize();
67-
6855
this.fetchTextures();
6956

70-
// Initialize program and uniforms
71-
const vert = new Shader(this._context, gl.VERTEX_SHADER, 'ndcvertices');
72-
vert.initialize(require('./data/env-projections.vert'));
73-
74-
const frag = new Shader(this._context, gl.FRAGMENT_SHADER, 'env-projections');
75-
frag.initialize(require('./data/env-projections.frag'));
76-
77-
this._program = new Program(this._context, 'EnvProjectionsProgram');
78-
this._program.initialize([vert, frag], false);
79-
80-
this._program.attribute('a_vertex', this._ndcTriangle.vertexLocation);
81-
this._program.link();
82-
83-
this._program.bind();
84-
gl.uniform1i(this._program.uniform('u_cubemap'), 0);
85-
gl.uniform1i(this._program.uniform('u_equirectmap'), 1);
86-
gl.uniform1i(this._program.uniform('u_spheremap'), 2);
87-
gl.uniform1iv(this._program.uniform('u_polarmap'), [3, 4]);
88-
89-
this._uViewProjection = this._program.uniform('u_viewProjection');
90-
this._uViewProjectionInverse = this._program.uniform('u_viewProjectionInverse');
91-
this._uViewport = this._program.uniform('u_viewport');
92-
this._uTime = this._program.uniform('u_time');
93-
this._uMode = this._program.uniform('u_mode');
94-
9557
// Initialize camera
9658
this._camera = new Camera();
9759
this._camera.eye = vec3.fromValues(0.0, 0.5, -1.0);
@@ -103,7 +65,9 @@ class EnvironmentProjectionRenderer extends Renderer {
10365
this._navigation = new Navigation(callback, eventProvider.mouseEventProvider!);
10466
this._navigation.camera = this._camera;
10567

106-
gl.uniform2iv(this._uViewport, this._canvasSize);
68+
this._environmentRenderingPass = new EnvironmentRenderingPass(this._context);
69+
this._environmentRenderingPass.initialize();
70+
this._environmentRenderingPass.camera = this._camera;
10771

10872
return true;
10973
}
@@ -142,63 +106,48 @@ class EnvironmentProjectionRenderer extends Renderer {
142106

143107
this._defaultFBO.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT, false, false);
144108

145-
this._cubeMap.bind(gl.TEXTURE0);
146-
this._equiRectangularMap.bind(gl.TEXTURE1);
147-
this._sphereMap.bind(gl.TEXTURE2);
148-
this._polarMaps[0].bind(gl.TEXTURE3);
149-
this._polarMaps[1].bind(gl.TEXTURE4);
150-
this._program.bind();
151-
152-
gl.uniform2iv(this._uViewport, this._canvasSize);
153-
gl.uniformMatrix4fv(this._uViewProjection, false, this._camera.viewProjection);
154-
gl.uniformMatrix4fv(this._uViewProjectionInverse, false, this._camera.viewProjectionInverse);
155-
156-
const t = ((new Date()).getTime() % 10000000) * 0.001;
157-
gl.uniform1f(this._uTime, t);
158-
159-
this._ndcTriangle.bind();
160-
161109
const b = 1.0;
162110
const w = (this._frameSize[0] - (4.0 - 1.0) * b) / 4.0;
163111
const h = this._frameSize[1];
164112

165113
gl.enable(gl.SCISSOR_TEST);
166114

115+
// Sphere Map
167116
gl.scissor((w + b) * 0.0, 0, w, h);
168-
gl.uniform1i(this._uMode, 2); // sphere map
169-
this._ndcTriangle.draw();
117+
this._environmentRenderingPass.environmentTexture = this._sphereMap;
118+
this._environmentRenderingPass.environmentTextureType = EnvironmentTextureType.SphereMap;
119+
this._environmentRenderingPass.frame();
170120

121+
// Equirectangular Map
171122
gl.scissor((w + b) * 1.0, 0, w, h);
172-
gl.uniform1i(this._uMode, 1); // equirectangular map
173-
this._ndcTriangle.draw();
123+
this._environmentRenderingPass.environmentTexture = this._equiRectangularMap;
124+
this._environmentRenderingPass.environmentTextureType = EnvironmentTextureType.EquirectangularMap;
125+
this._environmentRenderingPass.frame();
174126

127+
// Cube map
175128
gl.scissor((w + b) * 2.0, 0, w, h);
176-
gl.uniform1i(this._uMode, 0); // cube map
177-
this._ndcTriangle.draw();
129+
this._environmentRenderingPass.environmentTexture = this._cubeMap;
130+
this._environmentRenderingPass.environmentTextureType = EnvironmentTextureType.CubeMap;
131+
this._environmentRenderingPass.frame();
178132

179133
gl.scissor((w + b) * 3.0, 0, w, h);
180-
gl.uniform1i(this._uMode, 3); // dual paraboloid map
181-
this._ndcTriangle.draw();
134+
this._environmentRenderingPass.environmentTexture = this._polarMaps[0];
135+
this._environmentRenderingPass.environmentTexture2 = this._polarMaps[1];
136+
this._environmentRenderingPass.environmentTextureType = EnvironmentTextureType.PolarMap;
137+
this._environmentRenderingPass.frame();
182138

183139
gl.disable(gl.SCISSOR_TEST);
184-
185-
this._ndcTriangle.unbind();
186-
187-
this._program.unbind();
188140
}
189141

190142
protected onSwap(): void {
191143
this.invalidate();
192144
}
193145

194-
protected setupTexture2D(texture: Texture2D, unit: number): void {
146+
protected setupTexture2D(texture: Texture2D): void {
195147
const gl = this._context.gl;
196148

197-
texture.bind(unit);
198-
// gl.generateMipmap(gl.TEXTURE_2D);
199-
200-
texture.wrap(gl.REPEAT, gl.REPEAT, false, false);
201-
texture.filter(gl.NEAREST, gl.NEAREST, false, false);
149+
texture.wrap(gl.REPEAT, gl.REPEAT, true, false);
150+
texture.filter(gl.NEAREST, gl.NEAREST, false, true);
202151

203152
this.invalidate(true);
204153
}
@@ -231,7 +180,7 @@ class EnvironmentProjectionRenderer extends Renderer {
231180

232181
promises.push(
233182
this._equiRectangularMap.fetch('data/equirectangular-map.jpg').then(() => {
234-
this.setupTexture2D(this._equiRectangularMap, gl.TEXTURE1);
183+
this.setupTexture2D(this._equiRectangularMap);
235184
}));
236185

237186

@@ -240,7 +189,7 @@ class EnvironmentProjectionRenderer extends Renderer {
240189

241190
promises.push(
242191
this._sphereMap.fetch('data/sphere-map-ny.jpg').then(() => {
243-
this.setupTexture2D(this._sphereMap, gl.TEXTURE2);
192+
this.setupTexture2D(this._sphereMap);
244193
}));
245194

246195

@@ -250,7 +199,7 @@ class EnvironmentProjectionRenderer extends Renderer {
250199

251200
promises.push(
252201
this._polarMaps[0].fetch('data/paraboloid-map-py.jpg').then(() => {
253-
this.setupTexture2D(this._polarMaps[0], gl.TEXTURE3);
202+
this.setupTexture2D(this._polarMaps[0]);
254203
}));
255204

256205

@@ -259,7 +208,7 @@ class EnvironmentProjectionRenderer extends Renderer {
259208

260209
promises.push(
261210
this._polarMaps[1].fetch('data/paraboloid-map-ny.jpg').then(() => {
262-
this.setupTexture2D(this._polarMaps[1], gl.TEXTURE4);
211+
this.setupTexture2D(this._polarMaps[1]);
263212
}));
264213

265214
Promise.all(promises).then(() => {

examples/imagebasedlighting-example.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
Canvas,
99
Context,
1010
DefaultFramebuffer,
11+
EnvironmentRenderingPass,
12+
EnvironmentTextureType,
1113
EventProvider,
1214
GeosphereGeometry,
1315
Invalidate,
@@ -31,6 +33,7 @@ export class ImageBasedLightingRenderer extends Renderer {
3133

3234
protected _camera: Camera;
3335
protected _navigation: Navigation;
36+
protected _environmentRenderingPass: EnvironmentRenderingPass;
3437

3538
protected _sphere: GeosphereGeometry;
3639
protected _albedoTexture: Texture2D;
@@ -149,17 +152,7 @@ export class ImageBasedLightingRenderer extends Renderer {
149152
this._cubemap.filter(gl.LINEAR, gl.LINEAR_MIPMAP_LINEAR);
150153
this._cubemap.levels(0, MIPMAP_LEVELS - 1);
151154

152-
for (let mipLevel = 0; mipLevel < MIPMAP_LEVELS; ++mipLevel) {
153-
this._promises.push(
154-
this._cubemap.fetch({
155-
positiveX: `./data/imagebasedlighting/preprocessed-map-px-${mipLevel}.png`,
156-
negativeX: `./data/imagebasedlighting/preprocessed-map-nx-${mipLevel}.png`,
157-
positiveY: `./data/imagebasedlighting/preprocessed-map-py-${mipLevel}.png`,
158-
negativeY: `./data/imagebasedlighting/preprocessed-map-ny-${mipLevel}.png`,
159-
positiveZ: `./data/imagebasedlighting/preprocessed-map-pz-${mipLevel}.png`,
160-
negativeZ: `./data/imagebasedlighting/preprocessed-map-nz-${mipLevel}.png`,
161-
}, mipLevel));
162-
}
155+
this._promises.push(this._cubemap.fetchMipmapAtlas('./data/imagebasedlighting/ibl-map.png'))
163156

164157
Promise.all(this._promises).then(() => {
165158
this.finishLoading();
@@ -177,6 +170,12 @@ export class ImageBasedLightingRenderer extends Renderer {
177170
this._navigation = new Navigation(callback, eventProvider.mouseEventProvider);
178171
this._navigation.camera = this._camera;
179172

173+
this._environmentRenderingPass = new EnvironmentRenderingPass(context);
174+
this._environmentRenderingPass.initialize();
175+
this._environmentRenderingPass.environmentTextureType = EnvironmentTextureType.CubeMap;
176+
this._environmentRenderingPass.environmentTexture = this._cubemap;
177+
this._environmentRenderingPass.camera = this._camera;
178+
180179
return true;
181180
}
182181

@@ -235,6 +234,8 @@ export class ImageBasedLightingRenderer extends Renderer {
235234

236235
gl.viewport(0, 0, this._frameSize[0], this._frameSize[1]);
237236

237+
this._environmentRenderingPass.frame();
238+
238239
gl.enable(gl.CULL_FACE);
239240
gl.cullFace(gl.BACK);
240241
gl.enable(gl.DEPTH_TEST);

0 commit comments

Comments
 (0)