[code=pascal]// SPDX-License-Identifier: LGPL-3.0-linking-exception
{ Checkerboard effect using OpenGL shaders }
unit BGRASeascapeShader;
{$mode objfpc}{$H+}
interface
uses
BGRAOpenGL3D, BGRABitmapTypes, BGRACanvasGL, BGRAOpenGLType;
type
{ Shader computing Checkerboard }
TBGLSeascapeShader = class(TBGLShader3D)
private
function GetCanvasSize: TPointF;
procedure SetCanvasSize(AValue: TPointF);
function GetImageIndex: integer;
function GetTime: Single;
procedure SetImageIndex(AValue: integer);
procedure SetTime(AValue: Single);
protected
FCanvasSize: TUniformVariablePointF;
FImageIndex: TUniformVariableInteger;
FTime: TUniformVariableSingle;
procedure StartUse; override;
property CanvasSize: TPointF read GetCanvasSize write SetCanvasSize;
public
constructor Create(ACanvas: TBGLCustomCanvas);
function Render(AWidth, AHeight: integer): IBGLTexture; overload;
procedure RenderOnCanvas;
property ImageIndex: integer read GetImageIndex write SetImageIndex;
property Time: Single read GetTime write SetTime;
end;
implementation
{ TBGLSeascapeShader }
function TBGLSeascapeShader.GetCanvasSize: TPointF;
begin
result := FCanvasSize.Value;
end;
function TBGLSeascapeShader.GetTime: Single;
begin
result := FTime.Value;
end;
procedure TBGLSeascapeShader.SetCanvasSize(AValue: TPointF);
begin
FCanvasSize.Value := AValue;
end;
function TBGLSeascapeShader.GetImageIndex: integer;
begin
result := FImageIndex.Value;
end;
procedure TBGLSeascapeShader.SetImageIndex(AValue: integer);
begin
FImageIndex.Value := AValue;
end;
procedure TBGLSeascapeShader.SetTime(AValue: Single);
begin
FTime.Value := AValue;
end;
constructor TBGLSeascapeShader.Create(ACanvas: TBGLCustomCanvas);
begin
// vertex + fragment
inherited Create(ACanvas,
'uniform vec2 canvasSize;'#10+
'void main(void) {'#10 +
' gl_Position = gl_ProjectionMatrix * gl_Vertex;'#10 +
' //texCoord = vec2(gl_MultiTexCoord0);'#10 +
' texCoord = gl_Vertex.xy / canvasSize;;'#10 +
'}',
'uniform float time;'#10 +
'uniform sampler2D image;'#10 +
'out vec4 FragmentColor;'#10 +
'/* '#10+
' * "Seascape" by Alexander Alekseev aka TDM - 2014 '#10+
' * License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. '#10+
' * Contact: tdmaav@gmail.com '#10+
' */ '#10+
' '#10+
'const int NUM_STEPS = 16; '#10+
'const float PI = 3.141592; '#10+
'const float EPSILON = 1e-3; '#10+
'//#define AA '#10+
' '#10+
'// sea '#10+
'const int ITER_GEOMETRY = 3; '#10+
'const int ITER_FRAGMENT = 5; '#10+
'const float SEA_HEIGHT = 0.6; '#10+
'const float SEA_CHOPPY = 4.0; '#10+
'const float SEA_SPEED = 3.0; '#10+
'const float SEA_FREQ = 0.16; '#10+
'const vec3 SEA_BASE = vec3(0.0,0.09,0.18); '#10+
'const vec3 SEA_WATER_COLOR = vec3(0.8,0.9,0.6)*0.8; '#10+
'#define SEA_TIME (1.0 + time * SEA_SPEED) '#10+
'const mat2 octave_m = mat2(1.6,1.2,-1.2,1.6); '#10+
'// '#10+
' '#10+
' '#10+
'// math '#10+
'mat3 fromEuler(vec3 ang) { '#10+
' vec2 a1 = vec2(sin(ang.x),cos(ang.x)); '#10+
' vec2 a2 = vec2(sin(ang.y),cos(ang.y)); '#10+
' vec2 a3 = vec2(sin(ang.z),cos(ang.z)); '#10+
' mat3 m; '#10+
' m[0] = vec3(a1.y*a3.y+a1.x*a2.x*a3.x,a1.y*a2.x*a3.x+a3.y*a1.x,-a2.y*a3.x); '#10+
' m[1] = vec3(-a2.y*a1.x,a1.y*a2.y,a2.x); '#10+
' m[2] = vec3(a3.y*a1.x*a2.x+a1.y*a3.x,a1.x*a3.x-a1.y*a3.y*a2.x,a2.y*a3.y); '#10+
' return m; '#10+
'}'#10+
'float hash( vec2 p ) { '#10+
' float h = dot(p,vec2(127.1,311.7)); '#10+
' return fract(sin(h)*43758.5453123); '#10+
'}'#10+
'float noise( in vec2 p ) { '#10+
' vec2 i = floor( p ); '#10+
' vec2 f = fract( p ); '#10+
' vec2 u = f*f*(3.0-2.0*f); '#10+
' return -1.0+2.0*mix( mix( hash( i + vec2(0.0,0.0) ), '#10+
' hash( i + vec2(1.0,0.0) ), u.x), '#10+
' mix( hash( i + vec2(0.0,1.0) ), '#10+
' hash( i + vec2(1.0,1.0) ), u.x), u.y); '#10+
'}'#10+
' '#10+
'// lighting '#10+
'float diffuse(vec3 n,vec3 l,float p) { '#10+
' return pow(dot(n,l) * 0.4 + 0.6,p); '#10+
'}'#10+
'float specular(vec3 n,vec3 l,vec3 e,float s) { '#10+
' float nrm = (s + 8.0) / (PI * 8.0); '#10+
' return pow(max(dot(reflect(e,n),l),0.0),s) * nrm; '#10+
'}'#10+
' '#10+
'// sky '#10+
'vec3 getSkyColor(vec3 e) { '#10+
' e.y = (max(e.y,0.0)*0.8+0.2)*0.8; '#10+
' return vec3(pow(1.0-e.y,2.0), 1.0-e.y, 0.6+(1.0-e.y)*0.4) * 1.1; '#10+
'}'#10+
' '#10+
'// sea '#10+
'float sea_octave(vec2 uv, float choppy) { '#10+
' uv += noise(uv); '#10+
' vec2 wv = 1.0-abs(sin(uv)); '#10+
' vec2 swv = abs(cos(uv)); '#10+
' wv = mix(wv,swv,wv); '#10+
' return pow(1.0-pow(wv.x * wv.y,0.65),choppy); '#10+
'}'#10+
' '#10+
'float map(vec3 p) { '#10+
' float freq = SEA_FREQ; '#10+
' float amp = SEA_HEIGHT; '#10+
' float choppy = SEA_CHOPPY; '#10+
' vec2 uv = p.xz; uv.x *= 0.75; '#10+
' '#10+
' float d, h = 0.0; '#10+
' for(int i = 0; i < ITER_GEOMETRY; i++) { '#10+
' d = sea_octave((uv+SEA_TIME)*freq,choppy); '#10+
' d += sea_octave((uv-SEA_TIME)*freq,choppy); '#10+
' h += d * amp; '#10+
' uv *= octave_m; freq *= 1.9; amp *= 0.22; '#10+
' choppy = mix(choppy,1.0,0.2); '#10+
' } '#10+
' return p.y - h; '#10+
'}'#10+
' '#10+
'float map_detailed(vec3 p) { '#10+
' float freq = SEA_FREQ; '#10+
' float amp = SEA_HEIGHT; '#10+
' float choppy = SEA_CHOPPY; '#10+
' vec2 uv = p.xz; uv.x *= 0.75; '#10+
' '#10+
' float d, h = 0.0; '#10+
' for(int i = 0; i < ITER_FRAGMENT; i++) { '#10+
' d = sea_octave((uv+SEA_TIME)*freq,choppy); '#10+
' d += sea_octave((uv-SEA_TIME)*freq,choppy); '#10+
' h += d * amp; '#10+
' uv *= octave_m; freq *= 1.9; amp *= 0.22; '#10+
' choppy = mix(choppy,1.0,0.2); '#10+
' } '#10+
' return p.y - h; '#10+
'}'#10+
' '#10+
'vec3 getSeaColor(vec3 p, vec3 n, vec3 l, vec3 eye, vec3 dist) { '#10+
' float fresnel = clamp(1.0 - dot(n,-eye), 0.0, 1.0); '#10+
' fresnel = min(pow(fresnel,3.0), 0.5); '#10+
' '#10+
' vec3 reflected = getSkyColor(reflect(eye,n)); '#10+
' vec3 refracted = SEA_BASE + diffuse(n,l,80.0) * SEA_WATER_COLOR * 0.12; '#10+
' '#10+
' vec3 color = mix(refracted,reflected,fresnel); '#10+
' '#10+
' float atten = max(1.0 - dot(dist,dist) * 0.001, 0.0); '#10+
' color += SEA_WATER_COLOR * (p.y - SEA_HEIGHT) * 0.18 * atten; '#10+
' '#10+
' color += vec3(specular(n,l,eye,60.0)); '#10+
' '#10+
' return color; '#10+
'}'#10+
' '#10+
'// tracing '#10+
'vec3 getNormal(vec3 p, float eps) { '#10+
' vec3 n; '#10+
' n.y = map_detailed(p); '#10+
' n.x = map_detailed(vec3(p.x+eps,p.y,p.z)) - n.y; '#10+
' n.z = map_detailed(vec3(p.x,p.y,p.z+eps)) - n.y; '#10+
' n.y = eps; '#10+
' return normalize(n); '#10+
'}'#10+
' '#10+
'float heightMapTracing(vec3 ori, vec3 dir, out vec3 p) { '#10+
' float tm = 0.0; '#10+
' float tx = 1000.0; '#10+
' float hx = map(ori + dir * tx); '#10+
' if(hx > 0.0) { '#10+
' p = ori + dir * tx; '#10+
' return tx; '#10+
' } '#10+
' float hm = map(ori + dir * tm); '#10+
' float tmid = 0.0; '#10+
' for(int i = 0; i < NUM_STEPS; i++) { '#10+
' tmid = mix(tm,tx, hm/(hm-hx)); '#10+
' p = ori + dir * tmid; '#10+
' float hmid = map(p); '#10+
' if(hmid < 0.0) { '#10+
' tx = tmid; '#10+
' hx = hmid; '#10+
' } else { '#10+
' tm = tmid; '#10+
' hm = hmid; '#10+
' } '#10+
' } '#10+
' return tmid; '#10+
'}'#10+
// main
'void main(void)'#10 +
'{'#10 +
' '#10+
' '#10+
' vec2 uv = texCoord *2.-1.0; '#10+
' uv.y =1.-uv.y;'#10+
' // ray '#10+
' vec3 ang = vec3(sin(time*3.0)*0.1,sin(time)*0.2+0.8,time); '#10+
' vec3 ori = vec3(0.0,3.5,time*5.0); '#10+
' vec3 dir = normalize(vec3(uv.xy,-2.0)); '#10+
' dir = normalize(dir) * fromEuler(ang); '#10+
' '#10+
' // tracing '#10+
' vec3 p; '#10+
' heightMapTracing(ori,dir,p); '#10+
' vec3 dist = p - ori; '#10+
' vec3 n = getNormal(p, dot(dist,dist) * 0.0001); '#10+
' vec3 light = normalize(vec3(0.0,1.0,0.8)); '#10+
' vec3 color = mix( '#10+
' getSkyColor(dir), getSeaColor(p, n, light, dir, dist), pow(smoothstep(0.0, -0.05, dir.y), 0.3));'#10+
' '#10+
' '#10+
' FragmentColor = vec4(pow(color, vec3(0.75)), 1.0); '#10 +
'}',
'varying vec2 texCoord;', '130');
FCanvasSize := UniformPointF['canvasSize'];
// FImageIndex := UniformInteger['image'];
FTime := UniformSingle['time']; // float uniform
ImageIndex:= 0;
Time := 0;
end;
function TBGLSeascapeShader.Render(AWidth, AHeight: integer): IBGLTexture;
var previousBuf,buf: TBGLCustomFrameBuffer;
begin
previousBuf := Canvas.ActiveFrameBuffer;
buf := Canvas.CreateFrameBuffer(AWidth, AHeight);
Canvas.ActiveFrameBuffer := buf;
Canvas.Fill(BGRAPixelTransparent);
RenderOnCanvas;
Canvas.ActiveFrameBuffer := previousBuf;
result := buf.MakeTextureAndFree;
end;
procedure TBGLSeascapeShader.RenderOnCanvas;
var
previousShader: TBGLCustomShader;
begin
previousShader := Canvas.Lighting.ActiveShader;
Canvas.Lighting.ActiveShader := self;
CanvasSize := PointF(Canvas.Width, Canvas.Height);
Canvas.FillRect(0, 0, Canvas.Width, Canvas.Height, CSSYellow);
Canvas.Lighting.ActiveShader := previousShader;
end;
procedure TBGLSeascapeShader.StartUse;
begin
inherited StartUse;
FCanvasSize.Update;
// FImageIndex.Update;
FTime.Update;
end;
end.