Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function isBuiltIn(name: string): boolean {
- return name.startsWith("gl_") || name.startsWith("webgl_");
- }
- export function getAttribSetters(gl: WebGL2RenderingContext, program: WebGLProgram): void {
- const numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
- const uniformSetters: { [key: string]: any } = {};
- for(let i = 0; i < numUniforms; ++i) {
- const uniformInfo = gl.getActiveUniform(program, i);
- if(!uniformInfo || isBuiltIn(uniformInfo.name)) {
- continue;
- }
- let name = uniformInfo.name;
- if(name.substr(-3) === "[0]") {
- name = name.substr(0, name.length - 3);
- }
- const setter = createUniformSetter(gl, program, uniformInfo);
- }
- }
- // This gets set the first time getUniformSetters() is called to avoid circular dependency problems
- let uniformSetters: Array<[GLUniformType, typeof GLSetter]> | undefined;
- function getUniformSetter(type: GLUniformType): typeof GLSetter | undefined {
- if(!uniformSetters) {
- uniformSetters = [
- [GLUniformType.INT, GLIntSetter],
- [GLUniformType.BOOL, GLIntSetter],
- [GLUniformType.FLOAT, GLFloatSetter],
- [GLUniformType.VEC2, GLVec2Setter],
- [GLUniformType.VEC3, GLVec3Setter],
- [GLUniformType.VEC4, GLVec4Setter],
- [GLUniformType.MAT2, GLMat2Setter],
- [GLUniformType.MAT3, GLMat3Setter],
- [GLUniformType.MAT4, GLMat4Setter],
- [GLUniformType.SAMPLER2D, GLSampler2DSetter],
- ];
- }
- let setter: typeof GLSetter | undefined;
- for(const i in uniformSetters) {
- if(!uniformSetters[i]) { continue; }
- if(uniformSetters[i][0] === type) {
- setter = uniformSetters[i][1];
- }
- }
- return setter;
- }
- function createUniformSetter(gl: WebGL2RenderingContext, program: WebGLProgram, info: WebGLActiveInfo): void {
- const loc = gl.getUniformLocation(program, info.name);
- const isArray = (info.size > 1 && info.name.substr(-3) === "[0]");
- const type = info.type;
- const typeInfo = getUniformSetter(type);
- if(!typeInfo) {
- throw new Error(`Program uniform '${info.name}' type 0x${type.toString(16)} is unavailable.`);
- }
- // TODO: Finish creating uniform setters https://github.com/greggman/twgl.js/blob/master/src/programs.js#L840
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement