-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest.js
More file actions
131 lines (105 loc) · 3.77 KB
/
test.js
File metadata and controls
131 lines (105 loc) · 3.77 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
var createCamera = require('canvas-orbit-camera')
var mat4 = require('gl-matrix').mat4
var pack = require('array-pack-2d')
var unindex = require('unindex-mesh')
var faceNormals = require('face-normals')
var createContext = require('gl-context')
var ndarray = require('ndarray')
var normals = require('normals')
var glslify = require('glslify')
var bunny = require('bunny')
var createShader = require('gl-shader')
var parseOBJ = require('parse-wavefront-obj')
var pc2Parser = require('parse-pc2')
var fs = require('fs')
var createGeom = require('../')
var clear = require('gl-clear')({
color: [0xF0 / 255, 0xF1 / 255, 0xF2 / 255, 1],
depth: true,
stencil: false
})
// handles simplicial complexes with cells/positions properties
var scPos = bunny
var scNor = normals.vertexNormals(bunny.cells, bunny.positions)
createExample(scPos, scNor).title = 'Simplicial Complex'
// handles Float32Arrays
var uiPos = unindex(bunny.positions, bunny.cells)
var uiNor = faceNormals(uiPos)
createExample(uiPos, uiNor).title = 'Unindexed Mesh, Float32Arrays'
// handles (flat) ndarrays
var ndPos = ndarray(uiPos, [uiPos.length])
var ndNor = ndarray(uiNor, [uiNor.length])
createExample(ndPos, ndNor).title = 'Flat ndarrays'
// also supports .faces() method
createExample(scPos.positions, scNor, scPos.cells).title = '.faces(), Last Call'
// also supports .faces() method with packed data
createExample(pack(scPos.positions), scNor, pack(scPos.cells)).title = '.faces(), Packed Data'
// .faces(), order-independant
createExample(scPos.positions, scNor, scPos.cells, {facesFirst: true}).title = '.faces(), First Call'
createAttributeUpdateExample().title = 'Attribute Update'
function createExample (pos, norm, cells, options) {
options = options || { }
var canvas = document.body.appendChild(document.createElement('canvas'))
var gl = createContext(canvas, render)
var camera = createCamera(canvas)
if (options.zoom) camera.distance /= options.zoom
var projection = mat4.create()
var shader = createShader(gl,
glslify('./test.vert'),
glslify('./test.frag')
)
canvas.width = 300
canvas.height = 300
canvas.style.margin = '1em'
var geom = createGeom(gl)
if (cells && options.facesFirst) geom.faces(cells)
geom
.attr('position', pos)
.attr('normal', norm)
if (cells && !options.facesFirst) geom.faces(cells)
function render () {
if (options.update) {
options.update(geom)
}
var width = canvas.width
var height = canvas.height
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
if (!options.noculling) gl.enable(gl.CULL_FACE)
gl.enable(gl.DEPTH_TEST)
gl.viewport(0, 0, width, height)
clear(gl)
geom.bind(shader)
shader.attributes.position.location = 0
shader.uniforms.uView = camera.view()
shader.uniforms.uProjection = mat4.perspective(projection
, Math.PI / 4
, width / height
, 0.001
, 10000
)
geom.draw()
geom.unbind()
camera.tick()
}
return canvas
}
function createAttributeUpdateExample () {
var anim = pc2Parser.toObject(fs.readFileSync(__dirname + '/basicCloth.pc2'))
var obj = parseOBJ(fs.readFileSync(__dirname + '/basicCloth.obj'))
var norms = normals.vertexNormals(obj.cells, obj.positions)
var startDate = Date.now()
var currentFrame = 0
function update (geom) {
// Pick appropriate frame (24fps)
var delta_ms = Date.now() - startDate
var newFrame = Math.floor(24 * delta_ms / 1000) % anim.frames.length
// Update geometry on frame change
if (newFrame !== currentFrame) {
currentFrame = newFrame
var framePosition = anim.frames[currentFrame]
geom.attr('position', framePosition)
geom.attr('normal', normals.vertexNormals(obj.cells, framePosition))
}
}
return createExample(obj, norms, null, {noculling: true, update: update, zoom: 4})
}