-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
210 lines (165 loc) · 7.66 KB
/
index.html
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
<!-- Gary Ang | Ming | playgrd -->
<!-- three.js port of Daniel Shiffman's Nature of Code Chapter 2-->
<!-- Game of life in three.js -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Nature of Code in three.js</title>
<style>
</style>
<script type="text/javascript" src="./libs/three.js"></script>
<script type="text/javascript" src="./libs/OrbitControls.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.8/d3.min.js" type="text/JavaScript"></script>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/josephg/noisejs/master/perlin.js"></script>
</head>
<body>
<div id='svg'></div>
<script>
var p5 = new p5();
var scene = new THREE.Scene();
// var camera = new THREE.PerspectiveCamera( 1000, window.innerWidth / window.innerHeight, 0.1, 1000 );
var camera = new THREE.OrthographicCamera( 0,500,0,500, 0.1, 1000 );
camera.position.z = 30;
var renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setClearColor( 0xF9B3D1, 1 );
document.body.appendChild( renderer.domElement );
var orbit = new THREE.OrbitControls( camera, renderer.domElement );
orbit.enableZoom = false;
var ambientLight = new THREE.AmbientLight( '#FFFFFF' ),
hemiLight = new THREE.HemisphereLight('#FFFFFF', '#FFFFFF', 0 ),
light = new THREE.PointLight( '#FFFFFF', 1, 100 );
hemiLight.position.set( 0, 0, 0 );
light.position.set( 0, 0, 10 );
scene.add( ambientLight );
scene.add( hemiLight );
scene.add( light );
var group = new THREE.Group();
gol = new GOL(500,500);
gol.generate();
gol.display();
scene.add( group );
var prevFog = true;
var render = function () {
requestAnimationFrame( render );
// orbit.update();
gol.generate();
gol.display();
// group.rotation.x += 0.2;
// group.rotation.y += 0.2;
// group.rotation.z += 0.002;
renderer.render( scene, camera );
};
window.addEventListener( 'resize', function () {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}, false );
render();
function Cell(x_, y_, w_) {
this.x = x_;
this.y = y_;
this.w = w_;
this.size = 1;
this.color = 0x000000;
var geometry = new THREE.SphereGeometry(this.size,10,10);
var material = new THREE.MeshToonMaterial({ color: this.color, opacity:0.5, transparent:true, wireframe:true, emissive: 0xFFB4E0,emissiveIntensity:0.1} );
var sphere = new THREE.Mesh(geometry, material);
group.add(sphere);
this.state = Math.floor(random(2));
this.previous = this.state;
this.savePrevious = function() {
this.previous = this.state;
};
this.newState = function(s) {
this.state = s;
};
this.display = function() {
if (this.previous === 0 && this.state == 1) {
this.color = 0x44688E;
this.size = 2;
}
else if (this.state == 1) {
this.color = 0x000000;
this.size = 0.1;
}
else if (this.previous == 1 && this.state === 0) {
this.color = 0xE7B730;
this.size = 3;
}
else {
this.color = 0xED6A5A;
this.size = 4;
}
// console.log(this.color);
sphere.position.x = this.x;
sphere.position.y = this.y;
sphere.scale.x = this.size;
sphere.scale.y = this.size;
sphere.scale.z = this.size;
sphere.material.color.setHex(this.color);
// console.log(sphere.material.color);
};
}
function GOL(width,height) {
this.w = 10;
this.width = width;
this.height = height;
this.columns = this.width/this.w;
this.rows = this.height/this.w;
// Game of life board
this.board = new Array(this.columns);
for (var i = 0; i < this.columns; i++) {
this.board[i] = new Array(this.rows);
}
this.init = function() {
for (var i = 0; i < this.columns; i++) {
for (var j = 0; j < this.rows; j++) {
this.board[i][j] = new Cell(i*this.w, j*this.w, this.w);
}
}
};
this.init();
// The process of creating the new generation
this.generate = function() {
for ( var i = 0; i < this.columns;i++) {
for ( var j = 0; j < this.rows;j++) {
this.board[i][j].savePrevious();
}
}
// Loop through every spot in our 2D array and check spots neighbors
for (var x = 0; x < this.columns; x++) {
for (var y = 0; y < this.rows; y++) {
// Add up all the states in a 3x3 surrounding grid
var neighbors = 0;
for (var i = -1; i <= 1; i++) {
for (var j = -1; j <= 1; j++) {
neighbors += this.board[(x+i+this.columns)%this.columns][(y+j+this.rows)%this.rows].previous;
}
}
// A little trick to subtract the current cell's state since
// we added it in the above loop
neighbors -= this.board[x][y].previous;
// Rules of Life
if ((this.board[x][y].state == 1) && (neighbors < 2)) this.board[x][y].newState(0);
else if ((this.board[x][y].state == 1) && (neighbors > 3)) this.board[x][y].newState(0);
else if ((this.board[x][y].state === 0) && (neighbors == 3)) this.board[x][y].newState(1);
// else do nothing!
}
}
};
// This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0'
this.display = function() {
for ( var i = 0; i < this.columns;i++) {
for ( var j = 0; j < this.rows;j++) {
this.board[i][j].display();
}
}
};
}
</script>
</body>
</html>