This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest7.html
180 lines (134 loc) · 5.29 KB
/
test7.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/93/three.js"></script>
<script src="./drawing-lofi.js"></script>
<style media="screen">
body {
background: #ccc;
}
canvas {
outline: 1px solid #eee;
zoom: 0.5;
}
</style>
</head>
<body>
<div class="container"></div>
<script id="vertexShader" type="x-shader/x-vertex">
void main() {
gl_Position = vec4(position, 1.0);
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D pointsTexture;
uniform float pointLength;
// uniform vec2 resolution;
// Mostly ripped from https://www.shadertoy.com/view/4lGSDw,
// which has a lot more going including actually drawing the path,
// so check that out. I just wanted to quickly get this drawing.
// catmull version: https://www.shadertoy.com/view/MlGSz3
// Note: This is kind of a shitty hack to get around restrictions
// on variable loop indexes. We'll use this to break out of loops
// over input points. This means pointTextures wont be able to exceed
// whatever this value is. Treat this as temporary.
const int MAX_STEPS = 10000;
float sdSegmentSq( in vec2 p, in vec2 a, in vec2 b ) {
vec2 pa = p-a, ba = b-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
vec2 d = pa - ba*h;
return dot(d,d);
}
float sdPointSq( in vec2 p, in vec2 a ) {
vec2 d = p - a;
return dot(d,d);
}
void main() {
vec2 position = gl_FragCoord.xy;
vec3 col = vec3(1.0);
// Draw points and rigid path
//------------------------------------------------------
{
// Note: This smells. Looks like it was used as an excessive max
// for the following min functions. Ripped from source.
vec2 d = vec2(1000.0);
float dataTexelWidth = 1.0 / pointLength;
for( int i = 0; i < MAX_STEPS; i++ ) {
if (float(i) >= pointLength - 1.0) { break; }
vec2 thisPoint = vec2((float(i) / pointLength), 1.0);
vec2 nextPoint = vec2((float(i) / pointLength) + dataTexelWidth, 1.0);
vec2 a = texture2D(pointsTexture, thisPoint).xy;
vec2 b = texture2D(pointsTexture, nextPoint).xy;
d = min( d, vec2(sdSegmentSq( position,a,b ), sdPointSq(position,a) ) );
}
d.x = sqrt( d.x );
d.y = sqrt( min( d.y, sdPointSq(position, texture2D(pointsTexture, vec2((pointLength - 1.0) / pointLength, 1.0)).xy ) ) );
// line
// float lineThickness = 5.0;
// col = mix( col, vec3(0.0), 1.0-smoothstep(lineThickness, (lineThickness+1.0), d.x) );
// dot
float dotThickness = 2.0;
col = mix( col, vec3(0.9,0.2,0.0), 1.0-smoothstep(dotThickness, (dotThickness+1.0),d.y) );
}
gl_FragColor = vec4(col,1.0);
}
</script>
<script type="text/javascript">
// https://codepen.io/SereznoKot/pen/vNjJWd
// https://jsfiddle.net/andystanton/fvz46esu/
var w = 1920 + (200 * 2);
var h = 1080 + (200 * 2);
var scene = new THREE.Scene();
var camera = new THREE.OrthographicCamera(w / - 2, w / 2, h / 2, h / - 2, 1, 1000);
camera.position.set(0, 0, 100);
var renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(w, h);
document.querySelector('.container').appendChild(renderer.domElement);
var planeGeo = new THREE.PlaneBufferGeometry(w, h);
var material = new THREE.ShaderMaterial({
uniforms: {
// resolution: { type: "v2", value: new THREE.Vector2(w, h) },
pointsTexture: { type: "t", value: null },
pointLength: { type: "1f", value: 0 }
},
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
});
var plane = new THREE.Mesh(planeGeo, material);
scene.add(plane);
// material.uniforms.resolution.value.x = w;
// material.uniforms.resolution.value.y = h;
var points = DRAWING;
// flatten
var acc = [];
points.forEach((j) => j.forEach((i) => acc.push(i)));
// trim total number of points
points = [acc.slice(0, 1000)];
var drawingId = 0;
function render() {
var rawPointData = points[drawingId];
drawingId++;
if (drawingId >= points.length) drawingId = 0;
var data = new Float32Array(rawPointData.length * 4);
for (var i = 0; i < rawPointData.length; i++) {
var point = rawPointData[i];
data[4*i] = point[0] * 2; // x
data[4*i+1] = point[1] * 2; // y
data[4*i+2] = 0.0;
data[4*i+3] = 0.0;
}
var dataTex = new THREE.DataTexture(data, rawPointData.length, 1, THREE.RGBAFormat, THREE.FloatType);
dataTex.needsUpdate = true;
material.uniforms.pointsTexture.value = dataTex;
material.uniforms.pointLength.value = rawPointData.length;
/*Render*/
renderer.render(scene, camera);
requestAnimationFrame(render);
}
render();
</script>
</body>
</html>