-
Notifications
You must be signed in to change notification settings - Fork 0
/
html-mixer.ts
215 lines (187 loc) · 5.75 KB
/
html-mixer.ts
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
import * as THREE from "three";
import {
CSS3DObject,
CSS3DRenderer,
} from "three/examples/jsm/renderers/CSS3DRenderer.js";
export class HtmlMixerContext {
public rendererCss: CSS3DRenderer;
public rendererWebgl: THREE.WebGLRenderer | THREE.WebGL1Renderer;
public cssFactor: number;
public cssCamera: THREE.PerspectiveCamera;
public cssScene: THREE.Scene;
public autoUpdateObjects: boolean;
public updateFcts: (() => unknown)[] = [];
/**
* @param rendererWebgl The renderer in front.
* @param camera The camera used.
*/
constructor(
rendererWebgl: THREE.WebGLRenderer | THREE.WebGL1Renderer,
camera: THREE.PerspectiveCamera,
) {
// Build cssFactor to workaround bug due to no display.
this.cssFactor = 1000;
// Update Renderer
this.rendererCss = new CSS3DRenderer();
this.rendererWebgl = rendererWebgl;
// Handle Camera
this.cssCamera = new THREE.PerspectiveCamera(
camera.fov,
camera.aspect,
camera.near * this.cssFactor,
camera.far * this.cssFactor,
);
this.updateFcts.push(() => {
this.cssCamera.quaternion.copy(camera.quaternion);
this.cssCamera.position
.copy(camera.position)
.multiplyScalar(this.cssFactor);
});
// Create a new scene to hold CSS.
this.cssScene = new THREE.Scene();
// Auto update objects
this.autoUpdateObjects = true;
this.updateFcts.push(() => {
if (this.autoUpdateObjects !== true) return;
this.cssScene.traverse((cssObject) => {
if (cssObject instanceof THREE.Scene) return;
const mixerPlane = cssObject.userData.mixerPlane as HtmlMixerPlane;
if (mixerPlane === undefined) return;
mixerPlane.update();
});
});
// Render CssScene
this.updateFcts.push(() => {
this.rendererCss.render(this.cssScene, this.cssCamera);
});
}
public update() {
this.updateFcts.forEach((updateFct) => updateFct());
}
}
export interface HtmlMixerPlaneOpts {
elementW?: number;
object3d?: THREE.Mesh<THREE.PlaneGeometry>;
planeH?: number;
planeW?: number;
}
/** Define a THREEx Plane. */
export class HtmlMixerPlane {
public opts: HtmlMixerPlaneOpts;
public domElement: HTMLElement;
public object3d!: THREE.Mesh<THREE.PlaneGeometry>;
public cssObject: CSS3DObject;
public updateFcts: (() => unknown)[] = [];
public elementWidth = 0;
public aspectRatio = 0;
public elementHeight = 0;
public mixerContext: HtmlMixerContext;
/**
* @param mixerContext context
* @param domElement the dom element to mix
* @param opts options to set
*/
constructor(
mixerContext: HtmlMixerContext,
domElement: HTMLElement,
opts?: HtmlMixerPlaneOpts,
) {
this.mixerContext = mixerContext;
this.opts = opts || {};
this.opts.elementW =
this.opts.elementW !== undefined ? this.opts.elementW : 768;
this.opts.planeW = this.opts.planeW !== undefined ? this.opts.planeW : 1;
this.opts.planeH =
this.opts.planeH !== undefined ? this.opts.planeH : 3 / 4;
this.domElement = domElement;
if (!this.opts.object3d) {
const planeMaterial = new THREE.MeshBasicMaterial({
opacity: 0,
color: new THREE.Color("black"),
blending: THREE.NoBlending,
side: THREE.DoubleSide,
});
const geometry = new THREE.PlaneGeometry(
this.opts.planeW,
this.opts.planeH,
);
this.setObject3D(new THREE.Mesh(geometry, planeMaterial));
} else {
this.setObject3D(this.opts.object3d);
}
// width of iframe in pixels
this.correctSizes();
this.correctElementSizes();
// create a CSS3DObject to display element
this.cssObject = new CSS3DObject(domElement);
this.cssObject.scale
.set(1, 1, 1)
.multiplyScalar(
this.mixerContext.cssFactor / (this.elementWidth / this.opts.planeH),
);
// Hook cssObject to mixerPlane.
this.cssObject.userData.mixerPlane = this;
this.updateFcts.push(() => {
// get world position
this.object3d.updateMatrixWorld();
const worldMatrix = this.object3d.matrixWorld;
// get position/quaternion/scale of object3d
const position = new THREE.Vector3();
const scale = new THREE.Vector3();
const quaternion = new THREE.Quaternion();
worldMatrix.decompose(position, quaternion, scale);
// handle quaternion
this.cssObject.quaternion.copy(quaternion);
// handle position
this.cssObject.position
.copy(position)
.multiplyScalar(this.mixerContext.cssFactor);
// handle scale
const scaleFactor =
this.elementWidth / (this.object3d.geometry.parameters.width * scale.x);
this.cssObject.scale
.set(1, 1, 1)
.multiplyScalar(this.mixerContext.cssFactor / scaleFactor);
});
}
setDomElement(newDomElement: HTMLElement) {
// remove the oldDomElement
const oldDomElement = this.domElement;
if (oldDomElement.parentNode)
oldDomElement.parentNode.removeChild(oldDomElement);
// update local constiables
this.domElement = newDomElement;
// update cssObject
this.cssObject.element = newDomElement;
// reset the size of the domElement
this.correctElementSizes();
this.cssObject.updateMatrixWorld();
}
/**
* Hook event so cssObject is attached to cssScene when
* object3d is added/removed.
*
* @param newObject
*/
public setObject3D(newObject: typeof this.object3d) {
this.object3d = newObject;
this.object3d.addEventListener("added", () => {
this.mixerContext.cssScene.add(this.cssObject);
});
this.object3d.addEventListener("removed", () => {
this.mixerContext.cssScene.remove(this.cssObject);
});
}
public correctElementSizes() {
this.domElement.style.height = `${this.elementHeight}px`;
this.domElement.style.width = `${this.elementWidth}px`;
}
public correctSizes() {
this.aspectRatio = (this.opts.planeH ?? 1) / (this.opts.planeW ?? 1);
this.elementWidth = this.opts.elementW ?? 0;
this.elementHeight = this.elementWidth * this.aspectRatio;
}
public update() {
this.updateFcts.forEach((updateFct) => updateFct());
}
}