Skip to content

Commit

Permalink
fix: fix material uniformNode not update (#268)
Browse files Browse the repository at this point in the history
fix material uniformNode not update
opt pick collect
fix when no have light iblDiffuse is dark
  • Loading branch information
ZenderJK committed Jul 29, 2023
1 parent 01d24a1 commit 23db052
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 30 deletions.
2 changes: 1 addition & 1 deletion samples/material/Sample_UVMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Sample_UVMove {
// add plane into scene
let plane = new Object3D();
let renderer = plane.addComponent(MeshRenderer);
let material = new UnLitMaterial();
let material = new LitMaterial();
material.baseMap = await Engine3D.res.loadTexture("particle/T_Fx_Object_229.png");;
renderer.material = material;
renderer.geometry = new PlaneGeometry(100, 100, 1, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/assets/shader/lighting/BxDF_frag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export let BxDF_frag: string = /*wgsl*/ `
var iblDiffuseResult = irradiance * kdLast * fragData.Albedo.rgb ;
//***********indirect-ambient part*********
let sunLight = lightBuffer[0] ;
var indirectResult = (iblSpecularResult + iblDiffuseResult) * fragData.Ao * sunLight.quadratic ;
var indirectResult = (iblSpecularResult + iblDiffuseResult) * fragData.Ao * max(sunLight.quadratic,0.05) ;
ORI_FragmentOutput.color = vec4<f32>(0.0);
Expand Down
70 changes: 60 additions & 10 deletions src/gfx/graphics/webGpu/core/uniforms/UniformNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export class UniformNode {
private _data: UniformValue;
private _type: UniformType = UniformType.Number;

private _x: number = 0;
private _y: number = 0;
private _z: number = 0;
private _w: number = 0;

constructor(value: UniformValue) {
this.data = value;
}
Expand All @@ -30,21 +35,35 @@ export class UniformNode {
this._type = UniformType.Number;
if (value instanceof Vector2) {
this.size = 2;
this._x = value.x;
this._y = value.y;
this._type = UniformType.Vector2;
} else if (value instanceof Vector3) {
this.size = 3;
this._x = value.x;
this._y = value.y;
this._z = value.z;
this._type = UniformType.Vector3;
} else if (value instanceof Vector4) {
this.size = 4;
this._x = value.x;
this._y = value.y;
this._z = value.z;
this._w = value.w;
this._type = UniformType.Vector4;
} else if (value instanceof Color) {
this.size = 4;
this._x = value.r;
this._y = value.g;
this._z = value.b;
this._w = value.a;
this._type = UniformType.Color;
} else if (value instanceof Float32Array) {
this.size = value.length;
this._type = UniformType.Float32Array;
} else {
this.size = 1;
this._x = value.x;
this._type = UniformType.Number;
}
}
Expand All @@ -65,26 +84,33 @@ export class UniformNode {

public set color(value: Color) {
if (
this._data.r != value.r ||
this._data.g != value.g ||
this._data.b != value.b ||
this._data.a != value.a
this._x != value.r ||
this._y != value.g ||
this._z != value.b ||
this._w != value.a
) {
this._data.r = value.r;
this._data.g = value.g;
this._data.b = value.b;
this._data.a = value.a;

this._x = value.r;
this._y = value.g;
this._z = value.b;
this._w = value.a;

this.onChange();
}
}

public get value(): number {
return this._data;
return this._x;
}

public set value(value: number) {
if (this._data != value) {
if (this._x != value) {
this._data = value;
this._x = value;
this.onChange();
}
}
Expand All @@ -94,9 +120,15 @@ export class UniformNode {
}

public set vector2(value: Vector2) {
if (this._data.x != value.x || this._data.y != value.y) {
if (
this._x != value.x ||
this._y != value.y) {

this._data.x = value.x;
this._data.y = value.y;

this._x = value.x;
this._y = value.y;
this.onChange();
}
}
Expand All @@ -106,25 +138,43 @@ export class UniformNode {
}

public set vector3(value: Vector3) {
if (this._data.x != value.x || this._data.y != value.y || this._data.z != value.z) {
if (
this._x != value.x ||
this._y != value.y ||
this._z != value.z) {

this._data.x = value.x;
this._data.y = value.y;
this._data.z = value.z;

this._x = value.x;
this._y = value.y;
this._z = value.z;
this.onChange();
}
}

public get vector4(): Vector4 {
// this.onChange();
return this._data;//new Vector4(this._data.x, this._data.y, this._data.z, this._data.w);
}

public set vector4(value: Vector4) {
if (this._data.x != value.x || this._data.y != value.y || this._data.z != value.z || this._data.w != value.w) {
if (
this._x != value.x ||
this._y != value.y ||
this._z != value.z ||
this._w != value.w) {

this._data.x = value.x;
this._data.y = value.y;
this._data.z = value.z;
this._data.w = value.w;

this._x = value.x;
this._y = value.y;
this._z = value.z;
this._w = value.w;

this.onChange();
}
}
Expand Down
27 changes: 9 additions & 18 deletions src/io/PickFire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ColliderComponent } from '../components/ColliderComponent';
import { View3D } from '../core/View3D';
import { PointerEvent3D } from '../event/eventConst/PointerEvent3D';
import { HitInfo } from '../components/shape/ColliderShape';
import { ComponentCollect } from '..';

/**
* Management and triggering for picking 3D objects
Expand Down Expand Up @@ -98,8 +99,7 @@ export class PickFire extends CEventDispatcher {
this.isTouching = true;
this._mouseCode = e.mouseCode;

this.collectEntities();
this.pick(this._colliderOut, this._view.camera);
this.pick(this._view.camera);
let target = this.findNearestObj(this._interestList, this._view.camera);
this._lastDownTarget = target;
if (target) {
Expand All @@ -119,8 +119,7 @@ export class PickFire extends CEventDispatcher {
this.isTouching = false;
this._mouseCode = e.mouseCode;

this.collectEntities();
this.pick(this._colliderOut, this._view.camera);
this.pick(this._view.camera);
let target = this.findNearestObj(this._interestList, this._view.camera);
if (target) {
this._upEvent.target = target.object3D;
Expand All @@ -147,8 +146,7 @@ export class PickFire extends CEventDispatcher {
private onTouchMove(e: PointerEvent3D) {
this.isTouching = true;
this._mouseCode = e.mouseCode;
this.collectEntities();
this.pick(this._colliderOut, this._view.camera);
this.pick(this._view.camera);
let target = this.findNearestObj(this._interestList, this._view.camera);
if (target) {
this._mouseMove.target = target.object3D;
Expand Down Expand Up @@ -186,8 +184,7 @@ export class PickFire extends CEventDispatcher {
private onTouchOnce(e: PointerEvent3D) {
this.isTouching = true;
this._mouseCode = e.mouseCode;
this.collectEntities();
this.pick(this._colliderOut, this._view.camera);
this.pick(this._view.camera);
let target = this.findNearestObj(this._interestList, this._view.camera);
if (target) {
let info = Engine3D.setting.pick.mode == `pixel` ? this.getPickInfo() : null;
Expand Down Expand Up @@ -221,17 +218,9 @@ export class PickFire extends CEventDispatcher {
return list[0]?.collider;
}

private _colliderOut: ColliderComponent[] = [];

private collectEntities(): ColliderComponent[] {
this._colliderOut.length = 0;
this._view.scene.getComponents(ColliderComponent, this._colliderOut);
return this._colliderOut;
}

private _interestList: HitInfo[] = [];

private pick(colliders: ColliderComponent[], camera: Camera3D) {
private pick(camera: Camera3D) {
this._interestList.length = 0;
if (Engine3D.setting.pick.mode == `pixel`) {
this._pickCompute.compute(this._view);
Expand All @@ -245,7 +234,9 @@ export class PickFire extends CEventDispatcher {
} else if (Engine3D.setting.pick.mode == `bound`) {
this.ray = camera.screenPointToRay(Engine3D.inputSystem.mouseX, Engine3D.inputSystem.mouseY);
let intersect: HitInfo;
for (const collider of colliders) {
let colliders = ComponentCollect.componentsEnablePickerList.get(this._view);;
for (const item of colliders) {
let collider = item[0];
if (collider.enable) {
intersect = collider.rayPick(this.ray);
if (intersect) {
Expand Down

0 comments on commit 23db052

Please sign in to comment.