-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_lines.html
234 lines (189 loc) · 7.35 KB
/
sketch_lines.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lines Sketch</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;600&display=swap" rel="stylesheet">
<link href="./style.css?v=3" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container">
<div id="canvasFrame">
<canvas id="mainCanvas"></canvas>
<div id="canvasOutput">
Press 'R' to reset the cloud torus. Drag the mouse to control the camera.
</div>
</div>
<div id="detailsFrame">
<div id="detailsTitle">100,000 LINES</div>
<div id="detailsBlurb">
Exploring BufferGeometry and Line Segments in THREE.JS. A basic custom shader is used
to handle the transparency and additive blending of the lines. Each particle will randomly
roam through the system, leaving a diminishing tail of 100 individual line segments behind
to indicate their unique path. Simple easing keeps them close to their original spawn point.
</div>
<button id="btnGithub" onclick="location.href='https://github.com/mrharfang';"></button>
</div>
<div id="projectsFrame">
<input type="radio" onclick="window.location='index.html';" title="Gray-Scott Reactions"/>
<input type="radio" checked title="100,000 lines"/>
<input type="radio" onclick="window.location='sketch2.html';"title="Study: Mycelial Growth" />
<input type="radio" onclick="window.location='sketch3.html';" title="Mycelial Portraits"/>
</div>
<div id="footerFrame">
RICKBARRAZA.COM
</div>
</div>
<!-- WE NEED TO SETUP A CUSTOM SHADER SOLUTION SINCE WE WANT TO USE COLORS WITH TRANSPARENCY -->
<script id="vertexShader" type="x-shader/x-vertex">
attribute vec4 color;
varying vec4 vColor;
void main() {
vColor = color;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
varying vec4 vColor;
void main() {
vec4 color = vec4( vColor );
gl_FragColor = color;
}
</script>
<script type="module">
import * as THREE from './libs/three.module.js';
import { OrbitControls } from './libs/OrbitControls.js';
var canvas;
var width = 930.0;
var height = 600.0;
var mRenderer, mScene, mCamera;
var geometry;
var line;
var vec = new THREE.Vector3();
var pos = new THREE.Vector3();
var mMouseX, mMouseY;
var sparkles = [];
var totalSparkles = 1000;
var tailLength = 100;
var maxAlpha = .2;
class Sparkle {
constructor(pos) {
this.locations = [];
this.r = Math.random();
this.b = 1.0;
this.g = Math.random();
this.home = pos;
for ( var i = 0; i < tailLength; i++ ) {
this.locations.push(pos);
}
}
position() {
return this.locations[0];
}
newPosition(pos) {
this.locations.unshift(pos);
this.locations.pop();
}
};
function initData() {
sparkles = [];
var radius = 50;
for ( var i = 0; i < totalSparkles; i++ ) {
var x = Math.cos(i*.5) * radius;
var y = 0;
var z = Math.sin(i*.5) * radius;
var temp = new Sparkle(new THREE.Vector3(x,y,z));
sparkles.push(temp);
}
}
function init() {
canvas = document.getElementById('mainCanvas');
canvas.onmousedown = onMouseDown;
mCamera = new THREE.PerspectiveCamera(35, width/height, 1, 4000 )
mCamera.position.z = 250;
mCamera.position.y = 150;
mScene = new THREE.Scene();
// SETUP GEOMETRY
geometry = new THREE.BufferGeometry();
var material = new THREE.ShaderMaterial({
vertexShader: document.getElementById('vertexShader').textContent,
fragmentShader: document.getElementById('fragmentShader').textContent,
transparent: true,
blending: THREE.AdditiveBlending,
depthTest: false
});
initData();
updateData();
geometry.computeBoundingSphere();
// SET GEOMETRY AND MATERIAL TO LINE OBJECT
line = new THREE.LineSegments(geometry, material );
mScene.add( line );
// SETUP RENDERER LAST TO VISUALIZE ABOVE CHANGES
mRenderer = new THREE.WebGLRenderer({ canvas: canvas });
mRenderer.setPixelRatio(window.devicePixelRatio);
mRenderer.aspect = width/height;
mRenderer.setSize(width, height);
mRenderer.outputEncoding = THREE.sRGBEncoding;
var controls = new OrbitControls( mCamera, canvas );
document.addEventListener("keydown", onKeyDown, false);
render(0);
}
function onKeyDown(event) {
switch ( event.which) {
case 82: // 'R' for Reset
initData();
break;
}
}
var render = function(time) {
requestAnimationFrame(render);
updateData();
// RENDER THE SCENE
mRenderer.render(mScene, mCamera);
}
function updateData() {
var positions = [];
var colors = [];
for ( var i = 0; i < sparkles.length; i++) {
var temp = sparkles[i];
var pos = temp.position();
var home = temp.home;
var cx = pos.x + Math.random()*2 - 1;
var cy = pos.y + Math.random()*2 - 1;
var cz = pos.z + Math.random()*2 - 1;
cx += (home.x - cx) * .02;
cy += (home.y - cy) * .02;
cz += (home.z - cz) * .02;
temp.newPosition(new THREE.Vector3(cx, cy, cz));
for ( var p = 0; p < temp.locations.length - 1; p++) {
// DRAW THE TAILS
var alpha = maxAlpha*(1.0 - (p/temp.locations.length));
positions.push(temp.locations[p].x, temp.locations[p].y, temp.locations[p].z );
colors.push(temp.r, temp.g, temp.b, alpha);
positions.push(temp.locations[p+1].x, temp.locations[p+1].y, temp.locations[p+1].z );
colors.push(temp.r, temp.g, temp.b, alpha);
}
}
// SET POSITION AND COLOR TO GEOMETRY
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute(positions, 3));
geometry.setAttribute('color', new THREE.Float32BufferAttribute( colors, 4));
}
function onMouseDown(e) {
var ev = e ? e : window.event;
mMouseX = ev.pageX - canvas.offsetLeft;
mMouseY = ev.pageY - canvas.offsetTop;
// CALCULATE POSITION AT Z = 0
vec.set(
(mMouseX/width) * 2 - 1,
-(mMouseY/height) * 2 + 1,
0.5 );
vec.unproject( mCamera );
vec.sub( mCamera.position ).normalize();
var distance = - mCamera.position.z / vec.z;
pos.copy( mCamera.position ).add( vec.multiplyScalar( distance ) );
console.log(pos.x + ", " + pos.y + ", " + pos.z);
}
init();
</script>
</body>
</html>