|
1 |
| -import * as THREE from 'three' |
2 |
| -import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js' |
3 |
| -import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass.js' |
| 1 | +import * as THREE from 'three'; |
| 2 | +import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer'; |
| 3 | +import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass'; |
4 | 4 | import { TrackballControls } from 'three/examples/jsm/controls/TrackballControls';
|
5 | 5 | import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
|
6 | 6 |
|
7 | 7 | export default class ThreeJSR {
|
8 |
| - constructor (ref, newFrameHook, opts = {}) { |
9 |
| - this.ref = ref |
10 |
| - this.newFrameHook = newFrameHook |
11 |
| - this.camera = {} |
12 |
| - this.passes = opts.passes || [] |
13 |
| - this.updates = [] |
14 |
| - this.controls = {} |
| 8 | + constructor(ref, newFrameHook, opts = {}) { |
| 9 | + this.ref = ref; |
| 10 | + this.newFrameHook = newFrameHook; |
| 11 | + this.camera = {}; |
| 12 | + this.passes = opts.passes || []; |
| 13 | + this.updates = []; |
| 14 | + this.controls = {}; |
15 | 15 | }
|
16 | 16 |
|
17 |
| - afterMount (width, height) { |
18 |
| - ThreeJSR.verifyEnv() |
| 17 | + afterMount(width, height) { |
| 18 | + ThreeJSR.verifyEnv(); |
19 | 19 |
|
20 |
| - this.renderer = new THREE.WebGLRenderer({ antialias: true }) |
| 20 | + this.renderer = new THREE.WebGLRenderer({ antialias: true }); |
21 | 21 |
|
22 |
| - this.createThreeScene() |
| 22 | + this.createThreeScene(); |
23 | 23 |
|
24 |
| - this.onResize(width, height) |
25 |
| - this.ref.current.appendChild(this.renderer.domElement) |
| 24 | + this.onResize(width, height); |
| 25 | + this.ref.current.appendChild(this.renderer.domElement); |
26 | 26 |
|
27 |
| - this.renderer.render(this.scene, this.camera) |
28 |
| - this.renderer.setPixelRatio(window.devicePixelRatio * 1.5) |
29 |
| - this.frameId = requestAnimationFrame(this.newFrameHook) |
| 27 | + this.renderer.render(this.scene, this.camera); |
| 28 | + this.renderer.setPixelRatio(window.devicePixelRatio * 1.5); |
| 29 | + this.frameId = requestAnimationFrame(this.newFrameHook); |
30 | 30 |
|
31 | 31 | if (this.passes.length) {
|
32 |
| - this.composer = new EffectComposer(this.renderer) |
| 32 | + this.composer = new EffectComposer(this.renderer); |
33 | 33 |
|
34 |
| - const renderPass = new RenderPass(this.scene, this.camera) |
35 |
| - this.composer.addPass(renderPass) |
| 34 | + const renderPass = new RenderPass(this.scene, this.camera); |
| 35 | + this.composer.addPass(renderPass); |
36 | 36 |
|
37 |
| - this.passes.forEach(pass => this.composer.addPass(pass)) |
| 37 | + this.passes.forEach((pass) => this.composer.addPass(pass)); |
38 | 38 | }
|
39 | 39 | }
|
40 | 40 |
|
41 |
| - renderNextFrame (timestamp) { |
| 41 | + renderNextFrame(timestamp) { |
42 | 42 | if (timestamp) {
|
43 |
| - this.updates.forEach(update => update()) |
44 |
| - this.renderer.render(this.scene, this.camera) |
45 |
| - this.frameId = requestAnimationFrame(this.newFrameHook) |
| 43 | + this.updates.forEach((update) => update()); |
| 44 | + this.renderer.render(this.scene, this.camera); |
| 45 | + this.frameId = requestAnimationFrame(this.newFrameHook); |
46 | 46 | if (this.composer) {
|
47 |
| - this.composer.render() |
| 47 | + this.composer.render(); |
48 | 48 | }
|
49 | 49 | }
|
50 | 50 | }
|
51 | 51 |
|
52 |
| - addControls () { |
53 |
| - const trackball = new TrackballControls(this.camera, this.renderer.domElement) |
54 |
| - trackball.rotateSpeed = 1.0 |
55 |
| - trackball.zoomSpeed = 1.2 |
56 |
| - trackball.panSpeed = 0.8 |
57 |
| - trackball.keys = [65, 83, 68] |
| 52 | + addControls() { |
| 53 | + const trackball = new TrackballControls(this.camera, this.renderer.domElement); |
| 54 | + trackball.rotateSpeed = 1.0; |
| 55 | + trackball.zoomSpeed = 1.2; |
| 56 | + trackball.panSpeed = 0.8; |
| 57 | + trackball.keys = [65, 83, 68]; |
58 | 58 |
|
59 |
| - const orbit = new OrbitControls(this.camera, this.renderer.domElement) |
| 59 | + const orbit = new OrbitControls(this.camera, this.renderer.domElement); |
60 | 60 |
|
61 |
| - Object.assign(this.controls, { trackball, orbit }) |
| 61 | + Object.assign(this.controls, { trackball, orbit }); |
62 | 62 |
|
63 |
| - trackball.update() |
64 |
| - orbit.update() |
| 63 | + trackball.update(); |
| 64 | + orbit.update(); |
65 | 65 |
|
66 | 66 | this.updates.push(
|
67 | 67 | () => trackball.update(),
|
68 |
| - () => orbit.update() |
69 |
| - ) |
| 68 | + () => orbit.update(), |
| 69 | + ); |
70 | 70 | }
|
71 | 71 |
|
72 |
| - cleanup () { |
| 72 | + cleanup() { |
73 | 73 | if (this.frameId) {
|
74 |
| - cancelAnimationFrame(this.frameId) |
| 74 | + cancelAnimationFrame(this.frameId); |
75 | 75 | }
|
76 | 76 | }
|
77 | 77 |
|
78 |
| - createThreeScene () { |
79 |
| - throw new Error('must implement createThreeScene()') |
| 78 | + createThreeScene() { |
| 79 | + throw new Error('must implement createThreeScene()'); |
80 | 80 | }
|
81 | 81 |
|
82 |
| - onResize (width, height) { |
83 |
| - if (!width || !height) return |
84 |
| - const heightChanged = Math.abs(this.height - height) > 20 |
85 |
| - const widthChanged = Math.abs(this.width - width) > 20 |
86 |
| - if (!heightChanged && !widthChanged && this.width && this.height) return |
87 |
| - |
88 |
| - this.width = width |
89 |
| - this.height = Math.max(height, 200) |
90 |
| - this.camera.aspect = this.width / this.height |
91 |
| - this.camera.updateProjectionMatrix() |
92 |
| - this.renderer.setSize(this.width, this.height) |
| 82 | + onResize(width, height) { |
| 83 | + if (!width || !height) return; |
| 84 | + const heightChanged = Math.abs(this.height - height) > 20; |
| 85 | + const widthChanged = Math.abs(this.width - width) > 20; |
| 86 | + if (!heightChanged && !widthChanged && this.width && this.height) return; |
| 87 | + |
| 88 | + this.width = width; |
| 89 | + this.height = Math.max(height, 200); |
| 90 | + this.camera.aspect = this.width / this.height; |
| 91 | + this.camera.updateProjectionMatrix(); |
| 92 | + this.renderer.setSize(this.width, this.height); |
93 | 93 | }
|
94 | 94 |
|
95 |
| - static verifyEnv () { |
| 95 | + static verifyEnv() { |
96 | 96 | try {
|
97 |
| - const canvas = document.createElement('canvas') |
98 |
| - const meetsRequirements = !!(window.WebGLRenderingContext && |
99 |
| - (canvas.getContext('webgl') || canvas.getContext('experimental-webgl'))) |
| 97 | + const canvas = document.createElement('canvas'); |
| 98 | + const meetsRequirements = !!(window.WebGLRenderingContext |
| 99 | + && (canvas.getContext('webgl') || canvas.getContext('experimental-webgl'))); |
100 | 100 | if (!meetsRequirements) {
|
101 |
| - throw new Error() |
| 101 | + throw new Error(); |
102 | 102 | }
|
103 | 103 | } catch (e) {
|
104 |
| - throw new Error('WebGL is not available') |
| 104 | + throw new Error('WebGL is not available'); |
105 | 105 | }
|
106 | 106 | }
|
107 | 107 | }
|
0 commit comments