mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
fix shadow map generation (#7141)
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -20,7 +20,7 @@ export interface IBatchedItem {
|
||||
mergeCount: number;
|
||||
ia: GFXInputAssembler;
|
||||
ubo: GFXBuffer;
|
||||
uboData: UBOLocalBatched;
|
||||
uboData: Float32Array;
|
||||
descriptorSet: GFXDescriptorSet;
|
||||
hPass: PassHandle;
|
||||
hShader: ShaderHandle;
|
||||
@@ -119,7 +119,7 @@ export class BatchedBuffer {
|
||||
}
|
||||
|
||||
// update world matrix
|
||||
Mat4.toArray(batch.uboData.view, ro.model.transform.worldMatrix, UBOLocalBatched.MAT_WORLDS_OFFSET + batch.mergeCount * 16);
|
||||
Mat4.toArray(batch.uboData, ro.model.transform.worldMatrix, UBOLocalBatched.MAT_WORLDS_OFFSET + batch.mergeCount * 16);
|
||||
if (!batch.mergeCount) {
|
||||
descriptorSet.bindBuffer(UBOLocalBatched.BLOCK.binding, batch.ubo);
|
||||
descriptorSet.update();
|
||||
@@ -191,8 +191,8 @@ export class BatchedBuffer {
|
||||
descriptorSet.bindBuffer(UBOLocalBatched.BLOCK.binding, ubo);
|
||||
descriptorSet.update();
|
||||
|
||||
const uboData = new UBOLocalBatched();
|
||||
Mat4.toArray(uboData.view, ro.model.transform.worldMatrix, UBOLocalBatched.MAT_WORLDS_OFFSET);
|
||||
const uboData = new Float32Array(UBOLocalBatched.COUNT);
|
||||
Mat4.toArray(uboData, ro.model.transform.worldMatrix, UBOLocalBatched.MAT_WORLDS_OFFSET);
|
||||
|
||||
this.batches.push({
|
||||
mergeCount: 1,
|
||||
@@ -208,11 +208,4 @@ export class BatchedBuffer {
|
||||
batch.ia.vertexCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public clearUBO () {
|
||||
for (let i = 0; i < this.batches.length; ++i) {
|
||||
const batch = this.batches[i];
|
||||
batch.ubo.update(_localBatched.view.buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +89,6 @@ export const localDescriptorSetLayout: IDescriptorSetLayoutInfo = { bindings: []
|
||||
export enum PipelineGlobalBindings {
|
||||
UBO_GLOBAL,
|
||||
UBO_SHADOW,
|
||||
UBO_PCF_SHADOW,
|
||||
|
||||
SAMPLER_ENVIRONMENT,
|
||||
SAMPLER_SHADOWMAP,
|
||||
@@ -154,8 +153,7 @@ export class UBOGlobal {
|
||||
public static EXPOSURE_OFFSET: number = UBOGlobal.CAMERA_POS_OFFSET + 4;
|
||||
public static MAIN_LIT_DIR_OFFSET: number = UBOGlobal.EXPOSURE_OFFSET + 4;
|
||||
public static MAIN_LIT_COLOR_OFFSET: number = UBOGlobal.MAIN_LIT_DIR_OFFSET + 4;
|
||||
public static MAIN_SHADOW_MATRIX_OFFSET: number = UBOGlobal.MAIN_LIT_COLOR_OFFSET + 4;
|
||||
public static AMBIENT_SKY_OFFSET: number = UBOGlobal.MAIN_SHADOW_MATRIX_OFFSET + 16;
|
||||
public static AMBIENT_SKY_OFFSET: number = UBOGlobal.MAIN_LIT_COLOR_OFFSET + 4;
|
||||
public static AMBIENT_GROUND_OFFSET: number = UBOGlobal.AMBIENT_SKY_OFFSET + 4;
|
||||
public static GLOBAL_FOG_COLOR_OFFSET: number = UBOGlobal.AMBIENT_GROUND_OFFSET + 4;
|
||||
public static GLOBAL_FOG_BASE_OFFSET: number = UBOGlobal.GLOBAL_FOG_COLOR_OFFSET + 4;
|
||||
@@ -180,7 +178,6 @@ export class UBOGlobal {
|
||||
{ name: 'cc_exposure', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_mainLitDir', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_mainLitColor', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_shadowLightMatrix', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_ambientSky', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_ambientGround', type: GFXType.FLOAT4, count: 1 },
|
||||
{ name: 'cc_fogColor', type: GFXType.FLOAT4, count: 1 },
|
||||
@@ -188,8 +185,6 @@ export class UBOGlobal {
|
||||
{ name: 'cc_fogAdd', type: GFXType.FLOAT4, count: 1 },
|
||||
],
|
||||
};
|
||||
|
||||
public view: Float32Array = new Float32Array(UBOGlobal.COUNT);
|
||||
}
|
||||
globalDescriptorSetLayout.record[UBOGlobal.BLOCK.name] = UBOGlobal.BLOCK;
|
||||
globalDescriptorSetLayout.bindings[UBOGlobal.BLOCK.binding] = UBOGlobal.BLOCK;
|
||||
@@ -200,7 +195,8 @@ globalDescriptorSetLayout.bindings[UBOGlobal.BLOCK.binding] = UBOGlobal.BLOCK;
|
||||
*/
|
||||
export class UBOShadow {
|
||||
public static MAT_LIGHT_PLANE_PROJ_OFFSET: number = 0;
|
||||
public static SHADOW_COLOR_OFFSET: number = UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET + 16;
|
||||
public static MAT_LIGHT_VIEW_PROJ_OFFSET: number = UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET + 16;
|
||||
public static SHADOW_COLOR_OFFSET: number = UBOShadow.MAT_LIGHT_VIEW_PROJ_OFFSET + 16;
|
||||
public static COUNT: number = UBOShadow.SHADOW_COLOR_OFFSET + 4;
|
||||
public static SIZE: number = UBOShadow.COUNT * 4;
|
||||
|
||||
@@ -208,35 +204,14 @@ export class UBOShadow {
|
||||
stageFlags: GFXShaderStageFlagBit.ALL, descriptorType: GFXDescriptorType.UNIFORM_BUFFER, count: 1,
|
||||
set: SetIndex.GLOBAL, binding: PipelineGlobalBindings.UBO_SHADOW, name: 'CCShadow', members: [
|
||||
{ name: 'cc_matLightPlaneProj', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_matLightViewProj', type: GFXType.MAT4, count: 1 },
|
||||
{ name: 'cc_shadowColor', type: GFXType.FLOAT4, count: 1 },
|
||||
],
|
||||
};
|
||||
|
||||
public view: Float32Array = new Float32Array(UBOShadow.COUNT);
|
||||
}
|
||||
globalDescriptorSetLayout.record[UBOShadow.BLOCK.name] = UBOShadow.BLOCK;
|
||||
globalDescriptorSetLayout.bindings[UBOShadow.BLOCK.binding] = UBOShadow.BLOCK;
|
||||
|
||||
/**
|
||||
* The uniform buffer object for PCFshadow
|
||||
*/
|
||||
export class UBOPCFShadow {
|
||||
public static MAT_SHADOW_VIEW_PROJ_OFFSET: number = 0;
|
||||
public static COUNT: number = UBOPCFShadow.MAT_SHADOW_VIEW_PROJ_OFFSET + 16;
|
||||
public static SIZE: number = UBOPCFShadow.COUNT * 4;
|
||||
|
||||
public static BLOCK: IBlockInfo = {
|
||||
stageFlags: GFXShaderStageFlagBit.ALL, descriptorType: GFXDescriptorType.UNIFORM_BUFFER, count: 1,
|
||||
set: SetIndex.GLOBAL, binding: PipelineGlobalBindings.UBO_PCF_SHADOW, name: 'CCPCFShadow', members: [
|
||||
{ name: 'cc_shadowMatViewProj', type: GFXType.MAT4, count: 1 },
|
||||
],
|
||||
};
|
||||
|
||||
public view: Float32Array = new Float32Array(UBOPCFShadow.COUNT);
|
||||
}
|
||||
globalDescriptorSetLayout.record[UBOPCFShadow.BLOCK.name] = UBOPCFShadow.BLOCK;
|
||||
globalDescriptorSetLayout.bindings[UBOPCFShadow.BLOCK.binding] = UBOPCFShadow.BLOCK;
|
||||
|
||||
export const UNIFORM_SHADOWMAP: ISamplerInfo = {
|
||||
stageFlags: GFXShaderStageFlagBit.FRAGMENT, descriptorType: GFXDescriptorType.SAMPLER, count: 1,
|
||||
set: SetIndex.GLOBAL, binding: PipelineGlobalBindings.SAMPLER_SHADOWMAP, name: 'cc_shadowMap', type: GFXType.SAMPLER2D,
|
||||
@@ -270,8 +245,6 @@ export class UBOLocal {
|
||||
{ name: 'cc_lightingMapUVParam', type: GFXType.FLOAT4, count: 1 },
|
||||
],
|
||||
};
|
||||
|
||||
public view: Float32Array = new Float32Array(UBOLocal.COUNT);
|
||||
}
|
||||
localDescriptorSetLayout.record[UBOLocal.BLOCK.name] = UBOLocal.BLOCK;
|
||||
localDescriptorSetLayout.bindings[UBOLocal.BLOCK.binding] = UBOLocal.BLOCK;
|
||||
@@ -290,8 +263,6 @@ export class UBOLocalBatched {
|
||||
{ name: 'cc_matWorlds', type: GFXType.MAT4, count: UBOLocalBatched.BATCHING_COUNT },
|
||||
],
|
||||
};
|
||||
|
||||
public view: Float32Array = new Float32Array(UBOLocalBatched.COUNT);
|
||||
}
|
||||
localDescriptorSetLayout.record[UBOLocalBatched.BLOCK.name] = UBOLocalBatched.BLOCK;
|
||||
localDescriptorSetLayout.bindings[UBOLocalBatched.BLOCK.binding] = UBOLocalBatched.BLOCK;
|
||||
@@ -319,8 +290,6 @@ export class UBOForwardLight {
|
||||
{ name: 'cc_lightDir', type: GFXType.FLOAT4, count: UBOForwardLight.LIGHTS_PER_PASS },
|
||||
],
|
||||
};
|
||||
|
||||
public view: Float32Array = new Float32Array(UBOForwardLight.COUNT);
|
||||
}
|
||||
localDescriptorSetLayout.record[UBOForwardLight.BLOCK.name] = UBOForwardLight.BLOCK;
|
||||
localDescriptorSetLayout.bindings[UBOForwardLight.BLOCK.binding] = UBOForwardLight.BLOCK;
|
||||
|
||||
@@ -9,7 +9,7 @@ import { ForwardFlow } from './forward-flow';
|
||||
import { RenderTextureConfig, MaterialConfig } from '../pipeline-serialization';
|
||||
import { ShadowFlow } from '../shadow/shadow-flow';
|
||||
import { genSamplerHash, samplerLib } from '../../renderer';
|
||||
import { IRenderObject, UBOGlobal, UBOShadow, UBOPCFShadow,
|
||||
import { IRenderObject, UBOGlobal, UBOShadow,
|
||||
UNIFORM_SHADOWMAP, globalDescriptorSetLayout, localDescriptorSetLayout} from '../define';
|
||||
import { GFXBufferUsageBit, GFXMemoryUsageBit,
|
||||
GFXClearFlag, GFXFilter, GFXAddress, GFXCommandBufferType } from '../../gfx/define';
|
||||
@@ -17,24 +17,17 @@ import { GFXColorAttachment, GFXDepthStencilAttachment, GFXRenderPass, GFXLoadOp
|
||||
import { SKYBOX_FLAG } from '../../renderer';
|
||||
import { legacyCC } from '../../global-exports';
|
||||
import { RenderView } from '../render-view';
|
||||
import { Mat4, Vec3, Vec2, Quat, Vec4 } from '../../math';
|
||||
import { Mat4, Vec3, Vec2, Vec4 } from '../../math';
|
||||
import { GFXFeature } from '../../gfx/device';
|
||||
|
||||
const shadowCamera_W_P = new Vec3();
|
||||
const shadowCamera_W_R = new Quat();
|
||||
const shadowCamera_W_S = new Vec3();
|
||||
const shadowCamera_W_T = new Mat4();
|
||||
|
||||
const shadowCamera_M_V = new Mat4();
|
||||
const shadowCamera_M_P = new Mat4();
|
||||
const shadowCamera_M_V_P = new Mat4();
|
||||
const matShadowView = new Mat4();
|
||||
const matShadowViewProj = new Mat4();
|
||||
|
||||
// Define shadwoMapCamera
|
||||
const shadowCamera_Near = 0.1;
|
||||
const shadowCamera_Far = 1000.0;
|
||||
const shadowCamera_Fov = 45.0;
|
||||
const shadowCamera_Aspect = 1.0;
|
||||
const shadowCamera_OrthoSize = 20.0;
|
||||
const shadowCamera_Near = 1;
|
||||
const shadowCamera_Far = 30;
|
||||
const shadowCamera_Aspect = 1;
|
||||
const shadowCamera_OrthoSize = 5;
|
||||
|
||||
/**
|
||||
* @en The forward render pipeline
|
||||
@@ -43,25 +36,25 @@ const shadowCamera_OrthoSize = 20.0;
|
||||
@ccclass('ForwardPipeline')
|
||||
export class ForwardPipeline extends RenderPipeline {
|
||||
|
||||
public get isHDR () {
|
||||
get isHDR () {
|
||||
return this._isHDR;
|
||||
}
|
||||
|
||||
public set isHDR (val) {
|
||||
set isHDR (val) {
|
||||
if (this._isHDR === val) {
|
||||
return
|
||||
}
|
||||
|
||||
this._isHDR = val;
|
||||
const defaultGlobalUBOData = this._uboGlobal.view;
|
||||
const defaultGlobalUBOData = this._globalUBO;
|
||||
defaultGlobalUBOData[UBOGlobal.EXPOSURE_OFFSET + 2] = this._isHDR ? 1.0 : 0.0;
|
||||
}
|
||||
|
||||
public get shadingScale (): number {
|
||||
get shadingScale (): number {
|
||||
return this._shadingScale;
|
||||
}
|
||||
|
||||
public get fpScale (): number {
|
||||
get fpScale (): number {
|
||||
return this._fpScale;
|
||||
}
|
||||
|
||||
@@ -69,10 +62,14 @@ export class ForwardPipeline extends RenderPipeline {
|
||||
* @zh
|
||||
* 获取阴影贴图分辨率
|
||||
*/
|
||||
public get shadowMapSize () {
|
||||
get shadowMapSize () {
|
||||
return this._shadowMapSize;
|
||||
}
|
||||
|
||||
get shadowUBO () {
|
||||
return this._shadowUBO;
|
||||
}
|
||||
|
||||
@property({
|
||||
type: [RenderTextureConfig],
|
||||
})
|
||||
@@ -89,12 +86,12 @@ export class ForwardPipeline extends RenderPipeline {
|
||||
*/
|
||||
public renderObjects: IRenderObject[] = [];
|
||||
public shadowObjects: IRenderObject[] = [];
|
||||
protected _uboGlobal: UBOGlobal = new UBOGlobal();
|
||||
protected _isHDR: boolean = false;
|
||||
protected _shadingScale: number = 1.0;
|
||||
protected _fpScale: number = 1.0 / 1024.0;
|
||||
protected _renderPasses = new Map<GFXClearFlag, GFXRenderPass>();
|
||||
protected _uboPCFShadow: UBOPCFShadow = new UBOPCFShadow();
|
||||
protected _globalUBO = new Float32Array(UBOGlobal.COUNT);
|
||||
protected _shadowUBO = new Float32Array(UBOShadow.COUNT);
|
||||
protected _shadowMapSize: Vec2 = new Vec2(512, 512);
|
||||
|
||||
public initialize (info: IRenderPipelineInfo): boolean {
|
||||
@@ -175,36 +172,25 @@ export class ForwardPipeline extends RenderPipeline {
|
||||
const device = this.device;
|
||||
|
||||
if (mainLight) {
|
||||
shadowCamera_W_P.set(mainLight!.node!.getWorldPosition());
|
||||
shadowCamera_W_R.set(mainLight!.node!.getWorldRotation());
|
||||
shadowCamera_W_S.set(mainLight!.node!.getWorldScale());
|
||||
// light view
|
||||
Mat4.invert(matShadowView, mainLight!.node!.worldMatrix);
|
||||
|
||||
// world Transfrom
|
||||
Mat4.fromRTS(shadowCamera_W_T, shadowCamera_W_R, shadowCamera_W_P, shadowCamera_W_S);
|
||||
|
||||
// camera view
|
||||
Mat4.invert(shadowCamera_M_V, shadowCamera_W_T);
|
||||
|
||||
// camera proj
|
||||
// Mat4.perspective(shadowCamera_M_P, shadowCamera_Fov, shadowCamera_Aspect, shadowCamera_Near, shadowCamera_Far);
|
||||
// light proj
|
||||
const x = shadowCamera_OrthoSize * shadowCamera_Aspect;
|
||||
const y = shadowCamera_OrthoSize;
|
||||
Mat4.ortho(shadowCamera_M_P, -x, x, -y, y, shadowCamera_Near, shadowCamera_Far,
|
||||
device.clipSpaceMinZ, device.screenSpaceSignY);
|
||||
const projectionSignY = device.screenSpaceSignY * device.UVSpaceSignY;
|
||||
Mat4.ortho(matShadowViewProj, -x, x, -y, y, shadowCamera_Near, shadowCamera_Far,
|
||||
device.clipSpaceMinZ, projectionSignY);
|
||||
|
||||
// camera viewProj
|
||||
Mat4.multiply(shadowCamera_M_V_P, shadowCamera_M_P, shadowCamera_M_V);
|
||||
// light viewProj
|
||||
Mat4.multiply(matShadowViewProj, matShadowViewProj, matShadowView);
|
||||
|
||||
Mat4.toArray(this._uboGlobal.view, shadowCamera_M_V_P, UBOGlobal.MAIN_SHADOW_MATRIX_OFFSET);
|
||||
Mat4.toArray(this._shadowUBO, matShadowViewProj, UBOShadow.MAT_LIGHT_VIEW_PROJ_OFFSET);
|
||||
}
|
||||
|
||||
// update ubos
|
||||
this._descriptorSet.getBuffer(UBOGlobal.BLOCK.binding).update(this._uboGlobal.view);
|
||||
|
||||
// Fill Shadow UBO
|
||||
// Fill cc_shadowMatViewProj
|
||||
Mat4.toArray(this._uboPCFShadow.view, shadowCamera_M_V_P, UBOPCFShadow.MAT_SHADOW_VIEW_PROJ_OFFSET);
|
||||
this._descriptorSet.getBuffer(UBOPCFShadow.BLOCK.binding).update(this._uboPCFShadow.view);
|
||||
this._descriptorSet.getBuffer(UBOGlobal.BLOCK.binding).update(this._globalUBO);
|
||||
this._descriptorSet.getBuffer(UBOShadow.BLOCK.binding).update(this._shadowUBO);
|
||||
}
|
||||
|
||||
private _activeRenderer () {
|
||||
@@ -229,13 +215,6 @@ export class ForwardPipeline extends RenderPipeline {
|
||||
});
|
||||
this._descriptorSet.bindBuffer(UBOShadow.BLOCK.binding, shadowUBO);
|
||||
|
||||
const shadowPCFUBO = device.createBuffer({
|
||||
usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST,
|
||||
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
|
||||
size: UBOPCFShadow.SIZE,
|
||||
});
|
||||
this._descriptorSet.bindBuffer(UBOPCFShadow.BLOCK.binding, shadowPCFUBO);
|
||||
|
||||
const shadowMapSamplerHash = genSamplerHash([
|
||||
GFXFilter.LINEAR,
|
||||
GFXFilter.LINEAR,
|
||||
@@ -265,8 +244,7 @@ export class ForwardPipeline extends RenderPipeline {
|
||||
const mainLight = scene.mainLight;
|
||||
const ambient = scene.ambient;
|
||||
const fog = scene.fog;
|
||||
const uboGlobal = this._uboGlobal;
|
||||
const fv = uboGlobal.view;
|
||||
const fv = this._globalUBO;
|
||||
const device = this.device;
|
||||
|
||||
const shadingWidth = Math.floor(device.width);
|
||||
@@ -356,7 +334,6 @@ export class ForwardPipeline extends RenderPipeline {
|
||||
private destroyUBOs () {
|
||||
this._descriptorSet.getBuffer(UBOGlobal.BLOCK.binding).destroy();
|
||||
this._descriptorSet.getBuffer(UBOShadow.BLOCK.binding).destroy();
|
||||
this._descriptorSet.getBuffer(UBOPCFShadow.BLOCK.binding).destroy();
|
||||
}
|
||||
|
||||
public destroy () {
|
||||
|
||||
@@ -51,7 +51,7 @@ export class RenderBatchedQueue {
|
||||
batch.vbs[v].update(batch.vbDatas[v]);
|
||||
}
|
||||
batch.vbIdx.update(batch.vbIdxData.buffer);
|
||||
batch.ubo.update(batch.uboData.view);
|
||||
batch.ubo.update(batch.uboData);
|
||||
const shader = ShaderPool.get(batch.hShader);
|
||||
const pso = PipelineStateManager.getOrCreatePipelineState(device, batch.hPass, shader, renderPass, batch.ia);
|
||||
if (!boundPSO) { cmdBuff.bindPipelineState(pso); boundPSO = true; }
|
||||
|
||||
@@ -35,11 +35,11 @@ export class RenderShadowMapBatchedQueue {
|
||||
|
||||
public add (renderObj: IRenderObject, subModelIdx: number, passIdx: number) {
|
||||
const subModel = renderObj.model.subModels[subModelIdx];
|
||||
const shader = ShaderPool.get(SubModelPool.get(subModel.handle, SubModelView.SHADER_0 + passIdx));
|
||||
const pass = subModel.passes[passIdx];
|
||||
|
||||
if (pass.phase === this._phaseID) {
|
||||
if (this._shadowMapBuffer) {
|
||||
const shader = ShaderPool.get(SubModelPool.get(subModel.handle, SubModelView.SHADER_0 + passIdx));
|
||||
this._subModelsArray.push(subModel);
|
||||
this._shaderArray.push(shader);
|
||||
this._passArray.push(pass.handle);
|
||||
|
||||
@@ -178,7 +178,7 @@ export class RenderView {
|
||||
for (let i = 0; i < pipelineFlows.length; i++) {
|
||||
if (pipelineFlows[i].name === 'UIFlow') {
|
||||
this.flows.push(pipelineFlows[i]);
|
||||
break
|
||||
break;
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
@@ -55,7 +55,7 @@ export class ShadowFlow extends RenderFlow {
|
||||
if(!this._shadowRenderPass) {
|
||||
this._shadowRenderPass = device.createRenderPass({
|
||||
colorAttachments: [{
|
||||
format: GFXFormat.RGBA16F,
|
||||
format: GFXFormat.RGBA8,
|
||||
loadOp: GFXLoadOp.CLEAR, // should clear color attachment
|
||||
storeOp: GFXStoreOp.STORE,
|
||||
sampleCount: 1,
|
||||
@@ -79,7 +79,7 @@ export class ShadowFlow extends RenderFlow {
|
||||
this._shadowRenderTargets.push(device.createTexture({
|
||||
type: GFXTextureType.TEX2D,
|
||||
usage: GFXTextureUsageBit.COLOR_ATTACHMENT | GFXTextureUsageBit.SAMPLED,
|
||||
format: GFXFormat.RGBA16F,
|
||||
format: GFXFormat.RGBA8,
|
||||
width: shadowMapSize.x,
|
||||
height: shadowMapSize.y,
|
||||
}));
|
||||
|
||||
@@ -11,7 +11,7 @@ import { ForwardStagePriority } from '../forward/enum';
|
||||
import { RenderShadowMapBatchedQueue } from '../render-shadowMap-batched-queue';
|
||||
import { GFXFramebuffer } from '../../gfx/framebuffer';
|
||||
import { ForwardPipeline } from '../forward/forward-pipeline';
|
||||
import { UBOPCFShadow, SetIndex } from '../define';
|
||||
import { SetIndex, UBOShadow } from '../define';
|
||||
|
||||
const colors: GFXColor[] = [ { r: 1, g: 1, b: 1, a: 1 } ];
|
||||
const bufs: GFXCommandBuffer[] = [];
|
||||
@@ -58,7 +58,7 @@ export class ShadowStage extends RenderStage {
|
||||
*/
|
||||
public render (view: RenderView) {
|
||||
const pipeline = this._pipeline as ForwardPipeline;
|
||||
this._additiveShadowQueue.clear(pipeline.descriptorSet.getBuffer(UBOPCFShadow.BLOCK.binding));
|
||||
this._additiveShadowQueue.clear(pipeline.descriptorSet.getBuffer(UBOShadow.BLOCK.binding));
|
||||
|
||||
const shadowObjects = pipeline.shadowObjects;
|
||||
let m = 0; let p = 0;
|
||||
|
||||
@@ -12,6 +12,7 @@ import { GFXCommandBuffer, GFXDevice, GFXRenderPass, GFXDescriptorSet, GFXShader
|
||||
import { InstancedBuffer } from '../../pipeline/instanced-buffer';
|
||||
import { PipelineStateManager } from '../../pipeline/pipeline-state-manager';
|
||||
import { DSPool, ShaderPool, PassPool, PassView } from '../core/memory-pools';
|
||||
import { ForwardPipeline } from '../../pipeline';
|
||||
|
||||
const _forward = new Vec3(0, 0, -1);
|
||||
const _v3 = new Vec3();
|
||||
@@ -53,27 +54,18 @@ export class PlanarShadows {
|
||||
|
||||
set shadowColor (color: Color) {
|
||||
Color.toArray(this._data, color, UBOShadow.SHADOW_COLOR_OFFSET);
|
||||
this._globalDescriptorSet.getBuffer(UBOShadow.BLOCK.binding).update(this.data);
|
||||
}
|
||||
|
||||
get matLight () {
|
||||
return this._matLight;
|
||||
}
|
||||
|
||||
get data () {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
protected _scene: RenderScene;
|
||||
protected _enabled: boolean = false;
|
||||
protected _normal = new Vec3(0, 1, 0);
|
||||
protected _distance = 0;
|
||||
protected _matLight = new Mat4();
|
||||
protected _data = Float32Array.from([
|
||||
1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, // matLightPlaneProj
|
||||
0.0, 0.0, 0.0, 0.3, // shadowColor
|
||||
]);
|
||||
protected _globalDescriptorSet: GFXDescriptorSet;
|
||||
protected _data: Float32Array;
|
||||
protected _record = new Map<Model, IShadowRenderData>();
|
||||
protected _pendingModels: IShadowRenderData[] = [];
|
||||
protected _material: Material;
|
||||
@@ -82,7 +74,7 @@ export class PlanarShadows {
|
||||
|
||||
constructor (scene: RenderScene) {
|
||||
this._scene = scene;
|
||||
this._globalDescriptorSet = scene.root.pipeline.descriptorSet;
|
||||
this._data = (scene.root.pipeline as ForwardPipeline).shadowUBO;
|
||||
this._material = new Material();
|
||||
this._material.initialize({ effectName: 'pipeline/planar-shadow' });
|
||||
this._instancingMaterial = new Material();
|
||||
@@ -112,8 +104,7 @@ export class PlanarShadows {
|
||||
m.m13 = ly * d;
|
||||
m.m14 = lz * d;
|
||||
m.m15 = NdL;
|
||||
Mat4.toArray(this.data, this._matLight);
|
||||
this._globalDescriptorSet.getBuffer(UBOShadow.BLOCK.binding).update(this.data);
|
||||
Mat4.toArray(this._data, this._matLight, UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET);
|
||||
}
|
||||
|
||||
public updateDirLight (light: DirectionalLight) {
|
||||
@@ -140,8 +131,7 @@ export class PlanarShadows {
|
||||
m.m13 = ly * d;
|
||||
m.m14 = lz * d;
|
||||
m.m15 = 1;
|
||||
Mat4.toArray(this.data, this._matLight, UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET);
|
||||
this._globalDescriptorSet.getBuffer(UBOShadow.BLOCK.binding).update(this.data);
|
||||
Mat4.toArray(this._data, this._matLight, UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET);
|
||||
}
|
||||
|
||||
public updateShadowList (frstm: frustum, stamp: number, shadowVisible = false) {
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
in vec4 v_shadowPos;
|
||||
|
||||
#if CC_RECEIVE_SHADOW
|
||||
#pragma builtin(global)
|
||||
layout(set = 0, binding = 4) uniform sampler2D cc_shadowMap;
|
||||
#endif
|
||||
|
||||
#if CC_RECEIVE_SHADOW
|
||||
float CCCalcShadowFactor () {
|
||||
vec3 ProjCoords = v_shadowPos.xyz / v_shadowPos.w;
|
||||
vec2 UVCoords;
|
||||
UVCoords.x = ProjCoords.x * 0.5 + 0.5;
|
||||
UVCoords.y = ProjCoords.y * 0.5 + 0.5;
|
||||
float z = ProjCoords.z * 0.5 + 0.5;
|
||||
float Depth = texture(cc_shadowMap, UVCoords).x;
|
||||
if (Depth < (z - 0.0001))
|
||||
return 0.5;
|
||||
else
|
||||
return 1.0;
|
||||
}
|
||||
#endif
|
||||
@@ -1,7 +0,0 @@
|
||||
#include <cc-global>
|
||||
|
||||
out vec4 v_shadowPos;
|
||||
|
||||
void CCGetDirLightShadowPos (vec4 worldPos) {
|
||||
v_shadowPos = cc_shadowLightMatrix * worldPos;
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"ver": "1.0.0",
|
||||
"importer": "effect-header",
|
||||
"imported": true,
|
||||
"uuid": "879b227d-ad83-4bc4-8e91-4c0215cbecbe",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ layout(set = 0, binding = 0) uniform CCGlobal {
|
||||
mediump vec4 cc_exposure; // x: exposure, y: inverse exposure, z: HDR flag, w: exposure scale
|
||||
mediump vec4 cc_mainLitDir; // xyz: main direcitonal light direction
|
||||
mediump vec4 cc_mainLitColor; // xyz: main direcitonal light color, w: intensity
|
||||
highp mat4 cc_shadowLightMatrix; // matrix:
|
||||
mediump vec4 cc_ambientSky; //xyz: sky illumination color, w: intensity
|
||||
mediump vec4 cc_ambientGround; // xyz: ground albedo color, w: envmap LOD
|
||||
mediump vec4 cc_fogColor; // xyzw: global fog color
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
#pragma builtin(global)
|
||||
layout(set = 0, binding = 2) uniform CCPCFShadow {
|
||||
highp mat4 cc_shadowMatViewProj; // shadowCamera: view-projection matrix
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
|
||||
in vec4 v_shadowPos;
|
||||
|
||||
#include <cc-shadow>
|
||||
#include <packing>
|
||||
|
||||
#pragma builtin(global)
|
||||
layout(set = 0, binding = 4) uniform sampler2D cc_shadowMap;
|
||||
|
||||
vec4 CCGetShadowFactor () {
|
||||
vec3 clipPos = v_shadowPos.xyz / v_shadowPos.w * 0.5 + 0.5;
|
||||
float depth = unpackRGBAToDepth(texture(cc_shadowMap, clipPos.xy));
|
||||
if (depth < (clipPos.z - 0.001)) return cc_shadowColor;
|
||||
else return vec4(0);
|
||||
}
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
"ver": "1.0.0",
|
||||
"importer": "effect-header",
|
||||
"imported": true,
|
||||
"uuid": "b4e2020e-9f28-4fbc-ad20-4ac64bcb4380",
|
||||
"uuid": "5716d893-2ecc-4110-9291-88b7e939d37d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
@@ -0,0 +1,7 @@
|
||||
out vec4 v_shadowPos;
|
||||
|
||||
#include <cc-shadow>
|
||||
|
||||
#define CCPassShadowParams(worldPos) \
|
||||
v_shadowPos = cc_matLightViewProj * worldPos; \
|
||||
#pragma // empty pragma trick to get rid of trailing semicolons at effect compile time
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
"ver": "1.0.0",
|
||||
"importer": "effect-header",
|
||||
"imported": true,
|
||||
"uuid": "6836bb7c-0edc-4567-82a3-2d8548467e45",
|
||||
"uuid": "b2e7c003-9c9f-40ee-82c6-32d920a759e8",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
@@ -3,5 +3,6 @@
|
||||
#pragma builtin(global)
|
||||
layout(set = 0, binding = 1) uniform CCShadow {
|
||||
highp mat4 cc_matLightPlaneProj;
|
||||
highp mat4 cc_matLightViewProj;
|
||||
lowp vec4 cc_shadowColor;
|
||||
};
|
||||
|
||||
@@ -7,6 +7,5 @@ vec4 packDepthToRGBA (float depth) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
float unpackRGBAToDepth (vec4 color) {
|
||||
return dot(color, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0));
|
||||
}
|
||||
#define unpackRGBAToDepth(color)
|
||||
dot(color, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0))
|
||||
|
||||
@@ -99,7 +99,9 @@ struct StandardSurface {
|
||||
return vec4(finalColor, 0.0);
|
||||
}
|
||||
#else
|
||||
#include <cc-calcShadow-fs>
|
||||
#if CC_RECEIVE_SHADOW
|
||||
#include <cc-shadow-map-fs>
|
||||
#endif
|
||||
|
||||
vec4 CCStandardShading (StandardSurface s) {
|
||||
// Calculate diffuse & specular
|
||||
@@ -151,11 +153,11 @@ struct StandardSurface {
|
||||
#endif
|
||||
finalColor += s.emissive;
|
||||
|
||||
float shadow = 1.0;
|
||||
#if CC_RECEIVE_SHADOW
|
||||
shadow = CCCalcShadowFactor();
|
||||
vec4 shadow = CCGetShadowFactor();
|
||||
finalColor = shadow.rgb * shadow.a + finalColor * (1.0 - shadow.a);
|
||||
#endif
|
||||
|
||||
return vec4(finalColor, s.albedo.a) * shadow;
|
||||
return vec4(finalColor, s.albedo.a);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
"_tag": 0,
|
||||
"_flows": [
|
||||
{
|
||||
"__id__": 1
|
||||
"__id__": 5
|
||||
},
|
||||
{
|
||||
"__id__": 5
|
||||
"__id__": 1
|
||||
}
|
||||
],
|
||||
"renderTextures": [],
|
||||
@@ -71,4 +71,4 @@
|
||||
"_priority": 0,
|
||||
"_tag": 0
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
@@ -49,10 +49,8 @@ CCEffect %{
|
||||
- vert: shadowMap-vs:vert
|
||||
frag: shadowMap-fs:frag
|
||||
phase: shadow-add
|
||||
depthStencilState:
|
||||
depthFunc: less_equal
|
||||
depthTest: true
|
||||
depthWrite: false
|
||||
rasterizerState:
|
||||
cullMode: front
|
||||
- name: transparent
|
||||
passes:
|
||||
- vert: standard-vs:vert
|
||||
@@ -84,10 +82,8 @@ CCEffect %{
|
||||
- vert: shadowMap-vs:vert
|
||||
frag: shadowMap-fs:frag
|
||||
phase: shadow-add
|
||||
depthStencilState:
|
||||
depthFunc: less_equal
|
||||
depthTest: true
|
||||
depthWrite: false
|
||||
rasterizerState:
|
||||
cullMode: front
|
||||
}%
|
||||
|
||||
CCProgram shared-ubos %{
|
||||
@@ -108,7 +104,7 @@ CCProgram standard-vs %{
|
||||
#include <cc-local-batch>
|
||||
#include <shared-ubos>
|
||||
#include <cc-fog>
|
||||
#include <cc-calcShadow-vs>
|
||||
#include <cc-shadow-map-vs>
|
||||
|
||||
#if USE_VERTEX_COLOR
|
||||
in vec3 a_color;
|
||||
@@ -164,7 +160,7 @@ CCProgram standard-vs %{
|
||||
CCLightingMapCaclUV();
|
||||
#endif
|
||||
|
||||
CCGetDirLightShadowPos(pos);
|
||||
CCPassShadowParams(pos);
|
||||
|
||||
return cc_matProj * (cc_matView * matWorld) * In.position;
|
||||
}
|
||||
@@ -198,26 +194,25 @@ CCProgram standard-fs %{
|
||||
uniform sampler2D normalMap;
|
||||
#pragma define NORMAL_UV options([v_uv, v_uv1])
|
||||
#endif
|
||||
#pragma define PBR_UV options([v_uv, v_uv1])
|
||||
#if USE_PBR_MAP
|
||||
uniform sampler2D pbrMap;
|
||||
#pragma define PBR_UV options([v_uv, v_uv1])
|
||||
#endif
|
||||
#if USE_METALLIC_ROUGHNESS_MAP
|
||||
uniform sampler2D metallicRoughnessMap;
|
||||
#pragma define METALLIC_ROUGHNESS_UV options([v_uv, v_uv1])
|
||||
#endif
|
||||
#if USE_OCCLUSION_MAP
|
||||
uniform sampler2D occlusionMap;
|
||||
#pragma define OCCLUSION_UV options([v_uv, v_uv1])
|
||||
#endif
|
||||
#if USE_EMISSIVE_MAP
|
||||
uniform sampler2D emissiveMap;
|
||||
#pragma define EMISSIVE_UV options([v_uv, v_uv1])
|
||||
#endif
|
||||
|
||||
#pragma define OCCLUSION_CHANNEL options([r, g, b])
|
||||
#pragma define ROUGHNESS_CHANNEL options([g, b, r])
|
||||
#pragma define METALLIC_CHANNEL options([b, r, g])
|
||||
#define OCCLUSION_CHANNEL r
|
||||
#define ROUGHNESS_CHANNEL g
|
||||
#define METALLIC_CHANNEL b
|
||||
|
||||
#if USE_ALPHA_TEST
|
||||
#pragma define ALPHA_TEST_CHANNEL options([a, r])
|
||||
#endif
|
||||
@@ -258,12 +253,12 @@ CCProgram standard-fs %{
|
||||
pbr.z *= res.METALLIC_CHANNEL;
|
||||
#endif
|
||||
#if USE_METALLIC_ROUGHNESS_MAP
|
||||
vec4 metallicRoughness = texture(metallicRoughnessMap, METALLIC_ROUGHNESS_UV);
|
||||
vec4 metallicRoughness = texture(metallicRoughnessMap, PBR_UV);
|
||||
pbr.z *= metallicRoughness.METALLIC_CHANNEL;
|
||||
pbr.y *= metallicRoughness.ROUGHNESS_CHANNEL;
|
||||
#endif
|
||||
#if USE_OCCLUSION_MAP
|
||||
pbr.x *= texture(occlusionMap, OCCLUSION_UV).OCCLUSION_CHANNEL;
|
||||
pbr.x *= texture(occlusionMap, PBR_UV).OCCLUSION_CHANNEL;
|
||||
#endif
|
||||
s.occlusion = clamp(pbr.x, 0.0, 0.96);
|
||||
s.roughness = clamp(pbr.y, 0.04, 1.0);
|
||||
@@ -286,8 +281,8 @@ CCProgram standard-fs %{
|
||||
CCProgram shadowMap-vs %{
|
||||
precision highp float;
|
||||
#include <input-standard>
|
||||
#include <cc-shadow>
|
||||
#include <cc-local-batch>
|
||||
#include <cc-pcf-shadow>
|
||||
|
||||
out vec4 vTexCoord;
|
||||
|
||||
@@ -299,7 +294,7 @@ CCProgram shadowMap-vs %{
|
||||
CCGetWorldMatrixFull(matWorld, matWorldIT);
|
||||
|
||||
vec4 worldPos = matWorld * In.position;
|
||||
vec4 clipPos = cc_shadowMatViewProj * worldPos;
|
||||
vec4 clipPos = cc_matLightViewProj * worldPos;
|
||||
|
||||
vTexCoord = vec4(a_texCoord, clipPos.z, clipPos.w);
|
||||
|
||||
@@ -309,7 +304,8 @@ CCProgram shadowMap-vs %{
|
||||
|
||||
CCProgram shadowMap-fs %{
|
||||
precision highp float;
|
||||
#include <output-standard>
|
||||
#include <packing>
|
||||
|
||||
in vec4 vTexCoord;
|
||||
|
||||
#if USE_ALBEDO_MAP
|
||||
@@ -328,8 +324,6 @@ CCProgram shadowMap-fs %{
|
||||
#endif
|
||||
#endif
|
||||
|
||||
float depth = vTexCoord.z / vTexCoord.w * 0.5 + 0.5;
|
||||
color = vec4(depth, depth, depth, 1.0);
|
||||
return CCFragOutput(color);
|
||||
return packDepthToRGBA(vTexCoord.z / vTexCoord.w * 0.5 + 0.5);
|
||||
}
|
||||
}%
|
||||
|
||||
Reference in New Issue
Block a user