mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 16:12:40 +08:00
add planar shadow for C++ (#7801)
This commit is contained in:
committed by
GitHub
parent
93fc4561b6
commit
acbefafe6a
@@ -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);
|
||||
|
||||
@@ -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<Model, Shader>();
|
||||
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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<typeof ShadowsView> {
|
||||
[ShadowsView.ENABLE]: number;
|
||||
@@ -1135,6 +1136,7 @@ interface IShadowsViewType extends BufferTypeManifest<typeof ShadowsView> {
|
||||
[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<typeof ShadowsView> = {
|
||||
[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,
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user