This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrender.tsx
569 lines (434 loc) · 17.1 KB
/
render.tsx
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import {expect} from "chai";
import * as React from "react";
import * as Sinon from "sinon";
import {
BoxGeometry,
Camera, Mesh,
MeshLambertMaterial, Object3D,
PerspectiveCamera,
Scene, Vector3,
WebGLRenderer,
} from "three";
import ReactThreeRenderer from "../../../src/core/renderer/reactThreeRenderer";
import object3D from "../../../src/core/renderer/hostDescriptors/descriptors/objects/object3D";
import {RenderAction} from "../../../src/core/renderer/hostDescriptors/descriptors/render";
import webGLRenderer from "../../../src/core/renderer/hostDescriptors/descriptors/webGLRenderer";
import {mockConsole, testContainers} from "../index";
import Done = Mocha.Done;
const {div: testDiv} = testContainers;
describe("render", () => {
function verifyRenderCall(rendererSpy: Sinon.SinonSpy) {
expect(rendererSpy.callCount, "Render should only be called once").to.equal(1);
const lastCall = rendererSpy.lastCall;
const scene = lastCall.args[0] as Scene;
expect(scene).to.be.instanceOf(Scene);
expect(lastCall.args[1]).to.be.instanceOf(Camera);
expect(scene.children.length, "Scene should only have one child").to.equal(1);
const mesh = scene.children[0] as Mesh;
expect(mesh).to.be.instanceOf(Mesh);
expect(mesh.geometry).to.be.instanceOf(BoxGeometry);
expect(mesh.material).to.be.instanceOf(MeshLambertMaterial);
}
it("should be able to be rendered into a renderer", (done) => {
mockConsole.expectLog("THREE.WebGLRenderer", "100");
// mockConsole.expectWarn("THREE.WebGLProgram: gl.getProgramInfoLog()", "\n\n\n");
const renderer = new WebGLRenderer();
const renderCallSpy = Sinon.spy(renderer, "render");
ReactThreeRenderer.render(<render
camera={<perspectiveCamera/>}
scene={<scene>
<mesh>
<boxGeometry width={5} height={5} depth={5}/>
<meshLambertMaterial/>
</mesh>
</scene>}/>, renderer);
requestAnimationFrame(() => {
verifyRenderCall(renderCallSpy);
ReactThreeRenderer.unmountComponentAtNode(renderer, () => {
done();
});
});
});
it("should be able to be rendered into a container within a renderer", (done) => {
const rendererSpy = Sinon.spy();
const {canvas: testCanvas} = testContainers;
ReactThreeRenderer.render(<webGLRenderer
width={800}
height={600}
ref={rendererSpy}
/>, testCanvas);
expect(rendererSpy.callCount).to.equal(1);
const renderer = rendererSpy.lastCall.args[0];
expect(renderer).to.be.instanceOf(WebGLRenderer);
const renderCallSpy = Sinon.spy(rendererSpy.lastCall.args[0], "render");
expect(renderCallSpy.notCalled).to.equal(true);
ReactThreeRenderer.render(<webGLRenderer
width={800}
height={600}
ref={rendererSpy}
>
<render
camera={<perspectiveCamera/>}
scene={<scene>
<mesh>
<boxGeometry width={5} height={5} depth={5}/>
<meshLambertMaterial/>
</mesh>
</scene>}/>
</webGLRenderer>, testCanvas);
requestAnimationFrame(() => {
verifyRenderCall(renderCallSpy);
ReactThreeRenderer.unmountComponentAtNode(testCanvas, done);
});
});
it("should not render if scene or camera are null", (done: Done) => {
mockConsole.expectLog("THREE.WebGLRenderer", "100");
const renderer = new WebGLRenderer();
const renderCallSpy = Sinon.spy(renderer, "render");
ReactThreeRenderer.render(<render
camera={null}
scene={null}
/>, renderer);
requestAnimationFrame(() => {
expect(renderCallSpy.callCount).to.equal(0);
ReactThreeRenderer.render(<render
camera={null}
scene={<scene>
<mesh>
<boxGeometry width={5} height={5} depth={5}/>
<meshLambertMaterial/>
</mesh>
</scene>}/>, renderer);
requestAnimationFrame(() => {
expect(renderCallSpy.callCount).to.equal(0);
ReactThreeRenderer.render(<render
camera={<perspectiveCamera/>}
scene={null}/>, renderer);
requestAnimationFrame(() => {
expect(renderCallSpy.callCount).to.equal(0);
// mockConsole.expectWarn("THREE.WebGLProgram: gl.getProgramInfoLog()", "\n\n\n");
ReactThreeRenderer.render(<render
camera={<perspectiveCamera/>}
scene={<scene>
<mesh>
<boxGeometry width={5} height={5} depth={5}/>
<meshLambertMaterial/>
</mesh>
</scene>}/>, renderer);
requestAnimationFrame(() => {
expect(renderCallSpy.callCount).to.equal(1);
ReactThreeRenderer.unmountComponentAtNode(renderer, done);
});
});
});
});
});
it("should call the camera and scene refs with the correct objects", (done) => {
const perspectiveCameraRef = Sinon.spy();
const sceneRef = Sinon.spy();
const {canvas: testCanvas} = testContainers;
ReactThreeRenderer.render(<webGLRenderer
width={800}
height={600}
>
<render
camera={<perspectiveCamera ref={perspectiveCameraRef} name="some camera"/>}
scene={<scene ref={sceneRef} name="some scene">
<mesh>
<boxGeometry width={5} height={5} depth={5}/>
<meshLambertMaterial/>
</mesh>
</scene>}/>
</webGLRenderer>, testCanvas);
expect(perspectiveCameraRef.callCount).to.equal(1);
expect(sceneRef.callCount).to.equal(1);
const firstCamera: Camera = perspectiveCameraRef.lastCall.args[0];
const firstScene: Scene = sceneRef.lastCall.args[0];
expect(firstCamera.name).to.equal("some camera");
expect(firstScene.name).to.equal("some scene");
// they should have mounted into the same group
const renderActionGroup = firstScene.parent;
expect(renderActionGroup).to.not.equal(null);
if (renderActionGroup == null) {
throw new Error();
}
expect(renderActionGroup).to.equal(firstCamera.parent);
ReactThreeRenderer.render(<webGLRenderer
width={800}
height={600}
>
<render
camera={<perspectiveCamera ref={perspectiveCameraRef} name="same camera different name"/>}
scene={<scene ref={sceneRef} name="same scene different name">
<mesh>
<boxGeometry width={5} height={5} depth={5}/>
<meshLambertMaterial/>
</mesh>
</scene>}/>
</webGLRenderer>, testCanvas);
// the refs should not be called again
expect(perspectiveCameraRef.callCount).to.equal(1);
expect(sceneRef.callCount).to.equal(1);
expect(firstCamera.parent).to.equal(renderActionGroup);
expect(firstScene.parent).to.equal(renderActionGroup);
expect(firstCamera.name).to.equal("same camera different name");
expect(firstScene.name).to.equal("same scene different name");
ReactThreeRenderer.render(<webGLRenderer
width={800}
height={600}
>
<render
camera={<perspectiveCamera ref={perspectiveCameraRef} name="another camera" key={"3"}/>}
scene={<scene ref={sceneRef} name="another scene" key={"4"}>
<mesh>
<boxGeometry width={5} height={5} depth={5}/>
<meshLambertMaterial/>
</mesh>
</scene>}/>
</webGLRenderer>, testCanvas);
// but for different keys, they should be!
expect(perspectiveCameraRef.callCount).to.equal(3);
expect(sceneRef.callCount).to.equal(3);
// names should not have changed but they should have dismounted
expect(firstCamera.name).to.equal("same camera different name");
expect(firstScene.name).to.equal("same scene different name");
expect(firstCamera.parent).to.equal(null);
expect(firstScene.parent).to.equal(null);
const secondCamera = perspectiveCameraRef.lastCall.args[0];
const secondScene = sceneRef.lastCall.args[0];
expect(secondCamera).to.not.equal(firstCamera);
expect(secondScene).to.not.equal(firstScene);
// they should have replaced the first camera and scene in the same group
expect(secondCamera.parent).to.equal(renderActionGroup);
expect(secondScene.parent).to.equal(renderActionGroup);
expect(secondCamera.name).to.equal("another camera");
expect(secondScene.name).to.equal("another scene");
ReactThreeRenderer.render(<webGLRenderer
width={800}
height={600}
>
<render
camera={<perspectiveCamera name="second camera but without ref this time" key={"3"}/>}
scene={<scene name="second scene but without ref this time" key={"4"}>
<mesh>
<boxGeometry width={5} height={5} depth={5}/>
<meshLambertMaterial/>
</mesh>
</scene>}/>
</webGLRenderer>, testCanvas);
expect(perspectiveCameraRef.callCount).to.equal(4);
expect(sceneRef.callCount).to.equal(4);
expect(perspectiveCameraRef.lastCall.args[0]).to.equal(null);
expect(sceneRef.lastCall.args[0]).to.equal(null);
expect(firstCamera.name).to.equal("same camera different name");
expect(firstScene.name).to.equal("same scene different name");
// sure the ref may not be called but it's still the same object
expect(secondCamera.name).to.equal("second camera but without ref this time");
expect(secondScene.name).to.equal("second scene but without ref this time");
// and should remain in group
expect(secondCamera.parent).to.equal(renderActionGroup);
expect(secondScene.parent).to.equal(renderActionGroup);
ReactThreeRenderer.render(<webGLRenderer
width={800}
height={600}
>
<render
camera={<perspectiveCamera name="third camera" key={"10"}/>}
scene={<scene name="third scene" key={"20"}>
<mesh>
<boxGeometry width={5} height={5} depth={5}/>
<meshLambertMaterial/>
</mesh>
</scene>}/>
</webGLRenderer>, testCanvas);
expect(perspectiveCameraRef.callCount).to.equal(4);
expect(sceneRef.callCount).to.equal(4);
expect(perspectiveCameraRef.lastCall.args[0]).to.equal(null);
expect(sceneRef.lastCall.args[0]).to.equal(null);
expect(firstCamera.name).to.equal("same camera different name");
expect(firstScene.name).to.equal("same scene different name");
// second one should have dismounted, but the third one will be created
expect(secondCamera.name).to.equal("second camera but without ref this time");
expect(secondScene.name).to.equal("second scene but without ref this time");
expect(secondCamera.parent).to.equal(null);
expect(secondScene.parent).to.equal(null);
const thirdCamera = renderActionGroup.children.filter((child) => child instanceof Camera)[0];
const thirdScene = renderActionGroup.children.filter((child) => child instanceof Scene)[0];
expect(thirdCamera.name).to.equal("third camera");
expect(thirdScene.name).to.equal("third scene");
ReactThreeRenderer.unmountComponentAtNode(testCanvas, done);
});
it("should accept a scene as a parameter", (done) => {
mockConsole.expectLog("THREE.WebGLRenderer", "100");
const renderer = new WebGLRenderer();
const renderCallSpy = Sinon.spy(renderer, "render");
const perspectiveCameraSpy = Sinon.spy();
const scene = new Scene();
ReactThreeRenderer.render(<render
camera={<perspectiveCamera ref={perspectiveCameraSpy}/>}
scene={scene}
/>, renderer);
requestAnimationFrame(() => {
const lastCall = renderCallSpy.lastCall;
expect(lastCall.args[0], "Scene from object reference should have been used")
.to.equal(scene);
expect(lastCall.args[1], "Camera from element should have been used")
.to.equal(perspectiveCameraSpy.lastCall.args[0]);
ReactThreeRenderer.unmountComponentAtNode(testDiv, done);
});
});
it("should accept a camera as a parameter", (done) => {
mockConsole.expectLog("THREE.WebGLRenderer", "100");
const renderer = new WebGLRenderer();
const renderCallSpy = Sinon.spy(renderer, "render");
const sceneRef = Sinon.spy();
const camera = new PerspectiveCamera();
ReactThreeRenderer.render(<render
camera={camera}
scene={<scene ref={sceneRef}/>}
/>, renderer);
requestAnimationFrame(() => {
const lastCall = renderCallSpy.lastCall;
expect(lastCall.args[0], "Scene from element should have been used")
.to.equal(sceneRef.lastCall.args[0]);
expect(lastCall.args[1], "Camera from element should have been used")
.to.equal(camera);
ReactThreeRenderer.unmountComponentAtNode(testDiv, done);
});
});
it("should trigger a render when a visible element is added or removed", (done) => {
mockConsole.expectLog("THREE.WebGLRenderer", "100");
const renderer = new WebGLRenderer();
const renderCallSpy = Sinon.spy(renderer, "render");
let setChildren: ((childCount: number, callback: () => void) => void) | null = null;
class TestContainer extends React.Component<any, { childCount: number }> {
constructor(props: any, context: any) {
super(props, context);
this.state = {
childCount: 0,
};
setChildren = (childCount: number, callback: () => void) => {
this.setState({
childCount,
}, () => {
requestAnimationFrame(callback);
});
};
}
public render() {
const children = [];
for (let i = 0; i < this.state.childCount; ++i) {
children.push(<object3D key={`${i}`}/>);
}
return <object3D>
{children}
</object3D>;
}
}
ReactThreeRenderer.render(<render
camera={<perspectiveCamera/>}
scene={<scene>
<TestContainer/>
</scene>}
/>, renderer);
if (setChildren == null) {
return;
}
const updateChildren: ((childCount: number, callback: () => void) => void) = setChildren;
requestAnimationFrame(() => {
expect(renderCallSpy.callCount).to.equal(1);
requestAnimationFrame(() => {
// nothing has changed!
expect(renderCallSpy.callCount).to.equal(1);
// add a child
updateChildren(1, () => {
// it should render
expect(renderCallSpy.callCount).to.equal(2);
// do not add a child
updateChildren(1, () => {
// should not have rendered!
expect(renderCallSpy.callCount).to.equal(2);
// add another child
updateChildren(2, () => {
// should have rendered!
expect(renderCallSpy.callCount).to.equal(3);
// remove children
updateChildren(0, () => {
// should have rendered!
expect(renderCallSpy.callCount).to.equal(4);
ReactThreeRenderer.unmountComponentAtNode(testDiv, done);
});
});
});
});
});
});
});
it("should trigger a render only when a visible property is updated", (done) => {
mockConsole.expectLog("THREE.WebGLRenderer", "100");
const renderer = new WebGLRenderer();
let nameChangeEvent: any = null;
let positionChangeEvent: any = null;
class ObjectStateChanger extends React.Component<any, {
name: string,
position: Vector3,
}> {
constructor(props: any, context: any) {
super(props, context);
this.state = {
name: "test",
position: new Vector3(),
};
}
public componentDidMount() {
nameChangeEvent = (newName: string, callback: () => {}) => {
this.setState({
name: newName,
}, callback);
};
positionChangeEvent = (newPosition: Vector3, callback: () => {}) => {
this.setState({
position: newPosition,
}, callback);
};
}
public render() {
return <object3D
position={this.state.position}
name={this.state.name}
/>;
}
}
const changerSpy = Sinon.spy();
const renderRef = Sinon.spy();
ReactThreeRenderer.render(<render
ref={renderRef}
camera={<perspectiveCamera/>}
scene={<scene>
<ObjectStateChanger ref={changerSpy}/>
</scene>}/>, renderer);
const render: RenderAction = renderRef.lastCall.args[0];
const renderCallSpy = Sinon.spy(render, "triggerRender");
expect(renderCallSpy.callCount).to.equal(0);
const obj: Object3D = ReactThreeRenderer.findTHREEObject(changerSpy.lastCall.args[0]);
positionChangeEvent(new Vector3(1, 2, 3), () => {
expect(renderCallSpy.callCount).to.equal(1);
expect(obj.position.equals(new Vector3(1, 2, 3))).to.equal(true);
nameChangeEvent("new name", () => {
expect(renderCallSpy.callCount).to.equal(1);
expect(obj.name).to.equal("new name");
positionChangeEvent(new Vector3(3, 2, 1), () => {
expect(renderCallSpy.callCount).to.equal(2);
expect(obj.position.equals(new Vector3(3, 2, 1))).to.equal(true);
nameChangeEvent("newer name", () => {
expect(renderCallSpy.callCount).to.equal(2);
expect(obj.name).to.equal("newer name");
ReactThreeRenderer.unmountComponentAtNode(renderer, () => {
done();
});
});
});
});
});
});
});