-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (134 loc) · 4.27 KB
/
index.js
File metadata and controls
166 lines (134 loc) · 4.27 KB
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
var createCamera = require('orbit-camera')
var createTex2d = require('gl-texture2d')
var createGeom = require('gl-geometry')
var createShader = require('gl-shader')
var glslify = require('glslify')
var createFBO = require('gl-fbo')
var clear = require('gl-clear')({ color: [1, 1, 1, 1] })
var mat4 = require('gl-matrix').mat4
var quat = require('gl-matrix').quat
var triangle = require('a-big-triangle')
var debounce = require('frame-debounce')
var unindex = require('unindex-mesh')
var normals = require('face-normals')
var combine = require('mesh-combine')
var fit = require('canvas-fit')
var qbqb = require('cube-cube')
var getTime = require('right-now')
var clone = require('clone')
var TIMESCALE = 0.5
var SIZE = 16
var RES = [1 / SIZE, 1 / SIZE]
module.exports = function(canvas) {
canvas.style.position = 'fixed'
var gl = require('gl-context')(canvas, render)
var heightmap = createFBO(gl, [SIZE, SIZE], { float: true })
var gradient = createTex2d(gl, require('./gradient-map'))
var voxels = createMesh()
var projection = mat4.create()
var viewrot = mat4.create()
var view = mat4.create()
var model = mat4.create()
var camera = createCamera(
[0, 10, 30]
, [0, 0, 0]
, [0, 1, 0]
)
var shader = createShader(gl
, glslify('./shaders/voxel.vert')
, glslify('./shaders/voxel.frag')
)
var heightShader = createShader(gl
, glslify('./shaders/heightmap.vert')
, glslify('./shaders/heightmap.frag')
)
var geom = createGeom(gl)
.attr('aPosition', voxels.positions)
.attr('aCentroid', voxels.centroids)
.attr('aNormal', voxels.normals)
.attr('aEdge', voxels.edges)
camera.distance = 1.5
heightmap.color[0].wrap = gl.CLAMP_TO_EDGE
heightmap.color[0].minFilter = gl.NEAREST
heightmap.color[0].maxFilter = gl.NEAREST
mat4.translate(model, model, [-0.5, 0, -0.5])
window.addEventListener('resize'
, debounce(fit(canvas, window))
, false
)
function render() {
var width = canvas.width
var height = canvas.height
var now = getTime() * TIMESCALE
heightmap.bind()
gl.viewport(0, 0, SIZE, SIZE)
gl.disable(gl.DEPTH_TEST)
gl.disable(gl.CULL_FACE)
heightShader.bind()
heightShader.uniforms.uTime = now
heightShader.uniforms.uResolution = RES
triangle(gl)
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
gl.viewport(0, 0, width, height)
gl.enable(gl.DEPTH_TEST)
gl.enable(gl.CULL_FACE)
clear(gl)
mat4.perspective(projection
, Math.PI / 4
, width / height
, 0.001
, 100
)
quat.identity(camera.rotation)
quat.rotateY(camera.rotation, camera.rotation, now * 0.0002)
quat.rotateX(camera.rotation, camera.rotation, -0.5)
camera.view(view)
quat.identity(camera.rotation)
quat.rotateY(camera.rotation, camera.rotation, now * 0.0002)
geom.bind(shader)
shader.uniforms.uResolution = RES
shader.uniforms.uViewRotation = mat4.fromQuat(viewrot, camera.rotation)
shader.uniforms.uProjection = projection
shader.uniforms.uModel = model
shader.uniforms.uView = view
shader.uniforms.tHeightmap = heightmap.color[0].bind(0)
shader.uniforms.tGradient = gradient.bind(1)
geom.draw()
geom.unbind()
}
}
function createMesh() {
var voxels = qbqb(SIZE, 1, SIZE)
var positions = unindex(combine(voxels))
var edges = unindex(combine(getEdges(voxels)))
var centroids = unindex(combine(getCentroids(voxels)))
return {
positions: positions
, centroids: centroids
, normals: normals(positions)
, edges: edges
}
}
function getCentroids(meshes) {
return meshes.map(function(mesh) {
mesh = clone(mesh)
for (var i = 0; i < mesh.positions.length; i++) {
mesh.positions[i] = mesh.centroid
}
return mesh
})
}
function getEdges(meshes) {
return meshes.map(function(mesh) {
mesh = clone(mesh)
var idx = mesh.index
for (var i = 0; i < mesh.positions.length; i++) {
var pos = mesh.positions[i]
mesh.positions[i] = mesh.positions[i].slice()
mesh.positions[i][0] = (((pos[0] * SIZE) - idx[0]) - 0.5) * 2
mesh.positions[i][2] = (((pos[2] * SIZE) - idx[2]) - 0.5) * 2
mesh.positions[i][1] = (((pos[1]) - idx[1]) - 0.5) * 2
}
return mesh
})
}