-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththree-object.js
77 lines (65 loc) · 2.27 KB
/
three-object.js
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
import { ThreeScene } from "@petitatelier/three-scene";
import { LitElement } from "lit-element";
export class ThreeObject extends LitElement {
get scene() {
return (typeof this._sceneElement !== "undefined")
? this._sceneElement.scene : undefined;
}
constructor() {
console.log( "three-object › constructor()");
// Must call superconstructor first.
super();
// Initialize private properties
this._sceneElement = undefined; // Reference to parent scene element
}
/**
* Override, to initialize the object upon construction (called
* by `ThreeScene.init()`) or later dynamic addition to the DOM
* (called by `connectedCallback()`).
*/
init() {
}
/**
* Override, to programmatically animate the object.
*/
step( time, delta) {
}
/**
* Override, to dispose THREE cached resources, when element gets
* disconnected from DOM, to avoid memory leaks.
*
* Normally, resources get automatically garbage collected.
*
* Some THREE objects however require manual disposal of their resources;
* look at THREE.js docs for every `Object3D` being used, if it has a
* `dispose()` method, or if there are special instructions for disposal.
*/
dispose() {
}
/**
* This callback is called at different stages:
* 1. when the ‹three-*› element is first mounted in the DOM,
* during the whole web app construction;
* 2. when the ‹three-*› element is added at a later stage
* to the DOM, dynamically, with the whole web app already
* initialized.
*/
connectedCallback() {
super.connectedCallback();
console.log( "three-object › connectedCallback()");
// Lookup and store a reference to parent ‹three-scene› element
const sceneElement = this.closest( "three-scene");
if( !( sceneElement instanceof ThreeScene)) {
throw new Error( "Element ${this.tagName.toLowerCase()} must be a descendent of a ‹three-scene› element");
}
this._sceneElement = sceneElement;
// Initialize element – this is for the case of an element that was
// added to the DOM dynamically, at a later stage than the app init
this.init();
}
disconnectedCallback() {
this.dispose();
this._sceneElement = undefined;
super.disconnectedCallback();
}
}