Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shader Preprocessor and shader clean up #166

Merged
merged 6 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
10 changes: 10 additions & 0 deletions commons/src/main/com/mbrlabs/mundus/commons/shaders/compat.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifdef GL_ES
#define LOW lowp
#define MED mediump
#define HIGH highp
precision highp float;
#else
#define MED
#define LOW
#define HIGH
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* limitations under the License.
*/

#ifdef GL_ES
precision highp float;
#endif
#include "compat.glsl"

varying float v_clipDistance;

Expand Down
10 changes: 2 additions & 8 deletions commons/src/main/com/mbrlabs/mundus/commons/shaders/light.glsl
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// Lighting adapted from OGLDEV tutorials https://www.youtube.com/c/OGLDEV
#ifdef GL_ES
#define LOW lowp
#define MED mediump
#define HIGH highp
precision highp float;
#else
// Just stops the static analysis from complaining
#ifndef MED
#define MED
#define LOW
#define HIGH
#endif

struct BaseLight
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* limitations under the License.
*/

#ifdef GL_ES
precision highp float;
#endif
#include "compat.glsl"

varying vec2 v_texCoords0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
* limitations under the License.
*/

#ifdef GL_ES
precision highp float;
#endif

#include "compat.glsl"

uniform samplerCube u_texture;
uniform int u_fog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@
* limitations under the License.
*/

#ifdef GL_ES
#define LOW lowp
#define MED mediump
#define HIGH highp
precision highp float;
#else
#include "compat.glsl"
#include "light.glsl"

// Just stops the static analysis from complaining
#ifndef MED
#define MED
#define LOW
#define HIGH
#endif

#define PI 3.1415926535897932384626433832795
Expand Down Expand Up @@ -88,9 +85,15 @@ varying vec3 v_pos;
// light
varying mat3 v_TBN;

varying MED vec2 v_texCoord0;
varying vec2 v_texCoord0;
varying float v_clipDistance;

// Brings the normal from [0, 1] to [-1, 1]
vec3 unpackNormal(vec3 normal)
{
return normalize(normal * 2.0 - 1.0);
}

void main(void) {
if ( v_clipDistance < 0.0 )
discard;
Expand All @@ -100,7 +103,7 @@ void main(void) {
gl_FragColor = texture2D(u_baseTexture, v_texCoord0);

#ifdef baseNormalFlag
normal = texture2D(u_texture_base_normal, v_texCoord0).rgb;
normal = unpackNormal(texture2D(u_texture_base_normal, v_texCoord0).rgb);
#endif

// Mix splat textures
Expand All @@ -120,26 +123,31 @@ void main(void) {
#endif

#ifdef normalTextureFlag
vec3 splatNormal = vec3(0.0);
// Splat normals
#ifdef splatRNormalFlag
normal = mix(normal, texture2D(u_texture_r_normal, v_texCoord0).rgb, splat.r);
splatNormal += unpackNormal(texture2D(u_texture_r_normal, v_texCoord0).rgb) * splat.r;
#endif
#ifdef splatGNormalFlag
normal = mix(normal, texture2D(u_texture_g_normal, v_texCoord0).rgb, splat.g);
splatNormal += unpackNormal(texture2D(u_texture_g_normal, v_texCoord0).rgb) * splat.g;
#endif
#ifdef splatBNormalFlag
normal = mix(normal, texture2D(u_texture_b_normal, v_texCoord0).rgb, splat.b);
splatNormal += unpackNormal(texture2D(u_texture_b_normal, v_texCoord0).rgb) * splat.b;
#endif
#ifdef splatANormalFlag
normal = mix(normal, texture2D(u_texture_a_normal, v_texCoord0).rgb, splat.a);
splatNormal += unpackNormal(texture2D(u_texture_a_normal, v_texCoord0).rgb) * splat.a;
#endif

// The base normal should only be visible when the sum of the splat weights is less than 1.0
float normalBlendFactor = (1.0 - splat.r - splat.g - splat.b - splat.a);
normal = normalize((normal * normalBlendFactor) + splatNormal);
#endif

#endif

#ifdef normalTextureFlag
normal = normalize(v_TBN * ((2.0 * normal - 1.0)));
// Apply TBN matrix to tangent space normal to get world space normal
normal = normalize(v_TBN * normal);
#else
normal = normalize(v_TBN[2].xyz);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* limitations under the License.
*/

#ifdef GL_ES
precision highp float;
#endif
#include "compat.glsl"

attribute vec3 a_position;
attribute vec3 a_normal;
Expand Down
206 changes: 0 additions & 206 deletions commons/src/main/com/mbrlabs/mundus/commons/shaders/water.frag.glsl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#ifdef GL_ES
#define LOW lowp
#define MED mediump
#define HIGH highp
precision highp float;
#else
#include "compat.glsl"
#include "light.glsl"

// Just stops the static analysis from complaining
#ifndef MED
#define MED
#define LOW
#define HIGH
#endif

varying MED vec2 v_texCoord0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#ifdef GL_ES
precision highp float;
#endif
#include "compat.glsl"

attribute vec3 a_position;
attribute vec3 a_normal;
Expand Down
Loading