From acbefafe6ae673d2ffe65f35ab6adce8676e2d8d Mon Sep 17 00:00:00 2001 From: troublemaker52025 Date: Tue, 17 Nov 2020 16:29:41 +0800 Subject: [PATCH] add planar shadow for C++ (#7801) --- .../core/pipeline/forward/forward-pipeline.ts | 8 ++++-- .../pipeline/forward/planar-shadow-queue.ts | 5 ++-- cocos/core/pipeline/forward/scene-culling.ts | 27 +++++++++++++++++++ cocos/core/renderer/core/memory-pools.ts | 13 +++++---- cocos/core/renderer/scene/shadows.ts | 6 +++++ 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/cocos/core/pipeline/forward/forward-pipeline.ts b/cocos/core/pipeline/forward/forward-pipeline.ts index ae7936b5d6..b607d74c5a 100644 --- a/cocos/core/pipeline/forward/forward-pipeline.ts +++ b/cocos/core/pipeline/forward/forward-pipeline.ts @@ -40,12 +40,12 @@ import { ColorAttachment, DepthStencilAttachment, RenderPass, LoadOp, TextureLay import { SKYBOX_FLAG } from '../../renderer/scene/camera'; import { legacyCC } from '../../global-exports'; import { RenderView } from '../render-view'; -import { Mat4, Vec3, Vec4} from '../../math'; +import { Color, color, Mat4, Vec3, Vec4} from '../../math'; import { Fog } from '../../renderer/scene/fog'; import { Ambient } from '../../renderer/scene/ambient'; import { Skybox } from '../../renderer/scene/skybox'; import { Shadows, ShadowType } from '../../renderer/scene/shadows'; -import { sceneCulling, getShadowWorldMatrix } from './scene-culling'; +import { sceneCulling, getShadowWorldMatrix, updatePlanarPROJ } from './scene-culling'; import { UIFlow } from '../ui/ui-flow'; import { Light } from '../../renderer/scene/light'; @@ -249,8 +249,12 @@ export class ForwardPipeline extends RenderPipeline { this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 1] = shadowInfo.size.y; this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 2] = shadowInfo.pcf; this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 3] = shadowInfo.bias; + } else if (mainLight && shadowInfo.type === ShadowType.Planar) { + updatePlanarPROJ(shadowInfo, mainLight, this._shadowUBO); } + Color.toArray(this._shadowUBO, shadowInfo.shadowColor, UBOShadow.SHADOW_COLOR_OFFSET); + // update ubos this._commandBuffers[0].updateBuffer(this._descriptorSet.getBuffer(UBOGlobal.BINDING), this._globalUBO); this._commandBuffers[0].updateBuffer(this._descriptorSet.getBuffer(UBOShadow.BINDING), this._shadowUBO); diff --git a/cocos/core/pipeline/forward/planar-shadow-queue.ts b/cocos/core/pipeline/forward/planar-shadow-queue.ts index 56849be871..63b9a25e24 100644 --- a/cocos/core/pipeline/forward/planar-shadow-queue.ts +++ b/cocos/core/pipeline/forward/planar-shadow-queue.ts @@ -29,7 +29,7 @@ import { CommandBuffer, Device, RenderPass, Shader } from '../../gfx'; import { InstancedBuffer } from '../instanced-buffer'; import { PipelineStateManager } from '../../pipeline/pipeline-state-manager'; import { Model } from '../../renderer/scene'; -import { DSPool, ShaderPool, PassPool, PassView } from '../../renderer/core/memory-pools'; +import { DSPool, ShaderPool, PassPool, PassView, ShadowsPool, ShadowsView } from '../../renderer/core/memory-pools'; import { RenderInstancedQueue } from '../render-instanced-queue'; import { ForwardPipeline } from './forward-pipeline'; import { ShadowType } from '../../renderer/scene/shadows'; @@ -41,7 +41,6 @@ const _ab = new aabb(); export class PlanarShadowQueue { private _pendingModels: Model[] = []; private _instancedQueue = new RenderInstancedQueue(); - private _shaderCache = new Map(); private _pipeline: ForwardPipeline; constructor (pipeline: ForwardPipeline) { @@ -90,13 +89,13 @@ export class PlanarShadowQueue { const pass = shadows.material.passes[0]; const descriptorSet = DSPool.get(PassPool.get(pass.handle, PassView.DESCRIPTOR_SET)); cmdBuff.bindDescriptorSet(SetIndex.MATERIAL, descriptorSet); + const shader = ShaderPool.get(ShadowsPool.get(shadows.handle, ShadowsView.PLANAR_SHADER)); const modelCount = this._pendingModels.length; for (let i = 0; i < modelCount; i++) { const model = this._pendingModels[i]; for (let j = 0; j < model.subModels.length; j++) { const subModel = model.subModels[j]; - const shader = ShaderPool.get(pass.getShaderVariant(subModel.patches)); const ia = subModel.inputAssembler!; const pso = PipelineStateManager.getOrCreatePipelineState(device, pass, shader, renderPass, ia); cmdBuff.bindPipelineState(pso); diff --git a/cocos/core/pipeline/forward/scene-culling.ts b/cocos/core/pipeline/forward/scene-culling.ts index d371721a1e..b3fc2565b4 100644 --- a/cocos/core/pipeline/forward/scene-culling.ts +++ b/cocos/core/pipeline/forward/scene-culling.ts @@ -147,6 +147,33 @@ function updateDirLight (pipeline: ForwardPipeline, light: DirectionalLight) { Mat4.toArray(pipeline.shadowUBO, shadows.matLight, UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET); } +export function updatePlanarPROJ (shadowInfo: Shadows, light: DirectionalLight, shadowUBO: Float32Array){ + const dir = light.direction; + const n = shadowInfo.normal; const d = shadowInfo.distance + 0.001; // avoid z-fighting + const NdL = Vec3.dot(n, dir); const scale = 1 / NdL; + const lx = dir.x * scale; const ly = dir.y * scale; const lz = dir.z * scale; + const nx = n.x; const ny = n.y; const nz = n.z; + const m = shadowInfo.matLight; + m.m00 = 1 - nx * lx; + m.m01 = -nx * ly; + m.m02 = -nx * lz; + m.m03 = 0; + m.m04 = -ny * lx; + m.m05 = 1 - ny * ly; + m.m06 = -ny * lz; + m.m07 = 0; + m.m08 = -nz * lx; + m.m09 = -nz * ly; + m.m10 = 1 - nz * lz; + m.m11 = 0; + m.m12 = lx * d; + m.m13 = ly * d; + m.m14 = lz * d; + m.m15 = 1; + + Mat4.toArray(shadowUBO, m, UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET); +} + export function lightCollecting (view: RenderView, lightNumber: number) { _validLights.length = 0; diff --git a/cocos/core/renderer/core/memory-pools.ts b/cocos/core/renderer/core/memory-pools.ts index d30567a9b9..53c3211de9 100644 --- a/cocos/core/renderer/core/memory-pools.ts +++ b/cocos/core/renderer/core/memory-pools.ts @@ -1114,6 +1114,7 @@ export enum ShadowsView { DISTANCE, INSTANCE_PASS, PLANAR_PASS, + PLANAR_SHADER, NEAR, FAR, ASPECT, @@ -1122,11 +1123,11 @@ export enum ShadowsView { BIAS, ORTHO_SIZE, AUTO_ADAPT, // boolean - COLOR = 14, // Vec4 - SIZE = 18, // Vec2 - NORMAL = 20, // Vec3 - MAT_LIGHT = 23, // Mat4 - COUNT = 39 + COLOR = 15, // Vec4 + SIZE = 19, // Vec2 + NORMAL = 21, // Vec3 + MAT_LIGHT = 24, // Mat4 + COUNT = 40 } interface IShadowsViewType extends BufferTypeManifest { [ShadowsView.ENABLE]: number; @@ -1135,6 +1136,7 @@ interface IShadowsViewType extends BufferTypeManifest { [ShadowsView.DISTANCE]: number; [ShadowsView.INSTANCE_PASS]: PassHandle; [ShadowsView.PLANAR_PASS]: PassHandle; + [ShadowsView.PLANAR_SHADER]: ShaderHandle; [ShadowsView.NEAR]: number; [ShadowsView.FAR]: number; [ShadowsView.ASPECT]: number; @@ -1156,6 +1158,7 @@ const shadowsViewDataType: BufferDataTypeManifest = { [ShadowsView.DISTANCE]: BufferDataType.FLOAT32, [ShadowsView.INSTANCE_PASS]: BufferDataType.UINT32, [ShadowsView.PLANAR_PASS]: BufferDataType.UINT32, + [ShadowsView.PLANAR_SHADER]: BufferDataType.UINT32, [ShadowsView.NEAR]: BufferDataType.FLOAT32, [ShadowsView.FAR]: BufferDataType.FLOAT32, [ShadowsView.ASPECT]: BufferDataType.FLOAT32, diff --git a/cocos/core/renderer/scene/shadows.ts b/cocos/core/renderer/scene/shadows.ts index 2a3f2f460a..9612db75a7 100644 --- a/cocos/core/renderer/scene/shadows.ts +++ b/cocos/core/renderer/scene/shadows.ts @@ -29,6 +29,11 @@ import { Color, Mat4, Vec3, Vec2 } from '../../math'; import { legacyCC } from '../../global-exports'; import { Enum } from '../../value-types'; import { ShadowsPool, NULL_HANDLE, ShadowsView, ShadowsHandle } from '../core/memory-pools'; +import { IMacroPatch } from '../core/pass'; + +const shadowPatches: IMacroPatch[] = [ + { name: 'CC_RECEIVE_SHADOW', value: true }, +]; /** * @zh 阴影类型。 @@ -323,6 +328,7 @@ export class Shadows { this._material = new Material(); this._material.initialize({ effectName: 'planar-shadow' }); ShadowsPool.set(this._handle, ShadowsView.PLANAR_PASS, this._material.passes[0].handle); + ShadowsPool.set(this._handle, ShadowsView.PLANAR_SHADER,this._material.passes[0].getShaderVariant(shadowPatches)); } if (!this._instancingMaterial) { this._instancingMaterial = new Material();