Variable updates to follow a standardized glsl variable naming convention and shader updates.

This commit is contained in:
Chillance 2018-10-07 17:14:41 +02:00
parent 16e4c45283
commit 99aeaab533
4 changed files with 32 additions and 135 deletions

View File

@ -52,9 +52,9 @@ and we call that function like so:
``` go
EasyBindUniforms(canvas,
"u_resolution", &uResolution,
"u_time", &uTime,
"u_mouse", &uMouse,
"uResolution", &uResolution,
"uTime", &uTime,
"uMouse", &uMouse,
)
```
@ -63,9 +63,9 @@ and we call that function like so:
We also need to do some updates to the shader file itself to match these variables. First thing would be to add the variables we exposed in Go.
```
uniform vec2 u_resolution;
uniform float u_time;
uniform vec4 u_mouse;
uniform vec2 uResolution;
uniform float uTime;
uniform vec4 uMouse;
```
Then we just rename the variables to match.
@ -87,16 +87,20 @@ to
```
gl_FragCoord
```
because this is available globaly in the OpenGL space for the shader.
and rename:
We also need to add:
```
fragColor
out vec4 fragColor;
```
to
to expose that.
Lastly, we need to add:
```
gl_FragColor
#version 330 core
```
because these are available globaly in the OpenGL space for the shader.
at the top to tell what version we require.
## Using shader
@ -112,7 +116,7 @@ where fragSource is the fragment shader, not a path fo a file.
Here is a diff of the changes:
![seascape animation](shader_diffs.png "Seascape animation")
![code changes](shader_diffs.png "Code changes")
And that is it. Running the program we should see this:

View File

