-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
158 lines (133 loc) · 4 KB
/
demo.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
'use strict'
// based on https://github.com/hughsk/gl-geometry/blob/master/test.js
var createCamera = require('canvas-orbit-camera')
var mat4 = require('gl-matrix').mat4
var createContext = require('gl-context')
var glShader = require('gl-shader')
var glslify = require('glslify')
var createTexture = require('gl-texture2d')
var createBuffer = require('gl-buffer')
var createVAO = require('gl-vao')
var getPixels = require('get-pixels')
var parseBlockModel = require('./')
var canvas = document.body.appendChild(document.createElement('canvas'))
var gl = createContext(canvas, render)
var camera = createCamera(canvas)
var projection = mat4.create()
canvas.width = 512
canvas.height = 512
canvas.style.margin = '1em'
canvas.style.border = '1px solid black'
var textarea = document.createElement('textarea')
textarea.id = 'q'
textarea.rows = '50'
textarea.cols = '80'
var exampleData =
// example parsed JSON
[
{from: [0,0,0],
to: [16,8,16],
faceData: {
down: {},
up: {},
north: {},
south: {},
west: {},
east: {}},
}
]
var oldText = textarea.value = JSON.stringify(exampleData, null, ' ')
document.body.appendChild(textarea)
document.body.appendChild(document.createElement('br'))
var errorNode = document.createTextNode('')
document.body.appendChild(errorNode)
// create a mesh ready for rendering
var createBlockMesh = function(gl, vertices, uv) {
var verticesBuf = createBuffer(gl, new Float32Array(vertices))
var uvBuf = createBuffer(gl, new Float32Array(uv))
var mesh = createVAO(gl, [
{ buffer: verticesBuf,
size: 3
},
{
buffer: uvBuf,
size: 2
}
])
mesh.length = vertices.length/3
return mesh
};
var model = parseBlockModel(exampleData)//, undefined, 0,0,0)//1,1,1)
var mesh = createBlockMesh(gl, model.vertices, model.uv)
window.setInterval(function() {
var text = textarea.value
if (text.length === oldText.length && text === oldText) return // no change
oldText = text
errorNode.textContent = ''
try {
var data = JSON.parse(text)
var model = parseBlockModel(data)
mesh = createBlockMesh(gl, model.vertices, model.uv)
console.log('updated geometry',mesh)
} catch (e) {
errorNode.textContent = e.toString()
}
}, 200)
var modelMatrix = mat4.create()
var s = 5
mat4.scale(modelMatrix, modelMatrix, [s,s,s])
var shader = glShader(gl,
glslify("\
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("\
precision highp float;\
\
uniform sampler2D texture;\
varying vec2 vUv;\
\
void main() {\
gl_FragColor = texture2D(texture, vUv);\
}", {inline: true}));
// from https://github.com/deathcap/ProgrammerArt/blob/master/textures/blocks/glass_blue.png
var blueGlass = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAIElEQVQ4T2NgYGD4TyEGEf+NycOjBowaMGoAtQ0gHwMAeYYmHC+xF4EAAAAASUVORK5CYII='
var texture
getPixels(blueGlass, function(err, pixels) {
if (err) throw err
texture = createTexture(gl, pixels)
})
function render() {
var width = canvas.width
var height = canvas.height
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
gl.enable(gl.CULL_FACE)
gl.enable(gl.DEPTH_TEST)
gl.viewport(0, 0, width, height)
shader.bind()
shader.attributes.position.location = 0
shader.uniforms.view = camera.view()
shader.uniforms.model = modelMatrix
shader.uniforms.projection = mat4.perspective(projection
, Math.PI / 4
, width / height
, 0.001
, 10000
)
if (texture) shader.uniforms.texture = texture.bind()
// TODO: 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();
mesh.bind();
mesh.draw(gl.TRIANGLES, mesh.length);
mesh.unbind();
camera.tick()
}