Skip to content

Commit

Permalink
Merge pull request #395 from lirongf/master
Browse files Browse the repository at this point in the history
修改后期处理流程,增加shaderDataCMDType枚举,动态增加Scene Uniform,修改blur示例
  • Loading branch information
lirongf authored Sep 27, 2020
2 parents a4c3b14 + ddf4cca commit 0af8bad
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 36 deletions.
13 changes: 11 additions & 2 deletions src/layaAir/laya/d3/component/PostProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ export class PostProcess {
constructor() {
this._context = new PostProcessRenderContext();
this._context.compositeShaderData = this._compositeShaderData;
this._context.command = new CommandBuffer();
}

/**
*@internal
*/
_init(camera: Camera, command: CommandBuffer): void {
_init(camera: Camera): void {
this._context.camera = camera;
this._context.command = command;
this._context.command._camera = camera;
}

/**
Expand Down Expand Up @@ -141,6 +142,14 @@ export class PostProcess {
this._effects.splice(index, 1);
}

/**
* 调用指令集
* @internal
*/
_applyPostProcessCommandBuffers():void{
this._context.command._apply();
}

}


50 changes: 20 additions & 30 deletions src/layaAir/laya/d3/core/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,8 @@ export class Camera extends BaseCamera {

set postProcess(value: PostProcess) {
this._postProcess = value;
var postProcessCommandBuffer: CommandBuffer = new CommandBuffer();
this.addCommandBuffer(Camera.CAMERAEVENT_POSTPROCESS, postProcessCommandBuffer);
value && value._init(this, postProcessCommandBuffer);
if(!value) return;
value && value._init(this);
}

/**
Expand Down Expand Up @@ -411,14 +410,6 @@ export class Camera extends BaseCamera {
return this._postProcess || this._enableHDR ? true : false;//condition of internal RT
}

/**
* @internal
*/
_applyPostProcessCommandBuffers(): void {
for (var i: number = 0, n: number = this._postProcessCommandBuffers.length; i < n; i++)
this._postProcessCommandBuffers[i]._apply();
}

/**
* @internal
*/
Expand Down Expand Up @@ -611,7 +602,7 @@ export class Camera extends BaseCamera {
if (needInternalRT) {
if (this._postProcess) {
this._postProcess._render();
this._applyPostProcessCommandBuffers();
this._postProcess._applyPostProcessCommandBuffers();
} else if (this._enableHDR) {
var canvasWidth: number = this._getCanvasWidth(), canvasHeight: number = this._getCanvasHeight();
this._screenOffsetScale.setValue(viewport.x / canvasWidth, viewport.y / canvasHeight, viewport.width / canvasWidth, viewport.height / canvasHeight);
Expand All @@ -621,7 +612,6 @@ export class Camera extends BaseCamera {
}
RenderTexture.recoverToPool(this._internalRenderTexture);
}

if (needShadowCasterPass||spotneedShadowCasterPass)
shadowCasterPass.cleanUp();
}
Expand Down Expand Up @@ -710,29 +700,29 @@ export class Camera extends BaseCamera {
* 在特定渲染管线阶段添加指令缓存。
*/
addCommandBuffer(event: number, commandBuffer: CommandBuffer): void {
switch (event) {
case Camera.CAMERAEVENT_POSTPROCESS:
this._postProcessCommandBuffers.push(commandBuffer);
commandBuffer._camera = this;
break;
default:
throw "Camera:unknown event.";
}
// switch (event) {
// case Camera.CAMERAEVENT_POSTPROCESS:
// this._postProcessCommandBuffers.push(commandBuffer);
// commandBuffer._camera = this;
// break;
// default:
// throw "Camera:unknown event.";
// }
}

/**
* 在特定渲染管线阶段移除指令缓存。
*/
removeCommandBuffer(event: number, commandBuffer: CommandBuffer): void {
switch (event) {
case Camera.CAMERAEVENT_POSTPROCESS:
var index: number = this._postProcessCommandBuffers.indexOf(commandBuffer);
if (index !== -1)
this._postProcessCommandBuffers.splice(index, 1);
break;
default:
throw "Camera:unknown event.";
}
// switch (event) {
// case Camera.CAMERAEVENT_POSTPROCESS:
// var index: number = this._postProcessCommandBuffers.indexOf(commandBuffer);
// if (index !== -1)
// this._postProcessCommandBuffers.splice(index, 1);
// break;
// default:
// throw "Camera:unknown event.";
// }
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/layaAir/laya/d3/core/render/BaseRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ export class BaseRender extends EventDispatcher implements ISingletonElement, IO
var sharedMats: Material[] = this._sharedMaterials;
for (var i: number = 0, n: number = sharedMats.length; i < n; i++) {
var mat: Material = sharedMats[i];
this._surportReflectionProbe= (this._surportReflectionProbe||(mat&&mat._shader._supportReflectionProbe));
this._surportReflectionProbe= (this._surportReflectionProbe||(mat&&mat._shader._supportReflectionProbe));//TODO:最后一个判断是否合理
}
}

Expand Down
85 changes: 84 additions & 1 deletion src/layaAir/laya/d3/core/render/command/CommandBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { SetShaderDataTextureCMD } from "./SetShaderDataTextureCMD";
import { Command } from "./Command";
import { BaseTexture } from "../../../../resource/BaseTexture";
import { Vector4 } from "../../../math/Vector4";
import { Mesh } from "../../../resource/models/Mesh";
import { Matrix4x4 } from "../../../math/Matrix4x4";
import { Material } from "../../material/Material";
import { SetShaderDataCMD, ShaderDataType } from "./SetShaderDataCMD";
import { Vector3 } from "../../../math/Vector3";
import { Vector2 } from "../../../math/Vector2";

/**
* <code>CommandBuffer</code> 类用于创建命令流。
Expand All @@ -26,6 +32,7 @@ export class CommandBuffer {
}

/**
* 调用所有渲染指令
*@internal
*/
_apply(): void {
Expand All @@ -34,12 +41,75 @@ export class CommandBuffer {
}

/**
*@internal
* 设置shader图片数据
* @param shaderData
* @param nameID
* @param source
*/
setShaderDataTexture(shaderData: ShaderData, nameID: number, source: BaseTexture): void {
this._commands.push(SetShaderDataTextureCMD.create(shaderData, nameID, source));
}

/**
* 设置shader Vector4数据
* @param shaderData
* @param nameID
* @param value
*/
setShaderDataVector(shaderData:ShaderData,nameID,value:Vector4):void{
this._commands.push(SetShaderDataCMD.create(shaderData,nameID,value,ShaderDataType.Vector));
}

/**
* 设置shader Vector3数据
* @param shaderData
* @param nameID
* @param value
*/
setShaderDataVector3(shaderData:ShaderData,nameID,value:Vector3):void{
this._commands.push(SetShaderDataCMD.create(shaderData,nameID,value,ShaderDataType.Vector3));
}

/**
* 设置shader Vector2数据
* @param shaderData
* @param nameID
* @param value
*/
setShaderDataVector2(shaderData:ShaderData,nameID,value:Vector2):void{
this._commands.push(SetShaderDataCMD.create(shaderData,nameID,value,ShaderDataType.Vector2));
}

/**
* 设置shader Number属性
* @param shaderData
* @param nameID
* @param value
*/
setShaderDataNumber(shaderData:ShaderData,nameID,value:number):void{
this._commands.push(SetShaderDataCMD.create(shaderData,nameID,value,ShaderDataType.Number));
}

/**
* 设置shader Int属性
* @param shaderData
* @param nameID
* @param value
*/
setShaderDataInt(shaderData:ShaderData,nameID,value:number):void{
this._commands.push(SetShaderDataCMD.create(shaderData,nameID,value,ShaderDataType.Int));
}

/**
* 设置shader Matrix属性
* @param shaderData
* @param nameID
* @param value
*/
setShaderDataMatrix(shaderData:ShaderData,nameID,value:Matrix4x4):void{
this._commands.push(SetShaderDataCMD.create(shaderData,nameID,value,ShaderDataType.Matrix4x4));
}

/**
* 添加一条通过全屏四边形将源纹理渲染到目标渲染纹理指令。
* @param source 源纹理。
Expand Down Expand Up @@ -67,12 +137,25 @@ export class CommandBuffer {
}

/**
* 设置指令渲染目标
*@internal
*/
setRenderTarget(renderTexture: RenderTexture): void {
this._commands.push(SetRenderTargetCMD.create(renderTexture));
}

/**
* miner TODO:
* @param mesh
* @param matrix
* @param material
* @param submeshIndex
* @param shaderPass
*/
drawMesh(mesh:Mesh,matrix:Matrix4x4, material:Material ,submeshIndex:number,shaderPass:number){

}

/**
*@internal
*/
Expand Down
107 changes: 107 additions & 0 deletions src/layaAir/laya/d3/core/render/command/SetShaderDataCMD.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Command } from "./Command";
import { ShaderData } from "../../../shader/ShaderData"
import { BaseTexture } from "../../../../resource/BaseTexture";
import { Vector2 } from "../../../math/Vector2";
import { Vector3 } from "../../../math/Vector3";
import { Vector4 } from "../../../math/Vector4";
import { Quaternion } from "../../../math/Quaternion";
import { Matrix4x4 } from "../../../math/Matrix4x4";
export enum ShaderDataType{
Int,
Bool,
Number,
Vector2,
Vector3,
Vector,
Quaternion,
Matrix4x4,
Buffer,
Texture
}

/**
* @internal
* <code>SetShaderDataTextureCMD</code> 类用于创建设置渲染目标指令。
*/
export class SetShaderDataCMD extends Command {
/**@internal */
private static _pool: any[] = [];

/**@internal */
private _shaderData: ShaderData = null;
/**@internal */
private _nameID: number = 0;
/**@internal */
private _value: number|BaseTexture|boolean|Vector2|Vector3|Vector4|Float32Array|Quaternion|Matrix4x4 = null;
/**@internal */
private _dataType:number = -1;

/**
* @internal
*/
static create(shaderData: ShaderData, nameID: number, value:any,shaderDataType:ShaderDataType): SetShaderDataCMD {
var cmd: SetShaderDataCMD;
cmd = SetShaderDataCMD._pool.length > 0 ? SetShaderDataCMD._pool.pop() : new SetShaderDataCMD();
cmd._shaderData = shaderData;
cmd._nameID = nameID;
cmd._value = value;
cmd._dataType = shaderDataType
return cmd;
}

/**
* @inheritDoc
* @override
*/
run(): void {
switch(this._dataType){
case ShaderDataType.Int:
this._shaderData.setInt(this._nameID,this._value as number);
break;
case ShaderDataType.Number:
this._shaderData.setNumber(this._nameID,this._value as number);
break;
case ShaderDataType.Bool:
this._shaderData.setBool(this._nameID,this._value as boolean);
break;
case ShaderDataType.Matrix4x4:
this._shaderData.setMatrix4x4(this._nameID,this._value as Matrix4x4);
break;
case ShaderDataType.Quaternion:
this._shaderData.setQuaternion(this._nameID,this._value as Quaternion);
break;
case ShaderDataType.Texture:
this._shaderData.setTexture(this._nameID,this._value as BaseTexture);
break;
case ShaderDataType.Vector:
this._shaderData.setVector(this._nameID,this._value as Vector4);
break;
case ShaderDataType.Vector2:
this._shaderData.setVector2(this._nameID,this._value as Vector2);
break;
case ShaderDataType.Vector3:
this._shaderData.setVector3(this._nameID,this._value as Vector3);
break;
case ShaderDataType.Buffer:
this._shaderData.setBuffer(this._nameID,this._value as Float32Array);
break;
default:
throw "no type shaderValue on this CommendBuffer";
}
}

/**
* @inheritDoc
* @override
*/
recover(): void {
SetShaderDataCMD._pool.push(this);
this._shaderData = null;
this._nameID = 0;
this._value = null;
this._dataType = -1;
}

}


Loading

0 comments on commit 0af8bad

Please sign in to comment.