-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecals.js
276 lines (227 loc) · 7.16 KB
/
decals.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
'use strict';
var createBuffer = require('gl-buffer');
var createVAO = require('gl-vao');
var glShader = require('gl-shader');
var glslify = require('glslify');
var mat4 = require('gl-mat4');
module.exports = function(game, opts) {
return new DecalsPlugin(game, opts);
};
module.exports.pluginInfo = {
loadAfter: ['voxel-mesher', 'voxel-shader', 'voxel-stitch']
};
function DecalsPlugin(game, opts) {
this.game = game;
this.shell = game.shell;
this.mesherPlugin = game.plugins.get('voxel-mesher');
if (!this.mesherPlugin) throw new Error('voxel-decals requires voxel-mesher');
this.shaderPlugin = game.plugins.get('voxel-shader');
if (!this.shaderPlugin) throw new Error('voxel-decals requires voxel-shader');
this.stitchPlugin = game.plugins.get('voxel-stitch');
if (!this.stitchPlugin) throw new Error('voxel-stitch requires voxel-shader');
this.info = []
/* for testing
[
{position:[0,0,0], normal:[-1,0,0], texture:'furnace_top'},
{position:[0,1,0], normal:[+1,0,0], texture:'furnace_top'},
{position:[0,2,0], normal:[0,+1,0], texture:'furnace_top'},
{position:[0,3,0], normal:[0,-1,0], texture:'furnace_top'},
{position:[0,4,0], normal:[0,0,+1], texture:'furnace_front_on'},
{position:[0,5,0], normal:[0,0,-1], texture:'furnace_top'},
];
*/
this.enable();
}
DecalsPlugin.prototype.add = function(info) {
this.info.push(info);
};
DecalsPlugin.prototype.remove = function(info) {
var found = undefined;
if (Array.isArray(info)) {
// backwards compatibility - passed array, only match location
var position = info;
for (var i = 0; i < this.info.length; i += 1) {
if (this.info[i].position[0] === position[0] &&
this.info[i].position[1] === position[1] &&
this.info[i].position[2] === position[2]) {
found = i;
break;
}
}
} else {
for (var i = 0; i < this.info.length; i += 1) { // TODO: optimize to a map if this is too inefficient
if (this.info[i].position[0] === info.position[0] &&
this.info[i].position[1] === info.position[1] &&
this.info[i].position[2] === info.position[2] &&
this.info[i].normal[0] == info.normal[0] &&
this.info[i].normal[1] == info.normal[1] &&
this.info[i].normal[2] == info.normal[2]) {
found = i;
break;
}
}
}
if (found === undefined) return;
this.info.splice(found, 1);
};
DecalsPlugin.prototype.change = function(info) {
this.remove(info);
this.add(info); // TODO: optimize to change without removing if necessary
};
DecalsPlugin.prototype.enable = function() {
this.shell.on('gl-init', this.onInit = this.shaderInit.bind(this));
this.shell.on('gl-render', this.onRender = this.render.bind(this));
this.stitchPlugin.on('updateTexture', this.onUpdateTexture = this.update.bind(this));
};
DecalsPlugin.prototype.disable = function() {
this.stitchPlugin.removeListener('updateTexture', this.onUpdateTexture);
this.shell.removeListener('gl-render', this.onRender = this.render.bind(this));
this.shell.removeListener('gl-init', this.onInit);
};
DecalsPlugin.prototype.shaderInit = function() {
// TODO: refactor with voxel-decals, voxel-chunkborder?
this.shader = glShader(this.shell.gl,
glslify("/* voxel-decals vertex shader */\
attribute vec3 position;\
attribute vec2 uv;\
\
uniform mat4 projection;\
uniform mat4 view;\
uniform mat4 model;\
varying vec2 vUv;\
\
void main() {\
gl_Position = projection * view * model * vec4(position, 1.0);\
vUv = uv;\
}", {inline: true}),
glslify("/* voxel-decals fragment shader */\
precision highp float;\
\
uniform sampler2D texture;\
varying vec2 vUv;\
\
void main() {\
gl_FragColor = texture2D(texture, vUv);\
}", {inline: true}));
};
DecalsPlugin.prototype.update = function() {
// cube face vertices, indexed by normal (based on box-geometry)
var cube = {
// Back face
'0|0|1': [
0, 0, 1,
1, 0, 1,
1, 1, 1,
0, 0, 1,
1, 1, 1,
0, 1, 1],
// Front face
'0|0|-1': [
0, 0, 0,
0, 1, 0,
1, 1, 0,
0, 0, 0,
1, 1, 0,
1, 0, 0],
// Top face
'0|1|0': [
0, 1, 0,
0, 1, 1,
1, 1, 1,
0, 1, 0,
1, 1, 1,
1, 1, 0],
// Bottom face
'0|-1|0': [
0, 0, 0,
1, 0, 0,
1, 0, 1,
0, 0, 0,
1, 0, 1,
0, 0, 1],
// Left face
'1|0|0': [
1, 0, 0,
1, 1, 0,
1, 1, 1,
1, 0, 0,
1, 1, 1,
1, 0, 1],
// Right face
'-1|0|0': [
0, 0, 0,
0, 0, 1,
0, 1, 1,
0, 0, 0,
0, 1, 1,
0, 1, 0],
};
var vertices = [];
var uvArray = [];
for (var i = 0; i < this.info.length; i += 1) {
// start with plane corresponding to desired cube face
var normal = this.info[i].normal;
var plane = cube[normal.join('|')].slice(0);
// translate into position
for (var j = 0; j < plane.length; j += 1) {
plane[j] += this.info[i].position[j % 3];
// and raise out of surface by a small amount to prevent z-fighting
plane[j] += normal[j % 3] * 0.001;
}
vertices = vertices.concat(plane);
// texturing (textures loaded from voxel-stitch updateTexture event)
var tileUV = this.stitchPlugin.getTextureUV(this.info[i].texture);
if (!tileUV) throw new Error('failed to load decal texture: ' + this.info[i].texture + ' for ' + this.info[i]);
// cover the texture tile over the two triangles forming a flat plane
var planeUV = [
tileUV[3],
tileUV[0],
tileUV[1],
tileUV[2],
];
// rotate UVs so texture is always facing up
var r = 0;
if (normal[0] === -1 ||
normal[1] === -1 ||
normal[2] === 1) { // TODO: -1?
r = 3;
}
uvArray.push(planeUV[(0 + r) % 4][0]); uvArray.push(planeUV[(0 + r) % 4][1]);
uvArray.push(planeUV[(1 + r) % 4][0]); uvArray.push(planeUV[(1 + r) % 4][1]);
uvArray.push(planeUV[(2 + r) % 4][0]); uvArray.push(planeUV[(2 + r) % 4][1]);
uvArray.push(planeUV[(0 + r) % 4][0]); uvArray.push(planeUV[(0 + r) % 4][1]);
uvArray.push(planeUV[(2 + r) % 4][0]); uvArray.push(planeUV[(2 + r) % 4][1]);
uvArray.push(planeUV[(3 + r) % 4][0]); uvArray.push(planeUV[(3 + r) % 4][1]);
}
var uv = new Float32Array(uvArray);
var gl = this.shell.gl;
var verticesBuf = createBuffer(gl, new Float32Array(vertices));
var uvBuf = createBuffer(gl, uv);
this.mesh = createVAO(gl, [
{ buffer: verticesBuf,
size: 3
},
{
buffer: uvBuf,
size: 2
}
]);
this.mesh.length = vertices.length/3;
};
var scratch0 = mat4.create();
DecalsPlugin.prototype.render = function() {
if (this.mesh) {
var gl = this.shell.gl;
this.shader.bind();
this.shader.attributes.position.location = 0;
this.shader.attributes.uv.location = 1;
this.shader.uniforms.projection = this.shaderPlugin.projectionMatrix;
this.shader.uniforms.view = this.shaderPlugin.viewMatrix;
this.shader.uniforms.model = scratch0;
// use same atlas from voxel-shader TODO: can we reliably avoid binding? if already bound, seems to reuse
if (this.stitchPlugin.texture) this.shader.uniforms.texture = this.stitchPlugin.texture.bind();
this.mesh.bind();
this.mesh.draw(gl.TRIANGLES, this.mesh.length);
this.mesh.unbind();
}
};