Skip to content

Commit

Permalink
fix: fix InstanceDraw destroy error
Browse files Browse the repository at this point in the history
update instancedraw samples
  • Loading branch information
lslzl3000 committed Aug 25, 2024
1 parent 23868d0 commit 4529594
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GUIHelp } from '@orillusion/debug/GUIHelp';
import { Stats } from '@orillusion/stats'
import { Engine3D, Scene3D, AtmosphericComponent, CameraUtil, HoverCameraController, Object3D, MeshRenderer, BoxGeometry, LitMaterial, DirectLight, KelvinUtil, View3D, Vector3, Vector3Ex, UnLitMaterial, InstanceDrawComponent, LambertMaterial, Time, BoundingBox, Color, OcclusionSystem, PostProcessingComponent, GlobalFog, SphereGeometry } from '@orillusion/core';
import { GUIUtil } from '@samples/utils/GUIUtil';

// simple base demo
class Sample_drawCallInstance {
Expand Down Expand Up @@ -54,17 +53,13 @@ class Sample_drawCallInstance {
// start render
Engine3D.startRenderView(view);
GUIHelp.init();

GUIHelp.add(this, "anim").onChange = () => {
this.anim != this.anim;
};

GUIHelp.open();
GUIHelp.add(this, "anim").onChange = () => this.anim != this.anim;
this.initScene();
}


private _list: Object3D[] = [];
private _rotList: number[] = [];
initScene() {
{
this.lightObj3D = new Object3D();
Expand All @@ -79,8 +74,6 @@ class Sample_drawCallInstance {
directLight.castShadow = true;
directLight.intensity = 30;
directLight.indirect = 1;
GUIHelp.init();
GUIUtil.renderDirLight(directLight);
this.scene.addChild(this.lightObj3D);
}

Expand All @@ -95,10 +88,7 @@ class Sample_drawCallInstance {

let group = new Object3D();
let count = 10 * 10000;
// let count = 200;

GUIHelp.addFolder('info');
GUIHelp.open();
GUIHelp.addLabel(`use instance draw box`);
GUIHelp.addInfo(`count `, count);

Expand All @@ -123,9 +113,8 @@ class Sample_drawCallInstance {
obj.transform.rotationX = Math.random() * 360;
obj.transform.rotationY = Math.random() * 360;
obj.transform.rotationZ = Math.random() * 360;

this._rotList.push((Math.random() * 1 - 1 * 0.5) * 2.0 * Math.random() * 100);


// use localDetailRot to update rotation by time
obj.transform.localDetailRot = new Vector3(
(Math.random() * 1 - 1 * 0.5) * 2.0 * Math.random() * 50 * 0.001,
(Math.random() * 1 - 1 * 0.5) * 2.0 * Math.random() * 50 * 0.001,
Expand All @@ -136,9 +125,8 @@ class Sample_drawCallInstance {
}

group.addComponent(InstanceDrawComponent);
group.transform.localDetailRot = new Vector3(0, 1.0 * 0.001, 0);
this._rotList.push(1.0);

// use localDetailRot to update rotation by time
group.transform.localDetailRot = new Vector3(0, 0.01, 0);
group.bound = new BoundingBox(Vector3.SAFE_MIN, Vector3.SAFE_MAX);
this._list.push(group);
this.scene.addChild(group);
Expand All @@ -147,10 +135,8 @@ class Sample_drawCallInstance {

renderLoop() {
if (this.anim) {
let i = 0;
for (i = 0; i < this._list.length; i++) {
for (let i = 0; i < this._list.length; i++) {
let element = this._list[i];
// element.transform.rotationY += 1;
element.transform.localChange = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GUIHelp } from '@orillusion/debug/GUIHelp';
import { Stats } from '@orillusion/stats'
import { Engine3D, Scene3D, AtmosphericComponent, CameraUtil, HoverCameraController, Object3D, MeshRenderer, BoxGeometry, LitMaterial, DirectLight, KelvinUtil, View3D, Vector3, Vector3Ex, UnLitMaterial, InstanceDrawComponent, LambertMaterial, Time, BoundingBox, Color } from '@orillusion/core';
import { GUIUtil } from '@samples/utils/GUIUtil';

// simple base demo
class Sample_SphereDraw {
Expand All @@ -28,7 +27,7 @@ class Sample_SphereDraw {

// add a basic camera controller
let hoverCameraController = mainCamera.object3D.addComponent(HoverCameraController);
hoverCameraController.setCamera(15, -15, 100);
hoverCameraController.setCamera(15, -15, 200);

// add a basic direct light
let lightObj = new Object3D();
Expand All @@ -51,40 +50,38 @@ class Sample_SphereDraw {
Engine3D.startRenderView(view);

GUIHelp.init();

GUIHelp.open();
GUIHelp.add(this, "anim").onChange = () => {
this.anim != this.anim;
};

this.initScene();
}


private _list: Object3D[] = [];
initScene() {
let shareGeometry = new BoxGeometry();
let materials = [
new LambertMaterial()
];

for (let i = 0; i < materials.length; i++) {
const element = materials[i];
element.baseColor = Color.random();
let materials = [];
for (let i = 0; i < 1000; i++) {
let mat = new UnLitMaterial()
mat.baseColor = Color.random();
materials.push(mat)
}

// let material = new LitMaterial();

let group = new Object3D();
this.scene.addChild(group);
// let count = 150000;
let count = 10000;
let count = 100000;

GUIHelp.addLabel(`instance draw with multi materials`);
GUIHelp.addInfo(`object count `, count);
GUIHelp.addInfo(`material count`, materials.length)

for (let i = 0; i < count; i++) {
let pos = Vector3Ex.sphere(100);
// let pos = Vector3Ex.getRandomXYZ(-2, 2);
let obj = new Object3D();
let mr = obj.addComponent(MeshRenderer);
mr.geometry = shareGeometry;
mr.material = materials[Math.floor(Math.random() * materials.length)];
mr.material = materials[i % materials.length];
obj.localPosition = pos;
group.addChild(obj);
this._list.push(obj);
Expand All @@ -98,20 +95,26 @@ class Sample_SphereDraw {
obj.transform.scaleZ = Math.random() * 5 + 1;

obj.transform.forward = d;
obj["rot"] = (Math.random() * 1 - 1 * 0.5) * 2.0 * Math.random() * 20;

obj.transform.localDetailRot = new Vector3(
(Math.random() * 1 - 1 * 0.5) * 2.0 * Math.random() * 50 * 0.001,
(Math.random() * 1 - 1 * 0.5) * 2.0 * Math.random() * 50 * 0.001,
(Math.random() * 1 - 1 * 0.5) * 2.0 * Math.random() * 50 * 0.001);
}
group.addComponent(InstanceDrawComponent);
group["rot"] = 1.0;
// use localDetailRot to update rotation by time
group.transform.localDetailRot = new Vector3(0, 0.01, 0);
group.bound = new BoundingBox(Vector3.SAFE_MIN, Vector3.SAFE_MAX);
this._list.push(group);
}

renderLoop() {
if (this.anim) {
this._list[this._list.length - 1].rotationY += Time.delta * 0.01;
this._list.forEach((v) => {
// v.transform.rotationY += Time.delta * 0.01 * v["rot"];
})
for (let i = 0; i < this._list.length; i++) {
let element = this._list[i];
element.transform.localChange = true;
}
// this._list[this._list.length - 1].transform.rotationY += 0.1
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GUIHelp } from '@orillusion/debug/GUIHelp';
import { Stats } from '@orillusion/stats'
import { Engine3D, Scene3D, AtmosphericComponent, CameraUtil, HoverCameraController, Object3D, MeshRenderer, BoxGeometry, LitMaterial, DirectLight, KelvinUtil, View3D, Vector3, Vector3Ex, UnLitMaterial, InstanceDrawComponent, LambertMaterial, Time, BoundingBox, Color, OcclusionSystem, PostProcessingComponent, GlobalFog, SphereGeometry, RendererMask, RenderLayer } from '@orillusion/core';
import { GUIUtil } from '@samples/utils/GUIUtil';

// simple base demo
export class Sample_drawCallShareGeometry {
Expand Down Expand Up @@ -66,39 +65,27 @@ export class Sample_drawCallShareGeometry {
private _rotList: number[] = [];
initScene() {
let shareGeometry = new BoxGeometry();

let mats = [];
for (let i = 0; i < 1; i++) {
const mat = new LambertMaterial();
mat.baseColor = new Color(
Math.random() * 0.85,
Math.random() * 0.85,
Math.random() * 0.85,
)

// mat.baseColor = new Color().hexToRGB(0xcccccc)
mats.push(mat);
}


const mat = new LambertMaterial();
mat.baseColor = new Color(
Math.random() * 0.85,
Math.random() * 0.85,
Math.random() * 0.85,
)
let group = new Object3D();
let count = 5 * 10000;
let count = 10 * 10000;

GUIHelp.addFolder('info');
GUIHelp.open();
GUIHelp.addLabel(`normal draw box`);
GUIHelp.addInfo(`count `, count);

let ii = 0;
// let count = 70000;
for (let i = 0; i < count; i++) {
let pos = Vector3Ex.sphereXYZ(ii * 60 + 20, ii * 60 + 100, 100, i * 0.001 + 10, 100);
// let pos = Vector3Ex.getRandomXYZ(-2, 2);
let obj = new Object3D();
let mr = obj.addComponent(MeshRenderer);
// mr.renderLayer = RenderLayer.DynamicBatch;
mr.geometry = shareGeometry;
mr.material = mats[Math.floor(Math.random() * mats.length)];
mr.material = mat;
obj.localPosition = pos;
group.addChild(obj);
this._list.push(obj);
Expand All @@ -122,8 +109,7 @@ export class Sample_drawCallShareGeometry {
}
}

// group.addComponent(InstanceDrawComponent);
group.transform.localDetailRot = new Vector3(0, 1.0 * 0.001 * 0.15, 0);
group.transform.localDetailRot = new Vector3(0, 0.01, 0);
this._rotList.push(1.0 * 0.35);

group.bound = new BoundingBox(Vector3.SAFE_MIN, Vector3.SAFE_MAX);
Expand All @@ -136,8 +122,6 @@ export class Sample_drawCallShareGeometry {
let i = 0;
for (let i = 0; i < this._list.length; i++) {
const element = this._list[i];
// element.transform.rotationY += Time.delta * 0.01 * this._rotList[i];
// element.transform._localRot.y += Time.delta * 0.01 * this._rotList[i];
element.transform.localChange = true;
}
}
Expand Down
13 changes: 0 additions & 13 deletions samples/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
import { Sample_AnimCurve } from "./animation/Sample_CurveAnimation";
import { Sample_PointLight } from "./lights/Sample_PointLight";
import { Sample_PointLightShadow } from "./lights/Sample_PointLightShadow";
import { Sample_CarPaint } from "./material/Sample_CarPaint";
import { Sample_SSGI } from "./post/Sample_SSGI";

// new Sample_CarPaint().run();
// new Sample_SSGI().run();

// new Sample_AnimCurve().run();

// new Sample_PointLightShadow().run();

/******** Load all samples in /src/sample/ ********/
{
// find all demos in /sample
Expand Down
2 changes: 1 addition & 1 deletion src/assets/shader/materials/Lambert_shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export let Lambert_shader: string = /*wgsl*/ `
var uv = transformUV1.zw * ORI_VertexVarying.fragUV0 + transformUV1.xy;
let baseColor = textureSample(baseMap,baseMapSampler,uv) ;
if(baseColor.w < 0.5){
if(baseColor.w < materialUniform.alphaCutoff){
discard ;
}
Expand Down
2 changes: 1 addition & 1 deletion src/assets/shader/materials/UnLit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export let UnLit: string = /*wgsl*/ `
var uv = transformUV1.zw * ORI_VertexVarying.fragUV0 + transformUV1.xy;
let color = textureSample(baseMap,baseMapSampler,uv) ;
if(color.w < 0.5){
if(color.w < materialUniform.alphaCutoff){
discard ;
}
Expand Down
2 changes: 1 addition & 1 deletion src/assets/shader/materials/UnLitTextureArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export let UnLitTextureArray: string = /*wgsl*/ `
// irradiance = LinearToGammaSpace(irradiance.rgb) * color.rgb ;//* att ;
color += graphicNode.emissiveColor ;
if(color.w < 0.5){
if(color.w < materialUniform.alphaCutoff){
discard ;
}
Expand Down
25 changes: 17 additions & 8 deletions src/components/renderer/InstanceDrawComponent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { GPUContext } from "../../gfx/renderJob/GPUContext";
import { RTResourceMap } from "../../gfx/renderJob/frame/RTResourceMap";
import { ClusterLightingRender } from "../../gfx/renderJob/passRenderer/cluster/ClusterLightingRender";
import { RenderContext } from "../../gfx/renderJob/passRenderer/RenderContext";
import { MeshRenderer } from "./MeshRenderer";
import { RenderNode } from "./RenderNode";
Expand All @@ -9,6 +8,7 @@ import { View3D } from "../../core/View3D";
import { RendererPassState } from "../../gfx/renderJob/passRenderer/state/RendererPassState";
import { PassType } from "../../gfx/renderJob/passRenderer/state/PassType";
import { ClusterLightingBuffer } from "../../gfx/renderJob/passRenderer/cluster/ClusterLightingBuffer";
import { ComponentCollect } from "../../gfx/renderJob/collect/ComponentCollect";

export class InstanceDrawComponent extends RenderNode {

Expand Down Expand Up @@ -64,10 +64,14 @@ export class InstanceDrawComponent extends RenderNode {
})
}

public stop(): void {

public reset(){
if(this._keyRenderGroup.size > 0){
this._keyRenderGroup.clear()
this._keyBufferGroup.clear()
this._keyIdsGroup.clear()
this.start()
}
}

public nodeUpdate(view: View3D, passType: PassType, renderPassState: RendererPassState, clusterLightingBuffer?: ClusterLightingBuffer): void {
this._keyRenderGroup.forEach((v, k) => {
let instanceMatrixBuffer = this._keyBufferGroup.get(k);
Expand Down Expand Up @@ -107,11 +111,7 @@ export class InstanceDrawComponent extends RenderNode {
continue;

for (let j = 0; j < passes.length; j++) {
if (!passes || passes.length == 0)
continue;
let matPass = passes[j];
// if (!matPass.enable)
// continue;

GPUContext.bindGeometryBuffer(renderContext.encoder, renderNode.geometry);
const renderShader = matPass;
Expand All @@ -138,4 +138,13 @@ export class InstanceDrawComponent extends RenderNode {
}
}
}

public beforeDestroy(force?: boolean): void {
this._keyRenderGroup.clear();
this._keyBufferGroup.clear();
this._keyIdsGroup.clear();
//@ts-ignore
this._keyRenderGroup = this._keyBufferGroup = this._keyIdsGroup = undefined;
ComponentCollect.removeWaitStart(this.object3D, this);
}
}
4 changes: 0 additions & 4 deletions src/core/geometry/GeometryBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ export class GeometryBase {

constructor() {
this.instanceID = UUID();

this._attributeMap = new Map<string, VertexAttributeData>();
this._attributes = [];



this._vertexBuffer = new GeometryVertexBuffer();
}

Expand Down
1 change: 0 additions & 1 deletion src/loader/parser/prefab/mats/shader/UnLitShader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Engine3D } from "../../../../../Engine3D";
import { GPUCullMode } from "../../../../../gfx/graphics/webGpu/WebGPUConst";
import { Texture } from "../../../../../gfx/graphics/webGpu/core/texture/Texture";
import { RenderShaderPass } from "../../../../../gfx/graphics/webGpu/shader/RenderShaderPass";
Expand Down
Loading

0 comments on commit 4529594

Please sign in to comment.