Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more clear flag item in CameraClearFlags #843

Merged
merged 6 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ export class Camera extends Component {

/**
* Determining what to clear when rendering by a Camera.
* @defaultValue `CameraClearFlags.DepthColor`
* @defaultValue `CameraClearFlags.All`
*/
clearFlags: CameraClearFlags = CameraClearFlags.DepthColor;
clearFlags: CameraClearFlags = CameraClearFlags.All;

/**
* Culling mask - which layers the camera renders.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/RenderPipeline/BasicRenderPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export class BasicRenderPipeline {
} else {
this._opaqueQueue.render(camera, pass.replaceMaterial, pass.mask);
this._alphaTestQueue.render(camera, pass.replaceMaterial, pass.mask);
if (camera.clearFlags === CameraClearFlags.DepthColor) {
if (camera.clearFlags & CameraClearFlags.Color) {
if (background.mode === BackgroundMode.Sky) {
this._drawSky(engine, camera, background.sky);
} else if (background.mode === BackgroundMode.Texture && background.texture) {
Expand Down
22 changes: 17 additions & 5 deletions packages/core/src/enums/CameraClearFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
* Camera clear flags enumeration.
*/
export enum CameraClearFlags {
/* Clear depth and color from background. */
DepthColor,
/* Clear depth only. */
Depth,
/* Do nothing. */
None
None = 0,
/* Clear color only. */
Color = 1,
/* Clear depth only. */
Depth = 2,
/* Clear depth only. */
Stencil = 4,

/* Clear color and depth from background. */
ColorDepth = 3,
/* Clear color and stencil from background. */
ColorStencil = 5,
/* Clear depth and stencil from background. */
DepthStencil = 6,

/** Clear stencil, depth and color from background. */
All = 7
}
2 changes: 1 addition & 1 deletion packages/core/src/input/pointer/PointerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class PointerManager {
// TODO: Only check which colliders have listened to the input.
if (this._engine.physicsManager.raycast(camera.viewportPointToRay(point, ray), hitResult)) {
return hitResult.entity;
} else if (camera.clearFlags === CameraClearFlags.DepthColor) {
} else if (camera.clearFlags === CameraClearFlags.Color) {
return null;
}
}
Expand Down
58 changes: 32 additions & 26 deletions packages/rhi-webgl/src/WebGLRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ export class WebGLRenderer implements IHardwareRenderer {
private _extensions;
private _capability: GLCapability;
private _isWebGL2: boolean;
private _webCanvas: WebCanvas;

private _activeTextureID: number;
private _activeTextures: GLTexture[] = new Array(32);

// cache value
private _lastViewport: Vector4 = new Vector4(null, null, null, null);
private _lastClearColor: Color = new Color(null, null, null, null);
private _scissorEnable: boolean = false;

get isWebGL2() {
return this._isWebGL2;
Expand Down Expand Up @@ -106,8 +108,7 @@ export class WebGLRenderer implements IHardwareRenderer {
const option = this._options;
option.alpha === undefined && (option.alpha = false);
option.stencil === undefined && (option.stencil = true);

const webCanvas = (canvas as WebCanvas)._webCanvas;
const webCanvas = (this._webCanvas = (canvas as WebCanvas)._webCanvas);
const webGLMode = option.webGLMode || WebGLMode.Auto;
let gl: (WebGLRenderingContext & WebGLExtension) | WebGL2RenderingContext;

Expand Down Expand Up @@ -182,12 +183,21 @@ export class WebGLRenderer implements IHardwareRenderer {
}

viewport(x: number, y: number, width: number, height: number): void {
// gl.enable(gl.SCISSOR_TEST);
// gl.scissor(x, transformY, width, height);
const gl = this._gl;
const lv = this._lastViewport;

const { _gl: gl, _lastViewport: lv } = this;
if (x !== lv.x || y !== lv.y || width !== lv.z || height !== lv.w) {
const { _webCanvas: webCanvas } = this;
if (x === 0 && y === 0 && width === webCanvas.width && height === webCanvas.height) {
if (this._scissorEnable) {
gl.disable(gl.SCISSOR_TEST);
this._scissorEnable = false;
}
} else {
if (!this._scissorEnable) {
gl.enable(gl.SCISSOR_TEST);
this._scissorEnable = true;
}
gl.scissor(x, y, width, height);
}
gl.viewport(x, y, width, height);
lv.setValue(x, y, width, height);
}
Expand All @@ -197,26 +207,19 @@ export class WebGLRenderer implements IHardwareRenderer {
this._gl.colorMask(r, g, b, a);
}

clearRenderTarget(
engine: Engine,
clearFlags: CameraClearFlags.Depth | CameraClearFlags.DepthColor,
clearColor: Color
) {
clearRenderTarget(engine: Engine, clearFlags: CameraClearFlags, clearColor: Color) {
const gl = this._gl;
const {
blendState: { targetBlendState },
depthState,
stencilState
} = engine._lastRenderState;

let clearFlag = gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT;

if (clearFlags === CameraClearFlags.DepthColor) {
let clearFlag = 0;
if (clearFlags & CameraClearFlags.Color) {
clearFlag |= gl.COLOR_BUFFER_BIT;

const lc = this._lastClearColor;
const { r, g, b, a } = clearColor;

if (clearColor && (r !== lc.r || g !== lc.g || b !== lc.b || a !== lc.a)) {
gl.clearColor(r, g, b, a);
lc.setValue(r, g, b, a);
Expand All @@ -227,17 +230,20 @@ export class WebGLRenderer implements IHardwareRenderer {
targetBlendState.colorWriteMask = ColorWriteMask.All;
}
}

if (depthState.writeEnabled !== true) {
gl.depthMask(true);
depthState.writeEnabled = true;
if (clearFlags & CameraClearFlags.Depth) {
clearFlag |= gl.DEPTH_BUFFER_BIT;
if (depthState.writeEnabled !== true) {
gl.depthMask(true);
depthState.writeEnabled = true;
}
}

if (stencilState.writeMask !== 0xff) {
gl.stencilMask(0xff);
stencilState.writeMask = 0xff;
if (clearFlags & CameraClearFlags.Stencil) {
clearFlag |= gl.STENCIL_BUFFER_BIT;
if (stencilState.writeMask !== 0xff) {
gl.stencilMask(0xff);
stencilState.writeMask = 0xff;
}
}

gl.clear(clearFlag);
}

Expand Down