-
Notifications
You must be signed in to change notification settings - Fork 1
/
rock.js
64 lines (51 loc) · 1.43 KB
/
rock.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
var mat4 = require('gl-matrix').mat4
var unindex = require('unindex-mesh')
var normals = require('face-normals')
var Geometry = require('gl-geometry')
module.exports = function(obj, Shader) {
var shader
var geom
return function(gl, x, y, z) {
var model = mat4.create()
var scale = Math.random() * 0.1 + 0.2
mat4.translate(model, model, [x, y, z])
mat4.scale(model, model, [scale, scale, scale])
mat4.rotateY(model, model, Math.random() * Math.PI * 2)
shader = shader || Shader(gl)
geom = geom || createGeom()
render.prerender = prerender
render.postrender = postrender
return render
function render(params) {
shader.uniforms.uModel = model
geom.draw()
}
function prerender(params) {
geom.bind(shader)
shader.uniforms.uLightDirection = params.lightDirection
shader.uniforms.uLightThreshold = params.lightThreshold
shader.uniforms.uProjection = params.proj
shader.uniforms.uView = params.view
}
function postrender(params) {
// geom.unbind()
}
function createGeom() {
var geom = Geometry(gl)
if (obj.pos) {
var pos = obj.pos
var nor = normals(pos)
var idx = obj.idx
} else {
var pos = obj
var nor = normals(pos)
}
geom
.attr('position', pos)
.attr('normal', nor)
if (idx) geom
.attr('index', idx)
return geom
}
}
}