// Depth of Field by klepto2// from Minib3dPFXNewton0.40.tar.gzvarying vec4 texCoord0;varying vec4 texCoord1;uniform sampler2D colortex; // tex0uniform sampler2D depthtex;uniform float blursize; // 0.002void main(void){ vec4 blurFactor = texture2D(depthtex, texCoord1.st) * blursize; float bf = blurFactor.r; vec4 blurSample = vec4(0.0, 0.0, 0.0, 0.0); int lo = 2; // 2 int hi = 3; // 3 for(int tx =-lo; tx<hi; tx++){ for(int ty =-lo; ty<hi; ty++){ vec2 uv = texCoord0.st; uv.x = uv.x + float(tx) * bf; uv.y = uv.y + float(ty) * bf; blurSample = blurSample + texture2D(colortex, uv); } } float darken = 4.0 * float(hi * lo); // 23 blurSample = blurSample / darken; gl_FragColor = vec4(vec3(blurSample), 1.0);}

varying vec4 texCoord0;varying vec4 texCoord1;void main(){ texCoord0 = gl_TextureMatrix[0] * gl_MultiTexCoord0; texCoord1 = gl_TextureMatrix[1] * gl_MultiTexCoord1; gl_Position = ftransform();}

#include once "openb3d.bi"screen 18, 32, , &h10002 Graphics3d 640,480,32,1,1var camera=CreateCamera()CameraRange camera,0.5,1000.0 CameraClsColor camera,150,200,250var light=CreateLight()TurnEntity light,45,45,0var shader=LoadShader("","depthoffield.vert.glsl","depthoffield.frag.glsl")SetInteger(shader,"colortex",0) ' 0 is render textureSetInteger(shader,"depthtex",1) ' 1 is depth texturedim as single blursize=0.002UseFloat(shader,"blursize",blursize)var PostFx=createpostfx(camera,1)hideentity cameraPositionEntity camera,0,3,0 MoveEntity camera,0,0,-25var pass_no=0, numColBufs=1, depth=1 var source_pass=0, index=1, slot=0AddRenderTarget PostFx,pass_no,numColBufs,depthPostFXBuffer PostFx,pass_no,source_pass,index,slotPostFXShader PostFx,pass_no,shaderFor r as integer=0 To 6 For t as integer=-2 To 2 var sphere=CreateSphere() PositionEntity sphere,(t*4),1,(t*4)+((r-2)*8) If r Mod 3=1 Then EntityColor sphere,0,0,255 If r Mod 3=2 Then EntityColor sphere,0,255,255 If r Mod 3=0 Then EntityColor sphere,0,255,0 NextNextvar cube=CreateCube()ScaleMesh cube,2,2,2PositionEntity cube,0,8,40EntityColor cube,255,0,0var ground=CreatePlane(128)dim key as stringdo key=inkey if key=chr(255)+"=" And blursize<0.004 Then blursize=blursize+0.0001 if key=chr(255)+"-" And blursize>0.0004 Then blursize=blursize-0.0001 TurnEntity cube,0.1,0.2,0.3 updateworld 1 renderworld sleep 1 fliploop until key=chr(27)

' depthoffield.bmxStrictFramework Openb3dmax.B3dglgraphicsLocal width%=DesktopWidth(),height%=DesktopHeight()Graphics3D width,heightClearTextureFilters ' remove mipmap flag for postfx textureGlobal camera:TCamera=CreateCamera()CameraRange camera,0.5,1000.0 ' near must be closer than screen sprite to prevent clippingCameraClsColor camera,150,200,250Local light:TLight=CreateLight()TurnEntity light,45,45,0Local shader:TShader=LoadShader("","../glsl/depthoffield.vert.glsl","../glsl/depthoffield.frag.glsl")SetInteger(shader,"colortex",0) ' 0 is render textureSetInteger(shader,"depthtex",1) ' 1 is depth textureLocal blursize#=0.002UseFloat(shader,"blursize",blursize)Local PostFx:TPostFX=CreatePostFX(Camera,1)HideEntity Camera ' note: this boosts framerate as it prevents extra camera renderPositionEntity camera,0,3,0 ' move camera now sprite is parented to itMoveEntity camera,0,0,-25Local pass_no%=0, numColBufs%=1, depth%=1Local source_pass%=0, index%=1, slot%=0AddRenderTarget PostFx,pass_no,numColBufs,depthPostFXBuffer PostFx,pass_no,source_pass,index,slotPostFXShader PostFx,pass_no,shaderLocal sphere:TMeshFor Local r:Int=0 To 6 For Local t:Int=-2 To 2 sphere=CreateSphere() PositionEntity sphere,(t*4),1,(t*4)+((r-2)*8) If r Mod 3=1 EntityColor sphere,0,0,255 If r Mod 3=2 EntityColor sphere,0,255,255 If r Mod 3=0 EntityColor sphere,0,255,0 NextNextLocal cube:TMesh=CreateCube()ScaleMesh cube,2,2,2PositionEntity cube,0,8,40EntityColor cube,255,0,0Local ground:TMesh=CreatePlane(128)' main loopLocal postprocess%=0While Not KeyDown(KEY_ESCAPE) If KeyHit(KEY_SPACE) postprocess=Not postprocess If postprocess Then PostFXShader PostFx,0,Null If Not postprocess Then PostFXShader PostFx,0,shader EndIf If KeyDown(KEY_EQUALS) And blursize<0.004 Then blursize:+0.0001 If KeyDown(KEY_MINUS) And blursize>0.0004 Then blursize:-0.0001 ' control camera MoveEntity camera,KeyDown(KEY_D)-KeyDown(KEY_A),0,KeyDown(KEY_W)-KeyDown(KEY_S) TurnEntity camera,KeyDown(KEY_DOWN)-KeyDown(KEY_UP),KeyDown(KEY_LEFT)-KeyDown(KEY_RIGHT),0 TurnEntity cube,0.1,0.2,0.3 UpdateWorld RenderWorld FlipWendEnd

PostFXBuffer PostFx,pass_no,source_pass,index,slot

PostFXBuffer PostFx,pass_no,source_pass,0,1