-
Notifications
You must be signed in to change notification settings - Fork 0
/
c.html
213 lines (175 loc) · 6.52 KB
/
c.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
<!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>
/*
so what i want to do here is
pick a random point on the screen, within bounds
this is our origin
loop
pick a random point on the screen
draw triangles that point to our origin
we need to keep in mind that 0,0 is not the upper left corner
it's the center of the window
*/
var debugging = false;
//
// 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(
getRandomPoint( bounds, half_window_w ),
getRandomPoint( bounds, half_window_h ),
0
);
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
//
// COLORS
var colors = [
'#D6D2D2',
'#F1E4F3',
'#F4BBD3',
'#F686BD',
'#FE5D9F'
];
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
1,
500
);
camera.position.set( 0, 0, 300 );
camera.lookAt( new THREE.Vector3() );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
/*
// SAFETY X at 0,0,0
var material = new THREE.LineBasicMaterial( { color: 0xff0000 } );
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( 3, 3, 0 ) );
geometry.vertices.push( new THREE.Vector3( -3, -3, 0 ) );
var line = new THREE.Line( geometry, material );
scene.add( line );
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( -3, 3, 0 ) );
geometry.vertices.push( new THREE.Vector3( 3, -3, 0 ) );
var line = new THREE.Line( geometry, material );
scene.add( line );
*/
/*
// ORIGIN X
var material = new THREE.LineBasicMaterial( { color: 0xffffff } );
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( origin.x + 3, origin.y + 3, 0 ) );
geometry.vertices.push( new THREE.Vector3( origin.x - 3, origin.y - 3, 0 ) );
var line = new THREE.Line( geometry, material );
scene.add( line );
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( origin.x - 3, origin.y + 3, 0 ) );
geometry.vertices.push( new THREE.Vector3( origin.x + 3, origin.y - 3, 0 ) );
var line = new THREE.Line( geometry, material );
scene.add( line );
*/
var num_triangles = getRandomInt( bounds, bounds * 10 );
//num_triangles = 50;
if (debugging) { console.log( num_triangles + " triangles" ); }
for (var l = 0; l < num_triangles; l++) {
if (debugging) { console.log("@" + l.toString()); }
// pick a random color
var random_color = colors[Math.floor(Math.random() * colors.length)];
if (debugging) { console.log(random_color); }
// pick a random starting place
var random_point = new THREE.Vector3(
getRandomPoint( 0, half_window_w ),
getRandomPoint( 0, half_window_h ),
0
);
if (debugging) {
console.log(
random_point.x.toString() + "x" + random_point.y.toString()
);
}
var distance_from_origin = random_point.distanceTo(origin);
if (distance_from_origin < 100) {
if (debugging) { console.log('too close'); }
continue;
} else {
if (debugging) {
console.log("DISTANCE: " + distance_from_origin.toString());
}
}
var d = 100 / distance_from_origin;
d = 1 - (1 - d);
var line = new THREE.Line3( origin, random_point );
var end_point = new THREE.Vector3();
line.at( d, end_point );
if (debugging) {
console.log(end_point.x.toString() + "x" + end_point.y.toString());
}
/*
so random_point is one corner of the triangle
end_point is another corner, the closest to origin
we need to figure out the third point
*/
var foo = Math.random() / 4; // something like 0 .. 0.25?
foo += 0.75; // something like 0.75 .. 1?
var third_point = new THREE.Vector3();
line.at( foo, third_point );
if (debugging) {
console.log(third_point.x.toString() + "x" + third_point.y.toString());
}
/*
okay, that's somewhere alone the line between random_point and origin
let's adjust that "up" or "down"
*/
var foo = getRandomInt(5, 50);
if (Math.random() < 0.5) {
foo = foo * -1;
}
third_point.setY(third_point.y + foo);
var material = new THREE.LineBasicMaterial( {
color: random_color,
transparent: true,
opacity: Math.random()
} );
var geometry = new THREE.Geometry();
geometry.vertices.push( random_point );
geometry.vertices.push( end_point );
geometry.vertices.push( third_point );
var face = new THREE.Face3( 0, 1, 2 );
geometry.faces.push( face );
geometry.computeFaceNormals();
geometry.computeVertexNormals();
scene.add( new THREE.Mesh( geometry, material ) );
}
renderer.render( scene, camera );
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>