forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.js
138 lines (123 loc) Β· 3.13 KB
/
instance.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
var createContext = require('./util/create-context')
var createREGL = require('../../regl')
var tape = require('tape')
tape('instance', function (t) {
var gl = createContext(6, 6)
var regl = createREGL({gl: gl, extensions: ['ANGLE_instanced_arrays']})
function checkPixmap (args, expected, remark) {
var base = {
frag: [
'precision mediump float;',
'void main() {',
'gl_FragColor = vec4(1, 1, 1, 1);',
'}'
].join('\n'),
vert: [
'precision mediump float;',
'attribute vec2 position;',
'attribute vec2 offset;',
'varying vec4 fragColor;',
'void main() {',
'gl_Position=vec4(((position + offset) - 2.5) / 2.6, 0, 1);',
'}'
].join('\n'),
attributes: {
position: regl.buffer([
0, 0,
4, 0,
4, 4,
0, 4]),
offset: {
buffer: regl.buffer([[0.0, 0.0], [0.05, 1.0]]),
divisor: 2
}
},
depth: {enable: false, mask: false},
instances: 2
}
Object.keys(args).forEach(function (x) {
base[x] = args[x]
})
function runCheck (suffix, divisor) {
var pixels = regl.read()
var actual = new Array(36)
for (var i = 0; i < 36; ++i) {
actual[i] = Math.min(1, pixels[4 * i])
}
t.same(actual, divisor === 2 ? expected.div2Expected : expected.div1Expected, remark + ' - ' + suffix)
}
//
// Test when the offset-buffer is a static property.
//
var d
var command
for (d = 1; d <= 2; d++) {
base.attributes.offset.divisor = d
command = regl(base)
regl.clear({color: [0, 0, 0, 0]})
command()
runCheck('static, divisor ' + d, d)
}
//
// Test when the offset-buffer is a dynamic property.
//
for (d = 1; d <= 2; d++) {
base.attributes.offset.divisor = d
base.attributes.buffer = regl.prop('offsetBuffer')
command = regl(base)
regl.clear({color: [0, 0, 0, 0]})
command({offsetBuffer: regl.buffer([[0.0, 0.0], [0.05, 1.0]])})
runCheck('dynamic, divisor ' + d, d)
}
}
// lines
checkPixmap({
primitive: 'lines',
count: 2
}, {
div2Expected: [
1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0
],
div1Expected: [
1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0
]
}, 'line')
// triangles
checkPixmap({
primitive: 'triangles',
count: 3
}, {
div2Expected: [
1, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 0,
0, 0, 0, 1, 1, 0,
0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0
],
div1Expected: [
1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 0,
0, 0, 1, 1, 1, 0,
0, 0, 0, 1, 1, 0,
0, 0, 0, 0, 1, 0
]
}, 'triangles')
// TODO: testcases for 'points', 'line loop', 'triangle strip', and
// 'triangle fan'
regl.destroy()
t.equals(gl.getError(), 0, 'error ok')
createContext.destroy(gl)
t.end()
})