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

Feat: add particle shape for wireframeManager #253

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
154 changes: 150 additions & 4 deletions packages/auxiliary-lines/src/WireframeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ import {
SpotLight,
Transform,
Vector3,
DependentMode
DependentMode,
ParticleRenderer,
BoxShape,
CircleShape,
ConeShape,
HemisphereShape,
SphereShape
} from "@galacean/engine";
import { PlainColorMaterial } from "@galacean/engine-toolkit-custom-material";
import { WireframePrimitive } from "./WireframePrimitive";
Expand Down Expand Up @@ -111,7 +117,7 @@ export class WireframeManager extends Script {
*/
addEntityWireframe(entity: Entity, includeChildren = true): void {
if (includeChildren) {
const components = new Array<Camera | SpotLight | DirectLight | PointLight | Collider>();
const components = new Array<Camera | SpotLight | DirectLight | PointLight | Collider | ParticleRenderer>();
entity.getComponentsIncludeChildren(Camera, components);
for (let i = 0, n = components.length; i < n; i++) {
this.addCameraWireframe(<Camera>components[i]);
Expand Down Expand Up @@ -140,6 +146,12 @@ export class WireframeManager extends Script {
for (let i = componentsOffset, n = components.length; i < n; i++) {
this.addCollideWireframe(<Collider>components[i]);
}
componentsOffset = components.length;

entity.getComponentsIncludeChildren(ParticleRenderer, components);
for (let i = componentsOffset, n = components.length; i < n; i++) {
this.addParticleRendererEmissionShapeWireframe(<ParticleRenderer>components[i]);
}
} else {
const camera = entity.getComponent(Camera);
camera && this.addCameraWireframe(camera);
Expand All @@ -151,6 +163,8 @@ export class WireframeManager extends Script {
pointLight && this.addPointLightWireframe(pointLight);
const collider = entity.getComponent(Collider);
collider && this.addCollideWireframe(collider);
const particle = entity.getComponent(ParticleRenderer);
particle && this.addParticleRendererEmissionShapeWireframe(particle);
}
}

Expand Down Expand Up @@ -427,6 +441,134 @@ export class WireframeManager extends Script {
this._wireframeElements.push(new WireframeElement(transform, positionsOffset));
}

addParticleRendererEmissionShapeWireframe(particleRenderer: ParticleRenderer): void {
if (particleRenderer.generator.emission.enabled) {
const shape = particleRenderer.generator.emission.shape;
const transform = particleRenderer.entity.transform;
switch (shape.shapeType) {
case 0:
this.addBoxParticleShapeWireframe(shape as BoxShape, transform);
break;
case 1:
this.addCircleParticleShapeWireframe(shape as CircleShape, transform);
break;
case 2:
this.addConeParticleShapeWireframe(shape as ConeShape, transform);
break;
case 3:
this.addHemisphereParticleShapeWireframe(shape as HemisphereShape, transform);
break;
case 4:
this.addSphereParticleShapeWireframe(shape as SphereShape, transform);
break;
default:
break;
}
}
}

addBoxParticleShapeWireframe(shape: BoxShape, transform: Transform): void {
const { size } = shape;

const positionsOffset = this._localPositions.length;

const cuboidIndicesCount = WireframePrimitive.cuboidIndexCount;
this._growthIndexMemory(cuboidIndicesCount);
this._growthPosition(WireframePrimitive.cuboidPositionCount);
const { _indices: indices, _localPositions: localPositions } = this;
WireframePrimitive.createCuboidWireframe(
size.x,
size.y,
size.z,
localPositions,
positionsOffset,
indices,
this._indicesCount
);

this._indicesCount += cuboidIndicesCount;
this._wireframeElements.push(new WireframeElement(transform, positionsOffset, false));
}

addCircleParticleShapeWireframe(shape: CircleShape, transform: Transform): void {
const { radius } = shape;

const positionsOffset = this._localPositions.length;

const circleIndicesCount = WireframePrimitive.circleIndexCount;
this._growthIndexMemory(circleIndicesCount);
this._growthPosition(WireframePrimitive.circlePositionCount);
const { _indices: indices, _localPositions: localPositions } = this;
WireframePrimitive.createCircleWireframe(
radius,
0,
new Vector3(),
localPositions,
positionsOffset,
indices,
this._indicesCount
);
this._indicesCount += circleIndicesCount;
this._wireframeElements.push(new WireframeElement(transform, positionsOffset, false));
}

addConeParticleShapeWireframe(shape: ConeShape, transform: Transform): void {
const { radius, length, angle } = shape;

const positionsOffset = this._localPositions.length;

const frustumIndicesCount = WireframePrimitive.frustumIndexCount;
this._growthIndexMemory(frustumIndicesCount);
this._growthPosition(WireframePrimitive.frustumPositionCount);
const { _indices: indices, _localPositions: localPositions } = this;
WireframePrimitive.createFrustumWireframe(
radius,
length,
angle,
localPositions,
positionsOffset,
indices,
this._indicesCount
);
this._indicesCount += frustumIndicesCount;
this._wireframeElements.push(new WireframeElement(transform, positionsOffset, false));
}

addHemisphereParticleShapeWireframe(shape: HemisphereShape, transform: Transform): void {
const { radius } = shape;

const positionsOffset = this._localPositions.length;

const hemisphereIndicesCount = WireframePrimitive.hemisphereIndexCount;
this._growthIndexMemory(hemisphereIndicesCount);
this._growthPosition(WireframePrimitive.hemispherePositionCount);
const { _indices: indices, _localPositions: localPositions } = this;
WireframePrimitive.createHemisphereWireframe(
radius,
2,
localPositions,
positionsOffset,
indices,
this._indicesCount
);
this._indicesCount += hemisphereIndicesCount;
this._wireframeElements.push(new WireframeElement(transform, positionsOffset, false));
}

addSphereParticleShapeWireframe(shape: SphereShape, transform: Transform): void {
const { radius } = shape;

const positionsOffset = this._localPositions.length;

const sphereIndicesCount = WireframePrimitive.sphereIndexCount;
this._growthIndexMemory(sphereIndicesCount);
this._growthPosition(WireframePrimitive.spherePositionCount);
const { _indices: indices, _localPositions: localPositions } = this;
WireframePrimitive.createSphereWireframe(radius, localPositions, positionsOffset, indices, this._indicesCount);
this._indicesCount += sphereIndicesCount;
this._wireframeElements.push(new WireframeElement(transform, positionsOffset, false));
}

override onAwake(): void {
const engine = this.engine;
const mesh = new ModelMesh(engine);
Expand Down Expand Up @@ -481,7 +623,11 @@ export class WireframeManager extends Script {
if (wireframeElement.updateFlag.flag) {
const transform = wireframeElement.transform;
const worldMatrix = WireframeManager._tempMatrix;
Matrix.rotationTranslation(transform.worldRotationQuaternion, transform.worldPosition, worldMatrix);
if (wireframeElement.isScaleIgnored) {
Matrix.rotationTranslation(transform.worldRotationQuaternion, transform.worldPosition, worldMatrix);
} else {
worldMatrix.copyFrom(transform.worldMatrix);
}

for (let j = beginIndex; j < endIndex; j++) {
const localPosition = localPositions[positionIndex];
Expand Down Expand Up @@ -595,7 +741,7 @@ export class WireframeManager extends Script {
class WireframeElement {
updateFlag: BoolUpdateFlag;

constructor(public transform: Transform, public transformRanges: number) {
constructor(public transform: Transform, public transformRanges: number, public isScaleIgnored: boolean = true) {
this.updateFlag = transform.registerWorldChangeFlag();
}
}
Loading
Loading