File

projects/atft/src/lib/object/connector/line-connector.component.ts

Extends

AbstractConnector

Implements

OnDestroy OnChanges

Metadata

Index

Properties
Methods
Inputs
Outputs

Constructor

constructor(rendererService: RendererService, parent: AbstractObject3D<any>, animationService: AnimationService)
Parameters :
Name Type Optional
rendererService RendererService No
parent AbstractObject3D<any> No
animationService AnimationService No

Inputs

animated
Type : boolean
Default value : true
dashSize
Type : number
Default value : 3
gapSize
Type : number
Default value : 0.5
lineType
Type : LineType
Default value : LineType.dashed
lineWidth
Type : number
Default value : 2
materialColor
Type : number
Default value : 0xFFFFFF
opacity
Type : number
Default value : 1
solid
Type : boolean
Default value : false
source
Type : AbstractObject3D<THREE.Object3D>
Inherited from AbstractConnector
target
Type : AbstractObject3D<THREE.Object3D>
Inherited from AbstractConnector
layer
Type : number
Default value : 0
Inherited from AbstractObject3D
name
Type : string
Default value : uuidv4()
Inherited from AbstractObject3D
rotateX
Type : number
Inherited from AbstractObject3D

Rotation in Euler angles (radians) with order X, Y, Z.

rotateY
Type : number
Inherited from AbstractObject3D
rotateZ
Type : number
Inherited from AbstractObject3D
scaleX
Type : number
Default value : 1
Inherited from AbstractObject3D
scaleY
Type : number
Default value : 1
Inherited from AbstractObject3D
scaleZ
Type : number
Default value : 1
Inherited from AbstractObject3D
translateX
Type : number
Inherited from AbstractObject3D

Translate the geometry. This is typically done as a one time operation, and not during a loop.

translateY
Type : number
Inherited from AbstractObject3D
translateZ
Type : number
Inherited from AbstractObject3D

Outputs

changed
Type : EventEmitter
Inherited from AbstractObject3D

Methods

Private animate
animate()
Returns : void
Public createLineMesh
createLineMesh()
Inherited from AbstractConnector
Returns : Line2
Protected getPositions
getPositions()
Returns : number[]
Public ngOnChanges
ngOnChanges(changes: SimpleChanges)
Inherited from AbstractObject3D
Parameters :
Name Type Optional
changes SimpleChanges No
Returns : void
ngOnDestroy
ngOnDestroy()
Inherited from AbstractObject3D
Returns : void
updateLineGeometry
updateLineGeometry()
Inherited from AbstractConnector
Returns : void
Protected newObject3DInstance
newObject3DInstance()
Inherited from AbstractObject3D
Returns : THREE.Object3D
Private watchObjects
watchObjects()
Inherited from AbstractConnector
Returns : void
Public addChild
addChild(object: AbstractObject3D<any>)
Inherited from AbstractObject3D
Parameters :
Name Type Optional
object AbstractObject3D<any> No
Returns : void
Protected afterInit
afterInit()
Inherited from AbstractObject3D
Returns : void
Public applyRotation
applyRotation()
Inherited from AbstractObject3D
Returns : void
Public applyScale
applyScale()
Inherited from AbstractObject3D
Returns : void
Public applyTranslation
applyTranslation()
Inherited from AbstractObject3D
Returns : void
Public findByName
findByName(name: string)
Inherited from AbstractObject3D
Parameters :
Name Type Optional
name string No
Returns : any
Public getChildren
getChildren()
Inherited from AbstractObject3D
Public getObject
getObject()
Inherited from AbstractObject3D
Returns : T
Public ngAfterViewInit
ngAfterViewInit()
Inherited from AbstractObject3D
Returns : void
Public ngOnInit
ngOnInit()
Inherited from AbstractObject3D
Returns : void
Protected recursionByName
recursionByName(currentNode: AbstractObject3D<any>, name: string)
Inherited from AbstractObject3D
Parameters :
Name Type Optional
currentNode AbstractObject3D<any> No
name string No
Returns : any
Public removeChild
removeChild(object: AbstractObject3D<any>)
Inherited from AbstractObject3D
Parameters :
Name Type Optional
object AbstractObject3D<any> No
Returns : void
Public removeChildByName
removeChildByName(name: string)
Inherited from AbstractObject3D
Parameters :
Name Type Optional
name string No
Returns : void
Public updateParent
updateParent()
Inherited from AbstractObject3D
Returns : void

Properties

