All files / src/WebGL/core Object3D.ts

0.7% Statements 1/142
0% Branches 0/47
0% Functions 0/12
0.79% Lines 1/126

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285            18x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
import type { Material } from './../materials/Material';
import { Matrix4, Quaternion, Vector3 } from "../math";
import type { Geometry } from './Geometry';
import type { Fog } from '../Fog';
import type { Light } from '../Light';
 
export let Object3DIDCount = 0;
// Object3D base constructor function
export class Object3D {
  id = Object3DIDCount++;
  name = "";
  parent?: Object3D;
  children: Array<Object3D> = [];
  position = new Vector3();
  rotation: Vector3 | number = new Vector3();
  matrix = new Matrix4();
  matrixWorld = new Matrix4();
  quaternion = new Quaternion();
  eulerOrder = "XYZ";
  up = new Vector3(0, 1, 0);
  scale = new Vector3(1, 1, 1);
  matrixAutoUpdate = true;
  matrixWorldNeedsUpdate = true;
  rotationAutoUpdate = true;
  useQuaternion = false;
  visible = true;
  geometry?: Geometry;
  material?: Material;
 
  lookAt(vector: Vector3) {
    this.matrix.lookAt(vector, this.position, this.up);
    Iif (this.rotationAutoUpdate) {
      if (this.useQuaternion === true)
        this.quaternion.copy(this.matrix.decompose()[1] as Quaternion);
      else Iif (this.rotation instanceof Vector3)
        this.rotation.setEulerFromRotationMatrix(this.matrix, this.eulerOrder);
    }
  }
 
  //add child object
  add<T extends Object3D>(object: T): void {
    Iif (object === (this as Object3D)) {
      console.error("Can't add $3Dmol.Object3D to itself");
      return;
    }
 
    object.parent = this;
    this.children.push(object);
 
    //add to the scene (i.e. follow up this instance's parents until reach the top)
 
    var scene = this as Object3D;
 
    while (scene.parent !== undefined) scene = scene.parent;
 
    Iif (scene !== undefined && scene instanceof Scene)
      scene.__addObject(object);
  }
 
  remove<T extends Object3D>(object: T): void {
    var index = this.children.indexOf(object);
 
    Iif (index !== -1) {
      object.parent = undefined;
      this.children.splice(index, 1);
 
      //Remove from scene
 
      var scene = this as Object3D;
 
      while (scene.parent !== undefined) scene = scene.parent;
 
      Iif (scene !== undefined && scene instanceof Scene)
        scene.__removeObject(object);
    }
  }
 
  //convert to vrml
  vrml(indent?: string) {
    //attempt to pretty print
    Iif (!indent) indent = " ";
    //all objects have a transformation (usually identity)
    //not quite sure if getting rotation right here..
    var theta = 2 * Math.atan2(this.quaternion.lengthxyz(), this.quaternion.w);
    var x = 0,
      y = 0,
      z = 0;
    Iif (theta != 0) {
      let st = Math.sin(theta / 2);
      x = this.quaternion.x / st;
      y = this.quaternion.y / st;
      z = this.quaternion.z / st;
    }
    var ret =
      indent +
      "Transform {\n" +
      indent +
      " center " +
      this.position.x +
      " " +
      this.position.y +
      " " +
      this.position.z +
      "\n" +
      indent +
      " rotation " +
      x +
      " " +
      y +
      " " +
      z +
      " " +
      theta +
      "\n" +
      indent +
      " children [\n";
 
    Iif (this.geometry) {
      ret += this.geometry.vrml(indent, this.material);
    }
    for (var i = 0; i < this.children.length; i++) {
      ret += this.children[i].vrml(indent + " ") + ",\n";
    }
    ret += " ]\n";
    ret += "}";
    return ret;
  }
 
  updateMatrix() {
    this.matrix.setPosition(this.position);
 
    if (this.useQuaternion === false && this.rotation instanceof Vector3) {
      this.matrix.setRotationFromEuler(this.rotation, this.eulerOrder);
    } else {
      this.matrix.setRotationFromQuaternion(this.quaternion);
    }
 
    //TODO: Do I need this??
    Iif (this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1)
      this.matrix.scale(this.scale);
 
    this.matrixWorldNeedsUpdate = true;
  }
 
