Terrain anyone
// Project: road
// Created: 2018-08-11
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "road" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
createVSShaderFile()
createPSshaderFile()
chunk = CreateObjectPlane(60,60)
SetObjectColor(chunk,255,0,0,255)
SetObjectRotation(chunk,0,0,0)
vs=LoadShader("road.vs","road.ps")
SetObjectShader(chunk,vs)
a=1
SetObjectShaderConstantByName(chunk,"mouse",0,0,0,0)
do
x#=10 * sin(a)
y#=10 * cos(a)
inc a
SetObjectShaderConstantByName(chunk,"resolution",500+timer()*x#,500+timer()*y#,0,0)
SetObjectShaderConstantByName(chunk,"time",timer(),0,0,0)
Print( ScreenFPS() )
Sync()
loop
// This is the function which creates the two files VS and PS
// These just simply output lines of code to the two files
function createVSShaderFile()
fw = OpenToWrite("road.vs")
WriteLine(fw,"attribute highp vec3 position;")
WriteLine(fw,"attribute mediump vec3 normal;")
WriteLine(fw,"varying highp vec3 posVarying;")
WriteLine(fw,"varying mediump vec3 normalVarying;")
WriteLine(fw,"varying mediump vec3 lightVarying;")
WriteLine(fw,"mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );")
WriteLine(fw,"uniform highp mat3 agk_WorldNormal;")
WriteLine(fw,"uniform highp mat4 agk_World;")
WriteLine(fw,"uniform highp mat4 agk_ViewProj;")
WriteLine(fw,"attribute highp vec2 uv;")
WriteLine(fw,"varying highp vec2 uvVarying;")
WriteLine(fw,"uniform highp vec4 uvBounds0;")
WriteLine(fw,"void main()")
WriteLine(fw,"{ ")
WriteLine(fw,"uvVarying = uv * uvBounds0.xy + uvBounds0.zw;")
WriteLine(fw,"highp vec4 pos = agk_World * vec4(position,1.0);")
WriteLine(fw,"mediump vec3 norm = normalize(agk_WorldNormal * normal);")
WriteLine(fw,"gl_Position = agk_ViewProj * pos;")
WriteLine(fw,"posVarying = pos.xyz;")
WriteLine(fw,"normalVarying = norm;")
WriteLine(fw,"lightVarying = GetVSLighting( norm, posVarying );")
WriteLine(fw,"}")
CloseFile(fw)
endfunction
function createPSshaderFile()
fw=OpenToWrite("road.ps")
WriteLine(fw,"#ifdef GL_ES")
WriteLine(fw,"precision mediump float;")
WriteLine(fw,"#endif")
// glslsandbox uniforms
WriteLine(fw,"uniform float time;")
WriteLine(fw,"uniform vec2 resolution;")
// shadertoy globals
WriteLine(fw,"float iTime = 0.0;")
WriteLine(fw,"vec3 iResolution = vec3(0.0);")
// --------[ Original ShaderToy begins here ]---------- //
WriteLine(fw,"#ifdef GL_ES")
WriteLine(fw,"precision mediump float;")
WriteLine(fw,"#endif")
WriteLine(fw,"const float dMax = 28.0;")
// Simple noise algorithm contributed by Trisomie21 (Thanks!)
WriteLine(fw,"float snoise( vec2 p ) {")
WriteLine(fw,"vec2 f = fract(p);")
WriteLine(fw,"p = floor(p);")
WriteLine(fw,"float v = p.x+p.y*1000.0;")
WriteLine(fw,"vec4 r = vec4(v, v+1.0, v+1000.0, v+1001.0);")
WriteLine(fw,"r = fract(100000.0*sin(r*.001));")
WriteLine(fw,"f = f*f*(3.0-2.0*f);")
WriteLine(fw,"return 2.0*(mix(mix(r.x, r.y, f.x), mix(r.z, r.w, f.x), f.y))-1.0;")
WriteLine(fw,"}")
WriteLine(fw,"float terrain( vec2 p, int octaves ) { ")
WriteLine(fw,"float h = 0.0; // height")
WriteLine(fw,"float w = 0.5; // octave weight")
WriteLine(fw,"float m = 0.4; // octave multiplier")
WriteLine(fw,"for (int i=0; i<16; i++) {")
WriteLine(fw,"if (i<octaves) {")
WriteLine(fw,"h += w * snoise((p * m));")
WriteLine(fw,"}")
WriteLine(fw,"else break;")
WriteLine(fw,"w *= 0.5;")
WriteLine(fw,"m *= 2.0;")
WriteLine(fw,"}")
WriteLine(fw,"return h;")
WriteLine(fw,"}")
WriteLine(fw,"vec2 map( vec3 p, int octaves ) {")
WriteLine(fw,"float dMin = dMax; // nearest intersection")
WriteLine(fw,"float d; // depth")
WriteLine(fw,"float mID = -1.0; // material ID")
// terrain
WriteLine(fw,"float h = terrain(p.xz, octaves);")
WriteLine(fw,"h += smoothstep(-0.3, 1.5, h); // exaggerate the higher terrain")
WriteLine(fw,"h *= smoothstep(-1.5, -0.3, h); // smooth out the lower terrain")
WriteLine(fw,"d = p.y - h; ")
WriteLine(fw,"if (d<dMin) { ")
WriteLine(fw,"dMin = d;")
WriteLine(fw,"mID = 0.0;")
WriteLine(fw,"}")
// trees
WriteLine(fw,"if (h<0.0) { // no need to check for trees at higher elevations")
WriteLine(fw,"float f = terrain(p.xz*15.0, octaves);")
WriteLine(fw,"f = (0.1*f) - 0.3; // limit the altitude of the trees")
WriteLine(fw,"d = p.y - f;")
WriteLine(fw,"if (d<dMin) { ")
WriteLine(fw,"dMin = d;")
WriteLine(fw,"mID = 1.0;")
WriteLine(fw,"}")
WriteLine(fw,"} ")
WriteLine(fw,"return vec2(dMin, mID);")
WriteLine(fw,"}")
WriteLine(fw,"vec2 castRay( vec3 ro, vec3 rd, int octaves) {")
WriteLine(fw,"const float p = 0.0001; // precision")
WriteLine(fw,"float t = 0.0; // distance")
WriteLine(fw,"float h = p * 2.0; // step")
WriteLine(fw,"float m = -1.0;")
WriteLine(fw,"for (int i=0; i<34; i++) {")
WriteLine(fw,"if (abs(h)>p || t<dMax ) {")
WriteLine(fw,"t += h; // next step")
WriteLine(fw,"vec2 res = map(ro + rd*t, octaves); // get intersection")
WriteLine(fw,"h = res.x; // get distance")
WriteLine(fw,"m = res.y; // get material")
WriteLine(fw,"} ")
WriteLine(fw,"else break;")
WriteLine(fw,"}")
WriteLine(fw,"if (t>dMax) m = -1.0; // if no intersection, material ID is -1.0;")
WriteLine(fw,"return vec2(t, m);")
WriteLine(fw,"}")
WriteLine(fw,"vec3 calcNormal( vec3 p, int octaves) {")
WriteLine(fw,"const vec3 eps = vec3(0.0005, 0.0, 0.0);")
WriteLine(fw,"return normalize( vec3(map(p+eps.xyy, octaves).x - map(p-eps.xyy, octaves).x,")
WriteLine(fw," map(p+eps.yxy, octaves).x - map(p-eps.yxy, octaves).x,")
WriteLine(fw,"map(p+eps.yyx, octaves).x - map(p-eps.yyx, octaves).x) );")
WriteLine(fw,"}")
WriteLine(fw,"float shadows( vec3 ro, vec3 rd, float tMax, float k, int octaves ) {")
WriteLine(fw,"float res = 1.0;")
WriteLine(fw,"float t = 0.001;")
WriteLine(fw,"for(int i=0; i<5; i++) {")
WriteLine(fw," if (t<tMax) {")
WriteLine(fw,"float h = map(ro + rd*t, octaves).x;")
WriteLine(fw,"res = min( res, k*h/t );")
WriteLine(fw,"t += h;")
WriteLine(fw,"}")
WriteLine(fw,"else break;")
WriteLine(fw,"}")
WriteLine(fw,"return clamp(res, 0.0, 1.0);")
WriteLine(fw,"}")
WriteLine(fw,"vec3 render( vec3 ro, vec3 rd ) {")
WriteLine(fw,"const int geoLOD = 4;")
WriteLine(fw,"vec3 color = vec3(0.5,0.5,0.5); // base color is fog color")
WriteLine(fw,"vec2 res = castRay(ro, rd, geoLOD);")
WriteLine(fw," vec3 lPos = normalize( vec3(0.5, 0.5, 0.5) ); // light position")
WriteLine(fw,"vec3 lCol = vec3(1.0, 0.9, 0.8); // yellowish light")
// mat -1 = background/sky
WriteLine(fw,"if (res.y < -0.5) {")
WriteLine(fw,"float sun = clamp(dot(rd,lPos),0.0,1.0);")
WriteLine(fw,"color += 0.2 * lCol * sun*sun;")
WriteLine(fw,"return color;")
WriteLine(fw,"}")
WriteLine(fw,"int norLOD = int(max(2.0, 12.0-11.0*res.x/dMax));")
WriteLine(fw,"vec3 pos = ro + rd*res.x; // terrain pos")
WriteLine(fw,"vec3 nor = calcNormal(pos, norLOD); // terrain normals")
// mat 0 = terrain
WriteLine(fw,"if (res.y>-0.5&&res.y<0.5) {")
// base rock colors
WriteLine(fw,"color = mix( vec3(0.2, 0.2, 0.2), vec3(0.25, 0.2, 0.15), smoothstep(0.7, 1.0, nor.y) );")
// layer noise (to produdce lighter color bands of rock)
WriteLine(fw,"float n = 0.5*(snoise(pos.xy*vec2(2.0, 40.0))+1.0);")
// rock layers should show most where nomals are NOT straight up
WriteLine(fw,"color = mix( n*vec3(0.5, 0.4, 0.4), color, nor.y ); ")
// grass & moss grows thickest where normals are straight up
WriteLine(fw,"color = mix( color, vec3(0.0, 0.05, -0.05), smoothstep(0.7, 0.9, nor.y) );")
// add in lighting and shadows
WriteLine(fw,"float lAmb = clamp( 0.5 + 0.5 * nor.y, 0.0, 1.0); // ambient")
WriteLine(fw,"float lDif = clamp( dot( nor, lPos ), 0.0, 2.0); // diffuse")
// shadow octaves should match geometry octaves used in initial ray cast
WriteLine(fw,"if (lDif>0.05) lDif *= shadows(pos, lPos, 1.0, 1.0, geoLOD);")
WriteLine(fw,"color += (0.2*lAmb) * lCol;")
WriteLine(fw,"color *= (1.2*lDif) * lCol; ")
WriteLine(fw,"}")
//.mat 1 = trees
WriteLine(fw,"if (res.y>0.5) {")
WriteLine(fw,"color = mix( vec3(0.15, 0.05, 0.0), vec3(0.05, 0.1, 0.0), smoothstep(0.0, 0.7, nor.y) );")
// add in lighting and shadows
WriteLine(fw,"float lAmb = clamp( 0.5 + 0.5 * nor.y, 0.0, 1.0); // ambient")
WriteLine(fw,"float lDif = clamp( dot( nor, lPos ), 0.0, 2.0); // diffuse")
// shadow octaves should match geometry octaves used in initial ray cast
WriteLine(fw,"if (lDif>0.05) lDif *= shadows(pos, lPos, 1.0, 1.0, geoLOD);")
WriteLine(fw,"color += (0.2*lAmb) * lCol;")
WriteLine(fw,"color *= (1.2*lDif) * lCol;")
WriteLine(fw," }")
// fog
WriteLine(fw,"float n = smoothstep(-1.2, -0.2, terrain(pos.xz, 3)); // valley fog")
WriteLine(fw,"float fog = exp(-0.01 * res.x*res.x); // exponentioal fog equation")
WriteLine(fw,"color = mix(vec3(0.5,0.5,0.5), color, n*fog); // add fog in valleys and distance")
WriteLine(fw,"return color;")
WriteLine(fw,"}")
WriteLine(fw,"void mainImage( out vec4 fragColor, in vec2 fragCoord ) {")
WriteLine(fw,"vec2 pos = 2.0 * ( fragCoord.xy / iResolution.xy ) - 1.0; // bound screen coords to [0, 1]")
WriteLine(fw,"pos.x *= iResolution.x / iResolution.y; // correct for aspect ratio")
// camera
WriteLine(fw,"float x = 5.0 + (0.2*iTime);")
WriteLine(fw,"float y = 0.0;")
WriteLine(fw,"float z = 0.0 + 3.0*sin(0.1*iTime);")
WriteLine(fw,"vec3 cPos = vec3(x, y, z); // position")
WriteLine(fw,"cPos.y = terrain(cPos.xz, 1) + 1.5;")
WriteLine(fw," const vec3 cUp = vec3(0., 1., 0.); // up ")
WriteLine(fw,"vec3 cLook = vec3(cPos.x + 1.0, cPos.y*0.7, 0.0); // lookAt")
// camera matrix
WriteLine(fw,"vec3 ww = normalize( cLook-cPos );")
WriteLine(fw,"vec3 uu = normalize( cross(ww, cUp) );")
WriteLine(fw,"vec3 vv = normalize( cross(uu, ww) );")
WriteLine(fw,"vec3 rd = normalize( pos.x*uu + pos.y*vv + 2.0*ww );")
// render")
WriteLine(fw,"vec3 color = render(cPos, rd);")
WriteLine(fw,"fragColor = vec4( color, 1.0 );")
WriteLine(fw,"}")
WriteLine(fw,"void main(void)")
WriteLine(fw,"{")
WriteLine(fw,"iTime = time;")
WriteLine(fw,"iResolution = vec3(resolution, 0.0);")
WriteLine(fw," mainImage(gl_FragColor, gl_FragCoord.xy);")
WriteLine(fw,"}")
CloseFile(fw)
endfunction
Concrete world
// Project: road
// Created: 2018-08-11
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "road" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
createVSShaderFile()
createPSshaderFile()
chunk = CreateObjectPlane(60,60)
SetObjectColor(chunk,255,0,0,255)
SetObjectRotation(chunk,0,0,0)
vs=LoadShader("road.vs","road.ps")
SetObjectShader(chunk,vs)
a=1
SetObjectShaderConstantByName(chunk,"mouse",0,0,0,0)
do
x#=10 * sin(a)
y#=10 * cos(a)
inc a
SetObjectShaderConstantByName(chunk,"resolution",500+timer()*x#,500+timer()*y#,0,0)
SetObjectShaderConstantByName(chunk,"time",timer(),0,0,0)
Print( ScreenFPS() )
Sync()
loop
// This is the function which creates the two files VS and PS
// These just simply output lines of code to the two files
function createVSShaderFile()
fw = OpenToWrite("road.vs")
WriteLine(fw,"attribute highp vec3 position;")
WriteLine(fw,"attribute mediump vec3 normal;")
WriteLine(fw,"varying highp vec3 posVarying;")
WriteLine(fw,"varying mediump vec3 normalVarying;")
WriteLine(fw,"varying mediump vec3 lightVarying;")
WriteLine(fw,"mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );")
WriteLine(fw,"uniform highp mat3 agk_WorldNormal;")
WriteLine(fw,"uniform highp mat4 agk_World;")
WriteLine(fw,"uniform highp mat4 agk_ViewProj;")
WriteLine(fw,"attribute highp vec2 uv;")
WriteLine(fw,"varying highp vec2 uvVarying;")
WriteLine(fw,"uniform highp vec4 uvBounds0;")
WriteLine(fw,"void main()")
WriteLine(fw,"{ ")
WriteLine(fw,"uvVarying = uv * uvBounds0.xy + uvBounds0.zw;")
WriteLine(fw,"highp vec4 pos = agk_World * vec4(position,1.0);")
WriteLine(fw,"mediump vec3 norm = normalize(agk_WorldNormal * normal);")
WriteLine(fw,"gl_Position = agk_ViewProj * pos;")
WriteLine(fw,"posVarying = pos.xyz;")
WriteLine(fw,"normalVarying = norm;")
WriteLine(fw,"lightVarying = GetVSLighting( norm, posVarying );")
WriteLine(fw,"}")
CloseFile(fw)
endfunction
function createPSshaderFile()
fw=OpenToWrite("road.ps")
WriteLine(fw,"#ifdef GL_ES")
WriteLine(fw,"precision mediump float;")
WriteLine(fw,"#endif")
WriteLine(fw,"uniform float time;")
WriteLine(fw,"uniform vec2 mouse;")
WriteLine(fw,"uniform vec2 resolution;")
WriteLine(fw,"#define OCT 7")
WriteLine(fw,"#define ITER 192")
WriteLine(fw,"#define EPS 0.001")
WriteLine(fw,"#define NEAR .3")
WriteLine(fw,"#define FAR 16.")
WriteLine(fw,"vec3 rotX(vec3 p,float a){return vec3(p.x,p.y*cos(a)-p.z*sin(a),p.y*sin(a)+p.z*cos(a));}")
WriteLine(fw,"vec3 rotY(vec3 p,float a){return vec3(p.x*cos(a)-p.z*sin(a),p.y,p.x*sin(a)+p.z*cos(a));}")
WriteLine(fw,"vec3 rotZ(vec3 p,float a){return vec3(p.x*cos(a)-p.y*sin(a), p.x*sin(a)+p.y*cos(a), p.z);}")
WriteLine(fw,"vec3 hsv(float h,float s,float v){return ((clamp(abs(fract(h+vec3(0.,.666,.333))*6.-3.)-1.,0.,111.)-1.)*s+1.)*v;}")
WriteLine(fw,"float map(vec3 p){float r=1.,lr=0.,s=1.;;p=rotX(p,time*.15);p=rotZ(p,time*.111);p.xy+=vec2(3.4,9.3);p.z+=time;")
WriteLine(fw,"for(int i=0;i<OCT;i++){vec3 q=clamp(sin(p),.25,1.);r=max(lr,.3-dot(q,q));lr=r;s*=1.11;p*=s;}")
WriteLine(fw,"return r;}")
WriteLine(fw,"float trace(vec3 ro,vec3 rd,out float n){float t=NEAR,d;")
WriteLine(fw,"for(int i=0;i<ITER;i++){d=map(ro+rd*t);if(abs(d)<EPS||t>FAR)break;t+=step(d,1.)*d*.2+d*.5;n+=1.;}")
WriteLine(fw,"return min(t,FAR);}")
WriteLine(fw,"void main(void){")
WriteLine(fw,"vec2 uv=(gl_FragCoord.xy-.5*resolution.xy)/resolution.y;")
WriteLine(fw,"vec3 rd=vec3(uv,-.5);")
WriteLine(fw,"float n=0.,v=1.-trace(vec3(0),rd,n)/FAR;n/=float(ITER);")
WriteLine(fw,"gl_FragColor=vec4(mix(hsv(v,n,.92),vec3(1),n)*n,1);")
WriteLine(fw,"}")
CloseFile(fw)
endfunction
Ocean
// Project: road
// Created: 2018-08-11
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "road" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
createVSShaderFile()
createPSshaderFile()
chunk = CreateObjectPlane(60,60)
SetObjectColor(chunk,255,0,0,255)
SetObjectRotation(chunk,90,0,0)
vs=LoadShader("road.vs","road.ps")
SetObjectShader(chunk,vs)
a=1
SetObjectShaderConstantByName(chunk,"resolution",1024,768,0,0)
SetObjectShaderConstantByName(chunk,"mouse",359,277,0,0)
do
SetObjectShaderConstantByName(chunk,"time",timer(),0,0,0)
Print( ScreenFPS() )
Sync()
loop
// This is the function which creates the two files VS and PS
// These just simply output lines of code to the two files
function createVSShaderFile()
fw = OpenToWrite("road.vs")
WriteLine(fw,"attribute highp vec3 position;")
WriteLine(fw,"attribute mediump vec3 normal;")
WriteLine(fw,"varying highp vec3 posVarying;")
WriteLine(fw,"varying mediump vec3 normalVarying;")
WriteLine(fw,"varying mediump vec3 lightVarying;")
WriteLine(fw,"mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );")
WriteLine(fw,"uniform highp mat3 agk_WorldNormal;")
WriteLine(fw,"uniform highp mat4 agk_World;")
WriteLine(fw,"uniform highp mat4 agk_ViewProj;")
WriteLine(fw,"attribute highp vec2 uv;")
WriteLine(fw,"varying highp vec2 uvVarying;")
WriteLine(fw,"uniform highp vec4 uvBounds0;")
WriteLine(fw,"void main()")
WriteLine(fw,"{ ")
WriteLine(fw,"uvVarying = uv * uvBounds0.xy + uvBounds0.zw;")
WriteLine(fw,"highp vec4 pos = agk_World * vec4(position,1.0);")
WriteLine(fw,"mediump vec3 norm = normalize(agk_WorldNormal * normal);")
WriteLine(fw,"gl_Position = agk_ViewProj * pos;")
WriteLine(fw,"posVarying = pos.xyz;")
WriteLine(fw,"normalVarying = norm;")
WriteLine(fw,"lightVarying = GetVSLighting( norm, posVarying );")
WriteLine(fw,"}")
CloseFile(fw)
endfunction
function createPSshaderFile()
fw=OpenToWrite("road.ps")
WriteLine(fw,"#ifdef GL_ES")
WriteLine(fw,"precision mediump float;")
WriteLine(fw,"#endif")
WriteLine(fw,"uniform float time;")
WriteLine(fw,"uniform vec2 mouse;")
WriteLine(fw,"uniform vec2 resolution;")
//afl_ext 2018
WriteLine(fw,"#define DRAG_MULT 0.048")
WriteLine(fw,"#define ITERATIONS_RAYMARCH 10")
WriteLine(fw,"#define ITERATIONS_NORMAL 48")
WriteLine(fw,"#define WATER_DEPTH 2.1")
WriteLine(fw,"#define Mouse (mouse.xy)")
WriteLine(fw,"#define Resolution (resolution.xy)")
WriteLine(fw,"#define Time (time)")
// returns vec2 with wave height in X and its derivative in Y
WriteLine(fw,"vec2 wavedx(vec2 position, vec2 direction, float speed, float frequency, float timeshift) {")
WriteLine(fw,"direction = normalize(direction);")
WriteLine(fw,"float x = dot(direction, position) * frequency + timeshift * speed;")
WriteLine(fw,"float wave = exp(sin(x) - 1.0);")
WriteLine(fw,"float dx = wave * cos(x);")
WriteLine(fw,"return vec2(wave, - dx);")
WriteLine(fw,"}")
// dynamic loops not supported on glslsandbox")
WriteLine(fw,"float getwavesLowRes(vec2 position, int iterations){")
WriteLine(fw,"position *= 0.1;")
WriteLine(fw,"float iter = 0.0;")
WriteLine(fw,"float phase = 6.0;")
WriteLine(fw,"float speed = 2.0;")
WriteLine(fw,"float weight = 1.0;")
WriteLine(fw,"float w = 0.0;")
WriteLine(fw,"float ws = 0.0;")
WriteLine(fw,"for(int i=0;i<ITERATIONS_RAYMARCH;i++){")
WriteLine(fw,"vec2 p = vec2(sin(iter), cos(iter));")
WriteLine(fw,"vec2 res = wavedx(position, p, speed, phase, Time);")
WriteLine(fw,"position += normalize(p) * res.y * weight * DRAG_MULT;")
WriteLine(fw,"w += res.x * weight;")
WriteLine(fw,"iter += 12.0;")
WriteLine(fw,"ws += weight;")
WriteLine(fw,"weight = mix(weight, 0.0, 0.2);")
WriteLine(fw,"phase *= 1.18;")
WriteLine(fw,"speed *= 1.07;")
WriteLine(fw,"}")
WriteLine(fw,"return w / ws;")
WriteLine(fw,"}")
WriteLine(fw,"float getwaves(vec2 position, int iterations){")
WriteLine(fw,"position *= 0.1;")
WriteLine(fw,"float iter = 0.0;")
WriteLine(fw,"float phase = 6.0;")
WriteLine(fw,"float speed = 2.0;")
WriteLine(fw,"float weight = 1.0;")
WriteLine(fw,"float w = 0.0;")
WriteLine(fw,"float ws = 0.0;")
WriteLine(fw,"for(int i=0;i<ITERATIONS_NORMAL;i++){")
WriteLine(fw,"vec2 p = vec2(sin(iter), cos(iter));")
WriteLine(fw,"vec2 res = wavedx(position, p, speed, phase, Time);")
WriteLine(fw,"position += normalize(p) * res.y * weight * DRAG_MULT;")
WriteLine(fw,"w += res.x * weight;")
WriteLine(fw,"iter += 12.0;")
WriteLine(fw,"ws += weight;")
WriteLine(fw,"weight = mix(weight, 0.0, 0.2);")
WriteLine(fw,"phase *= 1.18;")
WriteLine(fw,"speed *= 1.07;")
WriteLine(fw,"}")
WriteLine(fw,"return w / ws;")
WriteLine(fw,"}")
WriteLine(fw,"float raymarchwater(vec3 camera, vec3 start, vec3 end, float depth){")
WriteLine(fw,"vec3 pos = start;")
WriteLine(fw,"float h = 0.0;")
WriteLine(fw,"float hupper = depth;")
WriteLine(fw,"float hlower = 0.0;")
WriteLine(fw,"vec2 zer = vec2(0.0);")
WriteLine(fw,"vec3 dir = normalize(end - start);")
WriteLine(fw,"for(int i=0;i<318;i++){")
WriteLine(fw,"h = getwavesLowRes(pos.xz, ITERATIONS_RAYMARCH) * depth - depth;")
WriteLine(fw,"if(h + 0.01 > pos.y) {")
WriteLine(fw,"return distance(pos, camera);")
WriteLine(fw,"}")
WriteLine(fw,"pos += dir * (pos.y - h);")
WriteLine(fw," }")
WriteLine(fw,"return -1.0;")
WriteLine(fw,"}")
WriteLine(fw,"float H = 0.0;")
WriteLine(fw,"vec3 normal(vec2 pos, float e, float depth){")
WriteLine(fw,"vec2 ex = vec2(e, 0);")
WriteLine(fw,"H = getwaves(pos.xy, ITERATIONS_NORMAL) * depth;")
WriteLine(fw,"vec3 a = vec3(pos.x, H, pos.y);")
WriteLine(fw,"return normalize(cross(normalize(a-vec3(pos.x - e, getwaves(pos.xy - ex.xy, ITERATIONS_NORMAL) * depth, pos.y)), ")
WriteLine(fw,"normalize(a-vec3(pos.x, getwaves(pos.xy + ex.yx, ITERATIONS_NORMAL) * depth, pos.y + e))));")
WriteLine(fw,"}")
WriteLine(fw,"mat3 rotmat(vec3 axis, float angle)")
WriteLine(fw,"{")
WriteLine(fw,"axis = normalize(axis);")
WriteLine(fw,"float s = sin(angle);")
WriteLine(fw,"float c = cos(angle);")
WriteLine(fw,"float oc = 1.0 - c;")
WriteLine(fw,"return mat3(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, ")
WriteLine(fw,"oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, ")
WriteLine(fw,"oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c);")
WriteLine(fw,"}")
WriteLine(fw,"vec3 getRay(vec2 uv){")
WriteLine(fw,"uv = (uv * 2.0 - 1.0) * vec2(Resolution.x / Resolution.y, 1.0);")
WriteLine(fw,"vec3 proj = normalize(vec3(uv.x, uv.y, 1.0) + vec3(uv.x, uv.y, -1.0) * pow(length(uv), 2.0) * 0.05); ")
WriteLine(fw,"if(Resolution.x < 800.0) return proj;")
WriteLine(fw,"vec3 ray = rotmat(vec3(0.0, -1.0, 0.0), 3.0 * (Mouse.x * 2.0 - 1.0)) * rotmat(vec3(1.0, 0.0, 0.0), 1.5 * (Mouse.y * 2.0 - 1.0)) * proj;")
WriteLine(fw,"return ray;")
WriteLine(fw,"}")
WriteLine(fw,"float intersectPlane(vec3 origin, vec3 direction, vec3 point, vec3 normal)")
WriteLine(fw,"{ ")
WriteLine(fw,"return clamp(dot(point - origin, normal) / dot(direction, normal), -1.0, 9991999.0); ")
WriteLine(fw,"}")
WriteLine(fw,"vec3 getatm(vec3 ray, float roughness){")
WriteLine(fw,"vec3 sharp = mix(vec3( 0.0293, 0.0698, 0.1717) * 10.0, vec3(3.0), pow(1.0 - ray.y, 8.0));")
WriteLine(fw,"vec3 rough = vec3(vec3( 0.0293, 0.0698, 0.1717) + vec3(1.0));")
WriteLine(fw,"return mix(sharp, rough, roughness);")
WriteLine(fw,"}")
WriteLine(fw,"float sun(vec3 ray){")
WriteLine(fw,"return pow(max(0.0, dot(ray, normalize(vec3(1.0, 1.0, 0.0)))), 668.0) * 110.0;")
WriteLine(fw,"}")
WriteLine(fw,"vec3 getColor(vec2 uv){")
WriteLine(fw,"vec3 ray = getRay(uv);")
WriteLine(fw," if(ray.y >= -0.01){")
WriteLine(fw," vec3 C = getatm(ray, 0.0) * 1.0 + sun(ray) * 2.0;")
WriteLine(fw,"return C; ")
WriteLine(fw,"}")
WriteLine(fw,"vec3 wfloor = vec3(0.0, -WATER_DEPTH, 0.0);")
WriteLine(fw,"vec3 wceil = vec3(0.0, 0.0, 0.0);")
WriteLine(fw,"vec3 orig = vec3(0.0, 2.0, 0.0);")
WriteLine(fw,"float hihit = intersectPlane(orig, ray, wceil, vec3(0.0, 1.0, 0.0));")
WriteLine(fw,"float lohit = intersectPlane(orig, ray, wfloor, vec3(0.0, 1.0, 0.0));")
WriteLine(fw,"vec3 hipos = orig + ray * hihit;")
WriteLine(fw,"vec3 lopos = orig + ray * lohit;")
WriteLine(fw,"float dist = raymarchwater(orig, hipos, lopos, WATER_DEPTH);")
WriteLine(fw,"vec3 pos = orig + ray * dist;")
WriteLine(fw,"vec3 N = normal(pos.xz, 0.01, WATER_DEPTH);")
WriteLine(fw,"vec3 R = reflect(ray, N);")
WriteLine(fw,"float roughness = 1.0 - 1.0 / (dist * 0.01 + 1.0);")
WriteLine(fw,"N = normalize(mix(N, vec3(0.0, 1.0, 0.0), roughness));")
WriteLine(fw,"R = normalize(mix(R, N, roughness));")
WriteLine(fw,"R.y = abs(R.y);")
WriteLine(fw,"float fresnel = (0.04 + (1.0-0.04)*(pow(1.0 - max(0.0, dot(-N, ray)), 5.0)));")
WriteLine(fw,"vec3 C = fresnel * (getatm(R, roughness) + sun(R));")
WriteLine(fw,"return C;")
WriteLine(fw,"}")
WriteLine(fw,"vec3 gammacorrect(vec3 c){")
WriteLine(fw,"return pow(c, vec3(1.0 / 2.4));")
WriteLine(fw,"}")
WriteLine(fw,"vec3 RRTAndODTFit(vec3 v)")
WriteLine(fw,"{")
WriteLine(fw,"vec3 a = v * (v + 0.0245786) - 0.000090537;")
WriteLine(fw,"vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081;")
WriteLine(fw,"return a / b;")
WriteLine(fw,"}")
WriteLine(fw,"mat3 transpose(mat3 m) {")
WriteLine(fw,"return mat3(m[0][0], m[1][0], m[2][0],")
WriteLine(fw,"m[0][1], m[1][1], m[2][1],")
WriteLine(fw,"m[0][2], m[1][2], m[2][2]);")
WriteLine(fw,"}")
WriteLine(fw,"vec3 ACESFitted(vec3 color)")
WriteLine(fw,"{")
WriteLine(fw,"mat3 ACESInputMat = mat3(")
WriteLine(fw,"0.59719, 0.35458, 0.04823,")
WriteLine(fw,"0.07600, 0.90834, 0.01566,")
WriteLine(fw,"0.02840, 0.13383, 0.83777")
WriteLine(fw,");")
WriteLine(fw,"mat3 ACESOutputMat = mat3(")
WriteLine(fw,"1.60475, -0.53108, -0.07367,")
WriteLine(fw,"-0.10208, 1.10813, -0.00605,")
WriteLine(fw,"-0.00327, -0.07276, 1.07602")
WriteLine(fw,");")
WriteLine(fw,"color = transpose(ACESInputMat) * color;")
WriteLine(fw,"color = RRTAndODTFit(color);")
WriteLine(fw,"color = (transpose(ACESOutputMat) * color);")
WriteLine(fw,"return gammacorrect(clamp(color, 0.0, 1.0));")
WriteLine(fw,"}")
WriteLine(fw,"vec3 render(vec2 uv){")
WriteLine(fw,"vec3 ray = getRay(uv);")
WriteLine(fw,"vec3 C = getColor(uv) * 1.8;")
WriteLine(fw,"return ACESFitted(C); ")
WriteLine(fw,"}")
WriteLine(fw,"void main( void ) {")
WriteLine(fw,"vec2 position = ( gl_FragCoord.xy / resolution.xy );")
WriteLine(fw,"gl_FragColor = vec4( render(position), 1.0 );")
WriteLine(fw,"}")
CloseFile(fw)
endfunction
Enjoy