This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
forked from AitorSimona/CENTRAL-3D
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #331 from Broken-Gem-Studio/waterShader
Water Shader
- Loading branch information
Showing
6 changed files
with
887 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
Broken Engine/Game/Assets/Shaders/WaterShaders/FinalShaderWater_v1.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#version 440 core | ||
|
||
#define VERTEX_SHADER | ||
#ifdef VERTEX_SHADER | ||
|
||
//Layouts | ||
layout (location = 0) in vec3 a_Position; | ||
layout (location = 1) in vec3 a_Normal; | ||
layout (location = 2) in vec3 a_Color; | ||
layout (location = 3) in vec2 a_TexCoord; | ||
|
||
//Uniforms | ||
uniform mat4 u_Model; | ||
uniform mat4 u_View; | ||
uniform mat4 u_Proj; | ||
|
||
uniform float time = 0.0; | ||
uniform float Amplitude = 5.0; | ||
uniform vec2 FOAMDirection = vec2(0, 0); | ||
uniform float FOAMVelocity = 0.5; | ||
uniform float MaxTime = 10.0; | ||
uniform float Velocity = 10.0; | ||
uniform float WaveLength = 1.0; | ||
uniform float WaveMovementMultiplicator = 2.0; | ||
|
||
//Variables | ||
const float pi = 3.14159; | ||
|
||
//Data sent to fragment shader (varying) | ||
out float v_VertHeight; | ||
out float v_MaxHeight; | ||
out vec2 v_TexCoords; | ||
|
||
float random(vec2 p) | ||
{ | ||
vec2 K1 = vec2(23.14069263277926, 2.665144142690225); | ||
|
||
//return fract( cos( mod( 12345678., 256. * dot(p,K1) ) ) ); // ver1 | ||
return fract(cos(dot(p,K1)) * 12345.6789); // ver3 | ||
} | ||
|
||
float random2(vec2 co) | ||
{ | ||
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); | ||
} | ||
|
||
void main() | ||
{ | ||
//Water Calcs. | ||
float height = Amplitude*sin(2.0*pi*((time/MaxTime)*Velocity - (a_Position.x/WaveLength))); | ||
float dX = sin(2.0*a_Position.x + time); | ||
float dZ = sin(2.0*a_Position.y + time); | ||
//float Xf = a_Position.x/u_WaveLength - 0.5; | ||
//float Yf = a_Position.y/u_Amplitude - 0.5; | ||
// | ||
//vec2 vec = vec2(Xf, Yf); | ||
//float height = random2(vec) * 2.0*pi*(u_Time/u_MaxTime); | ||
vec2 tCoordsDir = clamp(normalize(FOAMDirection), 0.0, 1.0); | ||
|
||
vec2 tCoords = a_TexCoord; | ||
tCoords.x -= FOAMVelocity * time/MaxTime * tCoordsDir.x; | ||
tCoords.y -= FOAMVelocity * time/MaxTime * tCoordsDir.y; | ||
|
||
v_VertHeight = height; | ||
v_MaxHeight = Amplitude; | ||
v_TexCoords = tCoords; | ||
|
||
gl_Position = u_Proj * u_View * u_Model * vec4(a_Position.x, a_Position.y, a_Position.z + dX*dZ*WaveMovementMultiplicator, 1.0); | ||
gl_Position.y += height; | ||
} | ||
|
||
|
||
#endif //VERTEX_SHADER | ||
|
||
#define FRAGMENT_SHADER | ||
#ifdef FRAGMENT_SHADER | ||
|
||
//Uniforms | ||
uniform sampler2D u_AlbedoTexture; | ||
uniform sampler2D u_SpecularTexture; | ||
|
||
uniform int u_TextureEmpty = 1; | ||
//uniform int u_UseTextures = 0; | ||
//uniform int u_HasDiffuseTexture = 0; | ||
|
||
//Uniforms | ||
uniform float u_GammaCorrection = 1.0; | ||
uniform vec4 u_AmbientColor = vec4(1.0); | ||
uniform vec4 u_Color = vec4(1.0); | ||
uniform float u_ColorGradingOffset = 0.0; //Keep it between 0 and 1 | ||
|
||
//Data sent from vertex shader | ||
in float v_VertHeight; | ||
in float v_MaxHeight; | ||
in vec2 v_TexCoords; | ||
|
||
//Color output | ||
out vec4 color; | ||
|
||
void main() | ||
{ | ||
float normalisedHeight = v_VertHeight/(v_MaxHeight + 2.0); | ||
float colorGrading = u_ColorGradingOffset; | ||
|
||
if(colorGrading >= 1.0) | ||
colorGrading = 1.0; | ||
if(colorGrading <= 0.0) | ||
colorGrading = 0.0; | ||
|
||
color = texture(u_AlbedoTexture, v_TexCoords) * u_AmbientColor * u_Color;// + texture(u_SpecularTexture, v_TexCoords) * 0.2; | ||
//color.r *= (colorGrading + normalisedHeight); | ||
//color.g *= (colorGrading + normalisedHeight); | ||
|
||
color = pow(color, vec4(vec3(1.0/u_GammaCorrection), 1.0)); | ||
} | ||
|
||
#endif |
155 changes: 155 additions & 0 deletions
155
Broken Engine/Game/Assets/Shaders/WaterShaders/FinalShaderWater_v2.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
#version 440 core | ||
|
||
#define VERTEX_SHADER | ||
#ifdef VERTEX_SHADER | ||
|
||
//Layouts | ||
layout (location = 0) in vec3 a_Position; | ||
layout (location = 1) in vec3 a_Normal; | ||
layout (location = 2) in vec3 a_Color; | ||
layout (location = 3) in vec2 a_TexCoord; | ||
|
||
//Uniforms | ||
uniform mat4 u_Model; | ||
uniform mat4 u_View; | ||
uniform mat4 u_Proj; | ||
uniform vec3 u_CameraPosition; | ||
|
||
uniform float time = 0.0; | ||
uniform float Amplitude = 5.0; | ||
uniform vec2 FOAMDirection = vec2(0, 0); | ||
uniform float FOAMVelocity = 0.5; | ||
uniform float MaxTime = 10.0; | ||
uniform float Velocity = 10.0; | ||
uniform float WaveLength = 1.0; | ||
uniform float WaveMovementMultiplicator = 2.0; | ||
|
||
//Variables | ||
const float pi = 3.14159; | ||
|
||
//Data sent to fragment shader (varying) | ||
out float v_VertHeight; | ||
out float v_MaxHeight; | ||
out vec2 v_TexCoords; | ||
out vec3 v_CamPos; | ||
out vec3 v_FragPos; | ||
out vec3 v_Normal; | ||
|
||
float random(vec2 p) | ||
{ | ||
vec2 K1 = vec2(23.14069263277926, 2.665144142690225); | ||
|
||
//return fract( cos( mod( 12345678., 256. * dot(p,K1) ) ) ); // ver1 | ||
return fract(cos(dot(p,K1)) * 12345.6789); // ver3 | ||
} | ||
|
||
float random2(vec2 co) | ||
{ | ||
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); | ||
} | ||
|
||
void main() | ||
{ | ||
//Water Calcs. | ||
float height = Amplitude*sin(2.0*pi*((time/MaxTime)*Velocity - (a_Position.x/WaveLength))); | ||
float dX = sin(2.0*a_Position.x + time); | ||
float dZ = sin(2.0*a_Position.y + time); | ||
//float Xf = a_Position.x/u_WaveLength - 0.5; | ||
//float Yf = a_Position.y/u_Amplitude - 0.5; | ||
// | ||
//vec2 vec = vec2(Xf, Yf); | ||
//float height = random2(vec) * 2.0*pi*(u_Time/u_MaxTime); | ||
vec2 tCoordsDir = clamp(normalize(FOAMDirection), 0.0, 1.0); | ||
|
||
vec2 tCoords = a_TexCoord; | ||
tCoords.x -= FOAMVelocity * time/MaxTime * tCoordsDir.x; | ||
tCoords.y -= FOAMVelocity * time/MaxTime * tCoordsDir.y; | ||
|
||
v_VertHeight = height; | ||
v_MaxHeight = Amplitude; | ||
v_TexCoords = tCoords; | ||
v_CamPos = u_CameraPosition; | ||
|
||
v_FragPos = vec3(u_Model * vec4(a_Position, 1.0)); | ||
v_Normal = mat3(transpose(inverse(u_Model))) * a_Normal; | ||
|
||
gl_Position = u_Proj * u_View * u_Model * vec4(a_Position.x, a_Position.y, a_Position.z + dX*dZ*WaveMovementMultiplicator, 1.0); | ||
gl_Position.y += height; | ||
} | ||
|
||
|
||
#endif //VERTEX_SHADER | ||
|
||
#define FRAGMENT_SHADER | ||
#ifdef FRAGMENT_SHADER | ||
|
||
//Uniforms | ||
uniform sampler2D u_AlbedoTexture; | ||
uniform sampler2D u_SpecularTexture; | ||
uniform samplerCube skybox; | ||
|
||
uniform int u_TextureEmpty = 1; | ||
//uniform int u_UseTextures = 0; | ||
//uniform int u_HasDiffuseTexture = 0; | ||
|
||
//Uniforms | ||
uniform float u_GammaCorrection = 1.0; | ||
uniform vec4 u_AmbientColor = vec4(1.0); | ||
uniform vec4 u_Color = vec4(1.0); | ||
uniform float u_ColorGradingOffset = 0.0; //Keep it between 0 and 1 | ||
|
||
uniform int None_Reflect_Refract = 0; | ||
uniform float refractive_reflective_index = 1.0; | ||
|
||
//Data sent from vertex shader | ||
in float v_VertHeight; | ||
in float v_MaxHeight; | ||
in vec2 v_TexCoords; | ||
in vec3 v_CamPos; | ||
in vec3 v_Normal; | ||
in vec3 v_FragPos; | ||
|
||
//Color output | ||
out vec4 color; | ||
|
||
void main() | ||
{ | ||
float normalisedHeight = v_VertHeight/(v_MaxHeight + 2.0); | ||
float colorGrading = u_ColorGradingOffset; | ||
|
||
if(colorGrading >= 1.0) | ||
colorGrading = 1.0; | ||
if(colorGrading <= 0.0) | ||
colorGrading = 0.0; | ||
|
||
vec4 reflectingColor = vec4(1.0); | ||
if(None_Reflect_Refract == 1) | ||
{ | ||
vec3 I = normalize(v_FragPos - v_CamPos); | ||
vec3 R = reflect(I, normalize(v_Normal)); | ||
|
||
reflectingColor = vec4(texture(skybox, R).rgb, 1.0) * refractive_reflective_index; | ||
|
||
//vec3 topTexture = texture(u_SpecularTexture, R.yz).rgb; | ||
//reflectingColor = vec4(topTexture, 1.0) * refractive_reflective_index; | ||
} | ||
else if(None_Reflect_Refract == 2) | ||
{ | ||
float ratio = 1.0/refractive_reflective_index; | ||
vec3 I = normalize (v_FragPos - v_CamPos); | ||
vec3 R = refract(I, normalize(v_Normal), ratio); | ||
|
||
reflectingColor = vec4(texture(skybox, R).rgb, 1.0); | ||
|
||
//vec3 topTexture = texture(u_SpecularTexture, R.yz).rgb; | ||
//reflectingColor = vec4(topTexture, 1.0) * ratio; | ||
} | ||
|
||
color = texture(u_AlbedoTexture, v_TexCoords) * u_AmbientColor * u_Color;// + texture(u_SpecularTexture, v_TexCoords) * 0.2; | ||
//color.r *= (colorGrading + normalisedHeight); | ||
//color.g *= (colorGrading + normalisedHeight); | ||
color *= reflectingColor; | ||
color = pow(color, vec4(vec3(1.0/u_GammaCorrection), 1.0)); | ||
} | ||
|
||
#endif |
Oops, something went wrong.