UNPKG

1.41 kBJavaScriptView Raw
1'use strict'
2
3module.exports = makeReflectTypes
4
5//Construct type info for reflection.
6//
7// This iterates over the flattened list of uniform type values and smashes them into a JSON object.
8//
9// The leaves of the resulting object are either indices or type strings representing primitive glslify types
10function makeReflectTypes(uniforms, useIndex) {
11 var obj = {}
12 for(var i=0; i<uniforms.length; ++i) {
13 var n = uniforms[i].name
14 var parts = n.split(".")
15 var o = obj
16 for(var j=0; j<parts.length; ++j) {
17 var x = parts[j].split("[")
18 if(x.length > 1) {
19 if(!(x[0] in o)) {
20 o[x[0]] = []
21 }
22 o = o[x[0]]
23 for(var k=1; k<x.length; ++k) {
24 var y = parseInt(x[k])
25 if(k<x.length-1 || j<parts.length-1) {
26 if(!(y in o)) {
27 if(k < x.length-1) {
28 o[y] = []
29 } else {
30 o[y] = {}
31 }
32 }
33 o = o[y]
34 } else {
35 if(useIndex) {
36 o[y] = i
37 } else {
38 o[y] = uniforms[i].type
39 }
40 }
41 }
42 } else if(j < parts.length-1) {
43 if(!(x[0] in o)) {
44 o[x[0]] = {}
45 }
46 o = o[x[0]]
47 } else {
48 if(useIndex) {
49 o[x[0]] = i
50 } else {
51 o[x[0]] = uniforms[i].type
52 }
53 }
54 }
55 }
56 return obj
57}
\No newline at end of file