Skip to content

Commit c1f68ba

Browse files
mvaligurskyMartin Valigursky
andauthored
refactor: use dedicated outline shader output (#9070)
Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
1 parent 71e30c0 commit c1f68ba

11 files changed

Lines changed: 39 additions & 20 deletions

File tree

src/extras/renderers/outline-renderer.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class OutlineRenderer {
132132
});
133133

134134
// custom shader pass for the outline camera
135-
this.outlineShaderPass = this.outlineCameraEntity.camera.setShaderPass('OutlineShaderPass');
135+
this.outlineShaderPass = this.outlineCameraEntity.camera.setShaderPass('pcOutline');
136136

137137
// function called after the camera has rendered the outline objects to the texture
138138
this.postRender = (cameraComponent) => {
@@ -168,27 +168,13 @@ class OutlineRenderer {
168168
});
169169

170170
this.quadRenderer = new QuadRender(this.shaderBlend);
171-
172-
this.whiteTex = new Texture(device, {
173-
name: 'OutlineWhiteTexture',
174-
width: 1,
175-
height: 1,
176-
format: PIXELFORMAT_SRGBA8,
177-
mipmaps: false
178-
});
179-
const pixels = this.whiteTex.lock();
180-
pixels.set(new Uint8Array([255, 255, 255, 255]));
181-
this.whiteTex.unlock();
182171
}
183172

184173
/**
185174
* Destroy the outline renderer and its resources.
186175
*/
187176
destroy() {
188177

189-
this.whiteTex.destroy();
190-
this.whiteTex = null;
191-
192178
this.outlineCameraEntity.destroy();
193179
this.outlineCameraEntity = null;
194180

@@ -248,7 +234,7 @@ class OutlineRenderer {
248234

249235
if (options.pass === outlineShaderPass) {
250236

251-
// custom shader for the outline shader pass, renders single color meshes using emissive color
237+
// custom shader for the outline shader pass, preserving material opacity
252238
const opts = new StandardMaterialOptions();
253239
opts.defines = options.defines;
254240
opts.opacityMap = options.opacityMap;
@@ -272,11 +258,10 @@ class OutlineRenderer {
272258
return options;
273259
};
274260

275-
// set emissive color override for the outline shader pass only
261+
// set the color consumed only by the pcOutline shader variant
276262
_tempColor.linear(color);
277263
const colArray = new Float32Array([_tempColor.r, _tempColor.g, _tempColor.b]);
278-
meshInstance.setParameter('material_emissive', colArray, 1 << this.outlineShaderPass);
279-
meshInstance.setParameter('texture_emissiveMap', this.whiteTex, 1 << this.outlineShaderPass);
264+
meshInstance.setParameter('pcOutlineColor', colArray);
280265
}
281266
});
282267

@@ -297,7 +282,7 @@ class OutlineRenderer {
297282
meshInstances.forEach((meshInstance) => {
298283
if (meshInstance.material instanceof StandardMaterial) {
299284
meshInstance.material.onUpdateShader = null;
300-
meshInstance.deleteParameter('material_emissive');
285+
meshInstance.deleteParameter('pcOutlineColor');
301286
}
302287
});
303288
}

src/scene/shader-lib/glsl/chunks/lit/frag/litMain.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default /* glsl */`
44
#include "varyingsPS"
55
#include "litUserDeclarationPS"
66
#include "frontendDeclPS"
7+
#include "outlineDeclarationPS"
78
89
#if defined(PICK_PASS) || defined(PREPASS_PASS)
910
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default /* glsl */`
2+
#ifdef PCOUTLINE_PASS
3+
uniform vec3 pcOutlineColor;
4+
#endif
5+
`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default /* glsl */`
2+
#ifdef PCOUTLINE_PASS
3+
gl_FragColor.rgb = gammaCorrectOutput(pcOutlineColor);
4+
#endif
5+
`;

src/scene/shader-lib/glsl/chunks/lit/frag/pass-forward/litForwardBackend.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,7 @@ void evaluateBackend() {
238238
// output when the shadow catcher is enabled - accumulated shadows
239239
gl_FragColor.rgb = vec3(dShadowCatcher);
240240
#endif
241+
242+
#include "outlineOutputPS"
241243
}
242244
`;

src/scene/shader-lib/glsl/collections/shader-chunks-glsl.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ import normalCoreVS from '../chunks/common/vert/normalCore.js';
8989
import normalMapPS from '../chunks/standard/frag/normalMap.js';
9090
import opacityPS from '../chunks/standard/frag/opacity.js';
9191
import opacityDitherPS from '../chunks/standard/frag/opacity-dither.js';
92+
import outlineDeclarationPS from '../chunks/lit/frag/outline-declaration.js';
93+
import outlineOutputPS from '../chunks/lit/frag/outline-output.js';
9294
import outputPS from '../chunks/lit/frag/output.js';
9395
import outputAlphaPS from '../chunks/lit/frag/outputAlpha.js';
9496
import outputTex2DPS from '../chunks/common/frag/outputTex2D.js';
@@ -260,6 +262,8 @@ const shaderChunksGLSL = {
260262
normalMapPS,
261263
opacityPS,
262264
opacityDitherPS,
265+
outlineDeclarationPS,
266+
outlineOutputPS,
263267
outputPS,
264268
outputAlphaPS,
265269
outputTex2DPS,

src/scene/shader-lib/wgsl/chunks/lit/frag/litMain.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default /* wgsl */`
44
#include "varyingsPS"
55
#include "litUserDeclarationPS"
66
#include "frontendDeclPS"
7+
#include "outlineDeclarationPS"
78
89
#if defined(PICK_PASS) || defined(PREPASS_PASS)
910
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default /* wgsl */`
2+
#ifdef PCOUTLINE_PASS
3+
uniform pcOutlineColor: vec3f;
4+
#endif
5+
`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default /* wgsl */`
2+
#ifdef PCOUTLINE_PASS
3+
output.color = vec4f(gammaCorrectOutput(uniform.pcOutlineColor), output.color.a);
4+
#endif
5+
`;

src/scene/shader-lib/wgsl/chunks/lit/frag/pass-forward/litForwardBackend.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ fn evaluateBackend() -> FragmentOutput {
244244
output.color = vec4f(vec3f(dShadowCatcher), output.color.a);
245245
#endif
246246
247+
#include "outlineOutputPS"
248+
247249
return output;
248250
}
249251
`;

0 commit comments

Comments
 (0)