diff --git a/cocos/3d/lights/directional-light-component.ts b/cocos/3d/lights/directional-light-component.ts index 1c6334f470..d7dedc2d01 100644 --- a/cocos/3d/lights/directional-light-component.ts +++ b/cocos/3d/lights/directional-light-component.ts @@ -28,13 +28,16 @@ * @module component/light */ -import { ccclass, help, executeInEditMode, menu, tooltip, serializable, formerlySerializedAs } from 'cc.decorator'; +import { ccclass, range, slide, type, editable, visible, help, executeInEditMode, + menu, tooltip, serializable, formerlySerializedAs } from 'cc.decorator'; import { Light } from './light-component'; import { scene } from '../../core/renderer'; import { legacyCC } from '../../core/global-exports'; -import { Camera } from '../../core/renderer/scene'; +import { Camera, PCFType, Shadows, ShadowType } from '../../core/renderer/scene'; import { Root } from '../../core/root'; import { property } from '../../core/data/class-decorator'; +import { CCBoolean, CCFloat } from '../../core/data/utils/attribute'; +import { clamp } from '../../core'; @ccclass('cc.DirectionalLight') @help('i18n:cc.DirectionalLight') @@ -48,6 +51,34 @@ export class DirectionalLight extends Light { @serializable protected _illuminanceLDR = 1.0; + // Public properties of shadow + @serializable + protected _shadowEnabled = false; + + // Shadow map properties + @serializable + protected _shadowPcf = PCFType.HARD; + @serializable + protected _shadowBias = 0.00001; + @serializable + protected _shadowNormalBias = 0.0; + @serializable + protected _shadowSaturation = 1.0; + @serializable + protected _shadowDistance = 100; + @serializable + protected _shadowInvisibleOcclusionRange = 200; + + // fixed area properties + @serializable + protected _shadowFixedArea = false; + @serializable + protected _shadowNear = 0.1; + @serializable + protected _shadowFar = 10.0; + @serializable + protected _shadowOrthoSize = 5; + protected _type = scene.LightType.DIRECTIONAL; protected _light: scene.DirectionalLight | null = null; @@ -77,6 +108,227 @@ export class DirectionalLight extends Light { } } + /** + * @en Whether activate shadow + * @zh 是否启用阴影? + */ + @visible(() => (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type === ShadowType.ShadowMap) + @property({ group: { name: 'ShadowSettings', displayOrder: 1 } }) + @editable + @type(CCBoolean) + get shadowEnabled () { + return this._shadowEnabled; + } + set shadowEnabled (val) { + this._shadowEnabled = val; + if (this._light) { + this._light.shadowEnabled = this._shadowEnabled; + } + } + + /** + * @en get or set shadow pcf. + * @zh 获取或者设置阴影pcf等级。 + */ + @visible(() => (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type === ShadowType.ShadowMap) + @property({ group: { name: 'ShadowSettings', displayOrder: 5 } }) + @editable + @type(PCFType) + get shadowPcf () { + return this._shadowPcf; + } + set shadowPcf (val) { + this._shadowPcf = val; + if (this._light) { + this._light.shadowPcf = this._shadowPcf; + } + } + + /** + * @en get or set shadow map sampler offset + * @zh 获取或者设置阴影纹理偏移值 + */ + @visible(() => (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type === ShadowType.ShadowMap) + @property({ group: { name: 'ShadowSettings', displayOrder: 6 } }) + @editable + @type(CCFloat) + get shadowBias () { + return this._shadowBias; + } + set shadowBias (val) { + this._shadowBias = val; + if (this._light) { + this._light.shadowBias = this._shadowBias; + } + } + + /** + * @en get or set normal bias. + * @zh 设置或者获取法线偏移。 + */ + @visible(() => (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type === ShadowType.ShadowMap) + @property({ group: { name: 'ShadowSettings', displayOrder: 7 } }) + @editable + @type(CCFloat) + get shadowNormalBias () { + return this._shadowNormalBias; + } + set shadowNormalBias (val) { + this._shadowNormalBias = val; + if (this._light) { + this._light.shadowNormalBias = this._shadowNormalBias; + } + } + + /** + * @en Shadow color saturation + * @zh 阴影颜色饱和度 + */ + @visible(() => (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type === ShadowType.ShadowMap) + @property({ group: { name: 'ShadowSettings', displayOrder: 8 } }) + @editable + @range([0.0, 1.0, 0.01]) + @slide + @type(CCFloat) + get shadowSaturation () { + return this._shadowSaturation; + } + set shadowSaturation (val) { + this._shadowSaturation = clamp(val, 0.0, 1.0); + + if (this._light) { + this._light.shadowSaturation = this._shadowSaturation; + } + } + + /** + * @en get or set shadow camera far + * @zh 获取或者设置潜在阴影产生的范围 + */ + @visible(function (this: DirectionalLight) { + return (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type + === ShadowType.ShadowMap && this._shadowFixedArea === false; + }) + @property({ group: { name: 'ShadowSettings', displayOrder: 9 } }) + @editable + @tooltip('shadow visible distance: shadow quality is inversely proportional of the magnitude of this value') + @range([0.0, 2000.0, 0.1]) + @slide + @type(CCFloat) + get shadowDistance () { + return this._shadowDistance; + } + set shadowDistance (val) { + this._shadowDistance = Math.min(val, Shadows.MAX_FAR); + if (this._light) { + this._light.shadowDistance = this._shadowDistance; + } + } + + /** + * @en get or set shadow camera far + * @zh 获取或者设置潜在阴影产生的范围 + */ + @visible(function (this: DirectionalLight) { + return (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type + === ShadowType.ShadowMap && this._shadowFixedArea === false; + }) + @property({ group: { name: 'ShadowSettings', displayOrder: 10 } }) + @editable + @tooltip('if shadow has been culled, increase this value to fix it') + @range([0.0, 2000.0, 0.1]) + @slide + @type(CCFloat) + get shadowInvisibleOcclusionRange () { + return this._shadowInvisibleOcclusionRange; + } + set shadowInvisibleOcclusionRange (val) { + this._shadowInvisibleOcclusionRange = Math.min(val, Shadows.MAX_FAR); + if (this._light) { + this._light.shadowInvisibleOcclusionRange = this._shadowInvisibleOcclusionRange; + } + } + + /** + * @en get or set fixed area shadow + * @zh 是否是固定区域阴影 + */ + @visible(() => (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type === ShadowType.ShadowMap) + @property({ group: { name: 'ShadowSettings', displayOrder: 11 } }) + @editable + @type(CCBoolean) + get fixedArea () { + return this._shadowFixedArea; + } + set fixedArea (val) { + this._shadowFixedArea = val; + if (this._light) { + this._light.fixedArea = this._shadowFixedArea; + } + } + + /** + * @en get or set shadow camera near + * @zh 获取或者设置阴影相机近裁剪面 + */ + @visible(function (this: DirectionalLight) { + return (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type + === ShadowType.ShadowMap && this._shadowFixedArea === true; + }) + @property({ group: { name: 'ShadowSettings', displayOrder: 12 } }) + @editable + @type(CCFloat) + get fixedNear () { + return this._shadowNear; + } + set fixedNear (val) { + this._shadowNear = val; + if (this._light) { + this._light.fixedNear = this._shadowNear; + } + } + + /** + * @en get or set shadow camera far + * @zh 获取或者设置阴影相机远裁剪面 + */ + @visible(function (this: DirectionalLight) { + return (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type + === ShadowType.ShadowMap && this._shadowFixedArea === true; + }) + @property({ group: { name: 'ShadowSettings', displayOrder: 13 } }) + @editable + @type(CCFloat) + get fixedFar () { + return this._shadowFar; + } + set fixedFar (val) { + this._shadowFar = Math.min(val, Shadows.MAX_FAR); + if (this._light) { + this._light.fixedFar = this._shadowFar; + } + } + + /** + * @en get or set shadow camera orthoSize + * @zh 获取或者设置阴影相机正交大小 + */ + @visible(function (this: DirectionalLight) { + return (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type + === ShadowType.ShadowMap && this._shadowFixedArea === true; + }) + @property({ group: { name: 'ShadowSettings', displayOrder: 14 } }) + @type(CCFloat) + get fixedOrthoSize () { + return this._shadowOrthoSize; + } + set fixedOrthoSize (val) { + this._shadowOrthoSize = val; + if (this._light) { + this._light.fixedOrthoSize = this._shadowOrthoSize; + } + } + constructor () { super(); this._lightType = scene.DirectionalLight; @@ -88,6 +340,16 @@ export class DirectionalLight extends Light { if (this._light) { this._light.illuminanceHDR = this._illuminanceHDR; this._light.illuminanceLDR = this._illuminanceLDR; + // shadow info + this._light.shadowEnabled = this._shadowEnabled; + this._light.shadowPcf = this._shadowPcf; + this._light.shadowBias = this._shadowBias; + this._light.shadowNormalBias = this._shadowNormalBias; + this._light.shadowSaturation = this._shadowSaturation; + this._light.fixedArea = this._shadowFixedArea; + this._light.fixedNear = this._shadowNear; + this._light.fixedFar = this._shadowFar; + this._light.fixedOrthoSize = this._shadowOrthoSize; } } } diff --git a/cocos/3d/lights/spot-light-component.ts b/cocos/3d/lights/spot-light-component.ts index acc5022e91..d79ed87b89 100644 --- a/cocos/3d/lights/spot-light-component.ts +++ b/cocos/3d/lights/spot-light-component.ts @@ -28,15 +28,16 @@ * @module component/light */ -import { - ccclass, help, executeInEditMode, menu, tooltip, type, slide, range, serializable, formerlySerializedAs, -} from 'cc.decorator'; +import { ccclass, range, slide, type, editable, help, executeInEditMode, + menu, tooltip, serializable, formerlySerializedAs, visible } from 'cc.decorator'; import { toRadian } from '../../core/math'; import { scene } from '../../core/renderer'; import { Light, PhotometricTerm } from './light-component'; import { legacyCC } from '../../core/global-exports'; import { Root } from '../../core/root'; -import { Camera } from '../../core/renderer/scene'; +import { Camera, PCFType, ShadowType } from '../../core/renderer/scene'; +import { property } from '../../core/data/class-decorator'; +import { CCBoolean, CCFloat } from '../../core/data/utils/attribute'; @ccclass('cc.SpotLight') @help('i18n:cc.SpotLight') @@ -62,6 +63,16 @@ export class SpotLight extends Light { @serializable protected _spotAngle = 60; + // Shadow map properties + @serializable + protected _shadowEnabled = false; + @serializable + protected _shadowPcf = PCFType.HARD; + @serializable + protected _shadowBias = 0.00001; + @serializable + protected _shadowNormalBias = 0.0; + protected _type = scene.LightType.SPOT; protected _light: scene.SpotLight | null = null; @@ -182,6 +193,78 @@ export class SpotLight extends Light { if (this._light) { this._light.spotAngle = toRadian(val); } } + /** + * @en Whether activate shadow + * @zh 是否启用阴影? + */ + @visible(() => (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type === ShadowType.ShadowMap) + @property({ group: { name: 'ShadowSettings', displayOrder: 1 } }) + @editable + @type(CCBoolean) + get shadowEnabled () { + return this._shadowEnabled; + } + set shadowEnabled (val) { + this._shadowEnabled = val; + if (this._light) { + this._light.shadowEnabled = val; + } + } + + /** + * @en get or set shadow pcf. + * @zh 获取或者设置阴影pcf等级。 + */ + @visible(() => (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type === ShadowType.ShadowMap) + @property({ group: { name: 'ShadowSettings', displayOrder: 2 } }) + @editable + @type(PCFType) + get shadowPcf () { + return this._shadowPcf; + } + set shadowPcf (val) { + this._shadowPcf = val; + if (this._light) { + this._light.shadowPcf = val; + } + } + + /** + * @en get or set shadow map sampler offset + * @zh 获取或者设置阴影纹理偏移值 + */ + @visible(() => (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type === ShadowType.ShadowMap) + @property({ group: { name: 'ShadowSettings', displayOrder: 3 } }) + @editable + @type(CCFloat) + get shadowBias () { + return this._shadowBias; + } + set shadowBias (val) { + this._shadowBias = val; + if (this._light) { + this._light.shadowBias = val; + } + } + + /** + * @en get or set normal bias. + * @zh 设置或者获取法线偏移。 + */ + @visible(() => (legacyCC.director.root as Root).pipeline.pipelineSceneData.shadows.type === ShadowType.ShadowMap) + @property({ group: { name: 'ShadowSettings', displayOrder: 4 } }) + @editable + @type(CCFloat) + get shadowNormalBias () { + return this._shadowNormalBias; + } + set shadowNormalBias (val) { + this._shadowNormalBias = val; + if (this._light) { + this._light.shadowNormalBias = val; + } + } + constructor () { super(); this._lightType = scene.SpotLight; @@ -201,6 +284,11 @@ export class SpotLight extends Light { if (this._light) { this._light.luminanceHDR = this._luminanceHDR; this._light.luminanceLDR = this._luminanceLDR; + // shadow info + this._light.shadowEnabled = this._shadowEnabled; + this._light.shadowPcf = this._shadowPcf; + this._light.shadowBias = this._shadowBias; + this._light.shadowNormalBias = this._shadowNormalBias; } } } diff --git a/cocos/core/pipeline/pipeline-ubo.ts b/cocos/core/pipeline/pipeline-ubo.ts index ecb7038195..35c16e69c3 100644 --- a/cocos/core/pipeline/pipeline-ubo.ts +++ b/cocos/core/pipeline/pipeline-ubo.ts @@ -33,7 +33,7 @@ import { legacyCC } from '../global-exports'; import { ShadowType } from '../renderer/scene/shadows'; import { updatePlanarNormalAndDistance, updatePlanarPROJ } from './scene-culling'; import { Light, LightType } from '../renderer/scene/light'; -import { SpotLight } from '../renderer/scene'; +import { DirectionalLight, SpotLight } from '../renderer/scene'; import { RenderWindow } from '../renderer/core/render-window'; import { builtinResMgr } from '../builtin/builtin-res-mgr'; import { Texture2D } from '../assets'; @@ -69,7 +69,6 @@ export class PipelineUBO { public static updateCameraUBOView (pipeline: RenderPipeline, bufferView: Float32Array, camera: Camera) { - const root = legacyCC.director.root; const scene = camera.scene ? camera.scene : legacyCC.director.getScene().renderScene; const mainLight = scene.mainLight; const sceneData = pipeline.pipelineSceneData; @@ -172,9 +171,9 @@ export class PipelineUBO { const matShadowView = shadowInfo.matShadowView; const matShadowProj = shadowInfo.matShadowProj; const matShadowViewProj = shadowInfo.matShadowViewProj; - if (shadowInfo.fixedArea) { - near = shadowInfo.near; - far = shadowInfo.far; + if (mainLight.fixedArea) { + near = mainLight.fixedNear; + far = mainLight.fixedFar; } else { near = 0.1; far = shadowInfo.shadowCameraFar; @@ -199,16 +198,16 @@ export class PipelineUBO { sv[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 0] = near; sv[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 1] = far; sv[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 2] = linear; - sv[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 3] = 1.0 - shadowInfo.saturation; + sv[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 3] = 1.0 - mainLight.shadowSaturation; sv[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 0] = shadowInfo.size.x; sv[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 1] = shadowInfo.size.y; - sv[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 2] = shadowInfo.pcf; - sv[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 3] = shadowInfo.bias; + sv[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 2] = mainLight.shadowPcf; + sv[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 3] = mainLight.shadowBias; sv[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 0] = 0.0; sv[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 1] = packing; - sv[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 2] = shadowInfo.normalBias; + sv[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 2] = mainLight.shadowNormalBias; sv[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 3] = 0.0; } else if (mainLight && shadowInfo.type === ShadowType.Planar) { updatePlanarPROJ(shadowInfo, mainLight, sv); @@ -231,10 +230,11 @@ export class PipelineUBO { const matShadowProj = shadowInfo.matShadowProj; const matShadowViewProj = shadowInfo.matShadowViewProj; switch (light.type) { - case LightType.DIRECTIONAL: - if (shadowInfo.fixedArea) { - near = shadowInfo.near; - far = shadowInfo.far; + case LightType.DIRECTIONAL: { + const mainLight = light as DirectionalLight; + if (mainLight.fixedArea) { + near = mainLight.fixedNear; + far = mainLight.fixedFar; } else { near = 0.1; far = shadowInfo.shadowCameraFar; @@ -254,13 +254,18 @@ export class PipelineUBO { Mat4.toArray(bufferView, matShadowViewProj, UBOShadow.MAT_LIGHT_VIEW_PROJ_OFFSET); - _vec4ShadowInfo.set(near, far, linear, 1.0 - shadowInfo.saturation); + _vec4ShadowInfo.set(near, far, linear, 1.0 - mainLight.shadowSaturation); Vec4.toArray(sv, _vec4ShadowInfo, UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET); - _vec4ShadowInfo.set(0.0, packing, shadowInfo.normalBias, 0.0); + _vec4ShadowInfo.set(0.0, packing, mainLight.shadowNormalBias, 0.0); Vec4.toArray(sv, _vec4ShadowInfo, UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET); + + _vec4ShadowInfo.set(shadowInfo.size.x, shadowInfo.size.y, mainLight.shadowPcf, mainLight.shadowBias); + Vec4.toArray(sv, _vec4ShadowInfo, UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET); break; - case LightType.SPOT: + } + case LightType.SPOT: { + const spotLight = light as SpotLight; Mat4.invert(_matShadowView, (light as any).node.getWorldMatrix()); Mat4.toArray(sv, _matShadowView, UBOShadow.MAT_LIGHT_VIEW_OFFSET); @@ -269,18 +274,19 @@ export class PipelineUBO { Mat4.multiply(_matShadowViewProj, _matShadowProj, _matShadowView); Mat4.toArray(sv, _matShadowViewProj, UBOShadow.MAT_LIGHT_VIEW_PROJ_OFFSET); - _vec4ShadowInfo.set(0.01, (light as SpotLight).range, linear, 1.0 - shadowInfo.saturation); + _vec4ShadowInfo.set(0.01, (light as SpotLight).range, linear, 0.0); Vec4.toArray(sv, _vec4ShadowInfo, UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET); - _vec4ShadowInfo.set(1.0, packing, shadowInfo.normalBias, 0.0); + _vec4ShadowInfo.set(1.0, packing, spotLight.shadowNormalBias, 0.0); Vec4.toArray(sv, _vec4ShadowInfo, UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET); + + _vec4ShadowInfo.set(shadowInfo.size.x, shadowInfo.size.y, spotLight.shadowPcf, spotLight.shadowBias); + Vec4.toArray(sv, _vec4ShadowInfo, UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET); break; + } default: } - _vec4ShadowInfo.set(shadowInfo.size.x, shadowInfo.size.y, shadowInfo.pcf, shadowInfo.bias); - Vec4.toArray(sv, _vec4ShadowInfo, UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET); - Color.toArray(sv, shadowInfo.shadowColor, UBOShadow.SHADOW_COLOR_OFFSET); } @@ -329,7 +335,6 @@ export class PipelineUBO { UBOCamera.SIZE, )); ds.bindBuffer(UBOCamera.BINDING, cameraUBO); - const shadowUBO = device.createBuffer(new BufferInfo( BufferUsageBit.UNIFORM | BufferUsageBit.TRANSFER_DST, MemoryUsageBit.HOST | MemoryUsageBit.DEVICE, diff --git a/cocos/core/pipeline/render-additive-light-queue.ts b/cocos/core/pipeline/render-additive-light-queue.ts index 9140e03c75..61ca513bd9 100644 --- a/cocos/core/pipeline/render-additive-light-queue.ts +++ b/cocos/core/pipeline/render-additive-light-queue.ts @@ -305,7 +305,7 @@ export class RenderAdditiveLightQueue { let matShadowProj : Mat4; let matShadowInvProj : Mat4; switch (light.type) { - case LightType.SPHERE: + case LightType.SPHERE: { // planar PROJ if (mainLight) { updatePlanarPROJ(shadowInfo, mainLight, this._shadowUBO); @@ -314,18 +314,20 @@ export class RenderAdditiveLightQueue { this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 0] = shadowInfo.size.x; this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 1] = shadowInfo.size.y; - this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 2] = shadowInfo.pcf; - this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 3] = shadowInfo.bias; + this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 2] = 1.0; + this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 3] = 0.0; this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 0] = 2.0; this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 1] = packing; - this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 2] = shadowInfo.normalBias; + this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 2] = 0.0; this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 3] = 0.0; // Reserve sphere light shadow interface Color.toArray(this._shadowUBO, shadowInfo.shadowColor, UBOShadow.SHADOW_COLOR_OFFSET); break; - case LightType.SPOT: + } + case LightType.SPOT: { + const spotLight = light as SpotLight; // planar PROJ if (mainLight) { @@ -350,16 +352,16 @@ export class RenderAdditiveLightQueue { this._shadowUBO[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 0] = 0.01; this._shadowUBO[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 1] = (light as SpotLight).range; this._shadowUBO[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 2] = linear; - this._shadowUBO[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 3] = 1.0 - shadowInfo.saturation; + this._shadowUBO[UBOShadow.SHADOW_NEAR_FAR_LINEAR_SATURATION_INFO_OFFSET + 3] = 0.0; this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 0] = shadowInfo.size.x; this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 1] = shadowInfo.size.y; - this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 2] = shadowInfo.pcf; - this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 3] = shadowInfo.bias; + this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 2] = spotLight.shadowPcf; + this._shadowUBO[UBOShadow.SHADOW_WIDTH_HEIGHT_PCF_BIAS_INFO_OFFSET + 3] = spotLight.shadowBias; this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 0] = 1.0; this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 1] = packing; - this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 2] = shadowInfo.normalBias; + this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 2] = spotLight.shadowNormalBias; this._shadowUBO[UBOShadow.SHADOW_LIGHT_PACKING_NBIAS_NULL_INFO_OFFSET + 3] = 0.0; this._shadowUBO[UBOShadow.SHADOW_PROJ_DEPTH_INFO_OFFSET + 0] = matShadowProj.m10; @@ -387,6 +389,7 @@ export class RenderAdditiveLightQueue { } } break; + } default: } descriptorSet.update(); diff --git a/cocos/core/pipeline/scene-culling.ts b/cocos/core/pipeline/scene-culling.ts index a30cdc75a5..8055c1c818 100644 --- a/cocos/core/pipeline/scene-culling.ts +++ b/cocos/core/pipeline/scene-culling.ts @@ -261,11 +261,11 @@ export function QuantizeDirLightShadowCamera (out: Frustum, pipeline: RenderPipe dirLight: DirectionalLight, camera: Camera, shadowInfo: Shadows) { const device = pipeline.device; - if (shadowInfo.fixedArea) { - const x = shadowInfo.orthoSize; - const y = shadowInfo.orthoSize; - const near = shadowInfo.near; - const far = shadowInfo.far; + if (dirLight.fixedArea) { + const x = dirLight.fixedOrthoSize; + const y = dirLight.fixedOrthoSize; + const near = dirLight.fixedNear; + const far = dirLight.fixedFar; Mat4.fromRT(_matShadowTrans, dirLight.node!.getWorldRotation(), dirLight.node!.getWorldPosition()); Mat4.invert(_matShadowView, _matShadowTrans); Mat4.ortho(_matShadowProj, -x, x, -y, y, near, far, @@ -278,12 +278,12 @@ export function QuantizeDirLightShadowCamera (out: Frustum, pipeline: RenderPipe Frustum.createOrtho(out, x * 2.0, y * 2.0, near, far, _matShadowViewInv); } else { - const invisibleOcclusionRange = shadowInfo.invisibleOcclusionRange; + const invisibleOcclusionRange = dirLight.shadowInvisibleOcclusionRange; const shadowMapWidth = shadowInfo.size.x; // Raw data getCameraWorldMatrix(_mat4_trans, camera); - Frustum.split(_validFrustum, camera, _mat4_trans, 0.1, shadowInfo.shadowDistance); + Frustum.split(_validFrustum, camera, _mat4_trans, 0.1, dirLight.shadowDistance); _lightViewFrustum = Frustum.clone(_validFrustum); // view matrix with range back @@ -434,10 +434,4 @@ export function sceneCulling (pipeline: RenderPipeline, camera: Camera) { } } } - - // FirstSetCSM flag Bit Control - if (shadows.firstSetCSM) { - shadows.shadowDistance = _castWorldBounds.halfExtents.length() * 2.0; - shadows.firstSetCSM = false; - } } diff --git a/cocos/core/renderer/scene/deprecated.ts b/cocos/core/renderer/scene/deprecated.ts index ccea0874dc..96b5e91f7d 100644 --- a/cocos/core/renderer/scene/deprecated.ts +++ b/cocos/core/renderer/scene/deprecated.ts @@ -193,4 +193,34 @@ removeProperty(Shadows.prototype, 'Shadows.prototype', [ { name: 'autoAdapt', }, + { + name: 'fixedArea', + }, + { + name: 'pcf', + }, + { + name: 'bias', + }, + { + name: 'normalBias', + }, + { + name: 'near', + }, + { + name: 'far', + }, + { + name: 'shadowDistance', + }, + { + name: 'invisibleOcclusionRange', + }, + { + name: 'orthoSize', + }, + { + name: 'saturation', + }, ]); diff --git a/cocos/core/renderer/scene/directional-light.ts b/cocos/core/renderer/scene/directional-light.ts index 4472646d57..ad6e2fd419 100644 --- a/cocos/core/renderer/scene/directional-light.ts +++ b/cocos/core/renderer/scene/directional-light.ts @@ -29,6 +29,7 @@ import { Vec3 } from '../../math'; import { Ambient } from './ambient'; import { Light, LightType } from './light'; import { NativeDirectionalLight } from './native-scene'; +import { PCFType, Shadows } from './shadows'; const _forward = new Vec3(0, 0, -1); const _v3 = new Vec3(); @@ -38,6 +39,23 @@ export class DirectionalLight extends Light { protected _illuminanceHDR: number = Ambient.SUN_ILLUM; protected _illuminanceLDR = 1.0; + // Public properties of shadow + protected _shadowEnabled = false; + + // Shadow map properties + protected _shadowPcf = PCFType.HARD; + protected _shadowBias = 0.00001; + protected _shadowNormalBias = 0.0; + protected _shadowSaturation = 1.0; + protected _shadowDistance = 100; + protected _shadowInvisibleOcclusionRange = 200; + + // fixed area properties + protected _shadowFixedArea = false; + protected _shadowNear = 0.1; + protected _shadowFar = 10.0; + protected _shadowOrthoSize = 5; + set direction (dir: Vec3) { Vec3.normalize(this._dir, dir); if (JSB) { @@ -87,6 +105,160 @@ export class DirectionalLight extends Light { } } + /** + * @en Whether activate shadow + * @zh 是否启用阴影? + */ + get shadowEnabled () { + return this._shadowEnabled; + } + set shadowEnabled (val) { + this._shadowEnabled = val; + if (JSB) { + (this._nativeObj as NativeDirectionalLight).setShadowEnabled(val); + } + } + + /** + * @en get or set shadow pcf. + * @zh 获取或者设置阴影pcf等级。 + */ + get shadowPcf () { + return this._shadowPcf; + } + set shadowPcf (val) { + this._shadowPcf = val; + if (JSB) { + (this._nativeObj as NativeDirectionalLight).setShadowPcf(val); + } + } + + /** + * @en get or set shadow map sampler offset + * @zh 获取或者设置阴影纹理偏移值 + */ + get shadowBias () { + return this._shadowBias; + } + set shadowBias (val) { + this._shadowBias = val; + if (JSB) { + (this._nativeObj as NativeDirectionalLight).setShadowBias(val); + } + } + + /** + * @en get or set normal bias. + * @zh 设置或者获取法线偏移。 + */ + get shadowNormalBias () { + return this._shadowNormalBias; + } + set shadowNormalBias (val: number) { + this._shadowNormalBias = val; + if (JSB) { + (this._nativeObj as NativeDirectionalLight).setShadowNormalBias(val); + } + } + + /** + * @en Shadow color saturation + * @zh 阴影颜色饱和度 + */ + get shadowSaturation () { + return this._shadowSaturation; + } + set shadowSaturation (val: number) { + this._shadowSaturation = val; + if (JSB) { + (this._nativeObj as NativeDirectionalLight).setShadowSaturation(this._shadowSaturation); + } + } + + /** + * @en get or set shadow camera far + * @zh 获取或者设置潜在阴影产生的范围 + */ + get shadowDistance () { + return this._shadowDistance; + } + set shadowDistance (val) { + this._shadowDistance = Math.min(val, Shadows.MAX_FAR); + if (JSB) { + (this._nativeObj as NativeDirectionalLight).setShadowDistance(val); + } + } + + /** + * @en get or set shadow camera far + * @zh 获取或者设置潜在阴影产生的范围 + */ + get shadowInvisibleOcclusionRange () { + return this._shadowInvisibleOcclusionRange; + } + set shadowInvisibleOcclusionRange (val) { + this._shadowInvisibleOcclusionRange = Math.min(val, Shadows.MAX_FAR); + if (JSB) { + (this._nativeObj as NativeDirectionalLight).setShadowInvisibleOcclusionRange(val); + } + } + + /** + * @en get or set fixed area shadow + * @zh 是否是固定区域阴影 + */ + get fixedArea () { + return this._shadowFixedArea; + } + set fixedArea (val) { + this._shadowFixedArea = val; + if (JSB) { + (this._nativeObj as NativeDirectionalLight).setShadowFixedArea(val); + } + } + + /** + * @en get or set shadow camera near + * @zh 获取或者设置阴影相机近裁剪面 + */ + get fixedNear () { + return this._shadowNear; + } + set fixedNear (val) { + this._shadowNear = val; + if (JSB) { + (this._nativeObj as NativeDirectionalLight).setShadowNear(val); + } + } + + /** + * @en get or set shadow camera far + * @zh 获取或者设置阴影相机远裁剪面 + */ + get fixedFar () { + return this._shadowFar; + } + set fixedFar (val) { + this._shadowFar = Math.min(val, Shadows.MAX_FAR); + if (JSB) { + (this._nativeObj as NativeDirectionalLight).setShadowFar(val); + } + } + + /** + * @en get or set shadow camera orthoSize + * @zh 获取或者设置阴影相机正交大小 + */ + get fixedOrthoSize () { + return this._shadowOrthoSize; + } + set fixedOrthoSize (val) { + this._shadowOrthoSize = val; + if (JSB) { + (this._nativeObj as NativeDirectionalLight).setShadowOrthoSize(val); + } + } + constructor () { super(); this._type = LightType.DIRECTIONAL; diff --git a/cocos/core/renderer/scene/native-scene.ts b/cocos/core/renderer/scene/native-scene.ts index f35ed780b9..6cfd8e9424 100644 --- a/cocos/core/renderer/scene/native-scene.ts +++ b/cocos/core/renderer/scene/native-scene.ts @@ -114,6 +114,17 @@ export const NativeDirectionalLight: Constructor<{ setDirection (dir: Vec3): void; setIlluminanceHDR (lum: number): void; setIlluminanceLDR(lum: number): void; + setShadowEnabled(val: boolean): void; + setShadowPcf(val: number): void; + setShadowBias(val: number): void; + setShadowNormalBias(val: number): void; + setShadowSaturation(val: number): void; + setShadowDistance(val: number): void; + setShadowInvisibleOcclusionRange(val: number): void; + setShadowFixedArea(val: boolean): void; + setShadowNear(val: number): void; + setShadowFar(val: number): void; + setShadowOrthoSize(val: number): void; } & NativeLight> = null!; export type NativeDirectionalLight = InstanceType; @@ -138,6 +149,10 @@ export const NativeSpotLight: Constructor<{ setAngle (angle: number): void; setLuminanceHDR (lum: number): void; setLuminanceLDR(lum: number): void; + setShadowEnabled(val: boolean): void; + setShadowPcf(val: number): void; + setShadowBias(val: number): void; + setShadowNormalBias(val: number): void; } & NativeLight> = null!; export type NativeSpotLight = InstanceType; @@ -284,25 +299,15 @@ export const NativeAmbient: Constructor<{ export type NativeAmbient = InstanceType; export const NativeShadow: Constructor<{ + enabled: boolean; + shadowType: number; normal: Vec3; distance: number; color: Color; - nearValue: number; - farValue: number; - invisibleOcclusionRange: number; - shadowDistance: number; - orthoSize: number; size: Vec2; - pcfType: number; shadowMapDirty: boolean; - bias: number; - normalBias: number; - fixedArea: boolean; planarPass: NativePass; instancePass: NativePass; - enabled: boolean; - shadowType: number; - saturation: number; }> = null!; export type NativeShadow = InstanceType; diff --git a/cocos/core/renderer/scene/shadows.ts b/cocos/core/renderer/scene/shadows.ts index bafb279396..15df344f87 100644 --- a/cocos/core/renderer/scene/shadows.ts +++ b/cocos/core/renderer/scene/shadows.ts @@ -150,6 +150,18 @@ export class Shadows { this.activate(); } + /** + * @en Shadow type. + * @zh 阴影类型。 + */ + get type (): number { + return this._type; + } + set type (val: number) { + this._setType(val); + this.activate(); + } + /** * @en The normal of the plane which receives shadow. * @zh 阴影接收平面的法线。 @@ -195,88 +207,6 @@ export class Shadows { } } - /** - * @en get or set shadow invisible Occlusion Range. - * @zh 控制潜在遮挡体产生的范围。 - */ - public get invisibleOcclusionRange (): number { - return this._invisibleOcclusionRange; - } - public set invisibleOcclusionRange (val: number) { - this._invisibleOcclusionRange = val; - if (JSB) { - this._nativeObj!.invisibleOcclusionRange = val; - } - } - - /** - * @en get or set shadow distance. - * @zh 控制阴影的可视范围。 - */ - public get shadowDistance (): number { - return this._shadowDistance; - } - public set shadowDistance (val: number) { - this._shadowDistance = val; - if (JSB) { - this._nativeObj!.shadowDistance = val; - } - } - - /** - * @en Shadow type. - * @zh 阴影类型。 - */ - get type (): number { - return this._type; - } - set type (val: number) { - this._setType(val); - this.activate(); - } - - /** - * @en get or set shadow camera near. - * @zh 获取或者设置阴影相机近裁剪面。 - */ - public get near (): number { - return this._near; - } - public set near (val: number) { - this._near = val; - if (JSB) { - this._nativeObj!.nearValue = val; - } - } - - /** - * @en get or set shadow camera far. - * @zh 获取或者设置阴影相机远裁剪面。 - */ - public get far (): number { - return this._far; - } - public set far (val: number) { - this._far = val; - if (JSB) { - this._nativeObj!.farValue = val; - } - } - - /** - * @en get or set shadow camera orthoSize. - * @zh 获取或者设置阴影相机正交大小。 - */ - public get orthoSize (): number { - return this._orthoSize; - } - public set orthoSize (val: number) { - this._orthoSize = val; - if (JSB) { - this._nativeObj!.orthoSize = val; - } - } - /** * @en get or set shadow camera orthoSize. * @zh 获取或者设置阴影纹理大小。 @@ -291,20 +221,6 @@ export class Shadows { } } - /** - * @en get or set shadow pcf. - * @zh 获取或者设置阴影pcf等级。 - */ - public get pcf (): number { - return this._pcf; - } - public set pcf (val: number) { - this._pcf = val; - if (JSB) { - this._nativeObj!.pcfType = val; - } - } - /** * @en shadow Map size has been modified. * @zh 阴影贴图大小是否被修改。 @@ -319,62 +235,6 @@ export class Shadows { } } - /** - * @en get or set shadow bias. - * @zh 获取或者设置阴影偏移量。 - */ - public get bias (): number { - return this._bias; - } - public set bias (val: number) { - this._bias = val; - if (JSB) { - this._nativeObj!.bias = val; - } - } - - /** - * @en get or set normal bias. - * @zh 设置或者获取法线偏移。 - */ - public get normalBias (): number { - return this._normalBias; - } - public set normalBias (val: number) { - this._normalBias = val; - if (JSB) { - this._nativeObj!.normalBias = val; - } - } - - /** - * @en get or set shadow saturation. - * @zh 设置或者获取阴影饱和度。 - */ - public get saturation (): number { - return this._saturation; - } - public set saturation (val: number) { - this._saturation = val; - if (JSB) { - this._nativeObj!.saturation = val; - } - } - - /** - * @en get or set fixed area shadow - * @zh 是否是固定区域阴影 - */ - public get fixedArea (): boolean { - return this._fixedArea; - } - public set fixedArea (val: boolean) { - this._fixedArea = val; - if (JSB) { - this._nativeObj!.fixedArea = val; - } - } - public get matLight () { return this._matLight; } @@ -406,26 +266,18 @@ export class Shadows { public matShadowProj = new Mat4(); public matShadowViewProj = new Mat4(); + protected _enabled = false; + protected _type = SHADOW_TYPE_NONE; + protected _distance = 0; protected _normal = new Vec3(0, 1, 0); protected _shadowColor = new Color(0, 0, 0, 76); + protected _size: Vec2 = new Vec2(512, 512); + protected _shadowMapDirty = false; + protected _matLight = new Mat4(); protected _material: Material | null = null; protected _instancingMaterial: Material | null = null; - protected _size: Vec2 = new Vec2(512, 512); - protected _enabled = false; - protected _distance = 0; - protected _type = SHADOW_TYPE_NONE; - protected _near = 0.1; - protected _far = 10; - protected _invisibleOcclusionRange = 200; - protected _shadowDistance = 100; - protected _orthoSize = 1; - protected _pcf = 0; - protected _shadowMapDirty = false; - protected _bias = 0; - protected _normalBias = 0; - protected _fixedArea = false; - protected _saturation = 0.75; + protected declare _nativeObj: NativeShadow | null; get native (): NativeShadow { @@ -478,23 +330,14 @@ export class Shadows { } public initialize (shadowsInfo: ShadowsInfo) { - this.near = shadowsInfo.near; - this.far = shadowsInfo.far; - this.invisibleOcclusionRange = shadowsInfo.invisibleOcclusionRange; - this.shadowDistance = shadowsInfo.shadowDistance; - this.orthoSize = shadowsInfo.orthoSize; - this.size = shadowsInfo.size; - this.pcf = shadowsInfo.pcf; + this._setEnable(shadowsInfo.enabled); + this._setType(shadowsInfo.type); + this.normal = shadowsInfo.normal; this.distance = shadowsInfo.distance; this.shadowColor = shadowsInfo.shadowColor; - this.bias = shadowsInfo.bias; - this.normalBias = shadowsInfo.normalBias; this.maxReceived = shadowsInfo.maxReceived; - this.fixedArea = shadowsInfo.fixedArea; - this._setEnable(shadowsInfo.enabled); - this._setType(shadowsInfo.type); - this.saturation = shadowsInfo.saturation; + this.size = shadowsInfo.size; } public activate () { diff --git a/cocos/core/renderer/scene/spot-light.ts b/cocos/core/renderer/scene/spot-light.ts index bd92e2520b..e6d2cd11fb 100644 --- a/cocos/core/renderer/scene/spot-light.ts +++ b/cocos/core/renderer/scene/spot-light.ts @@ -29,6 +29,7 @@ import { legacyCC } from '../../global-exports'; import { Mat4, Quat, Vec3 } from '../../math'; import { Light, LightType, nt2lm } from './light'; import { NativeSpotLight } from './native-scene'; +import { PCFType } from './shadows'; const _forward = new Vec3(0, 0, -1); const _qt = new Quat(); @@ -70,6 +71,12 @@ export class SpotLight extends Light { protected _aspect = 0; + // Shadow map properties + protected _shadowEnabled = false; + protected _shadowPcf = PCFType.HARD; + protected _shadowBias = 0.00001; + protected _shadowNormalBias = 0.0; + protected _init (): void { super._init(); if (JSB) { @@ -204,6 +211,62 @@ export class SpotLight extends Light { return this._frustum; } + /** + * @en Whether activate shadow + * @zh 是否启用阴影? + */ + get shadowEnabled () { + return this._shadowEnabled; + } + set shadowEnabled (val) { + this._shadowEnabled = val; + if (JSB) { + (this._nativeObj! as NativeSpotLight).setShadowEnabled(val); + } + } + + /** + * @en get or set shadow pcf. + * @zh 获取或者设置阴影pcf等级。 + */ + get shadowPcf () { + return this._shadowPcf; + } + set shadowPcf (val) { + this._shadowPcf = val; + if (JSB) { + (this._nativeObj! as NativeSpotLight).setShadowPcf(val); + } + } + + /** + * @en get or set shadow map sampler offset + * @zh 获取或者设置阴影纹理偏移值 + */ + get shadowBias () { + return this._shadowBias; + } + set shadowBias (val) { + this._shadowBias = val; + if (JSB) { + (this._nativeObj! as NativeSpotLight).setShadowBias(val); + } + } + + /** + * @en get or set normal bias. + * @zh 设置或者获取法线偏移。 + */ + get shadowNormalBias () { + return this._shadowNormalBias; + } + set shadowNormalBias (val: number) { + this._shadowNormalBias = val; + if (JSB) { + (this._nativeObj! as NativeSpotLight).setShadowNormalBias(val); + } + } + constructor () { super(); this._aabb = AABB.create(); diff --git a/cocos/core/scene-graph/deprecated.ts b/cocos/core/scene-graph/deprecated.ts index 3a81b7a039..98d0f032ef 100644 --- a/cocos/core/scene-graph/deprecated.ts +++ b/cocos/core/scene-graph/deprecated.ts @@ -153,6 +153,36 @@ removeProperty(SceneGlobals.prototype, 'SceneGlobals.prototype', [ { name: 'autoAdapt', }, + { + name: 'fixedArea', + }, + { + name: 'pcf', + }, + { + name: 'bias', + }, + { + name: 'normalBias', + }, + { + name: 'near', + }, + { + name: 'far', + }, + { + name: 'shadowDistance', + }, + { + name: 'invisibleOcclusionRange', + }, + { + name: 'orthoSize', + }, + { + name: 'saturation', + }, ]); removeProperty(Node.prototype, 'Node.prototype', [ diff --git a/cocos/core/scene-graph/scene-globals.ts b/cocos/core/scene-graph/scene-globals.ts index 492ad0ed47..67b85f9593 100644 --- a/cocos/core/scene-graph/scene-globals.ts +++ b/cocos/core/scene-graph/scene-globals.ts @@ -653,42 +653,20 @@ export class FogInfo { @ccclass('cc.ShadowsInfo') @help('i18n:cc.Shadow') export class ShadowsInfo { - @serializable - protected _type = ShadowType.Planar; @serializable protected _enabled = false; @serializable + protected _type = ShadowType.Planar; + @serializable protected _normal = new Vec3(0, 1, 0); @serializable protected _distance = 0; @serializable protected _shadowColor = new Color(0, 0, 0, 76); @serializable - protected _firstSetCSM = false; - @serializable - protected _fixedArea = false; - @serializable - protected _pcf = PCFType.HARD; - @serializable - protected _bias = 0.00001; - @serializable - protected _normalBias = 0.0; - @serializable - protected _near = 0.1; - @serializable - protected _far = 10.0; - @serializable - protected _shadowDistance = 100; - @serializable - protected _invisibleOcclusionRange = 200; - @serializable - protected _orthoSize = 5; - @serializable protected _maxReceived = 4; @serializable protected _size = new Vec2(512, 512); - @serializable - protected _saturation = 0.75; protected _resource: Shadows | null = null; @@ -759,8 +737,8 @@ export class ShadowsInfo { * @zh 阴影接收平面与原点的距离 */ @type(CCFloat) - @tooltip('i18n:shadow.distance') @visible(function (this: ShadowsInfo) { return this._type === ShadowType.Planar; }) + @tooltip('i18n:shadow.distance') set distance (val: number) { this._distance = val; if (this._resource) { this._resource.distance = val; } @@ -769,44 +747,6 @@ export class ShadowsInfo { return this._distance; } - /** - * @en Shadow color saturation - * @zh 阴影颜色饱和度 - */ - @editable - @range([0.0, 1.0, 0.01]) - @slide - @type(CCFloat) - @tooltip('i18n:shadow.saturation') - @visible(function (this: ShadowsInfo) { return this._type === ShadowType.ShadowMap; }) - set saturation (val: number) { - if (val > 1.0) { - this._saturation = val / val; - if (this._resource) { this._resource.saturation = val / val; } - } else { - this._saturation = val; - if (this._resource) { this._resource.saturation = val; } - } - } - get saturation () { - return this._saturation; - } - - /** - * @en The normal of the plane which receives shadow - * @zh 阴影接收平面的法线 - */ - @type(PCFType) - @tooltip('i18n:shadow.pcf') - @visible(function (this: ShadowsInfo) { return this._type === ShadowType.ShadowMap; }) - set pcf (val) { - this._pcf = val; - if (this._resource) { this._resource.pcf = val; } - } - get pcf () { - return this._pcf; - } - /** * @en get or set shadow max received * @zh 获取或者设置阴影接收的最大光源数量 @@ -821,36 +761,6 @@ export class ShadowsInfo { return this._maxReceived; } - /** - * @en get or set shadow map sampler offset - * @zh 获取或者设置阴影纹理偏移值 - */ - @type(CCFloat) - @tooltip('i18n:shadow.bias') - @visible(function (this: ShadowsInfo) { return this._type === ShadowType.ShadowMap; }) - set bias (val: number) { - this._bias = val; - if (this._resource) { this._resource.bias = val; } - } - get bias () { - return this._bias; - } - - /** - * @en on or off Self-shadowing. - * @zh 打开或者关闭自阴影。 - */ - @type(CCFloat) - @tooltip('i18n:shadow.normalBias') - @visible(function (this: ShadowsInfo) { return this._type === ShadowType.ShadowMap; }) - set normalBias (val: number) { - this._normalBias = val; - if (this._resource) { this._resource.normalBias = val; } - } - get normalBias () { - return this._normalBias; - } - /** * @en get or set shadow map size * @zh 获取或者设置阴影纹理大小 @@ -872,106 +782,6 @@ export class ShadowsInfo { return this._size; } - /** - * @en get or set fixed area shadow - * @zh 是否是固定区域阴影 - */ - @type(CCBoolean) - @tooltip('i18n:shadow.fixedArea') - @visible(function (this: ShadowsInfo) { return this._type === ShadowType.ShadowMap; }) - set fixedArea (val) { - this._fixedArea = val; - if (this._resource) { this._resource.fixedArea = val; } - } - get fixedArea () { - return this._fixedArea; - } - - /** - * @en get or set shadow camera near - * @zh 获取或者设置阴影相机近裁剪面 - */ - @type(CCFloat) - @tooltip('i18n:shadow.near') - @visible(function (this: ShadowsInfo) { return this._type === ShadowType.ShadowMap && this._fixedArea === true; }) - set near (val: number) { - this._near = val; - if (this._resource) { this._resource.near = val; } - } - get near () { - return this._near; - } - - /** - * @en get or set shadow camera far - * @zh 获取或者设置阴影相机远裁剪面 - */ - @type(CCFloat) - @tooltip('i18n:shadow.far') - @visible(function (this: ShadowsInfo) { return this._type === ShadowType.ShadowMap && this._fixedArea === true; }) - set far (val: number) { - this._far = Math.min(val, Shadows.MAX_FAR); - if (this._resource) { this._resource.far = this._far; } - } - get far () { - return this._far; - } - - /** - * @en get or set shadow camera far - * @zh 获取或者设置潜在阴影产生的范围 - */ - @editable - @range([0.0, 2000.0, 0.1]) - @slide - @type(CCFloat) - @tooltip('i18n:shadow.invisibleOcclusionRange') - @visible(function (this: ShadowsInfo) { return this._type === ShadowType.ShadowMap && this._fixedArea === false; }) - set invisibleOcclusionRange (val: number) { - this._invisibleOcclusionRange = Math.min(val, Shadows.MAX_FAR); - if (this._resource) { - this._resource.invisibleOcclusionRange = this._invisibleOcclusionRange; - } - } - get invisibleOcclusionRange () { - return this._invisibleOcclusionRange; - } - - /** - * @en get or set shadow camera far - * @zh 获取或者设置潜在阴影产生的范围 - */ - @editable - @range([0.0, 2000.0, 0.1]) - @slide - @type(CCFloat) - @tooltip('i18n:shadow.shadowDistance') - @visible(function (this: ShadowsInfo) { return this._type === ShadowType.ShadowMap && this._fixedArea === false; }) - set shadowDistance (val: number) { - this._shadowDistance = Math.min(val, Shadows.MAX_FAR); - if (this._resource) { - this._resource.shadowDistance = this._shadowDistance; - } - } - get shadowDistance () { - return this._shadowDistance; - } - - /** - * @en get or set shadow camera orthoSize - * @zh 获取或者设置阴影相机正交大小 - */ - @type(CCFloat) - @tooltip('i18n:shadow.orthoSize') - @visible(function (this: ShadowsInfo) { return this._type === ShadowType.ShadowMap && this._fixedArea === true; }) - set orthoSize (val: number) { - this._orthoSize = val; - if (this._resource) { this._resource.orthoSize = val; } - } - get orthoSize () { - return this._orthoSize; - } - /** * @en Set plane which receives shadow with the given node's world transformation * @zh 根据指定节点的世界变换设置阴影接收平面的信息 @@ -985,20 +795,7 @@ export class ShadowsInfo { } public activate (resource: Shadows) { - this.pcf = Math.min(this._pcf, PCFType.SOFT_2X); - this._resource = resource; - if (EDITOR && this._firstSetCSM) { - this._resource.firstSetCSM = this._firstSetCSM; - // Only the first time render in editor will trigger the auto calculation of shadowDistance - legacyCC.director.once(legacyCC.Director.EVENT_AFTER_DRAW, () => { - // Sync automatic calculated shadowDistance in renderer - this._firstSetCSM = false; - if (this._resource) { - this.shadowDistance = Math.min(this._resource.shadowDistance, Shadows.MAX_FAR); - } - }); - } this._resource.initialize(this); this._resource.activate(); }