Skip to content

Commit

Permalink
Add new particle renderer (#1682)
Browse files Browse the repository at this point in the history
* feat: add new particle renderer
  • Loading branch information
GuoLei1990 authored Sep 7, 2023
1 parent c4cbb2c commit a1ce393
Show file tree
Hide file tree
Showing 88 changed files with 4,323 additions and 1,401 deletions.
14 changes: 10 additions & 4 deletions packages/core/src/Camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class Camera extends Component {

private static _inverseViewMatrixProperty = ShaderProperty.getByName("camera_ViewInvMat");
private static _cameraPositionProperty = ShaderProperty.getByName("camera_Position");
private static _cameraForwardProperty = ShaderProperty.getByName("camera_Forward");
private static _cameraUpProperty = ShaderProperty.getByName("camera_Up");
private static _cameraDepthBufferParamsProperty = ShaderProperty.getByName("camera_DepthBufferParams");

/** Whether to enable frustum culling, it is enabled by default. */
Expand Down Expand Up @@ -625,13 +627,17 @@ export class Camera extends Component {
}

private _updateShaderData(): void {
const shaderData = this.shaderData;

const transform = this._transform;
shaderData.setMatrix(Camera._inverseViewMatrixProperty, transform.worldMatrix);
shaderData.setVector3(Camera._cameraPositionProperty, transform.worldPosition);
shaderData.setVector3(Camera._cameraForwardProperty, transform.worldForward);
shaderData.setVector3(Camera._cameraUpProperty, transform.worldUp);

const depthBufferParams = this._depthBufferParams;
const farDivideNear = this._farClipPlane / this._nearClipPlane;
depthBufferParams.set(1.0 - farDivideNear, farDivideNear, 0, 0);

const shaderData = this.shaderData;
shaderData.setMatrix(Camera._inverseViewMatrixProperty, this._transform.worldMatrix);
shaderData.setVector3(Camera._cameraPositionProperty, this._transform.worldPosition);
shaderData.setVector4(Camera._cameraDepthBufferParamsProperty, depthBufferParams);
}

Expand Down
16 changes: 12 additions & 4 deletions packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Canvas } from "./Canvas";
import { EngineSettings } from "./EngineSettings";
import { Entity } from "./Entity";
import { ClassPool } from "./RenderPipeline/ClassPool";
import { MeshRenderData } from "./RenderPipeline/MeshRenderData";
import { RenderContext } from "./RenderPipeline/RenderContext";
import { RenderData } from "./RenderPipeline/RenderData";
import { RenderElement } from "./RenderPipeline/RenderElement";
import { SpriteMaskManager } from "./RenderPipeline/SpriteMaskManager";
import { SpriteMaskRenderData } from "./RenderPipeline/SpriteMaskRenderData";
Expand All @@ -21,6 +21,7 @@ import { GLCapabilityType } from "./base/Constant";
import { ColorSpace } from "./enums/ColorSpace";
import { InputManager } from "./input";
import { Material } from "./material/Material";
import { ParticleBufferUtils } from "./particle/ParticleBufferUtils";
import { PhysicsScene } from "./physics/PhysicsScene";
import { ColliderShape } from "./physics/shape/ColliderShape";
import { IHardwareRenderer } from "./renderingHardwareInterface";
Expand Down Expand Up @@ -55,6 +56,8 @@ export class Engine extends EventDispatcher {
/** Input manager of Engine. */
readonly inputManager: InputManager;

/** @internal */
_particleBufferUtils: ParticleBufferUtils;
/** @internal */
_physicsInitialized: boolean = false;
/** @internal */
Expand All @@ -69,7 +72,7 @@ export class Engine extends EventDispatcher {
/* @internal */
_renderElementPool: ClassPool<RenderElement> = new ClassPool(RenderElement);
/* @internal */
_meshRenderDataPool: ClassPool<MeshRenderData> = new ClassPool(MeshRenderData);
_renderDataPool: ClassPool<RenderData> = new ClassPool(RenderData);
/* @internal */
_spriteRenderDataPool: ClassPool<SpriteRenderData> = new ClassPool(SpriteRenderData);
/* @internal */
Expand Down Expand Up @@ -263,6 +266,8 @@ export class Engine extends EventDispatcher {
const colorSpace = configuration.colorSpace || ColorSpace.Linear;
colorSpace === ColorSpace.Gamma && this._macroCollection.enable(Engine._gammaMacro);
innerSettings.colorSpace = colorSpace;

this._particleBufferUtils = new ParticleBufferUtils(this);
}

/**
Expand Down Expand Up @@ -308,7 +313,7 @@ export class Engine extends EventDispatcher {
this._frameInProcess = true;

this._renderElementPool.resetPool();
this._meshRenderDataPool.resetPool();
this._renderDataPool.resetPool();
this._spriteRenderDataPool.resetPool();
this._spriteMaskRenderDataPool.resetPool();
this._textRenderDataPool.resetPool();
Expand Down Expand Up @@ -644,6 +649,8 @@ export class Engine extends EventDispatcher {

private _onDeviceLost(): void {
this._isDeviceLost = true;
// Lose graphic resources
this.resourceManager._lostGraphicResources();
console.log("Device lost.");
this.dispatch("devicelost", this);
}
Expand All @@ -660,6 +667,7 @@ export class Engine extends EventDispatcher {
console.log("Graphic resource restored.");

// Restore resources content
this._particleBufferUtils.setBufferData();
resourceManager
._restoreResourcesContent()
.then(() => {
Expand All @@ -674,7 +682,7 @@ export class Engine extends EventDispatcher {

private _gc(): void {
this._renderElementPool.garbageCollection();
this._meshRenderDataPool.garbageCollection();
this._renderDataPool.garbageCollection();
this._spriteRenderDataPool.garbageCollection();
this._spriteMaskRenderDataPool.garbageCollection();
this._textRenderDataPool.garbageCollection();
Expand Down
28 changes: 0 additions & 28 deletions packages/core/src/RenderPipeline/MeshRenderData.ts

This file was deleted.

22 changes: 21 additions & 1 deletion packages/core/src/RenderPipeline/RenderData.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import { SubMesh } from "../graphic";
import { Primitive } from "../graphic/Primitive";
import { Material } from "../material";
import { Renderer } from "../Renderer";
import { IPoolElement } from "./IPoolElement";

export class RenderData {
export class RenderData implements IPoolElement {
component: Renderer;
material: Material;
primitive: Primitive;
subPrimitive: SubMesh;

multiRenderData: boolean;

setX(component: Renderer, material: Material, primitive: Primitive, subPrimitive: SubMesh): void {
this.component = component;
this.material = material;

this.primitive = primitive;
this.subPrimitive = subPrimitive;
}

dispose(): void {
this.component = null;
this.material = null;
this.primitive = null;
this.subPrimitive = null;
}
}
11 changes: 5 additions & 6 deletions packages/core/src/RenderPipeline/RenderQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Engine } from "../Engine";
import { Layer } from "../Layer";
import { Shader } from "../shader";
import { ShaderMacroCollection } from "../shader/ShaderMacroCollection";
import { MeshRenderData } from "./MeshRenderData";
import { RenderContext } from "./RenderContext";
import { RenderElement } from "./RenderElement";
import { SpriteBatcher } from "./SpriteBatcher";
Expand Down Expand Up @@ -70,13 +69,13 @@ export class RenderQueue {
continue;
}

if (!!(data as MeshRenderData).mesh) {
if (data.primitive) {
this._spriteBatcher.flush(camera);

const compileMacros = Shader._compileMacros;
const meshData = <MeshRenderData>data;
const renderer = meshData.component;
const material = meshData.material.destroyed ? engine._magentaMaterial : meshData.material;
const primitive = data.primitive;
const renderer = data.component;
const material = data.material.destroyed ? engine._magentaMaterial : data.material;
const rendererData = renderer.shaderData;
const materialData = material.shaderData;

Expand Down Expand Up @@ -157,7 +156,7 @@ export class RenderQueue {
material.shaderData
);

rhi.drawPrimitive(meshData.mesh, meshData.subMesh, program);
rhi.drawPrimitive(primitive, data.subPrimitive, program);
}
} else {
this._spriteBatcher.drawElement(element, camera);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/RenderPipeline/SpriteBatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class SpriteBatcher extends Basic2DBatcher {
program.uploadAll(program.materialUniformBlock, material.shaderData);

material.renderState._apply(engine, false, shaderPass._renderStateDataMap, material.shaderData);
engine._hardwareRenderer.drawPrimitive(mesh, subMesh, program);
engine._hardwareRenderer.drawPrimitive(mesh._primitive, subMesh, program);

maskManager.postRender(renderer);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/RenderPipeline/SpriteMaskRenderData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class SpriteMaskRenderData extends RenderData implements IPoolElement {
this.verticesData = verticesData;
}

dispose(): void {
override dispose(): void {
this.component = this.material = this.verticesData = null;
}
}
2 changes: 1 addition & 1 deletion packages/core/src/RenderPipeline/SpriteRenderData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class SpriteRenderData extends RenderData implements IPoolElement {
this.dataIndex = dataIndex;
}

dispose(): void {
override dispose(): void {
this.component = this.material = this.verticesData = this.texture = null;
}
}
2 changes: 1 addition & 1 deletion packages/core/src/RenderPipeline/TextRenderData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class TextRenderData extends RenderData implements IPoolElement {
this.multiRenderData = true;
}

dispose(): void {
override dispose(): void {
this.component = this.material = null;
this.charsData.length = 0;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Entity } from "./Entity";
import { RenderContext } from "./RenderPipeline/RenderContext";
import { Transform, TransformModifyFlags } from "./Transform";
import { assignmentClone, deepClone, ignoreClone } from "./clone/CloneManager";
import { ICustomClone } from "./clone/ComponentCloner";
import { IComponentCustomClone } from "./clone/ComponentCloner";
import { Material } from "./material";
import { ShaderMacro, ShaderProperty } from "./shader";
import { ShaderData } from "./shader/ShaderData";
Expand All @@ -18,7 +18,7 @@ import { ShaderDataGroup } from "./shader/enums/ShaderDataGroup";
* @decorator `@dependentComponents(Transform, DependentMode.CheckOnly)`
*/
@dependentComponents(Transform, DependentMode.CheckOnly)
export class Renderer extends Component implements ICustomClone {
export class Renderer extends Component implements IComponentCustomClone {
private static _tempVector0 = new Vector3();

private static _receiveShadowMacro = ShaderMacro.getByName("RENDERER_IS_RECEIVE_SHADOWS");
Expand Down Expand Up @@ -67,7 +67,7 @@ export class Renderer extends Component implements ICustomClone {
private _normalMatrix: Matrix = new Matrix();
@ignoreClone
private _materialsInstanced: boolean[] = [];
@ignoreClone
@assignmentClone
private _priority: number = 0;
@assignmentClone
private _receiveShadows: boolean = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export abstract class AnimationCurve<V extends KeyframeValueType> {
let newLength = 0;
for (let i = keys.length - 1; i >= 0; i--) {
const key = keys[i];
if (key.time > length) {
if (key.time > this._length) {
newLength = key.time;
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/asset/GraphicsResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import { Engine } from "../Engine";
import { ReferResource } from "./ReferResource";

export abstract class GraphicsResource extends ReferResource {
/** @internal */
_isContentLost: boolean = false;

/**
* Whether the content of the resource is lost.
*/
get isContentLost(): boolean {
return this._isContentLost;
}

protected constructor(engine: Engine) {
super(engine);
engine.resourceManager._addGraphicResource(this);
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/asset/ResourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ export class ResourceManager {
}
}

/**
* @internal
*/
_lostGraphicResources(): void {
const graphicResourcePool = this._graphicResourcePool;
for (const id in graphicResourcePool) {
graphicResourcePool[id]._isContentLost = true;
}
}

/**
* @internal
*/
Expand Down
Loading

0 comments on commit a1ce393

Please sign in to comment.