games.rednblack.editor.renderer.utils.ShaderCompiler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of runtime-libgdx Show documentation
Show all versions of runtime-libgdx Show documentation
HyperLap2D libGDX runtime to render exported scenes
The newest version!
package games.rednblack.editor.renderer.utils;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
public class ShaderCompiler {
public static int MAX_TEXTURE_UNIT = 1;
public static final String GET_TEXTURE_FROM_ARRAY_PLACEHOLDER = "";
public static ShaderProgram compileShader(FileHandle vertex, FileHandle fragment) {
return compileShader(vertex.readString(), fragment.readString());
}
public static ShaderProgram compileShader(String vertex, String fragment) {
if (!fragment.contains(GET_TEXTURE_FROM_ARRAY_PLACEHOLDER)) return new ShaderProgram(vertex, fragment);
return compileUnrolledArrayTextureShader(vertex, fragment);
}
public static ShaderProgram compileUnrolledArrayTextureShader(String vertex, String fragment) {
String funcConditional = "vec4 getTextureFromArray(vec2 uv) {\n";
for (int i = 0; i < MAX_TEXTURE_UNIT; i++) {
if (i != 0) funcConditional += " else ";
funcConditional += "if (v_texture_index < " + i + ".5) return texture2D(u_textures[" + i + "], uv);\n";
}
funcConditional += "}\n";
fragment = "#define MAX_TEXTURE_UNITS " + MAX_TEXTURE_UNIT + "\n" + fragment;
fragment = fragment.replace(GET_TEXTURE_FROM_ARRAY_PLACEHOLDER, funcConditional);
return new ShaderProgram(vertex, fragment);
}
}