-
Notifications
You must be signed in to change notification settings - Fork 4
/
ruminative-steven.js
203 lines (181 loc) · 5.15 KB
/
ruminative-steven.js
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
import Alea from 'alea'
import { GUI } from 'dat-gui'
const Delaunator = require('delaunator')
const createRegl = require('regl')
const glslify = require('glslify')
const fit = require('canvas-fit')
const mat4 = require('gl-mat4')
const vec3 = require('gl-vec3')
const createCamera = require('3d-view-controls')
import array from 'new-array'
import includeFont from './common/include-font'
import addTitle from './common/add-title'
import css from 'dom-css'
title('ruminative-steven', '#ddd')
const canvas = document.createElement('canvas')
const camera = createCamera(canvas)
const regl = createRegl(canvas)
let follower
camera.zoomSpeed = 4
camera.lookAt(
[2.5, 2.5, 2.5],
[0, 0, 0],
[0.52, -0.11, 50]
)
window.camera = camera
window.addEventListener('resize', fit(canvas), false)
document.body.appendChild(canvas)
const settings = guiSettings({
seed: [442, 0, 1000, 1, true],
points: [800, 3, 50000, 1, true],
noiseSize: [100, 1, 800, 1],
noiseMag: [15, 1, 100, 1],
speed: [0.5, 0.1, 50, 0.1],
colorVariance: [4, 0, 20, 1],
reflectionMult: [4.5, 0, 20, 0.1],
dotProdMult: [1.3, -20, 20, 0.01],
lightDistance: [1.5, 0, 10, 0.1]
}, setup)
let rand, drawTriangles
setup()
function setup () {
rand = new Alea(settings.seed)
follower = createFollower([1, -1, 0.5])
const points = array(settings.points).map(() => {
const x = rand() * 2 - 1
const y = rand() * 2 - 1
return {
position: [x, y],
color: [rand(), rand(), rand()]
}
})
const delaunay = new Delaunator(points.map(p => p.position))
const positions = []
const adjacentPositionsA = []
const adjacentPositionsB = []
const colors = []
for (let j = 0; j < delaunay.triangles.length; j += 3) {
const p1 = points[delaunay.triangles[j]]
const p2 = points[delaunay.triangles[j + 1]]
const p3 = points[delaunay.triangles[j + 2]]
positions.push(p1.position, p2.position, p3.position)
adjacentPositionsA.push(p2.position, p3.position, p1.position)
adjacentPositionsB.push(p3.position, p1.position, p2.position)
colors.push(p1.color, p2.color, p3.color)
}
window.delaunay = delaunay
drawTriangles = regl({
attributes: {
position: positions,
color: colors,
adjacentPositionA: adjacentPositionsA,
adjacentPositionB: adjacentPositionsB
},
count: positions.length
})
}
const drawGlobal = regl({
vert: glslify.file('./shaders/ruminative-steven.vert'),
frag: glslify.file('./shaders/simple.frag'),
uniforms: {
projection: ({viewportWidth, viewportHeight}) => (
mat4.perspective([],
Math.PI / 4,
viewportWidth / viewportHeight,
0.01,
1000)
),
view: () => camera.matrix,
lightSource: regl.prop('lightSource'),
colorVariance: () => settings.colorVariance / 100,
baseColor: [0.03, 0.13, 0.5],
reflectionMult: () => settings.reflectionMult,
dotProdMult: () => settings.dotProdMult,
noiseMag: () => settings.noiseMag,
noiseSize: () => settings.noiseSize,
tick: ({ tick }) => tick,
speed: () => settings.speed
},
primitive: 'triangles'
})
let lastTime = -1000
regl.frame(({ time }) => {
if (time - lastTime > 4) {
follower.updateDestination([
(rand() - 0.5) * settings.lightDistance * 0.1,
(rand() - 0.5) * settings.lightDistance * 0.1,
rand() / 2 * settings.lightDistance + 0.1
])
lastTime = time
}
regl.clear({
color: [0.19, 0.19, 0.19, 1],
depth: 1
})
camera.tick()
drawGlobal({
lightSource: follower.tick()
}, () => drawTriangles())
})
// ------------- helpers -------------
function guiSettings (settings, onChange) {
const settingsObj = {}
const gui = new GUI()
for (let key in settings) {
settingsObj[key] = settings[key][0]
const setting = gui
.add(settingsObj, key, settings[key][1], settings[key][2])
if (settings[key][3]) {
setting.step(settings[key][3])
}
if (settings[key][4]) {
setting.onChange(onChange)
}
}
return settingsObj
}
function title (name, color) {
includeFont({
fontFamily: '"Space Mono", sans-serif',
url: 'https://fonts.googleapis.com/css?family=Space+Mono:700'
})
const title = addTitle(name)
css(title, {
opacity: 0,
color: color,
bottom: '5vh',
right: '5vh',
transition: 'opacity 800ms linear',
zIndex: 10
})
document.body.appendChild(title)
setTimeout(() => {
css(title, 'opacity', 1)
}, 200)
}
function createFollower (start) {
let destination = start
let lastPosition = start
let curPosition = start
return {
updateDestination,
tick
}
function updateDestination (newDest) {
destination = newDest
}
function tick () {
let velocity = vec3.subtract([], curPosition, lastPosition)
const delta = vec3.subtract([], destination, curPosition)
const acceleration = vec3.scale([], delta, 0.001)
velocity = vec3.add(velocity, acceleration, velocity)
if (vec3.length(velocity) > 0.005) {
velocity = vec3.normalize(velocity, velocity)
velocity = vec3.scale(velocity, velocity, 0.005)
}
const nextPosition = vec3.add([], velocity, curPosition)
lastPosition = curPosition
curPosition = nextPosition
return curPosition
}
}