Skip to content

Commit 2b5bf20

Browse files
mvaligurskyMartin Valigursky
andauthored
Further small shadow rendering refactor + fix (playcanvas#3162)
* moved shadow matrix to lightRenderData * fix to cookie without shadows * suggested refactor Co-authored-by: Martin Valigursky <mvaligursky@snapchat.com>
1 parent 2b0ed11 commit 2b5bf20

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/scene/light.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class LightRenderData {
4545
// camera used to cull / render the shadow map
4646
this.shadowCamera = ShadowRenderer.createShadowCamera(device, light._shadowType, light._type, face);
4747

48+
this.shadowMatrix = new Mat4();
49+
4850
// face index, value is based on light type:
4951
// - spot: always 0
5052
// - omni: cubemap face, 0..5
@@ -134,7 +136,6 @@ class Light {
134136

135137
// Shadow mapping resources
136138
this._shadowMap = null;
137-
this._shadowMatrix = new Mat4();
138139
this._rendererParams = [];
139140

140141
// Shadow mapping properties
@@ -146,6 +147,9 @@ class Light {
146147
this._isVsm = false;
147148
this._isPcf = true;
148149

150+
// cookie matrix (used in case the shadow mapping is disabled and so the shadow matrix cannot be used)
151+
this._cookieMatrix = null;
152+
149153
this._scene = null;
150154
this._node = null;
151155

@@ -594,6 +598,13 @@ class Light {
594598
}
595599
}
596600

601+
get cookieMatrix() {
602+
if (!this._cookieMatrix) {
603+
this._cookieMatrix = new Mat4();
604+
}
605+
return this._cookieMatrix;
606+
}
607+
597608
get cookie() {
598609
return this._cookie;
599610
}

src/scene/renderer/forward-renderer.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -622,15 +622,15 @@ class ForwardRenderer {
622622
directional._normalOffsetBias;
623623

624624
this.lightShadowMapId[cnt].setValue(lightRenderData.shadowBuffer);
625-
this.lightShadowMatrixId[cnt].setValue(directional._shadowMatrix.data);
625+
this.lightShadowMatrixId[cnt].setValue(lightRenderData.shadowMatrix.data);
626626
var params = directional._rendererParams;
627627
params.length = 3;
628628
params[0] = directional._shadowResolution;
629629
params[1] = normalBias;
630630
params[2] = bias;
631631
this.lightShadowParamsId[cnt].setValue(params);
632632
if (this.mainLight < 0) {
633-
this.lightShadowMatrixVsId[cnt].setValue(directional._shadowMatrix.data);
633+
this.lightShadowMatrixVsId[cnt].setValue(lightRenderData.shadowMatrix.data);
634634
this.lightShadowParamsVsId[cnt].setValue(params);
635635
directional._direction.normalize();
636636
this.lightDirVs[cnt][0] = directional._direction.x;
@@ -730,6 +730,7 @@ class ForwardRenderer {
730730
this.lightDir[cnt][2] = spot._direction.z;
731731
this.lightDirId[cnt].setValue(this.lightDir[cnt]);
732732

733+
let cookieMatrix;
733734
if (spot.castShadows) {
734735
var bias;
735736
if (spot._isVsm) {
@@ -746,19 +747,22 @@ class ForwardRenderer {
746747
const lightRenderData = spot.getRenderData(null, 0);
747748
this.lightShadowMapId[cnt].setValue(lightRenderData.shadowBuffer);
748749

749-
this.lightShadowMatrixId[cnt].setValue(spot._shadowMatrix.data);
750+
this.lightShadowMatrixId[cnt].setValue(lightRenderData.shadowMatrix.data);
750751
var params = spot._rendererParams;
751752
params.length = 4;
752753
params[0] = spot._shadowResolution;
753754
params[1] = normalBias;
754755
params[2] = bias;
755756
params[3] = 1.0 / spot.attenuationEnd;
756757
this.lightShadowParamsId[cnt].setValue(params);
758+
759+
cookieMatrix = lightRenderData.shadowMatrix;
757760
}
761+
758762
if (spot._cookie) {
759763
this.lightCookieId[cnt].setValue(spot._cookie);
760764
if (!spot.castShadows) {
761-
const cookieCam = ShadowRenderer.getSpotCookieCamera();
765+
const cookieCam = ForwardRenderer.getSpotCookieCamera();
762766
cookieCam.fov = spot._outerConeAngle * 2;
763767

764768
const cookieNode = cookieCam._node;
@@ -768,9 +772,11 @@ class ForwardRenderer {
768772

769773
shadowCamView.setTRS(cookieNode.getPosition(), cookieNode.getRotation(), Vec3.ONE).invert();
770774
shadowCamViewProj.mul2(cookieCam.projectionMatrix, shadowCamView);
771-
spot._shadowMatrix.mul2(ShadowRenderer.scaleShiftMatrix, shadowCamViewProj);
775+
776+
cookieMatrix = spot.cookieMatrix;
777+
cookieMatrix.mul2(ShadowRenderer.scaleShiftMatrix, shadowCamViewProj);
772778
}
773-
this.lightShadowMatrixId[cnt].setValue(spot._shadowMatrix.data);
779+
this.lightShadowMatrixId[cnt].setValue(cookieMatrix.data);
774780
this.lightCookieIntId[cnt].setValue(spot.cookieIntensity);
775781
if (spot._cookieTransform) {
776782
spot._cookieTransformUniform[0] = spot._cookieTransform.x;

src/scene/renderer/shadow-renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ class ShadowRenderer {
513513
if (type !== LIGHTTYPE_OMNI) {
514514
shadowCamView.setTRS(shadowCamNode.getPosition(), shadowCamNode.getRotation(), Vec3.ONE).invert();
515515
shadowCamViewProj.mul2(shadowCam.projectionMatrix, shadowCamView);
516-
light._shadowMatrix.mul2(ShadowRenderer.scaleShiftMatrix, shadowCamViewProj);
516+
lightRenderData.shadowMatrix.mul2(ShadowRenderer.scaleShiftMatrix, shadowCamViewProj);
517517
}
518518

519519
forwardRenderer.setCamera(shadowCam, shadowCam.renderTarget, true, faceCount === 1);

0 commit comments

Comments
 (0)