mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-07 20:31:51 +09:00
more shader things that needs multiplatform investigation
This commit is contained in:
@@ -8,121 +8,11 @@ import com.badlogic.gdx.graphics.glutils.ShaderProgram
|
||||
*/
|
||||
object MacosGL32Shaders {
|
||||
fun createSpriteBatchShader(): ShaderProgram {
|
||||
val vertexShader = """
|
||||
#version 150
|
||||
in vec4 ${ShaderProgram.POSITION_ATTRIBUTE};
|
||||
in vec4 ${ShaderProgram.COLOR_ATTRIBUTE};
|
||||
in vec2 ${ShaderProgram.TEXCOORD_ATTRIBUTE}0;
|
||||
uniform mat4 u_projTrans;
|
||||
out vec4 v_color;
|
||||
out vec2 v_texCoords;
|
||||
|
||||
void main() {
|
||||
v_color = ${ShaderProgram.COLOR_ATTRIBUTE};
|
||||
v_color.a = v_color.a * (255.0/254.0);
|
||||
v_texCoords = ${ShaderProgram.TEXCOORD_ATTRIBUTE}0;
|
||||
gl_Position = u_projTrans * ${ShaderProgram.POSITION_ATTRIBUTE};
|
||||
}
|
||||
"""
|
||||
val fragmentShader = """
|
||||
#version 150
|
||||
#ifdef GL_ES
|
||||
#define LOWP lowp
|
||||
precision mediump float;
|
||||
#else
|
||||
#define LOWP
|
||||
#endif
|
||||
in LOWP vec4 v_color;
|
||||
in vec2 v_texCoords;
|
||||
uniform sampler2D u_texture;
|
||||
out vec4 fragColor;
|
||||
|
||||
void main() {
|
||||
fragColor = v_color * texture(u_texture, v_texCoords);
|
||||
}
|
||||
"""
|
||||
return App.loadShaderInline(vertexShader, fragmentShader)
|
||||
return App.loadShaderFromClasspath("shaders/gl32spritebatch.vert", "shaders/gl32spritebatch.frag")
|
||||
}
|
||||
|
||||
|
||||
fun createShapeRendererShader(): ShaderProgram {
|
||||
return App.loadShaderInline(createShadeRendererVertexShader(), createShapeRendererFragmentShader())
|
||||
}
|
||||
|
||||
|
||||
private fun createShadeRendererVertexShader(hasNormals: Boolean = false, hasColors: Boolean = true, numTexCoords: Int = 0): String {
|
||||
var shader = ("""
|
||||
#version 150
|
||||
in vec4 ${ShaderProgram.POSITION_ATTRIBUTE};
|
||||
|
||||
""".trimIndent()
|
||||
+ (if (hasNormals) """
|
||||
in vec3 ${ShaderProgram.NORMAL_ATTRIBUTE};
|
||||
|
||||
""".trimIndent()
|
||||
else "")
|
||||
+ if (hasColors) """
|
||||
in vec4 ${ShaderProgram.COLOR_ATTRIBUTE};
|
||||
|
||||
""".trimIndent()
|
||||
else "")
|
||||
for (i in 0 until numTexCoords) {
|
||||
shader += """
|
||||
in vec2 ${ShaderProgram.TEXCOORD_ATTRIBUTE}$i;
|
||||
|
||||
""".trimIndent()
|
||||
}
|
||||
shader += """
|
||||
uniform mat4 u_projModelView;
|
||||
${if (hasColors) "in vec4 v_col;\n" else ""}
|
||||
""".trimIndent()
|
||||
for (i in 0 until numTexCoords) {
|
||||
shader += "in vec2 v_tex$i;\n"
|
||||
}
|
||||
shader += """void main() {
|
||||
gl_Position = u_projModelView * ${ShaderProgram.POSITION_ATTRIBUTE};
|
||||
"""
|
||||
if (hasColors) {
|
||||
shader += """ v_col = ${ShaderProgram.COLOR_ATTRIBUTE};
|
||||
v_col.a *= 255.0 / 254.0;
|
||||
"""
|
||||
}
|
||||
for (i in 0 until numTexCoords) {
|
||||
shader += """ v_tex$i = ${ShaderProgram.TEXCOORD_ATTRIBUTE}$i;
|
||||
"""
|
||||
}
|
||||
shader += """ gl_PointSize = 1.0;
|
||||
}
|
||||
"""
|
||||
return shader
|
||||
}
|
||||
|
||||
private fun createShapeRendererFragmentShader(hasNormals: Boolean = false, hasColors: Boolean = true, numTexCoords: Int = 0): String {
|
||||
var shader = """
|
||||
#version 150
|
||||
#ifdef GL_ES
|
||||
precision mediump float;
|
||||
#endif
|
||||
|
||||
""".trimIndent()
|
||||
if (hasColors) shader += "in vec4 v_col;\n"
|
||||
for (i in 0 until numTexCoords) {
|
||||
shader += "in vec2 v_tex$i;\n"
|
||||
shader += "uniform sampler2D u_sampler$i;\n"
|
||||
}
|
||||
shader += """void main() {
|
||||
gl_FragColor = ${if (hasColors) "v_col" else "vec4(1, 1, 1, 1)"}"""
|
||||
if (numTexCoords > 0) shader += " * "
|
||||
for (i in 0 until numTexCoords) {
|
||||
shader += if (i == numTexCoords - 1) {
|
||||
" texture2D(u_sampler$i, v_tex$i)"
|
||||
}
|
||||
else {
|
||||
" texture2D(u_sampler$i, v_tex$i) *"
|
||||
}
|
||||
}
|
||||
shader += ";\n}"
|
||||
return shader
|
||||
return App.loadShaderFromClasspath("shaders/gl32shaperenderer.vert", "shaders/gl32shaperenderer.frag")
|
||||
}
|
||||
|
||||
}
|
||||
13
src/shaders/gl32shaperenderer.frag
Normal file
13
src/shaders/gl32shaperenderer.frag
Normal file
@@ -0,0 +1,13 @@
|
||||
#version 150
|
||||
|
||||
#ifdef GL_ES
|
||||
precision mediump float;
|
||||
#endif
|
||||
|
||||
in vec4 v_col;
|
||||
|
||||
out vec4 fragColor;
|
||||
|
||||
void main() {
|
||||
fragColor = v_col;
|
||||
}
|
||||
14
src/shaders/gl32shaperenderer.vert
Normal file
14
src/shaders/gl32shaperenderer.vert
Normal file
@@ -0,0 +1,14 @@
|
||||
#version 150
|
||||
|
||||
in vec4 a_position;
|
||||
in vec4 a_color;
|
||||
|
||||
uniform mat4 u_projModelView;
|
||||
out vec4 v_col;
|
||||
|
||||
void main() {
|
||||
gl_Position = u_projModelView * a_position;
|
||||
v_col = a_color;
|
||||
v_col.a *= 255.0 / 254.0;
|
||||
gl_PointSize = 1.0;
|
||||
}
|
||||
17
src/shaders/gl32spritebatch.frag
Normal file
17
src/shaders/gl32spritebatch.frag
Normal file
@@ -0,0 +1,17 @@
|
||||
#version 150
|
||||
|
||||
#ifdef GL_ES
|
||||
#define LOWP lowp
|
||||
precision mediump float;
|
||||
#else
|
||||
#define LOWP
|
||||
#endif
|
||||
|
||||
in LOWP vec4 v_color;
|
||||
in vec2 v_texCoords;
|
||||
uniform sampler2D u_texture;
|
||||
out vec4 fragColor;
|
||||
|
||||
void main() {
|
||||
fragColor = v_color * texture(u_texture, v_texCoords);
|
||||
}
|
||||
15
src/shaders/gl32spritebatch.vert
Normal file
15
src/shaders/gl32spritebatch.vert
Normal file
@@ -0,0 +1,15 @@
|
||||
#version 150
|
||||
|
||||
in vec4 a_position;
|
||||
in vec4 a_color;
|
||||
in vec2 a_texCoord0;
|
||||
uniform mat4 u_projTrans;
|
||||
out vec4 v_color;
|
||||
out vec2 v_texCoords;
|
||||
|
||||
void main() {
|
||||
v_color = a_color;
|
||||
v_color.a = v_color.a * (255.0/254.0);
|
||||
v_texCoords = a_texCoord0;
|
||||
gl_Position = u_projTrans * a_position;
|
||||
}
|
||||
Reference in New Issue
Block a user