diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-23 23:01:25 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-23 23:01:25 +1000 |
commit | 6832c10ed5482530b4a423a78700b279fc73212a (patch) | |
tree | b26c485a105659936b5378379a773365a6da5bf8 /assets/opensb/rendering | |
parent | 4328119e1c1f8578a92977a943a793b2ac9032e0 (diff) |
Split shaders into their own files
some unrelated directives thing too
Diffstat (limited to 'assets/opensb/rendering')
-rw-r--r-- | assets/opensb/rendering/opengl20.config | 38 | ||||
-rw-r--r-- | assets/opensb/rendering/opengl20.frag | 75 | ||||
-rw-r--r-- | assets/opensb/rendering/opengl20.vert | 41 |
3 files changed, 154 insertions, 0 deletions
diff --git a/assets/opensb/rendering/opengl20.config b/assets/opensb/rendering/opengl20.config new file mode 100644 index 0000000..923578c --- /dev/null +++ b/assets/opensb/rendering/opengl20.config @@ -0,0 +1,38 @@ +{ + "effectParameters" : { + "lightMapEnabled" : { + "type" : "bool", + "default" : false, + "uniform" : "lightMapEnabled" + }, + "lightMapScale" : { + "type" : "vec2", + "default" : [1, 1], + "uniform" : "lightMapScale" + }, + "lightMapOffset" : { + "type" : "vec2", + "default" : [0, 0], + "uniform" : "lightMapOffset" + }, + "lightMapMultiplier" : { + "type" : "float", + "default" : 1.0, + "uniform" : "lightMapMultiplier" + } + }, + + "effectTextures" : { + "lightMap" : { + "textureUniform" : "lightMap", + "textureSizeUniform" : "lightMapSize", + "textureAddressing" : "clamp", + "textureFiltering" : "linear" + } + }, + + "effectShaders" : { + "vertex" : "opengl20.vert", + "fragment" : "opengl20.frag" + } +} diff --git a/assets/opensb/rendering/opengl20.frag b/assets/opensb/rendering/opengl20.frag new file mode 100644 index 0000000..92a121f --- /dev/null +++ b/assets/opensb/rendering/opengl20.frag @@ -0,0 +1,75 @@ +#version 110 + +uniform sampler2D texture0; +uniform sampler2D texture1; +uniform sampler2D texture2; +uniform sampler2D texture3; +uniform bool lightMapEnabled; +uniform vec2 lightMapSize; +uniform sampler2D lightMap; +uniform float lightMapMultiplier; + +varying vec2 fragmentTextureCoordinate; +varying float fragmentTextureIndex; +varying vec4 fragmentColor; +varying float fragmentLightMapMultiplier; +varying vec2 fragmentLightMapCoordinate; + +vec4 cubic(float v) { + vec4 n = vec4(1.0, 2.0, 3.0, 4.0) - v; + vec4 s = n * n * n; + float x = s.x; + float y = s.y - 4.0 * s.x; + float z = s.z - 4.0 * s.y + 6.0 * s.x; + float w = 6.0 - x - y - z; + return vec4(x, y, z, w); +} + +vec4 bicubicSample(sampler2D texture, vec2 texcoord, vec2 texscale) { + texcoord = texcoord - vec2(0.5, 0.5); + + float fx = fract(texcoord.x); + float fy = fract(texcoord.y); + texcoord.x -= fx; + texcoord.y -= fy; + + vec4 xcubic = cubic(fx); + vec4 ycubic = cubic(fy); + + vec4 c = vec4(texcoord.x - 0.5, texcoord.x + 1.5, texcoord.y - 0.5, texcoord.y + 1.5); + vec4 s = vec4(xcubic.x + xcubic.y, xcubic.z + xcubic.w, ycubic.x + ycubic.y, ycubic.z + ycubic.w); + vec4 offset = c + vec4(xcubic.y, xcubic.w, ycubic.y, ycubic.w) / s; + + vec4 sample0 = texture2D(texture, vec2(offset.x, offset.z) * texscale); + vec4 sample1 = texture2D(texture, vec2(offset.y, offset.z) * texscale); + vec4 sample2 = texture2D(texture, vec2(offset.x, offset.w) * texscale); + vec4 sample3 = texture2D(texture, vec2(offset.y, offset.w) * texscale); + + float sx = s.x / (s.x + s.y); + float sy = s.z / (s.z + s.w); + + return mix( + mix(sample3, sample2, sx), + mix(sample1, sample0, sx), sy); +} + +void main() { + vec4 texColor; + if (fragmentTextureIndex > 2.9) { + texColor = texture2D(texture3, fragmentTextureCoordinate); + } else if (fragmentTextureIndex > 1.9) { + texColor = texture2D(texture2, fragmentTextureCoordinate); + } else if (fragmentTextureIndex > 0.9) { + texColor = texture2D(texture1, fragmentTextureCoordinate); + } else { + texColor = texture2D(texture0, fragmentTextureCoordinate); + } + if (texColor.a <= 0.0) + discard; + + vec4 finalColor = texColor * fragmentColor; + float finalLightMapMultiplier = fragmentLightMapMultiplier * lightMapMultiplier; + if (lightMapEnabled && finalLightMapMultiplier > 0.0) + finalColor.rgb *= bicubicSample(lightMap, fragmentLightMapCoordinate, 1.0 / lightMapSize).rgb * finalLightMapMultiplier; + gl_FragColor = finalColor; +}
\ No newline at end of file diff --git a/assets/opensb/rendering/opengl20.vert b/assets/opensb/rendering/opengl20.vert new file mode 100644 index 0000000..67998bc --- /dev/null +++ b/assets/opensb/rendering/opengl20.vert @@ -0,0 +1,41 @@ +#version 110 + +uniform vec2 textureSize0; +uniform vec2 textureSize1; +uniform vec2 textureSize2; +uniform vec2 textureSize3; +uniform vec2 screenSize; +uniform mat3 vertexTransform; +uniform vec2 lightMapSize; +uniform vec2 lightMapScale; +uniform vec2 lightMapOffset; + +attribute vec2 vertexPosition; +attribute vec2 vertexTextureCoordinate; +attribute float vertexTextureIndex; +attribute vec4 vertexColor; +attribute float vertexParam1; + +varying vec2 fragmentTextureCoordinate; +varying float fragmentTextureIndex; +varying vec4 fragmentColor; +varying float fragmentLightMapMultiplier; +varying vec2 fragmentLightMapCoordinate; + +void main() { + vec2 screenPosition = (vertexTransform * vec3(vertexPosition, 1.0)).xy; + fragmentLightMapMultiplier = vertexParam1; + fragmentLightMapCoordinate = (screenPosition / lightMapScale) - lightMapOffset * lightMapSize / screenSize; + if (vertexTextureIndex > 2.9) { + fragmentTextureCoordinate = vertexTextureCoordinate / textureSize3; + } else if (vertexTextureIndex > 1.9) { + fragmentTextureCoordinate = vertexTextureCoordinate / textureSize2; + } else if (vertexTextureIndex > 0.9) { + fragmentTextureCoordinate = vertexTextureCoordinate / textureSize1; + } else { + fragmentTextureCoordinate = vertexTextureCoordinate / textureSize0; + } + fragmentTextureIndex = vertexTextureIndex; + fragmentColor = vertexColor; + gl_Position = vec4(screenPosition / screenSize * 2.0 - 1.0, 0.0, 1.0); +}
\ No newline at end of file |