Skip to content

Commit

Permalink
Move from defaultObject3D inflator to ThreeTagComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlong committed Aug 1, 2020
1 parent b0da7c7 commit cb07e38
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 49 deletions.
5 changes: 0 additions & 5 deletions src/components/Object3DTags.js

This file was deleted.

10 changes: 10 additions & 0 deletions 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;
}
}
200 changes: 200 additions & 0 deletions 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;
}
}
File renamed without changes.
2 changes: 0 additions & 2 deletions 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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/index.d.ts
@@ -1,2 +1,2 @@
export { Object3DComponent } from "./Object3DComponent";
export * from "./Object3DTags";
export * from "./ThreeTagComponents";
4 changes: 3 additions & 1 deletion 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";
2 changes: 0 additions & 2 deletions src/defaultObject3DInflator.d.ts

This file was deleted.

28 changes: 0 additions & 28 deletions src/defaultObject3DInflator.js

This file was deleted.

12 changes: 10 additions & 2 deletions src/entity.js
Expand Up @@ -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);
}
Expand All @@ -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;
}

Expand Down
1 change: 0 additions & 1 deletion 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";
Expand Down
1 change: 0 additions & 1 deletion 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 = {}) {
Expand Down
30 changes: 24 additions & 6 deletions 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 {
Expand All @@ -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);
}
}
}
}

0 comments on commit cb07e38

Please sign in to comment.