Last active
February 4, 2018 03:05
-
-
Save romualdo97/5eea453cc34482bc5973d5491f501bd3 to your computer and use it in GitHub Desktop.
Simple texture advection: texture flow shader
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
#version 150 | |
// CONSTANTS | |
#define DEBUG_MODE | |
// UNIFORMS | |
uniform sampler2D tex0; // flow field texture | |
uniform sampler2D tex1; // texture A for swapping | |
uniform sampler2D tex2; // texture B for swapping | |
uniform float time; | |
uniform float cycle; | |
// VARIYNG FROM VERTEX SHADER | |
in vec2 vTexCoord; // uv coords | |
// FRAGMENT SHADER OUTPUT | |
out vec4 outputColor; | |
vec2 unpackFlowVector(vec2 flowVecData) | |
{ | |
vec2 flowVector = flowVecData * 2.0 - 1.0; | |
return flowVector; | |
} | |
void main() | |
{ | |
float halfCycle = cycle * 0.5; | |
float flowMapOffset0 = mod(time/20, cycle); | |
float flowMapOffset1 = mod((time/20) + halfCycle, cycle) | |
vec4 color = vec4( 1.0, 0.0, 0.0, 1.0 ); | |
vec4 flowMapTexel = texture(tex0, vTexCoord); | |
vec2 flowDir = unpackFlowVector(flowMapTexel.rg); | |
vec4 texelA = texture(tex1, vTexCoord + flowDir * flowMapOffset0); // texture2D is deprecated since version 1.30 | |
vec4 texelB = texture(tex2, vTexCoord + flowDir * flowMapOffset1); | |
float mixFactor = (abs(halfCycle - flowMapOffset0) / halfCycle); | |
color = mix(texelA, texelB, mixFactor); | |
#ifdef DEBUG_MODE | |
outputColor = mix(color, flowMapTexel, 0.3); | |
#else | |
outputColor = color; | |
#endif | |
} |
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
/* | |
In this version we expect to calculate the texture offset from cpu | |
*/ | |
#version 150 | |
// CONSTANTS | |
#define DEBUG_MODE | |
// UNIFORMS | |
uniform sampler2D tex0; // flow field texture | |
uniform sampler2D tex1; // texture A for swapping | |
uniform sampler2D tex2; // texture b for swapping | |
uniform float time; | |
float halfCycle; | |
//Flow map offsets used to scroll the wave maps | |
uniform float flowMapOffset0; | |
uniform float flowMapOffset1; | |
// VARIYNG FROM VERTEX SHADER | |
in vec2 vTexCoord; // uv coords | |
// FRAGMENT SHADER OUTPUT | |
out vec4 outputColor; | |
vec2 unpackFlowVector(vec2 flowVecData) | |
{ | |
vec2 flowVector = flowVecData * 2.0 - 1.0; | |
return flowVector; | |
} | |
void main() | |
{ | |
vec4 color = vec4( 1.0, 0.0, 0.0, 1.0 ); | |
vec4 flowMapTexel = texture(tex0, vTexCoord); | |
vec2 flowDir = unpackFlowVector(flowMapTexel.rg); | |
vec4 texelA = texture(tex1, vTexCoord + flowDir * flowMapOffset0); // texture2D is deprecated since version 1.30 | |
vec4 texelB = texture(tex2, vTexCoord + flowDir * flowMapOffset1); | |
float mixFactor = (abs(halfCycle - flowMapOffset0) / halfCycle); | |
color = mix(texelA, texelB, mixFactor); | |
#ifdef DEBUG_MODE | |
outputColor = mix(color, flowMapTexel, 0.3); | |
#else | |
outputColor = color; | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment