-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Shadow Catcher Script #7299
Open
marklundin
wants to merge
9
commits into
main
Choose a base branch
from
feat-shadow-catcher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Shadow Catcher Script #7299
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
82169b7
Add ShadowCatcherScript class to handle shadow catching
marklundin 095bc39
Refactor ShadowCatcherScript and remove unused import
marklundin 4ce2cbe
Add Light import and disable skybox in ShadowCatcherScript
marklundin ef18722
linting
marklundin 4d3dbe4
Update scripts/esm/shadow-catcher.mjs
marklundin a2e54b9
Update scripts/esm/shadow-catcher.mjs
marklundin 659b07c
Merge branch 'main' into feat-shadow-catcher
marklundin 66cd522
Merge branch 'main' into feat-shadow-catcher
marklundin df17bfd
Update shadow catcher light properties
marklundin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { | ||
Script, | ||
Entity, | ||
StandardMaterial, | ||
BLEND_NORMAL, | ||
SHADOW_VSM_16F, | ||
SHADOWUPDATE_REALTIME, | ||
CHUNKAPI_1_70, | ||
Vec2 | ||
} from 'playcanvas'; | ||
|
||
/** @import { AppBase, Entity, Light } from 'playcanvas' */ | ||
|
||
const endPS = ` | ||
litArgs_opacity = mix(light0_shadowIntensity, 0.0, shadow0); | ||
gl_FragColor.rgb = vec3(0.0); | ||
`; | ||
|
||
/** | ||
* This script generates a simple shadow plane for any render-able objects within | ||
* it's hierarchy. This means you can attach it to a parent entity and quickly get | ||
* visible ground plane, which helps to stage models. | ||
*/ | ||
class ShadowCatcherScript extends Script { | ||
/** | ||
* The shadow distance of the shadow catcher light. | ||
* @type {number} | ||
* @attribute | ||
*/ | ||
shadowDistance = 16; | ||
|
||
/** | ||
* The VSM blur size of the shadow catcher light. | ||
* @type {number} | ||
* @attribute | ||
*/ | ||
blurSize = 32; | ||
|
||
/** | ||
* The size of the shadow catcher. | ||
* @type {Vec2} | ||
* @attribute | ||
*/ | ||
size = new Vec2(1, 1); | ||
|
||
/** | ||
* @type {pc.Entity} | ||
*/ | ||
_plane; | ||
|
||
/** | ||
* @type {pc.Entity} | ||
*/ | ||
_light; | ||
|
||
initialize() { | ||
// create shadow catcher material | ||
const material = new StandardMaterial(); | ||
material.useSkybox = false; | ||
material.blendType = BLEND_NORMAL; | ||
material.chunks = { | ||
APIVersion: CHUNKAPI_1_70, | ||
endPS: endPS | ||
}; | ||
material.update(); | ||
|
||
// create shadow catcher geometry | ||
this._plane = new Entity('ShadowPlane'); | ||
this._plane.addComponent('render', { | ||
type: 'plane', | ||
castShadows: false, | ||
material | ||
}); | ||
this._plane.setLocalScale(this.size.x, 1, this.size.y); | ||
|
||
// create shadow catcher light | ||
this._light = new Entity('ShadowLight'); | ||
this._light.addComponent('light', { | ||
type: 'directional', | ||
castShadows: true, | ||
normalOffsetBias: 0, | ||
shadowBias: 0, | ||
shadowDistance: this.shadowDistance, | ||
shadowResolution: 1024, | ||
shadowType: SHADOW_VSM_16F, | ||
shadowUpdateMode: SHADOWUPDATE_REALTIME, | ||
vsmBlurSize: this.blurSize, | ||
shadowIntensity: 1, | ||
intensity: 0 | ||
}); | ||
|
||
this.app.root.addChild(this._plane); | ||
this.app.root.addChild(this._light); | ||
|
||
this.on('destroy', () => { | ||
this._plane.destroy(); | ||
this._light.destroy(); | ||
material.destroy(); | ||
}); | ||
} | ||
|
||
update() { | ||
this._plane.setLocalScale(this.size.x, 1, this.size.y ?? this.size.x); | ||
this._light.light.vsmBlurSize = this.blurSize; | ||
} | ||
} | ||
|
||
export { ShadowCatcherScript }; |
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.
few/most of the light properties should be exposed as attributes, allowing user control. Lets say I want soft shadows.
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.
Yep valid point