Skip to content

Commit

Permalink
Move global data of scene to pipeline. (cocos#7109)
Browse files Browse the repository at this point in the history
* Move global data of scene to pipeline.

* fix some code judgments.

* fix pipeline save error

* refine skybox and fog info

* fix planer shadow

* fix fog error

* delete setter of skybox and fog
  • Loading branch information
caryliu1999 authored Aug 17, 2020
1 parent 173b1a5 commit a6c536e
Show file tree
Hide file tree
Showing 14 changed files with 649 additions and 240 deletions.
62 changes: 51 additions & 11 deletions cocos/core/pipeline/forward/forward-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ import { GFXColorAttachment, GFXDepthStencilAttachment, GFXRenderPass, GFXLoadOp
import { SKYBOX_FLAG } from '../../renderer';
import { legacyCC } from '../../global-exports';
import { RenderView } from '../render-view';
import { Mat4, Vec3, Vec2, Vec4 } from '../../math';
import { Mat4, Vec3, Vec4} from '../../math';
import { GFXFeature } from '../../gfx/device';
import { Fog } from '../../renderer/scene/fog';
import { Ambient } from '../../renderer/scene/ambient';
import { Skybox } from '../../renderer/scene/skybox';
import { PlanarShadows } from '../../renderer/scene/planar-shadows';
import { ShadowInfo } from '../../renderer/scene/shadowInfo';

const matShadowView = new Mat4();
Expand Down Expand Up @@ -53,19 +57,50 @@ export class ForwardPipeline extends RenderPipeline {
return this._fpScale;
}

/**
* @en Get shadow UBO.
* @zh 获取阴影UBO。
*/
get shadowUBO (): Float32Array {
return this._shadowUBO;
}

@property({
type: [RenderTextureConfig],
displayOrder: 2,
})
protected renderTextures: RenderTextureConfig[] = [];

@property({
type: [MaterialConfig],
displayOrder: 3,
})
protected materials: MaterialConfig[] = [];

@property({
type: Fog,
displayOrder: 7,
visible: true,
})
public fog: Fog = new Fog();
@property({
type: Ambient,
displayOrder: 4,
visible: true,
})
public ambient: Ambient = new Ambient();
@property({
type: Skybox,
displayOrder: 5,
visible: true,
})
public skybox: Skybox = new Skybox();
@property({
type: PlanarShadows,
displayOrder: 6,
visible: true,
})
public planarShadows: PlanarShadows = new PlanarShadows();
/**
* @en The list for render objects, only available after the scene culling of the current frame.
* @zh 渲染对象数组,仅在当前帧的场景剔除完成后有效。
Expand Down Expand Up @@ -97,7 +132,7 @@ export class ForwardPipeline extends RenderPipeline {
public activate (): boolean {
this._globalDescriptorSetLayout = globalDescriptorSetLayout;
this._localDescriptorSetLayout = localDescriptorSetLayout;

this._macros = {};
const uiFlow = new UIFlow();
uiFlow.initialize(UIFlow.initInfo);
this._flows.push(uiFlow);
Expand All @@ -111,6 +146,11 @@ export class ForwardPipeline extends RenderPipeline {
return false;
}

this.ambient.activate();
this.fog.activate();
this.skybox.activate();
this.planarShadows.activate();

return true;
}

Expand Down Expand Up @@ -229,8 +269,8 @@ export class ForwardPipeline extends RenderPipeline {
const scene = camera.scene!;

const mainLight = scene.mainLight;
const ambient = scene.ambient;
const fog = scene.fog;
const ambient = this.ambient;
const fog = this.fog;
const fv = this._globalUBO;
const device = this.device;

Expand Down Expand Up @@ -296,17 +336,17 @@ export class ForwardPipeline extends RenderPipeline {
Vec4.toArray(fv, Vec4.ZERO, UBOGlobal.MAIN_LIT_COLOR_OFFSET);
}

const skyColor = ambient.skyColor;
const skyColor = ambient.colorArray;
if (this._isHDR) {
skyColor[3] = ambient.skyIllum * this._fpScale;
} else {
skyColor[3] = ambient.skyIllum * exposure;
}
fv.set(skyColor, UBOGlobal.AMBIENT_SKY_OFFSET);
fv.set(ambient.groundAlbedo, UBOGlobal.AMBIENT_GROUND_OFFSET);
fv.set(ambient.albedoArray, UBOGlobal.AMBIENT_GROUND_OFFSET);

if (fog.enabled) {
fv.set(fog.fogColor, UBOGlobal.GLOBAL_FOG_COLOR_OFFSET);
fv.set(fog.colorArray, UBOGlobal.GLOBAL_FOG_COLOR_OFFSET);

fv[UBOGlobal.GLOBAL_FOG_BASE_OFFSET] = fog.fogStart;
fv[UBOGlobal.GLOBAL_FOG_BASE_OFFSET + 1] = fog.fogEnd;
Expand All @@ -319,15 +359,15 @@ export class ForwardPipeline extends RenderPipeline {
}

private destroyUBOs () {
this._descriptorSet.getBuffer(UBOGlobal.BLOCK.binding).destroy();
this._descriptorSet.getBuffer(UBOShadow.BLOCK.binding).destroy();
if (this._descriptorSet) {
this._descriptorSet.getBuffer(UBOGlobal.BLOCK.binding).destroy();
this._descriptorSet.getBuffer(UBOShadow.BLOCK.binding).destroy();
}
}

public destroy () {
this.destroyUBOs();

this._descriptorSet.destroy();

const rpIter = this._renderPasses.values();
let rpRes = rpIter.next();
while (!rpRes.done) {
Expand Down
2 changes: 1 addition & 1 deletion cocos/core/pipeline/forward/forward-stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class ForwardStage extends RenderStage {
this._instancedQueue.recordCommandBuffer(device, renderPass, cmdBuff);
this._batchedQueue.recordCommandBuffer(device, renderPass, cmdBuff);
this._additiveLightQueue.recordCommandBuffer(device, renderPass, cmdBuff);
camera.scene!.planarShadows.recordCommandBuffer(device, renderPass, cmdBuff);
pipeline.planarShadows.enabled && pipeline.planarShadows.recordCommandBuffer(device, renderPass, cmdBuff);
this._renderQueues[1].recordCommandBuffer(device, renderPass, cmdBuff);

cmdBuff.endRenderPass();
Expand Down
10 changes: 5 additions & 5 deletions cocos/core/pipeline/forward/scene-culling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ export function sceneCulling (pipeline: ForwardPipeline, view: RenderView) {
shadowPool.freeArray(shadowObjects); shadowObjects.length = 0;

const mainLight = scene.mainLight;
const planarShadows = scene.planarShadows;
const planarShadows = pipeline.planarShadows;
if (mainLight) {
mainLight.update();
if (planarShadows.enabled && mainLight.node!.hasChangedFlags) {
if (planarShadows.enabled) {
planarShadows.updateDirLight(mainLight);
}
}

if (scene.skybox.enabled && (camera.clearFlag & SKYBOX_FLAG)) {
renderObjects.push(getRenderObject(scene.skybox, camera));
if (pipeline.skybox.enabled && pipeline.skybox.model && (camera.clearFlag & SKYBOX_FLAG)) {
renderObjects.push(getRenderObject(pipeline.skybox.model, camera));
}

const models = scene.models;
Expand Down Expand Up @@ -100,6 +100,6 @@ export function sceneCulling (pipeline: ForwardPipeline, view: RenderView) {
}

if (planarShadows.enabled) {
planarShadows.updateShadowList(camera.frustum, stamp, (camera.visibility & Layers.BitMask.DEFAULT) !== 0);
planarShadows.updateShadowList(scene, camera.frustum, stamp, (camera.visibility & Layers.BitMask.DEFAULT) !== 0);
}
}
2 changes: 2 additions & 0 deletions cocos/core/pipeline/render-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,5 @@ export abstract class RenderPipeline extends Asset {
return super.destroy();
}
}

legacyCC.RenderPipeline = RenderPipeline;
20 changes: 17 additions & 3 deletions cocos/core/pipeline/render-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ export class RenderView {
* @param flows The names of all [[RenderFlow]]s
*/
public setExecuteFlows (flows: string[] | undefined) {
this.flows.length = 0;
this._flows.length = 0;
const pipeline = legacyCC.director.root.pipeline;
const pipelineFlows = pipeline.flows;
if (flows && flows.length === 1 && flows[0] === 'UIFlow') {
for (let i = 0; i < pipelineFlows.length; i++) {
if (pipelineFlows[i].name === 'UIFlow') {
this.flows.push(pipelineFlows[i]);
this._flows.push(pipelineFlows[i]);
break;
}
}
Expand All @@ -186,7 +186,21 @@ export class RenderView {
for (let i = 0; i < pipelineFlows.length; ++i) {
const f = pipelineFlows[i];
if (f.tag === RenderFlowTag.SCENE || (flows && flows.indexOf(f.name) !== -1)) {
this.flows.push(f);
this._flows.push(f);
}
}
}

public onGlobalPipelineStateChanged () {
const pipeline = legacyCC.director.root.pipeline;
const pipelineFlows = pipeline.flows;
for (let i = 0; i < this._flows.length; ++i) {
const flow = this._flows[i];
for (let j = 0; j < pipelineFlows.length; j++) {
if (pipelineFlows[i].name === flow.name) {
this._flows[i] = pipelineFlows[i];
break;
}
}
}
}
Expand Down
79 changes: 64 additions & 15 deletions cocos/core/renderer/scene/ambient.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,98 @@
import { RenderScene } from './render-scene';
import { property, ccclass } from '../../data/class-decorator';
import { Color, Vec3 } from '../../math';
import { EDITOR } from 'internal:constants';

@ccclass('cc.Ambient')
export class Ambient {
public static SUN_ILLUM = 65000.0;
public static SKY_ILLUM = 20000.0;

get colorArray () {
return this._colorArray;
}

get albedoArray () {
return this._albedoArray;
}

/**
* @en Enable ambient
* @zh 是否开启环境光
*/
set enabled (val) {
if (this._enabled === val) {
return;
}
this._enabled = val;
this.activate();
}
get enabled () {
return this._enabled;
}

get skyColor (): Float32Array {
/**
* @en Sky color
* @zh 天空颜色
*/
get skyColor (): Color {
return this._skyColor;
}

set skyColor (color: Float32Array) {
set skyColor (color: Color) {
this._skyColor = color;
Color.toArray(this._colorArray, this._skyColor);
}

set skyIllum (illum: number) {
this._skyIllum = illum;
}

/**
* @en Sky illuminance
* @zh 天空亮度
*/
get skyIllum (): number {
return this._skyIllum;
}

get groundAlbedo (): Float32Array {
set skyIllum (illum: number) {
this._skyIllum = illum;
}

/**
* @en Ground color
* @zh 地面颜色
*/
get groundAlbedo (): Color {
return this._groundAlbedo;
}

set groundAlbedo (color: Float32Array) {
set groundAlbedo (color: Color) {
this._groundAlbedo = color;
Vec3.toArray(this._albedoArray, this._groundAlbedo);
}

protected _enabled = true;
protected _skyColor = Float32Array.from([0.2, 0.5, 0.8, 1.0]);
@property({
type: Color,
visible: true,
})
protected _skyColor = new Color(51, 128, 204, 1.0);
@property({
visible: true,
})
protected _skyIllum: number = Ambient.SKY_ILLUM;
protected _groundAlbedo = Float32Array.from([0.2, 0.2, 0.2, 1.0]);
@property({
type: Color,
visible: true,
})
protected _groundAlbedo = new Color(51, 51, 51, 255);

protected _albedoArray = Float32Array.from([0.2, 0.2, 0.2, 1.0]);
protected _colorArray = Float32Array.from([0.2, 0.5, 0.8, 1.0]);

protected _scene: RenderScene;
public activate () {
if (!this._enabled) {
return
}

constructor (scene: RenderScene) {
this._scene = scene;
Color.toArray(this._colorArray, this._skyColor);
Vec3.toArray(this._albedoArray, this._groundAlbedo);
}

public update () {}
Expand Down
Loading

0 comments on commit a6c536e

Please sign in to comment.