-
Notifications
You must be signed in to change notification settings - Fork 0
/
outline.js
152 lines (120 loc) · 4.47 KB
/
outline.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
'use strict';
var createBuffer = require('gl-buffer');
var createVAO = require('gl-vao');
var createSimpleShader = require('simple-3d-shader');
var mat4 = require('gl-mat4');
var inherits = require('inherits');
var EventEmitter = require('events').EventEmitter;
var throttle = require('lodash.throttle');
module.exports = function(game, opts) {
return new OutlinePlugin(game, opts);
};
module.exports.pluginInfo = {
loadAfter: ['voxel-mesher', 'voxel-shader']
};
function OutlinePlugin(game, opts) {
this.game = game;
this.shell = game.shell;
this.mesherPlugin = game.plugins.get('voxel-mesher');
if (!this.mesherPlugin) throw new Error('voxel-outline requires voxel-mesher');
this.shaderPlugin = game.plugins.get('voxel-shader');
if (!this.shaderPlugin) throw new Error('voxel-outline requires voxel-shader');
this.showOutline = opts.showOutline !== undefined ? opts.showOutline : true;
this.showThrough = opts.showThrough !== undefined ? opts.showThrough : false;
this.colorVector = opts.color !== undefined ? opts.color : [1,0,0]; // red, RGB TODO: convert from hex? TODO: same in voxel-chunkborder
this.frequency = opts.frequency ? opts.frequency : 100; // milliseconds to throttle raycasting
this.currentTarget = undefined;
this.modelMatrix = mat4.create();
this.enable();
}
inherits(OutlinePlugin, EventEmitter);
OutlinePlugin.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.game.on('tick', this.onTick = throttle(this.tick.bind(this), this.frequency));
};
OutlinePlugin.prototype.disable = function() {
this.game.removeListener('tick', this.onTick);
this.shell.removeListener('gl-render', this.onRender = this.render.bind(this));
this.shell.removeListener('gl-init', this.onInit);
this.currentTarget = undefined;
};
// slightly enlarge the outline to avoid z-fighting voxel
var epsilon = 0.001; // greater than voxel-wireframe (0.00001) so it doesn't fight there either
var epsilonVector = [1+epsilon, 1+epsilon, 1+epsilon];
OutlinePlugin.prototype.tick = function() {
var hit = this.game.raycastVoxels();
if (!hit) {
// remove outline if any
if (this.currentTarget) {
this.emit('remove', this.currentTarget.slice());
}
this.currentTarget = undefined;
return;
}
// if changed voxel target, update matrix and emit event
if (!this.currentTarget ||
hit.voxel[0] !== this.currentTarget[0] ||
hit.voxel[1] !== this.currentTarget[1] ||
hit.voxel[2] !== this.currentTarget[2]) {
// translate to voxel position
mat4.identity(this.modelMatrix);
mat4.translate(this.modelMatrix, this.modelMatrix, hit.voxel);
mat4.scale(this.modelMatrix, this.modelMatrix, epsilonVector);
if (this.currentTarget) {
this.emit('remove', this.currentTarget.slice());
}
this.currentTarget = hit.voxel.slice();
this.emit('highlight', this.currentTarget.slice(), hit);
}
};
OutlinePlugin.prototype.render = function() {
if (this.showOutline && this.currentTarget) {
var gl = this.shell.gl;
if (this.showThrough) gl.disable(gl.DEPTH_TEST);
this.outlineShader.bind();
this.outlineShader.attributes.position.location = 0;
this.outlineShader.uniforms.projection = this.shaderPlugin.projectionMatrix;
this.outlineShader.uniforms.view = this.shaderPlugin.viewMatrix;
this.outlineShader.uniforms.model = this.modelMatrix;
this.outlineShader.attributes.color = this.colorVector;
var outlineVAO = this.mesh;
outlineVAO.bind();
outlineVAO.draw(gl.LINES, outlineVAO.length);
outlineVAO.unbind();
}
};
OutlinePlugin.prototype.shaderInit = function() {
this.outlineShader = createSimpleShader(this.shell.gl);
// create outline mesh
// TODO: refactor with voxel-chunkborder, very similar
var w = 1;
var outlineVertexArray = new Uint8Array([
0,0,0,
0,0,w,
0,w,0,
0,w,w,
w,0,0,
w,0,w,
w,w,0,
w,w,w
]);
var indexArray = new Uint16Array([
0,1, 0,2, 2,3, 3,1,
0,4, 4,5, 5,1,
5,7, 7,3,
7,6, 6,2,
6,4
]);
var outlineVertexCount = indexArray.length;
var gl = this.shell.gl;
var outlineBuf = createBuffer(gl, outlineVertexArray);
var indexBuf = createBuffer(gl, indexArray, gl.ELEMENT_ARRAY_BUFFER);
var outlineVAO = createVAO(gl, [
{ buffer: outlineBuf,
type: gl.UNSIGNED_BYTE,
size: 3
}], indexBuf);
outlineVAO.length = outlineVertexCount;
this.mesh = outlineVAO;
};