Skip to content

Commit

Permalink
fix(light): remove light (#137)
Browse files Browse the repository at this point in the history
fix can not remove light
  • Loading branch information
ZenderJK committed May 12, 2023
1 parent dbecd95 commit da29404
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 95 deletions.
30 changes: 17 additions & 13 deletions samples/animation/Sample_Skeleton3.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { GUIHelp } from "@orillusion/debug/GUIHelp";
import { Object3D, Scene3D, Engine3D, AtmosphericComponent, CameraUtil, webGPUContext, HoverCameraController, View3D, SkeletonAnimationComponent, LitMaterial, MeshRenderer, BoxGeometry, DirectLight, KelvinUtil, Time, Object3DUtil } from "@orillusion/core";
import { Object3D, Scene3D, Engine3D, AtmosphericComponent, CameraUtil, webGPUContext, HoverCameraController, View3D, SkeletonAnimationComponent, LitMaterial, MeshRenderer, BoxGeometry, DirectLight, KelvinUtil, Time, Object3DUtil, BoundingBox, SkinnedMeshRenderer } from "@orillusion/core";
import { GUIUtil } from "@samples/utils/GUIUtil";

// Sample to use SkeletonAnimationComponent
class Sample_Skeleton3 {
lightObj3D: Object3D;
scene: Scene3D;
character: Object3D;
view: View3D;

async run() {
Engine3D.setting.shadow.autoUpdate = true;
Expand All @@ -30,24 +32,26 @@ class Sample_Skeleton3 {

await this.initScene(this.scene);

let view = new View3D();
view.scene = this.scene;
view.camera = mainCamera;
this.view = new View3D();
this.view.scene = this.scene;
this.view.camera = mainCamera;

Engine3D.startRenderView(view);
Engine3D.startRenderView(this.view);
}

async initScene(scene: Scene3D) {
{
let rootNode = await Engine3D.res.loadGltf('gltfs/glb/Soldier_draco.glb');
let character = rootNode.getObjectByName('Character') as Object3D;
character.scaleX = 0.3;
character.scaleY = 0.3;
character.scaleZ = 0.3;
character.rotationY = 180;
scene.addChild(character);

let animation = character.getComponentsInChild(SkeletonAnimationComponent)[0];
this.character = rootNode.getObjectByName('Character') as Object3D;
this.character.scaleX = 0.3;
this.character.scaleY = 0.3;
this.character.scaleZ = 0.3;
this.character.rotationY = 180;
scene.addChild(this.character);



let animation = this.character.getComponentsInChild(SkeletonAnimationComponent)[0];

const runClip = animation.getAnimationClip("Run");
runClip.addEvent("Begin", 0);
Expand Down
3 changes: 2 additions & 1 deletion src/Engine3D.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ShaderLib } from './assets/shader/ShaderLib';
import { ShaderUtil } from './gfx/graphics/webGpu/shader/util/ShaderUtil';
import { ComponentCollect } from './gfx/renderJob/collect/ComponentCollect';
import { ShadowLightsCollect } from './gfx/renderJob/collect/ShadowLightsCollect';
import { ProfilerUtil } from '.';

/**
* Orillusion 3D Engine
Expand Down Expand Up @@ -369,7 +370,7 @@ export class Engine3D {
Time.delta = time - Time.time;
Time.time = time;
Time.frame += 1;
// let camera = Camera3D.mainCamera;

Interpolator.tick(Time.delta);
if (this._beforeRender) this._beforeRender();

Expand Down
6 changes: 1 addition & 5 deletions src/assets/shader/cluster/ClusterBoundsSource_cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ export let ClusterBoundsSource_cs: string = /* wgsl */`
return result;
}
// @compute @workgroup_size(2,2,1)
@compute @workgroup_size(16,12,1)
@compute @workgroup_size(16,9,1)
fn CsMain( @builtin(workgroup_id) workgroup_id : vec3<u32> , @builtin(local_invocation_id) local_invocation_id : vec3<u32> ){
// let i = local_invocation_id.x ;
// let j = local_invocation_id.y ;
let i = local_invocation_id.x ;
let j = local_invocation_id.y ;
let k = workgroup_id.x ;
Expand Down
2 changes: 1 addition & 1 deletion src/assets/shader/cluster/ClusterLighting_cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn TestSphereAABB( lightIndex:i32 , clusterIndex : u32 ) -> bool
@compute @workgroup_size(16,12,1)
@compute @workgroup_size(16,9,1)
fn CsMain( @builtin(workgroup_id) workgroup_id : vec3<u32> , @builtin(local_invocation_id) local_invocation_id : vec3<u32> ){
clusterTileX = clustersUniform.clusterTileX;
clusterTileY = clustersUniform.clusterTileY;
Expand Down
2 changes: 1 addition & 1 deletion src/assets/shader/sky/CubeSky_Shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class CubeSky_Shader {
return ORI_VertexOut;
}
`

public static sky_fs_frag_wgsl: string = /* wgsl */ `
#include "GlobalUniform"
Expand Down
5 changes: 0 additions & 5 deletions src/components/renderer/MeshRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,4 @@ export class MeshRenderer extends RenderNode {
mr.rendererMask = this.rendererMask;
}

drawWireFrame() {
this.object3D.transform.worldPosition;
//view.graphic3D..drawMeshWireframe(`Wireframe_${this.object3D.uuid}`, this.geometry, this.object3D.transform);
}

}
2 changes: 1 addition & 1 deletion src/core/bound/Frustum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class Frustum {
obj.updateBound();

let r = Math.max(box.size.x, box.size.y, box.size.z);
let sr = r;
let sr = r * 2;
let scx = box.center.x;
let scy = box.center.y;
let scz = box.center.z;
Expand Down
7 changes: 4 additions & 3 deletions src/core/entities/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,17 @@ export class Entity extends CEventDispatcher {
this._bound = new BoundingBox(Vector3.ZERO.clone(), Vector3.ONE.clone());
}
for (const children of this.entityChildren) {
if (children._bound) {
this._bound.merge(children._bound);
}
// if (children._bound) {
this._bound.merge(children.genBounds());
// }
}
return this._bound;
}

public updateBound() {
if (!this._bound) {
this._bound = new BoundingBox(Vector3.ZERO.clone(), Vector3.ONE.clone());
// this.genBounds();
}
let worldMatrix = this.transform.worldMatrix;
worldMatrix.transformPoint(this._bound.min, this.bound.worldMin);
Expand Down
59 changes: 47 additions & 12 deletions src/core/geometry/GeometryBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export class GeometryBase {
public subGeometries: SubGeometry[] = [];
public morphTargetsRelative: boolean;
public morphTargetDictionary: { value: string; key: number };
public bounds: BoundingBox;
private _bounds: BoundingBox;

private _attributeMap: Map<string, VertexAttributeData>;
private _attributes: string[];
private _indicesBuffer: GeometryIndicesBuffer;
Expand All @@ -46,14 +47,7 @@ export class GeometryBase {
this._attributeMap = new Map<string, VertexAttributeData>();
this._attributes = [];

this.bounds = new BoundingBox(new Vector3(), new Vector3(1, 1, 1));
this.bounds.min.x = Number.MAX_VALUE;
this.bounds.min.y = Number.MAX_VALUE;
this.bounds.min.z = Number.MAX_VALUE;

this.bounds.max.x = -Number.MAX_VALUE;
this.bounds.max.y = -Number.MAX_VALUE;
this.bounds.max.z = -Number.MAX_VALUE;

this._vertexBuffer = new GeometryVertexBuffer();
}
Expand Down Expand Up @@ -81,10 +75,51 @@ export class GeometryBase {
this._vertexBuffer.geometryType = value;
}

/**
*/
public updateBounds(min: Vector3, max: Vector3) {
this.bounds.setFromMinMax(min, max);
public get bounds(): BoundingBox {
if (!this._bounds) {
this._bounds = new BoundingBox(new Vector3(), new Vector3(1, 1, 1));
this._bounds.min.x = Number.MAX_VALUE;
this._bounds.min.y = Number.MAX_VALUE;
this._bounds.min.z = Number.MAX_VALUE;

this._bounds.max.x = -Number.MAX_VALUE;
this._bounds.max.y = -Number.MAX_VALUE;
this._bounds.max.z = -Number.MAX_VALUE;

let attributes = this.getAttribute(VertexAttributeName.position);
if (attributes) {
for (let i = 0; i < attributes.data.length / 3; i++) {
const px = attributes.data[i * 3 + 0];
const py = attributes.data[i * 3 + 1];
const pz = attributes.data[i * 3 + 2];
if (this._bounds.min.x > px) {
this._bounds.min.x = px;
}
if (this._bounds.min.y > py) {
this._bounds.min.y = py;
}
if (this._bounds.min.z > pz) {
this._bounds.min.z = pz;
}

if (this._bounds.max.x < px) {
this._bounds.max.x = px;
}
if (this._bounds.max.y < py) {
this._bounds.max.y = py;
}
if (this._bounds.max.z < pz) {
this._bounds.max.z = pz;
}
}
}
this._bounds.setFromMinMax(this._bounds.min, this._bounds.max);
}
return this._bounds;
}

public set bounds(value: BoundingBox) {
this._bounds = value;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/gfx/graphics/webGpu/core/bindGroups/GlobalUniformGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export class GlobalUniformGroup {
this.uniformGPUBuffer.setInt32(`renderState_right`, Engine3D.setting.render.renderState_right);
this.uniformGPUBuffer.setFloat(`renderState_split`, Engine3D.setting.render.renderState_split);

let mouseX = Engine3D.inputSystem.mouseX * webGPUContext.pixelRatio * webGPUContext.super;
let mouseY = Engine3D.inputSystem.mouseY * webGPUContext.pixelRatio * webGPUContext.super;
let mouseX = Engine3D.inputSystem.mouseX * webGPUContext.pixelRatio;
let mouseY = Engine3D.inputSystem.mouseY * webGPUContext.pixelRatio;
this.uniformGPUBuffer.setFloat(`mouseX`, mouseX);
this.uniformGPUBuffer.setFloat(`mouseY`, mouseY);
this.uniformGPUBuffer.setFloat(`windowWidth`, webGPUContext.windowWidth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export class LightEntries {
}

public update(view: View3D) {
this.storageGPUBuffer.clean();

let lights = EntityCollect.instance.getLights(view.scene);
for (let i = 0; i < lights.length; i++) {
const light = lights[i].lightData;
Expand Down
5 changes: 5 additions & 0 deletions src/gfx/graphics/webGpu/core/buffer/GPUBufferBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ export class GPUBufferBase {
// this.seek += 1;
// }

public clean() {
let data = new Float32Array(this.memory.shareDataBuffer);
data.fill(0, 0, data.length);
}

public apply() {
webGPUContext.device.queue.writeBuffer(this.buffer, 0, this.memory.shareDataBuffer);//, this.memory.shareFloat32Array.byteOffset, this.memory.shareFloat32Array.byteLength);
}
Expand Down
9 changes: 4 additions & 5 deletions src/gfx/renderJob/jobs/RendererJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ export class RendererJob {
this.occlusionSystem.update(view.camera, view.scene);

this.clusterLightingRender.render(view, this.occlusionSystem);
if (this.shadowMapPassRenderer && Engine3D.setting.shadow.enable) {

if (this.shadowMapPassRenderer) {
this.shadowMapPassRenderer.render(view, this.occlusionSystem);
}

Expand All @@ -204,17 +205,15 @@ export class RendererJob {
}

if (this.depthPassRenderer) {
this.depthPassRenderer.beforeCompute(view, this.occlusionSystem);
this.depthPassRenderer.compute(view, this.occlusionSystem);
this.depthPassRenderer.render(view, this.occlusionSystem);
this.depthPassRenderer.lateCompute(view, this.occlusionSystem);
}

let passList = this.rendererMap.getAllPassRenderer();
for (let i = 0; i < passList.length; i++) {
const renderer = passList[i];
renderer.beforeCompute(view, this.occlusionSystem);
renderer.compute(view, this.occlusionSystem);
renderer.render(view, this.occlusionSystem, this.clusterLightingRender.clusterLightingBuffer);
renderer.lateCompute(view, this.occlusionSystem);
}

if (this.postRenderer && this.postRenderer.postList.length > 0) {
Expand Down
3 changes: 1 addition & 2 deletions src/gfx/renderJob/passRenderer/RendererBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ export class RendererBase extends CEventDispatcher {



public beforeCompute(view: View3D, occlusionSystem: OcclusionSystem) { }
public lateCompute(view: View3D, occlusionSystem: OcclusionSystem) { }
public compute(view: View3D, occlusionSystem: OcclusionSystem) { }

public render(view: View3D, occlusionSystem: OcclusionSystem, clusterLightingBuffer: ClusterLightingBuffer, maskTr: boolean = false) {
GPUContext.cleanCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ComputeGPUBuffer } from "../../../graphics/webGpu/core/buffer/ComputeGP
import { UniformGPUBuffer } from "../../../graphics/webGpu/core/buffer/UniformGPUBuffer";

export class ClusterLightingBuffer {

public clusterBuffer: ComputeGPUBuffer;
public lightAssignBuffer: ComputeGPUBuffer;
public assignTableBuffer: ComputeGPUBuffer;
Expand Down
43 changes: 15 additions & 28 deletions src/gfx/renderJob/passRenderer/cluster/ClusterLightingRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import { ClusterLighting_cs } from '../../../../assets/shader/cluster/ClusterLig
*/
export class ClusterLightingRender extends RendererBase {
public clusterTileX = 16;
public clusterTileY = 12;
public clusterTileZ = 24;
public clusterTileY = 9;
public clusterTileZ = 16;
public maxNumLights = 128;
public maxNumLightsPerCluster = 100;
public clusterPix = 1;
public clusterLightingBuffer: ClusterLightingBuffer;

private _currentLightCount = 0;
private _clusterGenerateCompute: ComputeShader;
private _clusterLightingCompute: ComputeShader;
constructor(view: View3D) {
Expand All @@ -34,7 +35,6 @@ export class ClusterLightingRender extends RendererBase {
}

private initCompute(view: View3D) {

this._clusterGenerateCompute = new ComputeShader(ClusterBoundsSource_cs);
this._clusterLightingCompute = new ComputeShader(ClusterLighting_cs);

Expand All @@ -61,38 +61,25 @@ export class ClusterLightingRender extends RendererBase {
this._clusterLightingCompute.setStorageBuffer(`lightBuffer`, lightBuffer.storageGPUBuffer);
this._clusterLightingCompute.setStorageBuffer(`lightAssignBuffer`, this.clusterLightingBuffer.lightAssignBuffer);
this._clusterLightingCompute.setStorageBuffer(`assignTable`, this.clusterLightingBuffer.assignTableBuffer);

this.debug(view);
}

render(view: View3D, occlusionSystem: OcclusionSystem) {
let camera = view.camera;
let scene = view.scene;
let near = camera.near;
let far = camera.far;

let lights: ILight[] = EntityCollect.instance.getLights(scene);
let size = webGPUContext.presentationSize;
// this.clustersUniformBuffer.setFloat('screenWidth', size[0] );
// this.clustersUniformBuffer.setFloat('screenHeight', size[1] );
this.clusterLightingBuffer.clustersUniformBuffer.setFloat('numLights', lights.length);
this.clusterLightingBuffer.clustersUniformBuffer.apply();

this._clusterGenerateCompute.workerSizeX = this.clusterTileZ;
this._clusterLightingCompute.workerSizeX = this.clusterTileZ;

let command = GPUContext.beginCommandEncoder();
// if(!this._createGrid){
// this._createGrid = true ;
GPUContext.computeCommand(command, [this._clusterGenerateCompute, this._clusterLightingCompute]);
// }else{
// GPUContext.compute_command(command,[this.clusterLightingCompute]);
// }
GPUContext.endCommandEncoder(command);
}
if (this._currentLightCount != lights.length) {
this._currentLightCount = lights.length;
this.clusterLightingBuffer.clustersUniformBuffer.setFloat('numLights', lights.length);
this.clusterLightingBuffer.clustersUniformBuffer.apply();

private _createGrid: boolean = false;
this._clusterGenerateCompute.workerSizeX = this.clusterTileZ;
this._clusterLightingCompute.workerSizeX = this.clusterTileZ;
}

private debug(view: View3D) {
if (lights.length > 0) {
let command = GPUContext.beginCommandEncoder();
GPUContext.computeCommand(command, [this._clusterGenerateCompute, this._clusterLightingCompute]);
GPUContext.endCommandEncoder(command);
}
}
}
1 change: 0 additions & 1 deletion src/gfx/renderJob/passRenderer/color/ColorPassRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class ColorPassRenderer extends RendererBase {
}

public render(view: View3D, occlusionSystem: OcclusionSystem, clusterLightingBuffer?: ClusterLightingBuffer, maskTr: boolean = false) {
// return ;
this.renderContext.clean();

let scene = view.scene;
Expand Down
Loading

0 comments on commit da29404

Please sign in to comment.