  updateMatrixWorld(force?: boolean) {
    Iif (this.matrixAutoUpdate === true) this.updateMatrix();
 
    Iif (this.matrixWorldNeedsUpdate === true || force === true) {
      if (this.parent === undefined) this.matrixWorld.copy(this.matrix);
      else
        this.matrixWorld.multiplyMatrices(this.parent.matrixWorld, this.matrix);
    }
 
    this.matrixWorldNeedsUpdate = false;
 
    //Update matrices of all children
    for (var i = 0; i < this.children.length; i++) {
      this.children[i].updateMatrixWorld(true);
    }
  }
 
  clone(object?: Object3D): Object3D {
    Iif (object === undefined) object = new Object3D();
 
    object.name = this.name;
 
    object.up.copy(this.up);
    object.position.copy(this.position);
    if (
      object.rotation instanceof Vector3 &&
      this.rotation instanceof Vector3
    ) {
      object.rotation.copy(this.rotation);
    } else {
      object.rotation = this.rotation;
    }
    object.eulerOrder = this.eulerOrder;
    object.scale.copy(this.scale);
 
    object.rotationAutoUpdate = this.rotationAutoUpdate;
    object.matrix.copy(this.matrix);
    object.matrixWorld.copy(this.matrixWorld);
    object.quaternion.copy(this.quaternion);
    object.matrixAutoUpdate = this.matrixAutoUpdate;
    object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
 
    object.useQuaternion = this.useQuaternion;
 
    object.visible = this.visible;
 
    for (var i = 0; i < this.children.length; i++) {
      var child = this.children[i];
      object.add(child.clone());
    }
 
    return object;
  }
 
  setVisible(val: boolean): void {
    //recursively set visibility
    this.visible = val;
    for (var i = 0; i < this.children.length; i++) {
      var child = this.children[i];
      child.setVisible(val);
    }
  }
}
 
 
/*
 * Scene class
 */
/** @constructor */
export class Scene extends Object3D {
  fog: Fog | null = null;
  //may not need...
  overrideMaterial: Material | null = null;
  matrixAutoUpdate = false;
  __objects = [] as Object3D[];
  __lights = [] as Light[];
  __objectsAdded = [] as Object3D[];
  __objectsRemoved = [] as Object3D[];
 
  __addObject<T extends Object3D>(object: T) {
    //Directional Lighting
    // @ts-ignore
    if (object instanceof window.$3Dmol.Light) {
      Iif (this.__lights.indexOf(object as unknown as Light) === -1) this.__lights.push(object as unknown as Light);
 
      //TODO: Do I need this??
      Iif ((object as unknown as Light).target && (object as unknown as Light).target.parent === undefined)
        this.add((object as unknown as Light).target);
    }
 
    //Rotation group
    else {
      Iif (this.__objects.indexOf(object) === -1) {
        this.__objects.push(object);
        this.__objectsAdded.push(object);
 
        //Check if previously removed
 
        var idx = this.__objectsRemoved.indexOf(object);
 
        Iif (idx !== -1) this.__objectsRemoved.splice(idx, 1);
      }
    }
 
    //Add object's children
 
    for (var i = 0; i < object.children.length; i++)
      this.__addObject(object.children[i]);
  }
 
  __removeObject<T extends Object3D>(object: T) {
    var idx;
    // @ts-ignore
    if (object instanceof window.$3Dmol.Light) {
      idx = this.__lights.indexOf(object as unknown as Light);
 
      Iif (idx !== -1) this.__lights.splice(idx, 1);
    }
 
    //Object3D
    else {
      idx = this.__objects.indexOf(object);
 
      Iif (idx !== -1) {
        this.__objects.splice(idx, 1);
        this.__objectsRemoved.push(object);
 
        //Check if previously added
 
        var ai = this.__objectsAdded.indexOf(object);
 
        Iif (ai !== -1) this.__objectsAdded.splice(idx, 1);
      }
    }
 
    //Remove object's children
    for (var i = 0; i < object.children.length; i++)
      this.__removeObject(object.children[i]);
  }
}