Skip to content

Commit

Permalink
fix(MeshRenderer): replace geometry error (Orillusion#415)
Browse files Browse the repository at this point in the history
* Fix: error when replace the geometry of MeshRenderer.

* chore: remove log in loaderbase

---------

Co-authored-by: ShuangLiu <[email protected]>
  • Loading branch information
hellmor and lslzl3000 authored Jul 15, 2024
1 parent c47257c commit c79e287
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 17 deletions.
73 changes: 73 additions & 0 deletions samples/geometry/Sample_ReplaceGeometry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { BoxGeometry, Engine3D, GeometryBase, LitMaterial, MeshRenderer, Object3D, Object3DUtil, Scene3D, SphereGeometry, TorusGeometry, Vector3 } from "@orillusion/core";
import { createExampleScene, createSceneParam } from "@samples/utils/ExampleScene";
import { GUIHelp } from "@orillusion/debug/GUIHelp";

// An sample to replace geometry of meshRenderer
class Sample_ReplaceGeometry {
scene: Scene3D;
geometries: GeometryBase[];
renderer: MeshRenderer;
index = 0;
async run() {
let param = createSceneParam();
param.light.intensity = 1;
param.camera.distance = 40;
await Engine3D.init();
let exampleScene = createExampleScene(param);

Engine3D.setting.shadow.shadowBound = 100;
Engine3D.setting.shadow.shadowSize = 2048;
Engine3D.setting.shadow.shadowBias = 0.04;

this.scene = exampleScene.scene;

Engine3D.startRenderView(exampleScene.view);


GUIHelp.init();
this.initGeometries();

let duck = await (await Engine3D.res.loadGltf('PBR/Duck/Duck.gltf')) as Object3D;
let duckGeometry = duck.getComponents(MeshRenderer)[0].geometry;
this.geometries.push(duckGeometry);

this.initRenderer();
let scale = new Vector3(1, 1, 1);
GUIHelp.add(this, 'index', 0, 4, 1).onChange((value) => {
this.renderer.geometry = this.geometries[value];
if (value == this.geometries.length - 1) {
scale.set(0.03, 0.03, 0.03);
} else {
scale.set(1, 1, 1);
}
this.renderer.object3D.localScale = scale;
})

GUIHelp.open();
this.initFloor();
}

initFloor() {
let floorHeight = 20;
let floor = Object3DUtil.GetSingleCube(1000, floorHeight, 1000, 0.5, 0.5, 0.5);
floor.y = -floorHeight;
this.scene.addChild(floor);
}

initGeometries() {
this.geometries = [];
this.geometries.push(new SphereGeometry(2, 20, 20));
this.geometries.push(new BoxGeometry(2, 8, 4));
this.geometries.push(new TorusGeometry(2, 0.4));
}

initRenderer() {
let obj = new Object3D();
this.renderer = obj.addComponent(MeshRenderer);
this.renderer.material = new LitMaterial();
this.renderer.geometry = this.geometries[0];
this.scene.addChild(obj);
}
}

new Sample_ReplaceGeometry().run();
32 changes: 19 additions & 13 deletions src/components/renderer/MeshRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,31 @@ export class MeshRenderer extends RenderNode {
public set geometry(value: GeometryBase) {
//this must use super geometry has reference in super
super.geometry = value;
let isMorphTarget = value.morphTargetDictionary != null;
if (isMorphTarget) {
this.morphData ||= new MorphTargetData();
this.morphData.morphTargetsRelative = value.morphTargetsRelative;
this.morphData.initMorphTarget(value);
}
this.morphData && (this.morphData.enable = isMorphTarget);
if (this.morphData && this.morphData.enable) {
this.addRendererMask(RendererMask.MorphTarget);
if (value) {
let isMorphTarget = value.morphTargetDictionary != null;
if (isMorphTarget) {
this.morphData ||= new MorphTargetData();
this.morphData.morphTargetsRelative = value.morphTargetsRelative;
this.morphData.initMorphTarget(value);
}
this.morphData && (this.morphData.enable = isMorphTarget);
if (this.morphData?.enable) {
this.addRendererMask(RendererMask.MorphTarget);
} else {
this.removeRendererMask(RendererMask.MorphTarget);
}

this.object3D.bound = this._geometry.bounds.clone();
} else {
if (this.morphData) {
this.morphData.enable = false;
}
this.removeRendererMask(RendererMask.MorphTarget);
// this.onCompute = null;
}

this.object3D.bound = this._geometry.bounds.clone();
if (!this._readyPipeline) {
this.initPipeline();

if (this._computes && this._computes) {
if (this._computes) {
this.onCompute = mergeFunctions(this.onCompute, () => {
for (let i = 0; i < this._computes.length; i++) {
const compute = this._computes[i];
Expand Down
12 changes: 9 additions & 3 deletions src/components/renderer/RenderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,14 @@ export class RenderNode extends ComponentBase {

public set geometry(value: GeometryBase) {
if (this._geometry != value) {
this._readyPipeline = false;

if (this._geometry) {
Reference.getInstance().detached(this._geometry, this)
}
Reference.getInstance().attached(value, this)
if (value) {
Reference.getInstance().attached(value, this)
}
}
this._geometry = value;
}
Expand Down Expand Up @@ -258,7 +262,6 @@ export class RenderNode extends ComponentBase {

protected initPipeline() {
if (this._geometry && this._materials.length > 0) {
let index = 0;
for (let j = 0; j < this._materials.length; j++) {
const material = this._materials[j];
let passList = material.getPass(PassType.COLOR);
Expand All @@ -274,7 +277,6 @@ export class RenderNode extends ComponentBase {
}
this._readyPipeline = true;

let transparent = false;
let sort = 0;
for (let i = 0; i < this.materials.length; i++) {
const element = this.materials[i];
Expand Down Expand Up @@ -364,6 +366,8 @@ export class RenderNode extends ComponentBase {
}

public renderPass(view: View3D, passType: PassType, renderContext: RenderContext) {
if (!this._geometry)
return;
let renderNode = this;
let worldMatrix = renderNode.transform._worldMatrix;
for (let i = 0; i < renderNode.materials.length; i++) {
Expand Down Expand Up @@ -433,6 +437,8 @@ export class RenderNode extends ComponentBase {
if (!this.enable)
return;
// this.nodeUpdate(view, passType, rendererPassState, clusterLightingBuffer);
if (!this._geometry)
return;

let node = this;
let worldMatrix = node.object3D.transform._worldMatrix;
Expand Down
2 changes: 1 addition & 1 deletion src/loader/LoaderBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class LoaderBase {
}
if (receivedArr.length > 0) {
for (let i = 0; i < chunks.length; i++) {
console.log(receivedArr[i]);
// console.log(receivedArr[i]);
if (loaderFunctions && loaderFunctions.onProgress) {
loaderFunctions.onProgress.call(this, receivedArr[i], receivedLength, url);
}
Expand Down

0 comments on commit c79e287

Please sign in to comment.