(vertexSource, fragmentSource)
| 831 | |
| 832 | |
| 833 | function Shader(vertexSource, fragmentSource) { |
| 834 | // Allow passing in the id of an HTML script tag with the source |
| 835 | |
| 836 | |
| 837 | function followScriptTagById(id) { |
| 838 | var element = document.getElementById(id); |
| 839 | return element ? element.text : id; |
| 840 | } |
| 841 | vertexSource = followScriptTagById(vertexSource); |
| 842 | fragmentSource = followScriptTagById(fragmentSource); |
| 843 | |
| 844 | // Headers are prepended to the sources to provide some automatic functionality. |
| 845 | var header = '\ |
| 846 | uniform mat3 gl_NormalMatrix;\ |
| 847 | uniform mat4 gl_ModelViewMatrix;\ |
| 848 | uniform mat4 gl_ProjectionMatrix;\ |
| 849 | uniform mat4 gl_ModelViewProjectionMatrix;\ |
| 850 | uniform mat4 gl_ModelViewMatrixInverse;\ |
| 851 | uniform mat4 gl_ProjectionMatrixInverse;\ |
| 852 | uniform mat4 gl_ModelViewProjectionMatrixInverse;\ |
| 853 | '; |
| 854 | var vertexHeader = header + '\ |
| 855 | attribute vec4 gl_Vertex;\ |
| 856 | attribute vec4 gl_TexCoord;\ |
| 857 | attribute vec3 gl_Normal;\ |
| 858 | attribute vec4 gl_Color;\ |
| 859 | vec4 ftransform() {\ |
| 860 | return gl_ModelViewProjectionMatrix * gl_Vertex;\ |
| 861 | }\ |
| 862 | '; |
| 863 | var fragmentHeader = '\ |
| 864 | precision highp float;\ |
| 865 | ' + header; |
| 866 | |
| 867 | // Check for the use of built-in matrices that require expensive matrix |
| 868 | // multiplications to compute, and record these in `usedMatrices`. |
| 869 | var source = vertexSource + fragmentSource; |
| 870 | var usedMatrices = {}; |
| 871 | regexMap(/\b(gl_[^;]*)\b;/g, header, function(groups) { |
| 872 | var name = groups[1]; |
| 873 | if(source.indexOf(name) != -1) { |
| 874 | var capitalLetters = name.replace(/[a-z_]/g, ''); |
| 875 | usedMatrices[capitalLetters] = LIGHTGL_PREFIX + name; |
| 876 | } |
| 877 | }); |
| 878 | if(source.indexOf('ftransform') != -1) usedMatrices.MVPM = LIGHTGL_PREFIX + 'gl_ModelViewProjectionMatrix'; |
| 879 | this.usedMatrices = usedMatrices; |
| 880 | |
| 881 | // The `gl_` prefix must be substituted for something else to avoid compile |
| 882 | // errors, since it's a reserved prefix. This prefixes all reserved names with |
| 883 | // `_`. The header is inserted after any extensions, since those must come |
| 884 | // first. |
| 885 | |
| 886 | |
| 887 | function fix(header, source) { |
| 888 | var replaced = {}; |
| 889 | var match = /^((\s*\/\/.*\n|\s*#extension.*\n)+)\^*$/.exec(source); |
| 890 | source = match ? match[1] + header + source.substr(match[1].length) : header + source; |
nothing calls this directly
no test coverage detected