-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
318 lines (249 loc) · 9.42 KB
/
Copy pathmain.js
File metadata and controls
318 lines (249 loc) · 9.42 KB
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
import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import { GUI } from 'three/addons/libs/lil-gui.module.min.js'
class DiskMeter {
scene
camera
renderer
projector
gui
params = {
numBase: 10,
rotationSpeed: 8,
disksCount: 4,
}
#controls
#counterInterval = null;
#counter = 0;
#counter3D = new THREE.Group();
#timeToNextTick = 0
#maxNumber = 9999
#textures = {
2: null,
8: null,
10: null,
16: null,
}
#currentTexture = null
constructor(scene, camera, renderer) {
this.scene = scene
this.camera = camera
this.renderer = renderer
this.#controls = new OrbitControls(this.camera, this.renderer.domElement)
this.#controls.enableDamping = true
this.gui = new GUI()
this.camera.position.set(0, 0, 25)
this.#setupListeners()
this.#setupGUI()
this.#prepareTextures()
this.startCounter()
this.scene.add(this.#counter3D)
}
startCounter() {
if (this.#counterInterval !== null) {
console.warn("Attempt to start already started counter")
return
}
this.#counterInterval = window.setInterval(() => {
if (this.#counter === this.#maxNumber) {
this.#counter = 0
} else {
this.#counter += 1
}
this.#timeToNextTick = Date.now() + 1000 / this.params.rotationSpeed
}, 1000 / this.params.rotationSpeed)
this.#timeToNextTick = Date.now() + 1000 / this.params.rotationSpeed
}
stopCounter() {
window.clearInterval(this.#counterInterval)
this.#counterInterval = null
}
resetCounter() {
this.#counter = 0
}
#setupGUI() {
this.params.startCounter = () => { this.startCounter() }
this.params.stopCounter = () => { this.stopCounter() }
this.params.resetCounter = () => { this.resetCounter() }
this.gui
.add(this.params, 'numBase', [2, 8, 10, 16])
.name("Numeric system base")
.onChange((n) => {
this.#maxNumber = Math.pow(n, this.getDisksCount()) - 1
this.#currentTexture = this.#textures[n]
for (let disk of this.#counter3D.children) {
disk.material.map = this.#currentTexture
}
this.#updateCounterLimits(n, this.getDisksCount())
})
this.gui
.add(this.params, 'rotationSpeed', 0, 120)
.name("Speed")
.onChange((n) => {
if (this.#counterInterval !== null) {
this.stopCounter()
if (n > 0) {
this.startCounter()
}
}
})
this.gui
.add(this.params, 'disksCount', 1, 16)
.name("Number of digits")
.step(1)
.onChange((diskCount) => {
this.#maxNumber = Math.pow(this.params.numBase, diskCount) - 1
this.#updateCounterLimits(this.params.numBase, diskCount)
})
this.gui
.add(this.params, 'startCounter')
.name('Start')
this.gui
.add(this.params, 'stopCounter')
.name('Stop')
this.gui
.add(this.params, 'resetCounter')
.name('Reset')
}
#updateCounterLimits(numBase, disksCount) {
const oldCounterDisplay = this.#counter.toString(numBase)
const newCounterDisplay = oldCounterDisplay.substring(oldCounterDisplay.length - disksCount)
this.#counter = parseInt(newCounterDisplay, numBase)
}
getDisksCount() {
return this.#counter3D.children.length
}
#prepareTextures() {
const loader = new THREE.TextureLoader()
this.#textures[2] = loader.load("textures/tex2.png")
this.#textures[8] = loader.load("textures/tex8.png")
this.#textures[10] = loader.load("textures/tex10.png")
this.#textures[16] = loader.load("textures/tex16.png")
for (let [, texture] of Object.entries(this.#textures)) {
texture.wrapS = THREE.RepeatWrapping
texture.wrapT = THREE.RepeatWrapping
texture.rotation = -(Math.PI / 2)
}
this.#currentTexture = this.#textures[this.params.numBase]
}
#addDisk() {
const disk = new THREE.Mesh(
new THREE.CylinderGeometry(1, 1, 0.225, 39),
new THREE.MeshBasicMaterial({ map: this.#currentTexture })
)
const diskSideGeometry = new THREE.CylinderGeometry(1, 1, 0.001, 39)
const diskSideMaterial = new THREE.MeshBasicMaterial({ color: 0x111111 })
const diskSideL = new THREE.Mesh(diskSideGeometry, diskSideMaterial)
const diskSideR = new THREE.Mesh(diskSideGeometry, diskSideMaterial)
disk.rotateX(Math.PI / 2)
disk.rotateZ(Math.PI / 2)
disk.position.x = this.#counter3D.children.length * 0.25
this.#counter3D.position.x -= 0.25 / 2
diskSideL.position.y += 0.115
diskSideR.position.y -= 0.115
disk.add(diskSideL)
disk.add(diskSideR)
// Reset counter to 0
for (let exDisk of this.#counter3D.children) {
exDisk.rotation.x = disk.rotation.x
}
this.#counter3D.add(disk)
}
#removeDisk() {
if (this.getDisksCount() == 0) {
console.warn("removeDisk: attempt to remove disk from empty counter ignored")
return
}
this.#counter3D.remove(this.#counter3D.children[this.getDisksCount() - 1])
this.#counter3D.position.x += 0.25 / 2
}
#setupListeners() {
window.addEventListener('resize', this.#onResize.bind(this))
}
#onResize(e) {
this.camera.aspect = window.innerWidth / window.innerHeight
this.camera.updateProjectionMatrix()
this.renderer.setSize(window.innerWidth, window.innerHeight)
}
getDigitValue(index) {
// Returns integer value of digit of current numeric base at index
const numRepresentationInCurrentBase = this.#counter.toString(this.params.numBase)
const zFilled = numRepresentationInCurrentBase.padStart(this.getDisksCount(), '0')
return parseInt(zFilled[index], this.params.numBase)
}
getRightDigitsSum(index) {
// Returns sum of integer values of digits of current numeric base starting from index
const numRepresentationInCurrentBase = this.#counter.toString(this.params.numBase)
const zFilled = numRepresentationInCurrentBase.padStart(this.getDisksCount(), '0')
let sum = 0
for (let digit of zFilled.substring(index)) {
sum += parseInt(digit, this.params.numBase)
}
return sum
}
renderRotation(disks) {
const ZERO_ANGLE_EULER = 1.5707963267948972
const singleNumberAngleRad = (Math.PI * 2) / this.params.numBase
const currentTime = Date.now()
const rPercent = (this.#timeToNextTick - currentTime) / (1000 / this.params.rotationSpeed)
if (currentTime > this.#timeToNextTick) {
return
}
for (const [index, disk] of disks.entries()) {
const rIndex = disks.length - index - 1
let digitValue = this.getDigitValue(index)
disk.rotation.x = ZERO_ANGLE_EULER
disk.rotateY(singleNumberAngleRad * digitValue)
let prevDisk = null
if (index < disks.length - 1) {
prevDisk = disks[index + 1]
}
if (rIndex == 0) {
disk.rotateY(-singleNumberAngleRad * rPercent)
} else if (prevDisk && this.getRightDigitsSum(index + 1) == 0) {
disk.rotateY(-singleNumberAngleRad * rPercent)
}
}
}
renderDisksCountChange(disks) {
if (this.params.disksCount !== disks.length) {
const delta = this.params.disksCount - disks.length
if (delta < 0) {
for (let i = delta; i != 0; i++) {
this.#removeDisk()
return
}
}
if (delta > 0) {
for (let i = 0; i < delta; i++) {
this.#addDisk()
return
}
}
}
}
animate() {
this.#animate()
}
#animate() {
const disks = this.#counter3D.children
this.renderDisksCountChange(disks)
this.renderRotation(disks)
this.#controls.update()
}
}
const scene = new THREE.Scene()
scene.background = new THREE.Color('#EEE')
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 2000)
camera.zoom = 4
camera.updateProjectionMatrix()
const renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
const dm = new DiskMeter(scene, camera, renderer)
function animate() {
requestAnimationFrame(animate)
dm.animate()
renderer.render(scene, camera)
}
animate()