Skip to content

Commit

Permalink
feat: add priority for renderer to order(galacean#803)
Browse files Browse the repository at this point in the history
* feat(renderer): add `priority` for renderer to order
  • Loading branch information
singlecoder authored May 24, 2022
1 parent b229bd6 commit 5048451
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
18 changes: 10 additions & 8 deletions packages/core/src/RenderPipeline/BasicRenderPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,16 @@ export class BasicRenderPipeline {
* @param element - Render element
*/
pushPrimitive(element: RenderElement | SpriteElement) {
const renderQueueType = element.material.renderQueueType;

if (renderQueueType > (RenderQueueType.Transparent + RenderQueueType.AlphaTest) >> 1) {
this._transparentQueue.pushPrimitive(element);
} else if (renderQueueType > (RenderQueueType.AlphaTest + RenderQueueType.Opaque) >> 1) {
this._alphaTestQueue.pushPrimitive(element);
} else {
this._opaqueQueue.pushPrimitive(element);
switch (element.material.renderQueueType) {
case RenderQueueType.Transparent:
this._transparentQueue.pushPrimitive(element);
break;
case RenderQueueType.AlphaTest:
this._alphaTestQueue.pushPrimitive(element);
break;
case RenderQueueType.Opaque:
this._opaqueQueue.pushPrimitive(element);
break;
}
}

Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/RenderPipeline/RenderQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ export class RenderQueue {
*/
static _compareFromNearToFar(a: Item, b: Item): number {
return (
a.material.renderQueueType - b.material.renderQueueType ||
a.component._distanceForSort - b.component._distanceForSort ||
b.component._renderSortId - a.component._renderSortId
a.component.priority - b.component.priority ||
a.component._distanceForSort - b.component._distanceForSort
);
}

Expand All @@ -30,9 +29,8 @@ export class RenderQueue {
*/
static _compareFromFarToNear(a: Item, b: Item): number {
return (
a.material.renderQueueType - b.material.renderQueueType ||
b.component._distanceForSort - a.component._distanceForSort ||
b.component._renderSortId - a.component._renderSortId
a.component.priority - b.component.priority ||
b.component._distanceForSort - a.component._distanceForSort
);
}

Expand Down
17 changes: 13 additions & 4 deletions packages/core/src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ export class Renderer extends Component {
@ignoreClone
_globalShaderMacro: ShaderMacroCollection = new ShaderMacroCollection();

/** @internal temp solution. */
@ignoreClone
_renderSortId: number = 0;

@ignoreClone
protected _overrideUpdate: boolean = false;
@shallowClone
Expand All @@ -69,6 +65,8 @@ export class Renderer extends Component {
private _normalMatrix: Matrix = new Matrix();
@ignoreClone
private _materialsInstanced: boolean[] = [];
@ignoreClone
private _priority: number = 0;

/**
* Material count.
Expand Down Expand Up @@ -97,6 +95,17 @@ export class Renderer extends Component {
return this._bounds;
}

/**
* The render priority of the renderer, lower values are rendered first and higher values are rendered last.
*/
get priority(): number {
return this._priority;
}

set priority(value: number) {
this._priority = value;
}

/**
* @internal
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/material/enums/RenderQueueType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/
export enum RenderQueueType {
/** Opaque queue. */
Opaque = 1000,
Opaque,
/** Opaque queue, alpha cutoff. */
AlphaTest = 2000,
AlphaTest,
/** Transparent queue, rendering from back to front to ensure correct rendering of transparent objects. */
Transparent = 3000
Transparent
}

0 comments on commit 5048451

Please sign in to comment.