-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix] effect related fixes (split maps, shadows, timeline) (#2396)
Signed-off-by: Ihor Dykhta <[email protected]>
- Loading branch information
1 parent
5e7dd9b
commit a36ec68
Showing
10 changed files
with
364 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// @ts-nocheck This is a hack, don't check types | ||
|
||
import {console as Console} from 'global/window'; | ||
import {LightingEffect, shadow} from '@deck.gl/core'; | ||
import {Texture2D, ProgramManager} from '@luma.gl/core'; | ||
|
||
/** | ||
* Inserts shader code before detected part. | ||
* @param {string} vs Original shader code. | ||
* @param {string} type Debug string. | ||
* @param {string} insertBeforeText Text chunk to insert before. | ||
* @param {string} textToInsert Text to insert. | ||
* @returns Modified shader code. | ||
*/ | ||
export function insertBefore(vs, type, insertBeforeText, textToInsert) { | ||
const at = vs.indexOf(insertBeforeText); | ||
if (at < 0) { | ||
Console.error(`Cannot edit ${type} layer shader`); | ||
return vs; | ||
} | ||
|
||
return vs.slice(0, at) + textToInsert + vs.slice(at); | ||
} | ||
|
||
const CustomShadowModule = shadow ? {...shadow} : undefined; | ||
|
||
/** | ||
* Custom shadow module | ||
* 1) Add u_outputUniformShadow uniform | ||
* 2) always produce full shadow when the uniform is set to true. | ||
*/ | ||
CustomShadowModule.fs = insertBefore( | ||
CustomShadowModule.fs, | ||
'custom shadow #1', | ||
'uniform vec4 shadow_uColor;', | ||
'uniform bool u_outputUniformShadow;' | ||
); | ||
|
||
CustomShadowModule.fs = insertBefore( | ||
CustomShadowModule.fs, | ||
'custom shadow #1', | ||
'vec4 rgbaDepth = texture2D(shadowMap, position.xy);', | ||
'if(u_outputUniformShadow) return 1.0;' | ||
); | ||
|
||
CustomShadowModule.getUniforms = (opts = {}, context = {}) => { | ||
const u = shadow.getUniforms(opts, context); | ||
if (opts.outputUniformShadow !== undefined) { | ||
u['u_outputUniformShadow'] = opts.outputUniformShadow; | ||
} | ||
return u; | ||
}; | ||
|
||
/** | ||
* Custom LightingEffect | ||
* 1) adds CustomShadowModule | ||
* 2) pass outputUniformShadow as module parameters | ||
* 3) properly removes CustomShadowModule | ||
*/ | ||
class CustomDeckLightingEffect extends LightingEffect { | ||
constructor(props) { | ||
super(props); | ||
this.useOutputUniformShadow = false; | ||
} | ||
|
||
preRender(gl, {layers, layerFilter, viewports, onViewportActive, views}) { | ||
if (!this.shadow) return; | ||
|
||
// create light matrix every frame to make sure always updated from light source | ||
this.shadowMatrices = this._calculateMatrices(); | ||
|
||
if (this.shadowPasses.length === 0) { | ||
this._createShadowPasses(gl); | ||
} | ||
if (!this.programManager) { | ||
this.programManager = ProgramManager.getDefaultProgramManager(gl); | ||
if (CustomShadowModule) { | ||
this.programManager.addDefaultModule(CustomShadowModule); | ||
} | ||
} | ||
|
||
if (!this.dummyShadowMap) { | ||
this.dummyShadowMap = new Texture2D(gl, { | ||
width: 1, | ||
height: 1 | ||
}); | ||
} | ||
|
||
for (let i = 0; i < this.shadowPasses.length; i++) { | ||
const shadowPass = this.shadowPasses[i]; | ||
shadowPass.render({ | ||
layers, | ||
layerFilter, | ||
viewports, | ||
onViewportActive, | ||
views, | ||
moduleParameters: { | ||
shadowLightId: i, | ||
dummyShadowMap: this.dummyShadowMap, | ||
shadowMatrices: this.shadowMatrices, | ||
useOutputUniformShadow: false | ||
} | ||
}); | ||
} | ||
} | ||
|
||
getModuleParameters(layer) { | ||
const parameters = super.getModuleParameters(layer); | ||
parameters.outputUniformShadow = this.outputUniformShadow; | ||
return parameters; | ||
} | ||
|
||
cleanup() { | ||
for (const shadowPass of this.shadowPasses) { | ||
shadowPass.delete(); | ||
} | ||
this.shadowPasses.length = 0; | ||
this.shadowMaps.length = 0; | ||
|
||
if (this.dummyShadowMap) { | ||
this.dummyShadowMap.delete(); | ||
this.dummyShadowMap = null; | ||
} | ||
|
||
if (this.shadow && this.programManager) { | ||
this.programManager.removeDefaultModule(CustomShadowModule); | ||
this.programManager = null; | ||
} | ||
} | ||
} | ||
|
||
export default CustomDeckLightingEffect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.