File size: 2,429 Bytes
c0dce6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export default class Uniform {

    /**
     * The parent MiniGL controller.
     *
     * @type {MiniGL}
     * @private
     */
    gl;

    /**
     * @type {string}
     */
    type;

    /**
     * @type {*}
     */
    value;

    /**
     * The mapped type function.
     *
     * @type {string}
     */
    typeFn;

    /**
     * Type function mappings.
     *
     * @type {object}
     * @private
     */
    _typeMap = {
        float: '1f',
        int: '1i',
        vec2: '2fv',
        vec3: '3fv',
        vec4: '4fv',
        mat4: 'Matrix4fv'
    };

    /**
     * @param {MiniGL} minigl
     * @param {string} type
     * @param {*} value
     * @param {object} properties
     */
    constructor(minigl, type, value, properties = {}) {

        // Add additional properties i.e. excludeFrom, transpose... etc
        Object.assign(this, properties);

        // Set required properties.
        this.gl = minigl;
        this.type = type;
        this.value = value;

        // Get type function from map.
        this.typeFn = this._typeMap[this.type] || this._typeMap.float;

        // Update.
        this.update();
    }

    update(value) {
        if (this.value) {

            var paramB = this.value;
            var paramC = null;

            if (this.typeFn.indexOf('Matrix') === 0) {
                paramB = this.transpose;
                paramC = this.value;
            }

            this.gl.getContext()[`uniform${this.typeFn}`](value, paramB, paramC);
        }
    }

    getDeclaration(name, type, length) {
        if (this.excludeFrom !== type) {

            if (this.type === 'array') {
                return `${this.value[0].getDeclaration(name, type, this.value.length)}
const int ${name}_length = ${this.value.length};`;
            }

            if (this.type === 'struct') {
                let namePrefix = name.replace('u_', '');
                namePrefix = namePrefix.charAt(0).toUpperCase() + namePrefix.slice(1);

                const declaration = Object.entries(this.value).map(([name, uniform]) => {
                    return uniform.getDeclaration(name, type).replace(/^uniform/, '');
                }).join('');

                return `uniform struct ${namePrefix} {
    ${declaration}
} ${name}${ length > 0 ? `[${length}]` : '' };`;

            }

            return `uniform ${this.type} ${name}${ length > 0 ? `[${length}]` : '' };`
        }
    }

}