-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrade ts-morph. move common Shadow Generator to common class - add …
…contribution to dennemark!
- Loading branch information
Showing
8 changed files
with
146 additions
and
18,767 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/customHosts/BaseShadowGeneratorLifecycleListener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { CreatedInstance } from "../CreatedInstance" | ||
import { Scene, AbstractMesh, Observer, Nullable, DirectionalLight, ShadowGenerator } from "@babylonjs/core" | ||
import DeferredCreationLifecycleListener from "./DeferredCreationLifecycleListener" | ||
|
||
/** | ||
* Create a Shadow Generator (CascadedShadowGenerator extends ShadowGenerator, so add/remove shadow casters is from parent class) | ||
*/ | ||
export default abstract class BaseShadowGeneratorLifecycleListener<T extends ShadowGenerator> extends DeferredCreationLifecycleListener<T> { | ||
|
||
private onMeshAddedObservable: Nullable<Observer<AbstractMesh>> = null; | ||
private onMeshRemovedObservable: Nullable<Observer<AbstractMesh>> = null; | ||
|
||
constructor(scene: Scene, props: any) { | ||
super(scene, props); | ||
} | ||
|
||
abstract createShadowGenerator: (mapSize: number, light: DirectionalLight, useFullFloatFirst?: boolean) => T; | ||
|
||
abstract get generatorType(): string; | ||
|
||
createInstance = (instance: CreatedInstance<T>, scene: Scene, props: any) : Nullable<T> => { | ||
let tmp: CreatedInstance<any> | null = instance.parent; | ||
let result: Nullable<T> = null; | ||
|
||
while (tmp !== null) { | ||
if (tmp.metadata.isShadowLight) { | ||
// console.log(`Creating ${this.generatorType} size: ${props.mapSize} with light`, tmp.hostInstance); | ||
instance.hostInstance = result = this.createShadowGenerator(props.mapSize, tmp.hostInstance, props.useFullFloatFirst); | ||
break | ||
} | ||
tmp = tmp.parent | ||
} | ||
|
||
if (instance.hostInstance === undefined) { | ||
console.error(`${this.generatorType} has no light source.`); | ||
return null; | ||
} | ||
|
||
if (instance.customProps.shadowCasters) { | ||
if (!Array.isArray(instance.customProps.shadowCasters)) { | ||
console.error("Shadow casters must be an array (of strings).", instance.customProps.shadowCasters); | ||
return null; | ||
} | ||
|
||
let shadowCasters: string[] = instance.customProps.shadowCasters; | ||
|
||
// TODO: also need a listener for models or if we want to add a predicate: | ||
this.onMeshAddedObservable = scene.onNewMeshAddedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCasters.indexOf(mesh.name) >= 0) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
|
||
this.onMeshRemovedObservable = scene.onMeshRemovedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCasters.indexOf(mesh.name) >= 0) { | ||
instance.hostInstance!.removeShadowCaster(mesh); | ||
} | ||
}); | ||
|
||
scene.meshes.forEach((mesh: AbstractMesh) => { | ||
if (shadowCasters.indexOf(mesh.name) >= 0) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
} else if (instance.customProps.shadowCastersExcluding) { | ||
if (!Array.isArray(instance.customProps.shadowCastersExcluding)) { | ||
console.error("Shadow casters excluding must be an array (of strings).", instance.customProps.shadowCastersExcluding); | ||
} else { | ||
let shadowCastersExcluding: string[] = instance.customProps.shadowCastersExcluding; | ||
|
||
// TODO: also need a listener for models or if we want to add a predicate: | ||
this.onMeshAddedObservable = scene.onNewMeshAddedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCastersExcluding.indexOf(mesh.name) === -1) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
|
||
this.onMeshRemovedObservable = scene.onMeshRemovedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCastersExcluding.indexOf(mesh.name) === -1) { | ||
instance.hostInstance!.removeShadowCaster(mesh); | ||
} | ||
}); | ||
|
||
scene.meshes.forEach((mesh: AbstractMesh) => { | ||
if (shadowCastersExcluding.indexOf(mesh.name) === -1) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
onParented(parent: CreatedInstance<any>, child: CreatedInstance<any>): any {/* empty */} | ||
|
||
onChildAdded(child: CreatedInstance<any>, parent: CreatedInstance<any>): any {/* empty */} | ||
|
||
onUnmount(): void { | ||
if (this.onMeshAddedObservable !== null) { | ||
this.scene.onNewMeshAddedObservable.remove(this.onMeshAddedObservable); | ||
this.onMeshAddedObservable = null; | ||
} | ||
|
||
if (this.onMeshRemovedObservable !== null) { | ||
this.scene.onMeshRemovedObservable.remove(this.onMeshRemovedObservable); | ||
this.onMeshRemovedObservable = null; | ||
} | ||
} | ||
} |
109 changes: 9 additions & 100 deletions
109
src/customHosts/CascadedShadowGeneratorLifecycleListener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,12 @@ | ||
import { CreatedInstance } from "../CreatedInstance" | ||
import { CascadedShadowGenerator, Scene, AbstractMesh, Observer, Nullable } from "@babylonjs/core" | ||
import DeferredCreationLifecycleListener from "./DeferredCreationLifecycleListener" | ||
import { CascadedShadowGenerator, DirectionalLight } from "@babylonjs/core"; | ||
import BaseShadowGeneratorLifecycleListener from "./BaseShadowGeneratorLifecycleListener"; | ||
|
||
export default class CascadedShadowGeneratorLifecycleListener extends DeferredCreationLifecycleListener<CascadedShadowGenerator> { | ||
|
||
private onMeshAddedObservable: Nullable<Observer<AbstractMesh>> = null; | ||
private onMeshRemovedObservable: Nullable<Observer<AbstractMesh>> = null; | ||
export default class CascadedShadowGeneratorLifecycleListener extends BaseShadowGeneratorLifecycleListener<CascadedShadowGenerator> { | ||
createShadowGenerator = (mapSize: number, light: DirectionalLight, useFullFloatFirst?: boolean) => { | ||
return new CascadedShadowGenerator(mapSize, light, useFullFloatFirst); | ||
}; | ||
|
||
constructor(scene: Scene, props: any) { | ||
super(scene, props) | ||
get generatorType(): string { | ||
return "CascadedShadowGenerator"; | ||
} | ||
|
||
createInstance = (instance: CreatedInstance<CascadedShadowGenerator>, scene: Scene, props: any) : Nullable<CascadedShadowGenerator> => { | ||
let tmp: CreatedInstance<any> | null = instance.parent | ||
let result: Nullable<CascadedShadowGenerator> = null; | ||
|
||
while (tmp !== null) { | ||
if (tmp.metadata.isShadowLight) { | ||
// console.log("Creating CascadedShadowGenerator. size:", this.props.mapSize, "light", tmp.hostInstance) | ||
instance.hostInstance = result = new CascadedShadowGenerator(props.mapSize, tmp.hostInstance, props.useFullFloatFirst) | ||
break | ||
} | ||
tmp = tmp.parent | ||
} | ||
|
||
if (instance.hostInstance === undefined) { | ||
console.error("CascadedShadowGenerator has no light source."); | ||
return null; | ||
} | ||
|
||
if (instance.customProps.shadowCasters) { | ||
if (!Array.isArray(instance.customProps.shadowCasters)) { | ||
console.error("Shadow casters must be an array (of strings).", instance.customProps.shadowCasters); | ||
return null; | ||
} | ||
|
||
let shadowCasters: string[] = instance.customProps.shadowCasters; | ||
|
||
// TODO: also need a listener for models or if we want to add a predicate: | ||
this.onMeshAddedObservable = scene.onNewMeshAddedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCasters.indexOf(mesh.name) >= 0) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
|
||
this.onMeshRemovedObservable = scene.onMeshRemovedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCasters.indexOf(mesh.name) >= 0) { | ||
instance.hostInstance!.removeShadowCaster(mesh); | ||
} | ||
}); | ||
|
||
scene.meshes.forEach((mesh: AbstractMesh) => { | ||
if (shadowCasters.indexOf(mesh.name) >= 0) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
} else if (instance.customProps.shadowCastersExcluding) { | ||
if (!Array.isArray(instance.customProps.shadowCastersExcluding)) { | ||
console.error("Shadow casters excluding must be an array (of strings).", instance.customProps.shadowCastersExcluding); | ||
} else { | ||
let shadowCastersExcluding: string[] = instance.customProps.shadowCastersExcluding; | ||
|
||
// TODO: also need a listener for models or if we want to add a predicate: | ||
this.onMeshAddedObservable = scene.onNewMeshAddedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCastersExcluding.indexOf(mesh.name) === -1) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
|
||
this.onMeshRemovedObservable = scene.onMeshRemovedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCastersExcluding.indexOf(mesh.name) === -1) { | ||
instance.hostInstance!.removeShadowCaster(mesh); | ||
} | ||
}); | ||
|
||
scene.meshes.forEach((mesh: AbstractMesh) => { | ||
if (shadowCastersExcluding.indexOf(mesh.name) === -1) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
onParented(parent: CreatedInstance<any>, child: CreatedInstance<any>): any {/* empty */} | ||
|
||
onChildAdded(child: CreatedInstance<any>, parent: CreatedInstance<any>): any {/* empty */} | ||
|
||
onUnmount(): void { | ||
if (this.onMeshAddedObservable !== null) { | ||
this.scene.onNewMeshAddedObservable.remove(this.onMeshAddedObservable); | ||
this.onMeshAddedObservable = null; | ||
} | ||
|
||
if (this.onMeshRemovedObservable !== null) { | ||
this.scene.onMeshRemovedObservable.remove(this.onMeshRemovedObservable); | ||
this.onMeshRemovedObservable = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,12 @@ | ||
import { CreatedInstance } from "../CreatedInstance" | ||
import { ShadowGenerator, Scene, AbstractMesh, Observer, Nullable } from "@babylonjs/core" | ||
import DeferredCreationLifecycleListener from "./DeferredCreationLifecycleListener" | ||
import { ShadowGenerator, DirectionalLight } from "@babylonjs/core" | ||
import BaseShadowGeneratorLifecycleListener from "./BaseShadowGeneratorLifecycleListener"; | ||
|
||
export default class ShadowGeneratorLifecycleListener extends DeferredCreationLifecycleListener<ShadowGenerator> { | ||
|
||
private onMeshAddedObservable: Nullable<Observer<AbstractMesh>> = null; | ||
private onMeshRemovedObservable: Nullable<Observer<AbstractMesh>> = null; | ||
export default class ShadowGeneratorLifecycleListener extends BaseShadowGeneratorLifecycleListener<ShadowGenerator> { | ||
createShadowGenerator = (mapSize: number, light: DirectionalLight, useFullFloatFirst?: boolean) => { | ||
return new ShadowGenerator(mapSize, light, useFullFloatFirst); | ||
}; | ||
|
||
constructor(scene: Scene, props: any) { | ||
super(scene, props) | ||
get generatorType(): string { | ||
return "ShadowGenerator"; | ||
} | ||
|
||
createInstance = (instance: CreatedInstance<ShadowGenerator>, scene: Scene, props: any) : Nullable<ShadowGenerator> => { | ||
let tmp: CreatedInstance<any> | null = instance.parent | ||
let result: Nullable<ShadowGenerator> = null; | ||
|
||
while (tmp !== null) { | ||
if (tmp.metadata.isShadowLight) { | ||
// console.log("Creating ShadowGenerator. size:", this.props.mapSize, "light", tmp.hostInstance) | ||
instance.hostInstance = result = new ShadowGenerator(props.mapSize, tmp.hostInstance, props.useFullFloatFirst) | ||
break | ||
} | ||
tmp = tmp.parent | ||
} | ||
|
||
if (instance.hostInstance === undefined) { | ||
console.error("ShadowGenerator has no light source."); | ||
return null; | ||
} | ||
|
||
if (instance.customProps.shadowCasters) { | ||
if (!Array.isArray(instance.customProps.shadowCasters)) { | ||
console.error("Shadow casters must be an array (of strings).", instance.customProps.shadowCasters); | ||
return null; | ||
} | ||
|
||
let shadowCasters: string[] = instance.customProps.shadowCasters; | ||
|
||
// TODO: also need a listener for models or if we want to add a predicate: | ||
this.onMeshAddedObservable = scene.onNewMeshAddedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCasters.indexOf(mesh.name) >= 0) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
|
||
this.onMeshRemovedObservable = scene.onMeshRemovedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCasters.indexOf(mesh.name) >= 0) { | ||
instance.hostInstance!.removeShadowCaster(mesh); | ||
} | ||
}); | ||
|
||
scene.meshes.forEach((mesh: AbstractMesh) => { | ||
if (shadowCasters.indexOf(mesh.name) >= 0) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
} else if (instance.customProps.shadowCastersExcluding) { | ||
if (!Array.isArray(instance.customProps.shadowCastersExcluding)) { | ||
console.error("Shadow casters excluding must be an array (of strings).", instance.customProps.shadowCastersExcluding); | ||
} else { | ||
let shadowCastersExcluding: string[] = instance.customProps.shadowCastersExcluding; | ||
|
||
// TODO: also need a listener for models or if we want to add a predicate: | ||
this.onMeshAddedObservable = scene.onNewMeshAddedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCastersExcluding.indexOf(mesh.name) === -1) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
|
||
this.onMeshRemovedObservable = scene.onMeshRemovedObservable.add((mesh: AbstractMesh) => { | ||
if (shadowCastersExcluding.indexOf(mesh.name) === -1) { | ||
instance.hostInstance!.removeShadowCaster(mesh); | ||
} | ||
}); | ||
|
||
scene.meshes.forEach((mesh: AbstractMesh) => { | ||
if (shadowCastersExcluding.indexOf(mesh.name) === -1) { | ||
instance.hostInstance!.addShadowCaster(mesh); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
onParented(parent: CreatedInstance<any>, child: CreatedInstance<any>): any {/* empty */} | ||
|
||
onChildAdded(child: CreatedInstance<any>, parent: CreatedInstance<any>): any {/* empty */} | ||
|
||
onUnmount(): void { | ||
if (this.onMeshAddedObservable !== null) { | ||
this.scene.onNewMeshAddedObservable.remove(this.onMeshAddedObservable); | ||
this.onMeshAddedObservable = null; | ||
} | ||
|
||
if (this.onMeshRemovedObservable !== null) { | ||
this.scene.onMeshRemovedObservable.remove(this.onMeshRemovedObservable); | ||
this.onMeshRemovedObservable = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.