@ -42,9 +42,9 @@ func run() {
uResolution := mgl32.Vec2{float32(win.Bounds().W()), float32(win.Bounds().H())}
EasyBindUniforms(canvas,
"u_resolution", &uResolution,
"u_time", &uTime,
"u_mouse", &uMouse,
"uResolution", &uResolution,
"uTime", &uTime,
"uMouse", &uMouse,
)
canvas.SetFragmentShader(fragSource)
@ -71,114 +71,3 @@ func run() {
func main() {
pixelgl.Run(run)
}
var cloudsFragmentShader = `
#version 330 core
#ifdef GL_ES
precision highp float;
#endif
#define HOW_CLOUDY 0.2
#define SHADOW_THRESHOLD 0.4
#define SHADOW 0.3
#define SUBSURFACE 1.0
#define WIND_DIRECTION 0.3
#define TIME_SCALE 0.6
#define SCALE 0.1
//#define ENABLE_SHAFTS
in vec2 texcoords;
out vec4 fragColor;
mat2 RM = mat2(cos(WIND_DIRECTION), -sin(WIND_DIRECTION), sin(WIND_DIRECTION), cos(WIND_DIRECTION));
uniform float u_time;
uniform vec2 u_mouse;
//uniform vec2 u_resolution;
uniform vec4 u_texbounds;
uniform sampler2D u_texture;
uniform vec2 u_gopherpos;
float hash( float n )
{
return fract(sin(n)*758.5453);
}
float noise( in vec3 x )
{
vec3 p = floor(x);
vec3 f = fract(x);
float n = p.x + p.y*57.0 + p.z*800.0;
float res = mix(mix(mix( hash(n+ 0.0), hash(n+ 1.0),f.x), mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y),
mix(mix( hash(n+800.0), hash(n+801.0),f.x), mix( hash(n+857.0), hash(n+858.0),f.x),f.y),f.z);
return res;
}
float fbm( vec3 p )
{
float f = 0.0;
f += 0.50000*noise( p ); p = p*2.02;
f -= 0.25000*noise( p ); p = p*2.03;
f += 0.12500*noise( p ); p = p*3.01;
f += 0.06250*noise( p ); p = p*3.04;
f += 0.03500*noise( p ); p = p*4.01;
f += 0.01250*noise( p ); p = p*4.04;
f -= 0.00125*noise( p );
return f/0.784375;
}
float cloud(vec3 p)
{
p-=fbm(vec3(p.x,p.y,0.0)*0.5)*1.25;
float a = min((fbm(p*3.0)*2.2-1.1), 0.0);
return a*a;
}
float shadow = 1.0;
float clouds(vec2 p){
float ic = cloud(vec3(p * 2.0, u_time*0.01 * TIME_SCALE)) / HOW_CLOUDY;
float init = smoothstep(0.1, 1.0, ic) * 5.0;
shadow = smoothstep(0.0, SHADOW_THRESHOLD, ic) * SHADOW + (1.0 - SHADOW);
init = (init * cloud(vec3(p * (6.0), u_time*0.01 * TIME_SCALE)) * ic);
init = (init * (cloud(vec3(p * (11.0), u_time*0.01 * TIME_SCALE))*0.5 + 0.4) * init);
return min(1.0, init);
}
//uniform sampler2D bb;
float cloudslowres(vec2 p){
return 1.0 - (texture(u_texture, p).a - 0.9) * 10.0;
}
vec2 ratio = vec2(1.0, 1.0);
vec4 getresult(){
vec2 uvmouse = (u_mouse/(texcoords - u_texbounds.xy));
vec2 t = (texcoords - u_texbounds.xy) / u_texbounds.zw;
//vec2 surfacePosition = ((( t ) * vec2(u_gopherpos.x , u_gopherpos.y)) * 2.0 - 1.0)*SCALE;
vec2 surfacePosition = t+u_gopherpos*10.0;
vec2 position = ( surfacePosition * SCALE);
vec2 sun = (uvmouse.xy * vec2(texcoords.x / texcoords.y, 1.0)*2.0-1.0) * SCALE;
float dst = distance(sun * ratio, position * ratio);
float suni = pow(dst + 1.0, -10.0);
float shaft =0.0;
float st = 0.05;
float w = 1.0;
vec2 dir = sun - position;
float c = clouds(position);
#ifdef ENABLE_SHAFTS
for(int i=0;i<50;i++){
float occl = cloudslowres(clamp((t) + dir * st, 0.0, 1.0));
w *= 0.99;
st *= 1.05;
shaft += max(0.0, (1.0 - occl)) * w;
}
#endif
shadow = min(1.0, shadow + suni * suni * 0.2 * SUBSURFACE);
suni *= (shaft * 0.03);
return vec4(pow(mix(vec3(shadow), pow(vec3(0.23, 0.33, 0.48), vec3(2.2)) + suni, c), vec3(1.0/2.2)), c*0.1 + 0.9);
}
void main( void ) {
fragColor = getresult().rgba;
}
`

Binary file not shown.

Before

Width:  |  Height:  |  Size: 379 KiB

After

Width:  |  Height:  |  Size: 381 KiB

View File

@ -4,14 +4,18 @@
* Contact: tdmaav@gmail.com
*/
uniform vec2 u_resolution;
uniform float u_time;
uniform vec4 u_mouse;
#version 330 core
uniform vec2 uResolution;
uniform float uTime;
uniform vec4 uMouse;
out vec4 fragColor;
const int NUM_STEPS = 8;
const float PI = 3.141592;
const float EPSILON = 1e-3;
#define EPSILON_NRM (0.1 / u_resolution.x)
#define EPSILON_NRM (0.1 / uResolution.x)
// sea
const int ITER_GEOMETRY = 3;
@ -22,7 +26,7 @@ const float SEA_SPEED = 0.8;
const float SEA_FREQ = 0.16;
const vec3 SEA_BASE = vec3(0.1,0.19,0.22);
const vec3 SEA_WATER_COLOR = vec3(0.8,0.9,0.6);
#define SEA_TIME (1.0 + u_time * SEA_SPEED)
#define SEA_TIME (1.0 + uTime * SEA_SPEED)
const mat2 octave_m = mat2(1.6,1.2,-1.2,1.6);
// math
@ -160,10 +164,10 @@ float heightMapTracing(vec3 ori, vec3 dir, out vec3 p) {
// main
void main()
{
vec2 uv = gl_FragCoord.xy / u_resolution.xy;
vec2 uv = gl_FragCoord.xy / uResolution.xy;
uv = uv * 2.0 - 1.0;
uv.x *= u_resolution.x / u_resolution.y;
float time = u_time * 0.3 + u_mouse.x*0.01;
uv.x *= uResolution.x / uResolution.y;
float time = uTime * 0.3 + uMouse.x*0.01;
// ray
vec3 ang = vec3(sin(time*3.0)*0.1,sin(time)*0.2+0.3,time);
@ -185,5 +189,5 @@ void main()
pow(smoothstep(0.0,-0.05,dir.y),0.3));
// post
gl_FragColor = vec4(pow(color,vec3(0.75)), 1.0);
fragColor = vec4(pow(color,vec3(0.75)), 1.0);
}