Skip to content

Commit

Permalink
upgrade ts-morph. move common Shadow Generator to common class - add …
Browse files Browse the repository at this point in the history
…contribution to dennemark!
  • Loading branch information
brianzinn committed Sep 2, 2020
1 parent d691de3 commit 9a92c0a
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 18,767 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ With declarative (TSX/JSX) coding and HMR, you experience the same development w

8. **Behaviors** - autoRotationBehavior, bouncingBehavior, framingBehavior, attachToBoxBehavior, fadeInOutBehavior, multiPointerScaleBehavior, pointerDragBehavior, sixDofDragBehavior

9. **Others** - environmentHelper, physicsImpostor, shadowGenerator, vrExperienceHelper
9. **Others** - environmentHelper, physicsImpostor, shadowGenerator, cascadedShadowGenerator, vrExperienceHelper

## @babylonjs/gui
1. GUI3DManager
Expand Down Expand Up @@ -291,4 +291,6 @@ Thanks to [seacloud9](https://github.com/seacloud9) for adding storybook (and [G

Lots of contributions from [hookex](https://github.com/hookex) :) Proper texture handling [demo](https://brianzinn.github.io/react-babylonjs/?path=/story/textures--image-texture), Node parenting [demo](https://brianzinn.github.io/react-babylonjs/?path=/story/babylon-basic--transform-node) Full Screen GUI [demo](https://brianzinn.github.io/react-babylonjs/?path=/story/gui--gui-full-screen), Effect Layers [glow demo](https://brianzinn.github.io/react-babylonjs/?path=/story/special-fx--glow-layer), behaviors [demo](https://brianzinn.github.io/react-babylonjs/?path=/story/behaviors--pointer-drag-behavior), useHover & useClick [demo](https://brianzinn.github.io/react-babylonjs/?path=/story/hooks--use-hover-event) and react-spring integration [demo](https://brianzinn.github.io/react-babylonjs/?path=/story/integrations--react-spring).

[dennemark](https://github.com/dennemark) added Cascaded Shadow Generator

Made with ♥ by Brian Zinn
18,552 changes: 0 additions & 18,552 deletions package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"rollup-plugin-terser": "^5.3.0",
"rollup-plugin-typescript2": "^0.26.0",
"storybook": "^5.3.19",
"ts-morph": "^4.0.1",
"ts-morph": "^8.1.0",
"ts-node": "^7.0.1",
"tsc-watch": "^1.0.31",
"tsconfig-paths": "^3.9.0",
Expand Down
110 changes: 110 additions & 0 deletions src/customHosts/BaseShadowGeneratorLifecycleListener.ts
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 src/customHosts/CascadedShadowGeneratorLifecycleListener.ts
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;
}
}
}
}
109 changes: 9 additions & 100 deletions src/customHosts/ShadowGeneratorLifecycleListener.ts
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;
}
}
}
}
4 changes: 1 addition & 3 deletions src/generatedCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11642,9 +11642,7 @@ export class FiberDynamicTerrainPropsHandler implements PropsHandler<FiberDynami
}
}

/**
* This code has been generated
*/
/** This code has been generated */
export class FiberDynamicTerrain implements HasPropsHandlers<FiberDynamicTerrainProps> {
private propsHandlers: PropsHandler<FiberDynamicTerrainProps>[];

Expand Down
Loading

0 comments on commit 9a92c0a

Please sign in to comment.