-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shadows for terrain #3595
Merged
Merged
Shadows for terrain #3595
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,15 @@ define([ | |
'../Core/destroyObject', | ||
'../Core/TerrainQuantization', | ||
'../Renderer/ShaderProgram', | ||
'../Scene/SceneMode' | ||
'../Scene/SceneMode', | ||
'../Scene/ShadowMapShader' | ||
], function( | ||
defined, | ||
destroyObject, | ||
TerrainQuantization, | ||
ShaderProgram, | ||
SceneMode) { | ||
SceneMode, | ||
ShadowMapShader) { | ||
'use strict'; | ||
|
||
function GlobeSurfaceShader(numberOfDayTextures, flags, shaderProgram) { | ||
|
@@ -31,6 +33,7 @@ define([ | |
|
||
this._shadersByTexturesFlags = []; | ||
this._pickShaderPrograms = []; | ||
this._shadowCastPrograms = []; | ||
} | ||
|
||
function getPositionMode(sceneMode) { | ||
|
@@ -65,7 +68,7 @@ define([ | |
return useWebMercatorProjection ? get2DYPositionFractionMercatorProjection : get2DYPositionFractionGeographicProjection; | ||
} | ||
|
||
GlobeSurfaceShaderSet.prototype.getShaderProgram = function(frameState, surfaceTile, numberOfDayTextures, applyBrightness, applyContrast, applyHue, applySaturation, applyGamma, applyAlpha, showReflectiveOcean, showOceanWaves, enableLighting, hasVertexNormals, useWebMercatorProjection, enableFog) { | ||
GlobeSurfaceShaderSet.prototype.getShaderProgram = function(frameState, surfaceTile, numberOfDayTextures, applyBrightness, applyContrast, applyHue, applySaturation, applyGamma, applyAlpha, showReflectiveOcean, showOceanWaves, enableLighting, hasVertexNormals, useWebMercatorProjection, enableFog, receiveShadows) { | ||
var quantization = 0; | ||
var quantizationDefine = ''; | ||
|
||
|
@@ -90,7 +93,8 @@ define([ | |
(hasVertexNormals << 11) | | ||
(useWebMercatorProjection << 12) | | ||
(enableFog << 13) | | ||
(quantization << 14); | ||
(quantization << 14) | | ||
(receiveShadows << 15); | ||
|
||
var surfaceShader = surfaceTile.surfaceShader; | ||
if (defined(surfaceShader) && | ||
|
@@ -187,6 +191,11 @@ define([ | |
vs.sources.push(getPositionMode(sceneMode)); | ||
vs.sources.push(get2DYPositionFraction(useWebMercatorProjection)); | ||
|
||
if (receiveShadows) { | ||
vs.sources[1] = ShadowMapShader.createReceiveShadowsVertexShader(vs.sources[1]); | ||
fs.sources[0] = ShadowMapShader.createReceiveShadowsFragmentShader(fs.sources[0], frameState.context); | ||
} | ||
|
||
var shader = ShaderProgram.fromCache({ | ||
context : frameState.context, | ||
vertexShaderSource : vs, | ||
|
@@ -240,7 +249,51 @@ define([ | |
return pickShader; | ||
}; | ||
|
||
GlobeSurfaceShaderSet.prototype.getShadowCastProgram = function(frameState, surfaceTile, useWebMercatorProjection, castShadows) { | ||
if (!castShadows) { | ||
return undefined; | ||
} | ||
|
||
var quantization = 0; | ||
var quantizationDefine = ''; | ||
|
||
var terrainEncoding = surfaceTile.pickTerrain.mesh.encoding; | ||
var quantizationMode = terrainEncoding.quantization; | ||
if (quantizationMode === TerrainQuantization.BITS12) { | ||
quantization = 1; | ||
quantizationDefine = 'QUANTIZATION_BITS12'; | ||
} | ||
|
||
var sceneMode = frameState.mode; | ||
var flags = sceneMode | (useWebMercatorProjection << 2) | (quantization << 3); | ||
var shadowCastShader = this._shadowCastPrograms[flags]; | ||
|
||
if (!defined(shadowCastShader)) { | ||
var vs = this.baseVertexShaderSource.clone(); | ||
var fs = this.baseFragmentShaderSource.clone(); | ||
|
||
vs.defines.push(quantizationDefine); | ||
vs.sources.push(getPositionMode(sceneMode)); | ||
vs.sources.push(get2DYPositionFraction(useWebMercatorProjection)); | ||
|
||
vs.sources[1] = ShadowMapShader.createShadowCastVertexShader(vs.sources[1]); | ||
fs.sources[0] = ShadowMapShader.createShadowCastFragmentShader(fs.sources[0], frameState.context, true); | ||
|
||
shadowCastShader = this._shadowCastPrograms[flags] = ShaderProgram.fromCache({ | ||
context : frameState.context, | ||
vertexShaderSource : vs, | ||
fragmentShaderSource : fs, | ||
attributeLocations : terrainEncoding.getAttributeLocations() | ||
}); | ||
} | ||
|
||
return shadowCastShader; | ||
}; | ||
|
||
GlobeSurfaceShaderSet.prototype.destroy = function() { | ||
var flags; | ||
var shader; | ||
|
||
var shadersByTexturesFlags = this._shadersByTexturesFlags; | ||
for (var textureCount in shadersByTexturesFlags) { | ||
if (shadersByTexturesFlags.hasOwnProperty(textureCount)) { | ||
|
@@ -249,9 +302,9 @@ define([ | |
continue; | ||
} | ||
|
||
for (var flags in shadersByFlags) { | ||
for (flags in shadersByFlags) { | ||
if (shadersByFlags.hasOwnProperty(flags)) { | ||
var shader = shadersByFlags[flags]; | ||
shader = shadersByFlags[flags]; | ||
if (defined(shader)) { | ||
shader.shaderProgram.destroy(); | ||
} | ||
|
@@ -260,8 +313,24 @@ define([ | |
} | ||
} | ||
|
||
var pickShaderPrograms = this._pickShaderPrograms; | ||
for (flags in pickShaderPrograms) { | ||
if (pickShaderPrograms.hasOwnProperty(flags)) { | ||
shader = pickShaderPrograms[flags]; | ||
shader.destroy(); | ||
} | ||
} | ||
|
||
var shadowCastPrograms = this._shadowCastPrograms; | ||
for (flags in shadowCastPrograms) { | ||
if (shadowCastPrograms.hasOwnProperty(flags)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also longer-term, the pick shader can likely be the same as the shadow cast shader since all it wants to do is render depth for opaque geometry. |
||
shader = shadowCastPrograms[flags]; | ||
shader.destroy(); | ||
} | ||
} | ||
|
||
return destroyObject(this); | ||
}; | ||
|
||
return GlobeSurfaceShaderSet; | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the easiest way for me to modify the Globe shader to support shadows. I am open to other ideas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine for now - standard for the globe shader.
Longer-term (which could be soon), we need a better low-level material system (that still allows dynamically recompiling the shader for when the shadow flags are enabled/disabled). Perhaps we will beef up
ShaderProgram
to know about shadows, and add flags to commands for if they cast/receive shadows.