From cb07e3894e71eb0d84c825e6b928071df0565a12 Mon Sep 17 00:00:00 2001 From: Robert Long Date: Sat, 1 Aug 2020 12:49:51 -0700 Subject: [PATCH] Move from defaultObject3D inflator to ThreeTagComponent --- src/components/Object3DTags.js | 5 - src/components/ThreeTagComponent.js | 10 + src/components/ThreeTagComponents.js | 200 ++++++++++++++++++ ...bject3DTags.d.ts => ThreeTagComponents.ts} | 0 src/components/WebGLRendererComponent.js | 2 - src/components/index.d.ts | 2 +- src/components/index.js | 4 +- src/defaultObject3DInflator.d.ts | 2 - src/defaultObject3DInflator.js | 28 --- src/entity.js | 12 +- src/index.js | 1 - src/initialize.js | 1 - src/world.js | 30 ++- 13 files changed, 248 insertions(+), 49 deletions(-) delete mode 100644 src/components/Object3DTags.js create mode 100644 src/components/ThreeTagComponent.js create mode 100644 src/components/ThreeTagComponents.js rename src/components/{Object3DTags.d.ts => ThreeTagComponents.ts} (100%) delete mode 100644 src/defaultObject3DInflator.d.ts delete mode 100644 src/defaultObject3DInflator.js diff --git a/src/components/Object3DTags.js b/src/components/Object3DTags.js deleted file mode 100644 index fe2f040..0000000 --- a/src/components/Object3DTags.js +++ /dev/null @@ -1,5 +0,0 @@ -import { TagComponent } from "ecsy"; - -export class SceneTagComponent extends TagComponent {} -export class CameraTagComponent extends TagComponent {} -export class MeshTagComponent extends TagComponent {} diff --git a/src/components/ThreeTagComponent.js b/src/components/ThreeTagComponent.js new file mode 100644 index 0000000..b6c14a5 --- /dev/null +++ b/src/components/ThreeTagComponent.js @@ -0,0 +1,10 @@ + +import { TagComponent } from "ecsy"; + +export class ThreeTagComponent extends TagComponent { + static isThreeTagComponent = true; + + static matchesObject3D(object) { + return false; + } +} \ No newline at end of file diff --git a/src/components/ThreeTagComponents.js b/src/components/ThreeTagComponents.js new file mode 100644 index 0000000..d87b105 --- /dev/null +++ b/src/components/ThreeTagComponents.js @@ -0,0 +1,200 @@ +import { ThreeTagComponent } from "./ThreeTagComponent"; + +// Tag components for every Object3D in the three.js core library + +// audio +export class AudioTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.type === "Audio"; + } +} + +export class AudioListenerTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.type === "AudioListener"; + } +} + +export class PositionalAudioTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + // PositionalAudio doesn't have a type or isPositionalAudio property. + return object.type === "Audio" && object.panner !== undefined; + } +} + +// cameras +export class ArrayCameraTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isArrayCamera; + } +} + +export class CameraTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isCamera; + } +} + +export class CubeCameraTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.type === "CubeCamera"; + } +} + +export class OrthographicCameraTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isOrthographicCamera; + } +} + +export class PerspectiveCameraTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isPerspectiveCamera; + } +} + +// extras/objects +export class ImmediateRenderObjectTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isImmediateRenderObject; + } +} + +// helpers + +// Due to inconsistency in implementing consistent identifying properties like "type" on helpers, we've +// chosen to exclude helper tag components. + +// lights +export class AmbientLightTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isAmbientLight; + } +} + +export class AmbientLightProbeTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isAmbientLightProbe; + } +} + +export class DirectionalLightTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isDirectionalLight; + } +} + +export class HemisphereLightTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isHemisphereLight; + } +} + +export class HemisphereLightProbeTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isHemisphereLightProbe; + } +} + +export class LightTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isLight; + } +} + +export class LightProbeTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isLightProbe; + } +} + +export class PointLightTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isPointLight; + } +} + +export class RectAreaLightTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isRectAreaLight; + } +} + +export class SpotLightTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isSpotLight; + } +} + +// objects +export class BoneTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isBone; + } +} + +export class GroupTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isGroup; + } +} + +export class InstancedMeshTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isInstancedMesh; + } +} + +export class LODTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isLOD; + } +} + +export class LineTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isLine; + } +} + +export class LineLoopTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isLineLoop; + } +} + +export class LineSegmentsTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isLineSegments; + } +} + +export class MeshTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isMesh; + } +} +export class PointsTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isPoints; + } +} + +export class SkinnedMeshTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isSkinnedMesh; + } +} + +export class SpriteTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isSprite; + } +} + +// scenes +export class SceneTagComponent extends ThreeTagComponent { + static matchesObject3D(object) { + return object.isScene; + } +} diff --git a/src/components/Object3DTags.d.ts b/src/components/ThreeTagComponents.ts similarity index 100% rename from src/components/Object3DTags.d.ts rename to src/components/ThreeTagComponents.ts diff --git a/src/components/WebGLRendererComponent.js b/src/components/WebGLRendererComponent.js index c70bf5a..3752afa 100644 --- a/src/components/WebGLRendererComponent.js +++ b/src/components/WebGLRendererComponent.js @@ -1,6 +1,4 @@ import { Component, Types } from "ecsy"; -import { WebGLRenderer } from "three"; -import { ECSYThreeEntity } from "../entity.js"; export class WebGLRendererComponent extends Component {} WebGLRendererComponent.schema = { diff --git a/src/components/index.d.ts b/src/components/index.d.ts index 7127c9e..f96d097 100644 --- a/src/components/index.d.ts +++ b/src/components/index.d.ts @@ -1,2 +1,2 @@ export { Object3DComponent } from "./Object3DComponent"; -export * from "./Object3DTags"; +export * from "./ThreeTagComponents"; diff --git a/src/components/index.js b/src/components/index.js index facf665..7f30713 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -1,2 +1,4 @@ export { Object3DComponent } from "./Object3DComponent.js"; -export * from "./Object3DTags.js"; +export { ThreeTagComponent } from "./ThreeTagComponent.js"; +export { WebGLRendererComponent } from "./WebGLRendererComponent.js"; +export * from "./ThreeTagComponents.js"; diff --git a/src/defaultObject3DInflator.d.ts b/src/defaultObject3DInflator.d.ts deleted file mode 100644 index 2cf4dde..0000000 --- a/src/defaultObject3DInflator.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -import { Object3DInflator } from "./World"; -export const defaultObject3DInflator: Object3DInflator; diff --git a/src/defaultObject3DInflator.js b/src/defaultObject3DInflator.js deleted file mode 100644 index 7745caf..0000000 --- a/src/defaultObject3DInflator.js +++ /dev/null @@ -1,28 +0,0 @@ -import { - MeshTagComponent, - SceneTagComponent, - CameraTagComponent -} from "./components/Object3DTags.js"; - -export const defaultObject3DInflator = { - inflate: (entity, obj) => { - // TODO support more tags and probably a way to add user defined ones - if (obj.isMesh) { - entity.addComponent(MeshTagComponent); - } else if (obj.isScene) { - entity.addComponent(SceneTagComponent); - } else if (obj.isCamera) { - entity.addComponent(CameraTagComponent); - } - }, - deflate: (entity, obj) => { - // TODO support more tags and probably a way to add user defined ones - if (obj.isMesh) { - entity.removeComponent(MeshTagComponent); - } else if (obj.isScene) { - entity.removeComponent(SceneTagComponent); - } else if (obj.isCamera) { - entity.removeComponent(CameraTagComponent); - } - } -}; diff --git a/src/entity.js b/src/entity.js index b41586a..7fefa80 100644 --- a/src/entity.js +++ b/src/entity.js @@ -5,7 +5,7 @@ export class ECSYThreeEntity extends _Entity { addObject3DComponent(obj, parentEntity) { obj.entity = this; this.addComponent(Object3DComponent, { value: obj }); - this._entityManager.world.object3DInflator.inflate(this, obj); + this._entityManager.world.inflateObject3D(this, obj); if (parentEntity && parentEntity.hasComponent(Object3DComponent)) { parentEntity.getObject3D().add(obj); } @@ -19,7 +19,15 @@ export class ECSYThreeEntity extends _Entity { obj.parent && obj.parent.remove(obj); } this.removeComponent(Object3DComponent); - this._entityManager.world.object3DInflator.deflate(this, obj); + + for (let i = this._ComponentTypes.length - 1; i >= 0; i--) { + const Component = this._ComponentTypes[i]; + + if (Component.isThreeTagComponent) { + this.removeComponent(Component); + } + } + obj.entity = null; } diff --git a/src/index.js b/src/index.js index ef94940..46733bf 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,5 @@ export { ECSYThreeWorld } from "./world.js"; export { ECSYThreeSystem } from "./system.js"; -export { defaultObject3DInflator } from "./defaultObject3DInflator.js"; import * as _ThreeTypes from "./types/index.js"; export const ThreeTypes = _ThreeTypes; export * from "./components/index.js"; diff --git a/src/initialize.js b/src/initialize.js index f7a94cb..4d8546e 100644 --- a/src/initialize.js +++ b/src/initialize.js @@ -1,7 +1,6 @@ import { ECSYThreeWorld } from "./world.js"; import { WebGLRendererSystem } from "./systems/WebGLRendererSystem.js"; import { WebGLRendererComponent } from "./components/WebGLRendererComponent.js"; -import { SceneTagComponent, CameraTagComponent } from "./components/Object3DTags.js"; import { PerspectiveCamera, Scene, WebGLRenderer, Clock } from "three"; export function initialize(world, options = {}) { diff --git a/src/world.js b/src/world.js index 7380f2e..cc5e160 100644 --- a/src/world.js +++ b/src/world.js @@ -1,7 +1,6 @@ import { World } from "ecsy"; import { ECSYThreeEntity } from "./entity.js"; -import { defaultObject3DInflator } from "./defaultObject3DInflator"; -import * as Object3DTagComponents from "./components/Object3DTags.js"; +import * as ThreeTagComponents from "./components/ThreeTagComponents.js"; import { Object3DComponent } from "./components/Object3DComponent.js"; export class ECSYThreeWorld extends World { @@ -10,19 +9,38 @@ export class ECSYThreeWorld extends World { Object.assign( { entityClass: ECSYThreeEntity, - registerTagComponents: true + registerThreeTagComponents: true }, options ) ); - if (this.options.registerTagComponents) { + this.threeTagComponents = []; + + if (this.options.registerThreeTagComponents) { this.registerComponent(Object3DComponent); - Object.values(Object3DTagComponents).forEach(Component => { + + Object.values(ThreeTagComponents).forEach(Component => { this.registerComponent(Component) }); } + } + + registerComponent(Component, objectPool) { + if (Component.isThreeTagComponent) { + this.threeTagComponents.push(Component); + } + + return super.registerComponent(Component, objectPool); + } - this.object3DInflator = defaultObject3DInflator; + inflateObject3D(entity, object3D) { + for (let i = 0; i < this.threeTagComponents.length; i++) { + const ThreeTagComponent = this.threeTagComponents[i]; + + if (ThreeTagComponent.matchesObject3D(object3D)) { + entity.addComponent(ThreeTagComponent); + } + } } }