diff --git a/samples/lights/Sample_AddRemovePointLight.ts b/samples/lights/Sample_AddRemovePointLight.ts new file mode 100644 index 00000000..c2f45519 --- /dev/null +++ b/samples/lights/Sample_AddRemovePointLight.ts @@ -0,0 +1,97 @@ +import { GUIHelp } from "@orillusion/debug/GUIHelp"; +import { AtmosphericComponent, BoxGeometry, CameraUtil, Color, Engine3D, HoverCameraController, LitMaterial, MeshRenderer, Object3D, Object3DUtil, PointLight, Scene3D, SphereGeometry, View3D, } from "@orillusion/core"; +import { PointLightsScript } from "./PointLightsScript"; + +class Sample_AddRemovePointLight { + scene: Scene3D; + hoverCameraController: HoverCameraController; + lightObj: any; + constructor() { } + + async run() { + + await Engine3D.init({}); + + GUIHelp.init(); + + this.scene = new Scene3D(); + this.scene.addComponent(AtmosphericComponent); + // init camera3D + let mainCamera = CameraUtil.createCamera3D(null, this.scene); + mainCamera.perspective(60, Engine3D.aspect, 1, 2000.0); + //set camera data + mainCamera.object3D.addComponent(HoverCameraController).setCamera(0, -25, 500); + + await this.initScene(this.scene); + + let view = new View3D(); + view.scene = this.scene; + view.camera = mainCamera; + + Engine3D.startRenderViews([view]); + + } + + initScene(scene: Scene3D) { + let lightObj3D = new Object3D(); + let render = lightObj3D.addComponent(MeshRenderer); + render.geometry = new SphereGeometry(5, 30, 30); + render.material = new LitMaterial(); + + scene.addChild(lightObj3D); + + + let cube = new BoxGeometry(10, 10, 10); + let mat = new LitMaterial(); + + // make 20 box + for (let i = 0; i < 20; i++) { + for (let j = 0; j < 10; j++) { + let box = new Object3D(); + let mr2 = box.addComponent(MeshRenderer); + mr2.geometry = cube; + mr2.material = mat; + scene.addChild(box); + + box.transform.x = i * 40 - 200; + box.transform.y = 5; + box.transform.z = j * 40 - 200; + } + } + + //create floor + let floor = Object3DUtil.GetSingleCube(2000, 1, 2000, 0.5, 0.5, 0.5); + this.scene.addChild(floor); + + let list: Object3D[] = []; + GUIHelp.addButton("addPointLight", () => { + for (let i = 0; i < 5; i++) { + let pointLight = new Object3D(); + let script = pointLight.addComponent(PointLight); + script.lightColor = Color.random(); + script.intensity = 6 * Math.random() + 3; + script.range = 25 * Math.random() + 15; + script.castShadow = true; + pointLight.x = Math.random() * 200 - 100; + pointLight.y = 5; + pointLight.z = Math.random() * 200 - 100; + scene.addChild(pointLight); + list.push(pointLight); + } + }); + + GUIHelp.addButton("removePointLight", () => { + for (let i = 0; i < Math.min(5, list.length); i++) { + let index = Math.floor(list.length * Math.random()); + let obj = list[index]; + if (obj) { + list.splice(index, 1) + scene.removeChild(obj) + obj.destroy(); + } + } + }); + } +} + +new Sample_AddRemovePointLight().run(); \ No newline at end of file diff --git a/samples/lights/Sample_DirectLightShadow.ts b/samples/lights/Sample_DirectLightShadow.ts index b05f275e..2743b7e8 100644 --- a/samples/lights/Sample_DirectLightShadow.ts +++ b/samples/lights/Sample_DirectLightShadow.ts @@ -4,73 +4,73 @@ import { GUIUtil } from "@samples/utils/GUIUtil"; //sample of direction light class Sample_DirectLightShadow { - scene: Scene3D; - - async run() { - Engine3D.setting.shadow.autoUpdate = true; - Engine3D.setting.shadow.shadowBias = 0.0001; - Engine3D.setting.shadow.shadowBound = 100; - - await Engine3D.init({}); - - GUIHelp.init(); - - this.scene = new Scene3D(); - this.scene.addComponent(AtmosphericComponent); - - // init camera3D - let mainCamera = CameraUtil.createCamera3D(null, this.scene); - mainCamera.perspective(60, Engine3D.aspect, 1, 5000.0); - //set camera data - mainCamera.object3D.z = -15; - mainCamera.object3D.addComponent(HoverCameraController).setCamera(-15, -35, 150); - - await this.initScene(); - - let view = new View3D(); - view.scene = this.scene; - view.camera = mainCamera; - - this.initLight(); - Engine3D.startRenderView(view); - - } - - // create direction light - private initLight() { - // add a direction light - let lightObj3D = new Object3D(); - let sunLight = lightObj3D.addComponent(DirectLight); - sunLight.intensity = 15; - sunLight.lightColor = KelvinUtil.color_temperature_to_rgb(6553); - sunLight.castShadow = true; - lightObj3D.rotationX = 53.2; - lightObj3D.rotationY = 220; - lightObj3D.rotationZ = 5.58; - - GUIUtil.renderDirLight(sunLight); - this.scene.addChild(lightObj3D); - - let obj = new Object3D(); - let mr = obj.addComponent(MeshRenderer); - mr.geometry = new BoxGeometry(20, 100, 20); - mr.material = new LitMaterial(); - this.scene.addChild(obj); - } - - - - initScene() { - let mat = new LitMaterial(); - mat.baseMap = Engine3D.res.grayTexture; - // mat.roughness = 0.4; - // mat.metallic = 0.6; - let floor = new Object3D(); - let render = floor.addComponent(MeshRenderer); - render.geometry = new BoxGeometry(200, 1, 200); - render.material = mat; - this.scene.addChild(floor); - } + scene: Scene3D; + + async run() { + Engine3D.setting.shadow.autoUpdate = true; + Engine3D.setting.shadow.shadowBias = 0.0001; + Engine3D.setting.shadow.shadowBound = 100; + + await Engine3D.init({}); + + GUIHelp.init(); + + this.scene = new Scene3D(); + this.scene.addComponent(AtmosphericComponent); + + // init camera3D + let mainCamera = CameraUtil.createCamera3D(null, this.scene); + mainCamera.perspective(60, Engine3D.aspect, 1, 5000.0); + //set camera data + mainCamera.object3D.z = -15; + mainCamera.object3D.addComponent(HoverCameraController).setCamera(-15, -35, 150); + + await this.initScene(); + + let view = new View3D(); + view.scene = this.scene; + view.camera = mainCamera; + + this.initLight(); + Engine3D.startRenderView(view); + + } + + // create direction light + private initLight() { + // add a direction light + let lightObj3D = new Object3D(); + let sunLight = lightObj3D.addComponent(DirectLight); + sunLight.intensity = 15; + sunLight.lightColor = KelvinUtil.color_temperature_to_rgb(6553); + sunLight.castShadow = true; + lightObj3D.rotationX = 53.2; + lightObj3D.rotationY = 220; + lightObj3D.rotationZ = 5.58; + + GUIUtil.renderDirLight(sunLight); + this.scene.addChild(lightObj3D); + + let obj = new Object3D(); + let mr = obj.addComponent(MeshRenderer); + mr.geometry = new BoxGeometry(20, 100, 20); + mr.material = new LitMaterial(); + this.scene.addChild(obj); + } + + + + initScene() { + let mat = new LitMaterial(); + mat.baseMap = Engine3D.res.grayTexture; + // mat.roughness = 0.4; + // mat.metallic = 0.6; + let floor = new Object3D(); + let render = floor.addComponent(MeshRenderer); + render.geometry = new BoxGeometry(200, 1, 200); + render.material = mat; + this.scene.addChild(floor); + } } new Sample_DirectLightShadow().run(); \ No newline at end of file diff --git a/src/gfx/graphics/webGpu/core/bindGroups/MatrixBindGroup.ts b/src/gfx/graphics/webGpu/core/bindGroups/MatrixBindGroup.ts index 21453b60..dbefda97 100644 --- a/src/gfx/graphics/webGpu/core/bindGroups/MatrixBindGroup.ts +++ b/src/gfx/graphics/webGpu/core/bindGroups/MatrixBindGroup.ts @@ -1,7 +1,5 @@ import { Matrix4 } from '../../../../../math/Matrix4'; import { UUID } from '../../../../../util/Global'; -import { Time } from '../../../../../util/Time'; -import { WebGPUDescriptorCreator } from '../../descriptor/WebGPUDescriptorCreator'; import { webGPUContext } from '../../Context3D'; import { StorageGPUBuffer } from '../buffer/StorageGPUBuffer'; /** diff --git a/src/gfx/graphics/webGpu/core/buffer/GPUBufferBase.ts b/src/gfx/graphics/webGpu/core/buffer/GPUBufferBase.ts index 356be8a1..154e9de5 100644 --- a/src/gfx/graphics/webGpu/core/buffer/GPUBufferBase.ts +++ b/src/gfx/graphics/webGpu/core/buffer/GPUBufferBase.ts @@ -352,7 +352,6 @@ export class GPUBufferBase { this.apply(); } - this.outFloat32Array = new Float32Array(size); } protected createBufferByStruct(usage: GPUBufferUsageFlags, struct: { new(): T }, count: number) { @@ -381,6 +380,8 @@ export class GPUBufferBase { private _readFlag: boolean = false; public readBuffer() { + this.outFloat32Array ||= new Float32Array(this.memory.shareDataBuffer.byteLength / 4); + if (!this._readBuffer) { this._readBuffer = webGPUContext.device.createBuffer({ size: this.memory.shareDataBuffer.byteLength,