-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk5.html
215 lines (170 loc) · 6 KB
/
k5.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
211
212
213
214
215
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>three.js</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script src="../js/three.js"></script>
<script src="../js/helpers.js"></script>
<script>
/*
hexes
*/
var debugging = true;
//
// FIND OUR ORIGIN
var bounds = 100; // pixels from the edge of the window
var half_window_w = window.innerWidth / 2;
var half_window_h = window.innerHeight / 2;
var origin = new THREE.Vector3();
if (debugging) {
console.log( "WINDOW: " + half_window_w + "x" + half_window_h );
console.log( "BOUNDS: " + bounds );
console.log( "ORIGIN: " + origin.x.toString() + "x" + origin.y.toString() );
}
// okay that wasn't so bad
// Load the background texture
var texture = THREE.ImageUtils.loadTexture( "img/638_PIA20732_800w.jpg" );
var backgroundMesh = new THREE.Mesh(
new THREE.PlaneGeometry(2, 2 * (1000/800)),
new THREE.MeshBasicMaterial({
map: texture
}));
backgroundMesh.material.depthTest = false;
backgroundMesh.material.depthWrite = false;
// Create your background scene
var backgroundScene = new THREE.Scene();
var backgroundCamera = new THREE.Camera();
backgroundScene.add(backgroundCamera );
backgroundScene.add(backgroundMesh );
// foreground
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
1,
1000
);
camera.position.set( 0, 0, 750 );
camera.lookAt( new THREE.Vector3() );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.autoClear = false;
document.body.appendChild( renderer.domElement );
var num_shapes = getRandomInt( bounds / 10, bounds );
if (debugging) { console.log( num_shapes + " shapes" ); }
var angle_incr = 15 * Math.PI/180; // degrees, converted to radians
var outer_rad = window.innerWidth * 0.8;
for (var l = 0; l < num_shapes; l++) {
if (debugging) { console.log("@" + l.toString()); }
// where is this going?
// https://krazydad.com/tutorials/circles/showexample.php?ex=basic_spiral
var ratio = l / num_shapes;
var angle = l * angle_incr;
var spiral_rad = ratio * outer_rad;
var x = Math.cos(angle) * spiral_rad;
var y = Math.sin(angle) * spiral_rad;
var hex_point = new THREE.Vector3( x, y, 0 );
if (debugging) {
console.log(
hex_point.x.toString() + "x" + hex_point.y.toString()
);
}
var hex_size = 10 + (l*3);
var mesh = make_hex( hex_point, hex_size, '#005377', l );
if (Math.random() < 0.5) {
var spin = (30 * Math.PI) / 180;
mesh.rotateZ(spin);
}
scene.add( mesh );
}
function animateAllTheThings() {
requestAnimationFrame( animateAllTheThings );
renderer.clear();
renderer.render( backgroundScene, backgroundCamera );
renderer.render( scene, camera );
}
animateAllTheThings();
function make_hex(p, r, c, depth) {
// calculate a transparency based on that
var transparency = calculateTransparency(depth) / 2;
if (debugging) {
console.log("DEPTH (z) " + depth.toString());
console.log("TRANSPARENCY " + transparency.toString());
}
var hex = new THREE.Shape( hex_points( p, r ) );
var geometry = new THREE.ShapeGeometry( hex );
var material = new THREE.LineBasicMaterial( {
color: c,
transparent: true,
opacity: transparency
} );
var mesh = new THREE.Mesh( geometry, material );
mesh.position.z = depth;
return mesh;
}
function make_hex_line( p, r, c ) {
var depth = getRandomInt(0, 100);
var hex = new THREE.Shape( hex_points( p, r, true ) );
var geometry = new THREE.ShapeGeometry( hex );
var material = new THREE.LineBasicMaterial( { color: '#fffff' });
var shape = new THREE.Line( geometry, material );
shape.position.z = depth;
return shape;
}
/*
totally (more or less) copied this from
https://codepen.io/jamaak/pen/KqOzom
*/
function hex_points( p, r, return_to_origin = false ) {
var points = [];
points.push( new THREE.Vector3(
p.x,
p.y + r
) );
points.push( new THREE.Vector3(
p.x + r * 0.866,
p.y + r * 0.5
) );
points.push( new THREE.Vector3(
p.x + r * 0.866,
p.y - r * 0.5
) );
points.push( new THREE.Vector3(
p.x,
p.y - r
) );
points.push( new THREE.Vector3(
p.x - r * 0.866,
p.y - r * 0.5
) );
points.push( new THREE.Vector3(
p.x - r * 0.866,
p.y + r * 0.5
) );
if ( return_to_origin ) {
points.push( new THREE.Vector3(
p.x,
p.y + (r-0.01) // I don't know why you can't go to the same point
) );
}
console.log(points);
return points;
}
function getRandomPoint(lower, upper) {
var p = getRandomInt( lower, upper );
// positive or negative?
var whatwhat = Math.random();
if (whatwhat > 0.5) {
p = p * -1;
}
return p;
}
</script>
</body>
</html>