Protected animation
Type : Subscription
Protected clock
Default value : new THREE.Clock()
Protected line
Type : Line2
Private matLine
Type : LineMaterial
Protected time
Type : number
Default value : 0
Protected timeScale
Type : number
Default value : 5
Protected sourceSub
Type : Subscription
Inherited from AbstractConnector
Protected targetSub
Type : Subscription
Inherited from AbstractConnector
Protected childlren
Type : Array<AbstractObject3D<any>>
Default value : []
Inherited from AbstractObject3D
Protected object
Type : T
Inherited from AbstractObject3D
import {Component, Input, OnChanges, OnDestroy, Optional, SimpleChanges, SkipSelf} from '@angular/core';
import {Subscription} from 'rxjs';
import * as THREE from 'three';
import {AnimationService} from '../../animation';
import {RendererService} from '../../renderer/renderer.service';
import {provideParent} from '../../util';
import {AbstractObject3D} from '../abstract-object-3d';
import {AbstractConnector} from './abstract-connector';
import {Line2} from 'three/examples/jsm/lines/Line2';
import {LineGeometry} from 'three/examples/jsm/lines/LineGeometry';
import {LineMaterial} from 'three/examples/jsm/lines/LineMaterial';

export enum LineType {
  dashed = 'dash',
  solid = 'solid'
}

@Component({
  selector: 'atft-line-connector',
  providers: [provideParent(LineConnectorComponent)],
  template: '<ng-content></ng-content>'
})
export class LineConnectorComponent extends AbstractConnector<Line2> implements OnDestroy, OnChanges {

  @Input() materialColor = 0xFFFFFF;
  @Input() solid = false;
  @Input() lineWidth = 2;
  @Input() dashSize = 3;
  @Input() gapSize = 0.5;
  @Input() opacity = 1;
  @Input() lineType: LineType = LineType.dashed;

  @Input() animated = true;
  protected animation!: Subscription;
  protected time = 0;
  protected timeScale = 5;
  protected clock = new THREE.Clock();

  protected line!: Line2;
  private matLine!: LineMaterial;

  constructor(
    protected override rendererService: RendererService,
    @SkipSelf() @Optional() protected override parent: AbstractObject3D<any>,
    protected animationService: AnimationService,
  ) {
    super(rendererService, parent);
  }

  public createLineMesh(): Line2 {
    const positions = this.getPositions();
    const geometry = new LineGeometry();
    geometry.setPositions(positions);

    this.matLine = new LineMaterial({
      // wrong type in three@types def color?: number;
      // color: parseInt(Number(this.materialColor).toString(), 10),
      color: this.materialColor,
      linewidth: this.lineWidth,
      vertexColors: false,
      dashed: !this.solid,
      dashSize: this.dashSize,
      dashOffset: 0,
      gapSize: this.gapSize,
      opacity: this.opacity,
      transparent: this.opacity < 1,
      depthWrite: true
    });
    this.matLine.resolution.set(window.innerWidth, window.innerHeight);
    if (!this.solid) {
      this.matLine.defines['USE_DASH'] = '';
    }

    this.line = new Line2(geometry, this.matLine);
    this.line.computeLineDistances();

    if (this.animated) {
      this.animate = this.animate.bind(this);
      this.animation = this.animationService.animate.subscribe(this.animate);
    }

    return this.line;
  }

  updateLineGeometry(): void {
    const positions = this.getPositions();
    this.line.geometry.dispose();
    this.line.geometry.setPositions(positions);
    this.line.computeLineDistances();
  }

  protected getPositions(): number[] {
    if (!this.source || !this.target) {
      throw new Error('AbstractConnector: source or target inputs are missing!');
    }

    const source = this.source.getObject().position;
    const target = this.target.getObject().position;

    const positions = [];
    positions.push(source.x, source.y, source.z);
    positions.push(target.x, target.y, target.z);
    return positions;
  }


  override ngOnDestroy() {
    super.ngOnDestroy();
    if (this.animation) {
      this.animation.unsubscribe();
    }
  }

  private animate() {
    // console.log('LineConnectorComponent.animate');
    const material: any = this.line?.material;
    if (this.line?.material) {
      // console.log('LineConnectorComponent.animate do');
      this.time += this.clock.getDelta();
      // material.uniforms.time.value = -1 * this.time * this.timeScale;
      material.dashOffset = -1 * this.time * this.timeScale;
      this.line.computeLineDistances();
    }
  }


  public override ngOnChanges(changes: SimpleChanges) {
    if (!this.object) {
      return;
    }
    super.ngOnChanges(changes);

    let modified = false;

    if (['materialColor'].some(propName => propName in changes)) {
      console.log('Changed color to', this.materialColor);
      this.line.material.color = new THREE.Color(this.materialColor);
      this.line.material.needsUpdate = true;
      modified = true;
    }

    if (modified) {
      this.changed.emit();
      this.rendererService.render();
    }

  }

}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""