-
Notifications
You must be signed in to change notification settings - Fork 0
/
m.html
195 lines (157 loc) · 5.99 KB
/
m.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
<!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>
/*
triangles
what i want to do is draw three equilateral triangles
that touch corners
forming another equilateral triangle
*/
var debugging = false;
var half_window_w = window.innerWidth / 2;
var half_window_h = window.innerHeight / 2;
var origin = new THREE.Vector3(); // 0 0 0
var bounds_radius_lower = 10;
var bounds_radius_upper = window.innerWidth / 2 - bounds_radius_lower;
if (debugging) {
console.log( "WINDOW: " + half_window_w + "x" + half_window_h );
console.log( "ORIGIN: " + origin.x.toString() + "x" + origin.y.toString() );
}
//
// COLORS
var colors = [
'#5DD9C1',
'#ACFCD9',
'#B084CC',
'#665687',
'#190933'
];
var scene = new THREE.Scene();
scene.background = new THREE.Color( 'white' );
var camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
1,
500
);
camera.position.set( 0, 0, 100 );
camera.lookAt( new THREE.Vector3() );
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// pick a random color
var random_color = colors[Math.floor(Math.random() * colors.length)];
if (debugging) { console.log(random_color); }
// pick a random size
var side = 50;
if (debugging) {
console.log("SIDE: " + side.toString());
}
if (debugging) {
drawX(origin, scene, '#ff0000');
}
var transparency = 0.8;
var material = new THREE.LineBasicMaterial( {
color: random_color,
transparent: true,
opacity: transparency
} );
// so
// i need three points
// equally distant from origin (random_height-ish)
// and equally distant from each other (equilateral)
// https://en.wikipedia.org/wiki/Equilateral_triangle
var point_a, point_b, point_c;
var shape;
var h = (Math.sqrt(3) / 2) * side;
if (debugging) {
console.log("H: " + h.toString());
}
//
// first triangle, above origin
point_a = new THREE.Vector3( 0 - (side / 2), (h/3), 0 );
point_b = new THREE.Vector3( side / 2, (h/3), 0 );
point_c = new THREE.Vector3( 0, h + (h/3), 0 );
if (debugging) {
console.log(
point_a.x.toString() + "x" + point_a.y.toString() + ", " +
point_b.x.toString() + "x" + point_b.y.toString() + ", " +
point_c.x.toString() + "x" + point_c.y.toString()
);
drawX( point_a, scene, random_color );
drawX( point_b, scene, random_color );
drawX( point_c, scene, random_color );
}
shape = drawTriangle( point_a, point_b, point_c, material );
scene.add( shape );
//
// second triangle, to the left
// point a doesn't need to change
point_b = new THREE.Vector3( 0, 0 - ((h/3) * 2), 0 );
point_c = new THREE.Vector3( 0 - side, 0 - ((h/3) * 2), 0 );
if (debugging) {
console.log(
point_a.x.toString() + "x" + point_a.y.toString() + ", " +
point_b.x.toString() + "x" + point_b.y.toString() + ", " +
point_c.x.toString() + "x" + point_c.y.toString()
);
drawX( point_a, scene, random_color );
drawX( point_b, scene, random_color );
drawX( point_c, scene, random_color );
}
shape = drawTriangle( point_a, point_c, point_b, material );
scene.add( shape );
//
// third triangle, to the right
point_a = new THREE.Vector3( side / 2, (h/3), 0 );
point_b = new THREE.Vector3( 0, 0 - ((h/3) * 2), 0 );
point_c = new THREE.Vector3( side, 0 - ((h/3) * 2), 0 );
if (debugging) {
console.log(
point_a.x.toString() + "x" + point_a.y.toString() + ", " +
point_b.x.toString() + "x" + point_b.y.toString() + ", " +
point_c.x.toString() + "x" + point_c.y.toString()
);
drawX( point_a, scene, random_color );
drawX( point_b, scene, random_color );
drawX( point_c, scene, random_color );
}
shape = drawTriangle( point_b, point_c, point_a, material );
scene.add( shape );
renderer.render( scene, camera );
function drawTriangle(pa, pb, pc, material) {
var geometry = new THREE.Geometry();
geometry.vertices.push( pa );
geometry.vertices.push( pb );
geometry.vertices.push( pc );
var face = new THREE.Face3( 0, 1, 2 );
geometry.faces.push( face );
geometry.computeFaceNormals();
geometry.computeVertexNormals();
return new THREE.Mesh( geometry, material );
}
function drawX(v3, scene, c) {
var material = new THREE.LineBasicMaterial( { color: c } );
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( v3.x + 3, v3.y + 3, 0 ) );
geometry.vertices.push( new THREE.Vector3( v3.x - 3, v3.y - 3, 0 ) );
scene.add( new THREE.Line( geometry, material ) );
geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( v3.x - 3, v3.y + 3, 0 ) );
geometry.vertices.push( new THREE.Vector3( v3.x + 3, v3.y - 3, 0 ) );
scene.add( new THREE.Line( geometry, material ) );
}
</script>
</body>
</html>