diff --git a/samples/animation/Sample_Skeleton3.ts b/samples/animation/Sample_Skeleton3.ts index 4321b1f9..3beca6c6 100644 --- a/samples/animation/Sample_Skeleton3.ts +++ b/samples/animation/Sample_Skeleton3.ts @@ -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; @@ -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); diff --git a/src/Engine3D.ts b/src/Engine3D.ts index 85d92e08..c4bf1a4c 100644 --- a/src/Engine3D.ts +++ b/src/Engine3D.ts @@ -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 @@ -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(); diff --git a/src/assets/shader/cluster/ClusterBoundsSource_cs.ts b/src/assets/shader/cluster/ClusterBoundsSource_cs.ts index b54d144f..f5c24da9 100644 --- a/src/assets/shader/cluster/ClusterBoundsSource_cs.ts +++ b/src/assets/shader/cluster/ClusterBoundsSource_cs.ts @@ -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 , @builtin(local_invocation_id) local_invocation_id : vec3 ){ - // 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 ; diff --git a/src/assets/shader/cluster/ClusterLighting_cs.ts b/src/assets/shader/cluster/ClusterLighting_cs.ts index 6e43afe0..b96d8b93 100644 --- a/src/assets/shader/cluster/ClusterLighting_cs.ts +++ b/src/assets/shader/cluster/ClusterLighting_cs.ts @@ -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 , @builtin(local_invocation_id) local_invocation_id : vec3 ){ clusterTileX = clustersUniform.clusterTileX; clusterTileY = clustersUniform.clusterTileY; diff --git a/src/assets/shader/sky/CubeSky_Shader.ts b/src/assets/shader/sky/CubeSky_Shader.ts index dca45731..ce51b86f 100644 --- a/src/assets/shader/sky/CubeSky_Shader.ts +++ b/src/assets/shader/sky/CubeSky_Shader.ts @@ -40,7 +40,7 @@ export class CubeSky_Shader { return ORI_VertexOut; } ` - + public static sky_fs_frag_wgsl: string = /* wgsl */ ` #include "GlobalUniform" diff --git a/src/components/renderer/MeshRenderer.ts b/src/components/renderer/MeshRenderer.ts index 1a44499c..80ef70b0 100644 --- a/src/components/renderer/MeshRenderer.ts +++ b/src/components/renderer/MeshRenderer.ts @@ -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); - } - } diff --git a/src/core/bound/Frustum.ts b/src/core/bound/Frustum.ts index fa799941..b7fd4f8e 100644 --- a/src/core/bound/Frustum.ts +++ b/src/core/bound/Frustum.ts @@ -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; diff --git a/src/core/entities/Entity.ts b/src/core/entities/Entity.ts index d8191f11..e696ca77 100644 --- a/src/core/entities/Entity.ts +++ b/src/core/entities/Entity.ts @@ -335,9 +335,9 @@ 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; } @@ -345,6 +345,7 @@ export class Entity extends CEventDispatcher { 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); diff --git a/src/core/geometry/GeometryBase.ts b/src/core/geometry/GeometryBase.ts index a942a945..b40b2ad8 100644 --- a/src/core/geometry/GeometryBase.ts +++ b/src/core/geometry/GeometryBase.ts @@ -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; private _attributes: string[]; private _indicesBuffer: GeometryIndicesBuffer; @@ -46,14 +47,7 @@ export class GeometryBase { this._attributeMap = new Map(); 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(); } @@ -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; } /** diff --git a/src/gfx/graphics/webGpu/core/bindGroups/GlobalUniformGroup.ts b/src/gfx/graphics/webGpu/core/bindGroups/GlobalUniformGroup.ts index bab2d9b3..e890e1c5 100644 --- a/src/gfx/graphics/webGpu/core/bindGroups/GlobalUniformGroup.ts +++ b/src/gfx/graphics/webGpu/core/bindGroups/GlobalUniformGroup.ts @@ -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); diff --git a/src/gfx/graphics/webGpu/core/bindGroups/groups/LightEntries.ts b/src/gfx/graphics/webGpu/core/bindGroups/groups/LightEntries.ts index 1420d90c..71465e04 100644 --- a/src/gfx/graphics/webGpu/core/bindGroups/groups/LightEntries.ts +++ b/src/gfx/graphics/webGpu/core/bindGroups/groups/LightEntries.ts @@ -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; diff --git a/src/gfx/graphics/webGpu/core/buffer/GPUBufferBase.ts b/src/gfx/graphics/webGpu/core/buffer/GPUBufferBase.ts index bd5b125e..b169da0f 100644 --- a/src/gfx/graphics/webGpu/core/buffer/GPUBufferBase.ts +++ b/src/gfx/graphics/webGpu/core/buffer/GPUBufferBase.ts @@ -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); } diff --git a/src/gfx/renderJob/jobs/RendererJob.ts b/src/gfx/renderJob/jobs/RendererJob.ts index db6056bb..28375f1b 100644 --- a/src/gfx/renderJob/jobs/RendererJob.ts +++ b/src/gfx/renderJob/jobs/RendererJob.ts @@ -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); } @@ -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) { diff --git a/src/gfx/renderJob/passRenderer/RendererBase.ts b/src/gfx/renderJob/passRenderer/RendererBase.ts index 7190bc54..e4ecc134 100644 --- a/src/gfx/renderJob/passRenderer/RendererBase.ts +++ b/src/gfx/renderJob/passRenderer/RendererBase.ts @@ -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(); diff --git a/src/gfx/renderJob/passRenderer/cluster/ClusterLightingBuffer.ts b/src/gfx/renderJob/passRenderer/cluster/ClusterLightingBuffer.ts index a59c258f..d69877d1 100644 --- a/src/gfx/renderJob/passRenderer/cluster/ClusterLightingBuffer.ts +++ b/src/gfx/renderJob/passRenderer/cluster/ClusterLightingBuffer.ts @@ -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; diff --git a/src/gfx/renderJob/passRenderer/cluster/ClusterLightingRender.ts b/src/gfx/renderJob/passRenderer/cluster/ClusterLightingRender.ts index 81902203..32f2c745 100644 --- a/src/gfx/renderJob/passRenderer/cluster/ClusterLightingRender.ts +++ b/src/gfx/renderJob/passRenderer/cluster/ClusterLightingRender.ts @@ -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) { @@ -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); @@ -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); + } } } diff --git a/src/gfx/renderJob/passRenderer/color/ColorPassRenderer.ts b/src/gfx/renderJob/passRenderer/color/ColorPassRenderer.ts index b2e58ddb..abdbf6c1 100644 --- a/src/gfx/renderJob/passRenderer/color/ColorPassRenderer.ts +++ b/src/gfx/renderJob/passRenderer/color/ColorPassRenderer.ts @@ -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; diff --git a/src/gfx/renderJob/passRenderer/preDepth/PreDepthPassRenderer.ts b/src/gfx/renderJob/passRenderer/preDepth/PreDepthPassRenderer.ts index b5843574..0532acaa 100644 --- a/src/gfx/renderJob/passRenderer/preDepth/PreDepthPassRenderer.ts +++ b/src/gfx/renderJob/passRenderer/preDepth/PreDepthPassRenderer.ts @@ -45,14 +45,8 @@ export class PreDepthPassRenderer extends RendererBase { this.setRenderStates(rtFrame); } - public lateCompute(view: View3D, occlusionSystem: OcclusionSystem) { - // this.zCullingCompute.compute(scene, occlusionSystem); - } - render(view: View3D, occlusionSystem: OcclusionSystem) { return; - - // ProfilerUtil.print( "DepthPass Renderer" ); } diff --git a/src/gfx/renderJob/passRenderer/shadow/PointLightShadowRenderer.ts b/src/gfx/renderJob/passRenderer/shadow/PointLightShadowRenderer.ts index af9683cb..f6bc8296 100644 --- a/src/gfx/renderJob/passRenderer/shadow/PointLightShadowRenderer.ts +++ b/src/gfx/renderJob/passRenderer/shadow/PointLightShadowRenderer.ts @@ -164,7 +164,6 @@ export class PointLightShadowRenderer extends RendererBase { ); } GPUContext.endCommandEncoder(qCommand); - // Camera3D.mainCamera.cubeShadowCameras[li] = cubeShadowMapInfo.cubeCamera; li++; } } @@ -213,7 +212,6 @@ export class PointLightShadowRenderer extends RendererBase { let worldMatrix = renderNode.object3D.transform._worldMatrix; for (let i = 0; i < passes.length; i++) { const renderShader = passes[i].renderShader; - // const renderShader = RenderShader.getShader(passes[i].shaderID); renderShader.setUniformFloat("cameraFar", shadowCamera.far); renderShader.setUniformVector3("lightWorldPos", shadowCamera.transform.worldPosition); diff --git a/src/gfx/renderJob/passRenderer/shadow/ShadowMapPassRenderer.ts b/src/gfx/renderJob/passRenderer/shadow/ShadowMapPassRenderer.ts index 6a37b927..837f79b2 100644 --- a/src/gfx/renderJob/passRenderer/shadow/ShadowMapPassRenderer.ts +++ b/src/gfx/renderJob/passRenderer/shadow/ShadowMapPassRenderer.ts @@ -63,22 +63,18 @@ export class ShadowMapPassRenderer extends RendererBase { render(view: View3D, occlusionSystem: OcclusionSystem) { - // return ; if (!Engine3D.setting.shadow.enable) return; let camera = view.camera; let scene = view.scene; - - // return ; this.shadowPassCount = 0; - // return ; if (!Engine3D.setting.shadow.needUpdate) return; if (!(Time.frame % Engine3D.setting.shadow.updateFrameRate == 0)) return; - // return ;// + camera.transform.updateWorldMatrix(); //*********************/ //***shadow light******/ @@ -135,7 +131,7 @@ export class ShadowMapPassRenderer extends RendererBase { this._forceUpdate = false; } - public beforeCompute() { + public compute() { }