-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
199 lines (165 loc) · 3.8 KB
/
main.js
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
import './style.css'
import scene from './Scene'
import {
AmbientLight,
BoxGeometry,
DirectionalLight,
Mesh,
MeshBasicMaterial,
MeshNormalMaterial,
PerspectiveCamera,
PlaneGeometry,
PointLight,
Raycaster,
Vector2,
WebGLRenderer,
} from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import Cell from './Cell'
const cursor = new Vector2(0, 0)
const speed = 0.15
/**
* Grid resolution
*/
const d = Math.max(window.innerWidth, window.innerHeight) / 30
const resolution = {
x: Math.max(Math.floor(d), 50),
y: Math.max(Math.floor(d), 50),
}
/**
* Generate cells
*/
for (let i = 0; i < resolution.x; i++) {
for (let j = 0; j < resolution.y; j++) {
new Cell(resolution, 0.9, speed)
}
}
Cell.cells.forEach((c) => c.computeNeighborsIndexes())
/**
* Plane for ray caster
*/
const planeGeometry = new PlaneGeometry(
resolution.x,
resolution.y,
resolution.x,
resolution.y
)
planeGeometry.rotateX(-Math.PI * 0.5)
const planeMaterial = new MeshBasicMaterial({
color: 0xff0000,
wireframe: true,
opacity: 0,
transparent: true,
})
planeGeometry.translate(-0.5, 0, -0.5)
const plane = new Mesh(planeGeometry, planeMaterial)
scene.add(plane)
/**
* camera
*/
const sizes = {
width: window.innerWidth,
height: window.innerHeight,
}
const camera = new PerspectiveCamera(60)
// camera.position.z = resolution.x / 2
// camera.position.x = -resolution.x / 2
camera.position.y = resolution.x / 2
/**
* Renderer
*/
const renderer = new WebGLRenderer({ antialias: true })
document.body.appendChild(renderer.domElement)
/**
* controls
*/
const controls = new OrbitControls(camera, renderer.domElement)
controls.enableDamping = true
controls.enableRotate = false
controls.enablePan = false
controls.enableZoom = false
onResize()
/**
* lights
*/
const dLight = new DirectionalLight(0xffffff, 0.2)
dLight.position.set(0, 10, 0)
dLight.position.z *= -1
dLight.target.position.set(0, 0, 0)
const pLight = new PointLight(0xffffdd, 5, resolution.x)
pLight.position.y = 10
scene.add(pLight, dLight, dLight.target)
/**
* Handle resize
*/
window.addEventListener('resize', onResize)
function onResize() {
sizes.width = window.innerWidth
sizes.height = window.innerHeight
camera.aspect = sizes.width / sizes.height
camera.updateProjectionMatrix()
renderer.setSize(sizes.width, sizes.height)
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
}
/**
* Raycaster
*/
const raycaster = new Raycaster()
const coords = new Vector2()
/**
* Frame loop
*/
function animate() {
requestAnimationFrame(animate)
camera.rotation.z += 0.0002
raycaster.setFromCamera(cursor, camera)
const intersects = raycaster.intersectObject(plane)
let point = intersects[0]?.point
console.log(point)
if (point) {
coords.x = point.x
coords.y = point.z
const cell = Cell.getCellFromCoords(coords, resolution)
if (!cell.isAlive) {
cell.born()
}
}
// controls.update()
renderer.render(scene, camera)
}
requestAnimationFrame(animate)
/**
* Cellular automata lifecycle
*/
function step() {
const mustDieCell = []
const mustBeBornCell = []
Cell.cells.forEach((cell) => {
if (cell.mustDie) {
mustDieCell.push(cell.index)
}
if (cell.mustBeBorn) {
mustBeBornCell.push(cell.index)
}
})
mustDieCell.forEach((i) => Cell.cells[i].die())
mustBeBornCell.forEach((i) => Cell.cells[i].born())
setTimeout(step, speed * 1000)
}
// window.addEventListener('click', step)
step()
/**
* Cursor coordinates for raycasting
* @param {*} event
*/
function onMouseMove(event) {
// console.log(event)
if ((event.touches || []).length) {
event = event.touches[0]
}
cursor.x = 2 * (event.clientX / window.innerWidth) - 1
cursor.y = -2 * (event.clientY / window.innerHeight) + 1
// console.log(cursor)
}
window.addEventListener('mousemove', onMouseMove)
window.addEventListener('touchmove', onMouseMove)