Skip to content

Commit

Permalink
fix(light): fix light enable (Orillusion#266)
Browse files Browse the repository at this point in the history
fix light enable
fix light cast shadow
  • Loading branch information
ZenderJK authored Jul 28, 2023
1 parent 6e51c74 commit 50429ea
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 35 deletions.
2 changes: 0 additions & 2 deletions samples/benchmark/Sample_drawCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ class Sample_drawCall {
obj.transform.rotationZ = Math.random() * 360;

this._rotList.push((Math.random() * 1 - 1 * 0.5) * 2.0 * Math.random() * 100);
obj.transform.rotatingY = 16 * 0.01 * this._rotList[i];

if (i % 10000 == 0) {
ii++;
Expand All @@ -111,7 +110,6 @@ class Sample_drawCall {

group.addComponent(InstanceDrawComponent);
this._rotList.push(1.0);
group.transform.rotatingY = 16 * 0.01 * 1;

group.bound = new BoundingBox(Vector3.SAFE_MIN, Vector3.SAFE_MAX);
this._list.push(group);
Expand Down
4 changes: 1 addition & 3 deletions samples/lights/Sample_AddRemovePointLight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class Sample_AddRemovePointLight {
view.camera = mainCamera;

Engine3D.startRenderViews([view]);
sky.relativeTransform = this.lightObj.transform;

}

initScene(scene: Scene3D) {
Expand Down Expand Up @@ -82,7 +80,7 @@ class Sample_AddRemovePointLight {
});

GUIHelp.addButton("removePointLight", () => {
for (let i = 0; i < Math.min(5, list.length); i++) {
for (let i = 0; i < Math.min(1, list.length); i++) {
let index = Math.floor(list.length * Math.random());
let obj = list[index];
if (obj) {
Expand Down
95 changes: 95 additions & 0 deletions samples/lights/Sample_LightEnable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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_LightEnable {
scene: Scene3D;
hoverCameraController: HoverCameraController;
lightObj: any;
constructor() { }

async run() {

await Engine3D.init({});

GUIHelp.init();

this.scene = new Scene3D();
let sky = 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);

for (let i = 0; i < 5; i++) {
let pointLight = new Object3D();
pointLight.name = "pointLight_" + i;
let script = pointLight.addComponent(PointLight);
script.lightColor = Color.random();
script.intensity = 6 * Math.random() + 3;
script.range = 45 * Math.random() + 80;
script.castShadow = true;
pointLight.x = i * 55 + 15;
pointLight.y = 5;
pointLight.z = 0;
scene.addChild(pointLight);

let obj = {};
obj[pointLight.name + ":enable"] = true;
GUIHelp.add(obj, pointLight.name + ":enable").onChange((e) => {
script.enable = e;
});

let obj2 = {};
obj2[pointLight.name + ":castShadow"] = true;
GUIHelp.add(obj2, pointLight.name + ":castShadow").onChange((e) => {
script.castShadow = e;
});
}


}
}

new Sample_LightEnable().run();
1 change: 1 addition & 0 deletions src/assets/shader/lighting/LightingFunction_frag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn pointLighting( albedo:vec3<f32>,WP:vec3<f32>, N:vec3<f32>, V:vec3<f32>, rough
if( light.castShadow >= 0 )
{
#if USE_SHADOWMAPING
// atten *= shadowStrut.pointShadows[light.castShadow] ;
for (var j: i32 = 0; j < 8; j += 1) {
if(j == light.castShadow){
atten *= shadowStrut.pointShadows[j] ;
Expand Down
22 changes: 12 additions & 10 deletions src/assets/shader/materials/program/ShadowMapping_frag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ export let ShadowMapping_frag: string = /*wgsl*/ `
pointShadowMapCompare(globalUniform.pointShadowBias);
}
const dirCount:i32 = 8 ;
const pointCount:i32 = 8 ;
fn directShadowMaping(shadowBias: f32) {
for (var i: i32 = 0; i < 8 ; i = i + 1) {
for (var i: i32 = 0; i < dirCount ; i = i + 1) {
if( i >= shadowBuffer.nDirShadowStart && i <= shadowBuffer.nDirShadowEnd ){
let ldx = shadowBuffer.shadowLights[i];
var light = lightBuffer[ldx];
Expand Down Expand Up @@ -72,17 +75,12 @@ export let ShadowMapping_frag: string = /*wgsl*/ `
fn pointShadowMapCompare(shadowBias: f32){
let worldPos = ORI_VertexVarying.vWorldPos.xyz;
let offset = 0.1;
// let lightIndex = getCluster(ORI_VertexVarying.fragCoord);
// let start = max(lightIndex.start, 0.0);
// let count = max(lightIndex.count, 0.0);
// let end = max(start + count, 0.0);
for (var i: i32 = 0; i < 8 ; i = i + 1) {
if( i >= shadowBuffer.nPointShadowStart && i <= shadowBuffer.nPointShadowEnd ){
for (var i: i32 = 0; i < pointCount ; i = i + 1) {
if( i >= shadowBuffer.nPointShadowStart && i < shadowBuffer.nPointShadowEnd ){
let ldx = shadowBuffer.shadowLights[i];
let light = lightBuffer[ldx] ;
shadowStrut.pointShadows[i] = 1.0;
#if USE_SHADOWMAPING
let lightPos = light.position.xyz;
var shadow = 0.0;
Expand Down Expand Up @@ -131,7 +129,11 @@ export let ShadowMapping_frag: string = /*wgsl*/ `
}
#endif
shadowStrut.pointShadows[i] = 1.0 - shadow;
for (var j = 0; j < pointCount ; j+=1 ) {
if(i32(light.castShadow) == j){
shadowStrut.pointShadows[j] = 1.0 - shadow ;
}
}
#endif
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/components/Transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ export class Transform extends ComponentBase {
private _down: Vector3 = new Vector3();
public readonly _worldMatrix: Matrix4;

public rotatingX: number = 0;
public rotatingY: number = 0;
public rotatingZ: number = 0;

private _targetPos: Vector3;
public static: boolean = false;

Expand Down Expand Up @@ -368,7 +364,6 @@ export class Transform extends ComponentBase {
append(this._worldMatrix, this.parent.worldMatrix, this._worldMatrix);
// WasmMatrix4.makeMatrix44Append(this._localRot, this._localPos, this.localScale, this._worldMatrix, this._worldMatrix, this.parent.worldMatrix, this._worldMatrix);
} else {
this._localRot.y += this.rotatingY;
makeMatrix44(this._localRot, this._localPos, this.localScale, this._worldMatrix);
// WasmMatrix4.makeMatrix44(this._localRot, this._localPos, this.localScale, this._worldMatrix);
}
Expand Down
4 changes: 4 additions & 0 deletions src/components/lights/LightBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class LightBase extends ComponentBase implements ILight {
if (this._castGI) {
EntityCollect.instance.state.giLightingChange = true;
}

if (this._castShadow) {
this.needUpdateShadow = true;
ShadowLightsCollect.addShadowLight(this);
Expand Down Expand Up @@ -110,6 +111,7 @@ export class LightBase extends ComponentBase implements ILight {
public onDisable(): void {
this.onChange();
EntityCollect.instance.removeLight(this.transform.scene3D, this);
ShadowLightsCollect.removeShadowLight(this);
}

public set iesProfiles(iesProfiles: IESProfiles) {
Expand Down Expand Up @@ -251,6 +253,8 @@ export class LightBase extends ComponentBase implements ILight {

public destroy(force?: boolean): void {
this.bindOnChange = null;
EntityCollect.instance.removeLight(this.transform.scene3D, this);
ShadowLightsCollect.removeShadowLight(this);
this.transform.eventDispatcher.removeEventListener(Transform.ROTATION_ONCHANGE, this.onRotChange, this);
this.transform.eventDispatcher.removeEventListener(Transform.SCALE_ONCHANGE, this.onScaleChange, this);
super.destroy(force);
Expand Down
23 changes: 13 additions & 10 deletions src/gfx/renderJob/collect/ShadowLightsCollect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,27 +133,24 @@ export class ShadowLightsCollect {
50000,
);
}
let has = list.indexOf(light) == -1;
if (has) {
if (list.length < 8) {
light.lightData.castShadowIndex = list.length;
}
if (list.indexOf(light) == -1) {
list.push(light);
}
return list;
} else if (light.lightData.lightType == LightType.PointLight || light.lightData.lightType == LightType.SpotLight) {
let list = this.pointLightList.get(scene);
if (list && list.length >= 8) {
return list;
}
if (!list) {
list = [];
this.pointLightList.set(scene, list);
}
let has = list.indexOf(light) == -1;
if (has) {
if (list.length < 8) {
light.lightData.castShadowIndex = list.length;
}
if (list.indexOf(light) == -1) {
list.push(light);
}


return list;
}
}
Expand All @@ -168,6 +165,7 @@ export class ShadowLightsCollect {
list.splice(index, 1);
}
}
light.lightData.castShadowIndex = -1;
return list;
} else if (light.lightData.lightType == LightType.PointLight || light.lightData.lightType == LightType.SpotLight) {
let list = this.pointLightList.get(light.transform.view3D.scene);
Expand All @@ -177,6 +175,7 @@ export class ShadowLightsCollect {
list.splice(index, 1);
}
}
light.lightData.castShadowIndex = -1;
return list;
}
}
Expand All @@ -194,9 +193,11 @@ export class ShadowLightsCollect {
let nPointShadowEnd: number = 0;
shadowLights.fill(0);
if (directionLightList) {
let j = 0;
for (let i = 0; i < directionLightList.length; i++) {
const light = directionLightList[i];
shadowLights[i] = light.lightData.index;
light.lightData.castShadowIndex = j++;
}
nDirShadowEnd = directionLightList.length;
}
Expand All @@ -205,9 +206,11 @@ export class ShadowLightsCollect {

if (pointLightList) {
nPointShadowStart = nDirShadowEnd;
let j = 0;
for (let i = nPointShadowStart; i < pointLightList.length; i++) {
const light = pointLightList[i];
shadowLights[i] = light.lightData.index;
light.lightData.castShadowIndex = j++;
}
nPointShadowEnd = nPointShadowStart + pointLightList.length;
}
Expand Down
10 changes: 5 additions & 5 deletions src/gfx/renderJob/passRenderer/cluster/ClusterLightingRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ export class ClusterLightingRender extends RendererBase {
this._clusterLightingCompute.workerSizeX = this.clusterTileZ;
}

if (lights.length > 0) {
let command = GPUContext.beginCommandEncoder();
GPUContext.computeCommand(command, [this._clusterGenerateCompute, this._clusterLightingCompute]);
GPUContext.endCommandEncoder(command);
}
// if (lights.length > 0) {
let command = GPUContext.beginCommandEncoder();
GPUContext.computeCommand(command, [this._clusterGenerateCompute, this._clusterLightingCompute]);
GPUContext.endCommandEncoder(command);
// }
}
}

0 comments on commit 50429ea

Please sign in to comment.