-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathdom-renderer.jsx
221 lines (198 loc) · 5.9 KB
/
dom-renderer.jsx
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
import * as THREE from 'three';
// import {sceneLowerPriority} from './renderer.js';
import easing from './easing.js';
import physicsManager from './physics-manager.js';
const cubicBezier = easing(0, 1, 0, 1);
const physicsScene = physicsManager.getScene();
class DomItem extends THREE.Object3D {
constructor(position, quaternion, scale, width, height, worldWidth, render) {
super();
this.name = 'domNode';
this.position.copy(position);
this.quaternion.copy(quaternion);
this.scale.copy(scale);
this.width = width;
this.height = height;
this.worldWidth = worldWidth;
this.render = render;
this.enabled = false;
this.value = 0;
this.animation = null;
const floatNode = new THREE.Object3D();
floatNode.name = 'floatNode';
this.floatNode = floatNode;
this.add(floatNode);
const iframeMesh = new IFrameMesh({
width,
height,
});
iframeMesh.name = 'domPunchThroughNode';
this.iframeMesh = iframeMesh;
floatNode.add(iframeMesh);
const p = position;
const q = quaternion;
const hs = new THREE.Vector3(iframeMesh.worldWidth, iframeMesh.worldHeight, 0.01)
.multiplyScalar(0.5);
const dynamic = false;
const physicsObject = physicsScene.addBoxGeometry(
p,
q,
hs,
dynamic
);
physicsScene.disableActor(physicsObject);
physicsScene.disableGeometryQueries(physicsObject);
this.physicsObject = physicsObject;
}
startAnimation(enabled, startTime, endTime) {
this.enabled = enabled;
const startValue = this.value;
const endValue = enabled ? 1 : 0;
this.animation = {
startTime,
endTime,
startValue,
endValue,
};
}
update(timestamp) {
if (this.animation) {
const {startTime, endTime, startValue, endValue} = this.animation;
let factor = Math.min(Math.max((timestamp - startTime) / (endTime - startTime), 0), 1);
factor = cubicBezier(factor);
if (factor < 1) {
this.value = startValue + (endValue - startValue) * factor;
} else {
this.value = endValue;
this.animation = null;
}
} else {
this.value = this.enabled ? 1 : 0;
}
if (this.value > 0) {
const w = this.value;
const shiftOffset = (1 - w) * this.worldWidth/2;
this.iframeMesh.position.x = -shiftOffset;
this.iframeMesh.scale.set(w, 1, 1);
this.iframeMesh.updateMatrixWorld();
this.iframeMesh.material.opacity = 1 - this.value;
this.physicsObject.position.setFromMatrixPosition(this.iframeMesh.matrixWorld);
this.physicsObject.quaternion.setFromRotationMatrix(this.iframeMesh.matrixWorld);
this.physicsObject.updateMatrixWorld();
physicsScene.setTransform(this.physicsObject);
this.visible = true;
} else {
this.visible = false;
}
}
onBeforeRaycast() {
if (this.enabled) {
physicsScene.enableActor(this.physicsObject);
physicsScene.enableGeometryQueries(this.physicsObject);
}
}
onAfterRaycast() {
if (this.enabled) {
physicsScene.disableActor(this.physicsObject);
physicsScene.disableGeometryQueries(this.physicsObject);
}
}
destroy() {
physicsScene.enableActor(this.physicsObject);
physicsScene.removeGeometry(this.physicsObject);
}
}
class IFrameMesh extends THREE.Mesh {
constructor({
width,
height,
}) {
const scaleFactor = DomRenderEngine.getScaleFactor(width, height);
const worldWidth = width * scaleFactor;
const worldHeight = height * scaleFactor;
const geometry = new THREE.PlaneBufferGeometry(worldWidth, worldHeight);
const material = new THREE.MeshBasicMaterial({
color: 0xFFFFFF,
side: THREE.DoubleSide,
opacity: 0,
transparent: true,
blending: THREE.MultiplyBlending
});
super(geometry, material);
this.frustumCulled = false;
this.onBeforeRender = renderer => {
const context = renderer.getContext();
context.disable(context.SAMPLE_ALPHA_TO_COVERAGE);
};
this.onAfterRender = renderer => {
const context = renderer.getContext();
context.enable(context.SAMPLE_ALPHA_TO_COVERAGE);
};
this.worldWidth = worldWidth;
this.worldHeight = worldHeight;
}
}
export class DomRenderEngine extends EventTarget {
constructor() {
super();
this.doms = [];
this.physicsObjects = [];
this.lastHover = false;
}
static getScaleFactor(width, height) {
return Math.min(1/width, 1/height);
}
addDom({
position = new THREE.Vector3(),
quaternion = new THREE.Quaternion(),
scale = new THREE.Vector3(1, 1, 1),
width = 600,
height = 400,
worldWidth = 1,
render = () => (<div />),
}) {
const dom = new DomItem(position, quaternion, scale, width, height, worldWidth, render);
this.doms.push(dom);
this.physicsObjects.push(dom.physicsObject);
this.dispatchEvent(new MessageEvent('update'));
return dom;
}
removeDom(dom) {
const index = this.doms.indexOf(dom);
if (index !== -1) {
this.doms.splice(index, 1);
this.physicsObjects.splice(index, 1);
}
}
/* update() {
const hover = this.doms.some(dom => {
return false;
});
if (hover !== this.lastHover) {
this.lastHover = hover;
this.dispatchEvent(new MessageEvent('hover', {
data: {
hover,
},
}));
}
} */
getPhysicsObjects() {
return this.physicsObjects;
}
onBeforeRaycast() {
for (const dom of this.doms) {
dom.onBeforeRaycast();
}
}
onAfterRaycast() {
for (const dom of this.doms) {
dom.onAfterRaycast();
}
}
destroy() {
// XXX finish this
}
}
const domRenderer = new DomRenderEngine();
export default domRenderer;