; Requires Extended language mode.
; Textured noise, an emissive frontier, and matching cut-out shadows are shown.
Include "_common\ShowcaseCamera.bb"

Graphics3D 960,600,0,2:SetBuffer BackBuffer():HDRRendering True
camera=CreateCamera():PositionEntity camera,0,1,-9:CameraClsColor camera,7,9,17
light=CreateLight(1):RotateEntity light,45,-35,0:LightColor light,255,225,195
AmbientLight 15,18,26
floor=CreateCube():ScaleEntity floor,7,.08,7:PositionEntity floor,0,-1.45,3:EntityColor floor,52,58,72

base=CreateDissolveTexture(False):noise=CreateDissolveTexture(True)
shader=LoadShader("builtin:dissolve")
If shader=0 Then RuntimeError "Could not load builtin:dissolve"
ShaderTexture shader,"Texture",base:ShaderTexture shader,"NoiseMap",noise
ShaderColor shader,"EdgeColor",255,55,6
sphere=CreateSphere(48):PositionEntity sphere,-2,0,3:EntityShader sphere,shader
cylinder=CreateCylinder(40):PositionEntity cylinder,0,0,3:EntityShader cylinder,shader
cube=CreateCube():PositionEntity cube,2,0,3:TurnEntity cube,20,35,8:EntityShader cube,shader

amount#=0:edge#=.1:phase#=0:automatic=True:ResetShowcaseCameraTimer()
While Not KeyHit(1)
    UpdateShowcaseCamera camera,4,70
    If KeyHit(57) Then automatic=Not automatic
    If automatic Then phase=phase+.7:amount=(Sin(phase)+1)*.48
    If Not automatic And KeyDown(44) Then amount=amount-.01
    If Not automatic And KeyDown(45) Then amount=amount+.01
    If KeyDown(12) Then edge=edge-.002
    If KeyDown(13) Then edge=edge+.002
    If amount<0 Then amount=0:If amount>.96 Then amount=.96
    If edge<.01 Then edge=.01:If edge>.3 Then edge=.3
    ShaderFloat shader,"Amount",amount:ShaderFloat shader,"EdgeWidth",edge
    TurnEntity cube,.15,.35,0:TurnEntity sphere,0,.2,0
    RenderWorld
    Color 255,255,255:Text 12,12,"PROCEDURAL DISSOLVE"
    Text 12,32,"Watch the emissive frontier and its matching cut-out shadow"
    Text 12,50,"SPACE auto/manual   Z/X amount: "+Int(amount*100)+"%   -/= edge: "+Int(edge*1000)/1000.0
    DrawShowcaseCameraHelp 570:Flip
Wend
FreeShader shader:FreeTexture base:FreeTexture noise:End

Function CreateDissolveTexture(noiseMap)
    texture=CreateTexture(64,64,1+8):SetBuffer TextureBuffer(texture)
    For y=0 To 63:For x=0 To 63
        If noiseMap
            value=(x*37+y*73+(x*y) Mod 97) Mod 256:Color value,value,value
        Else
            If ((x/8+y/8) Mod 2)=0 Then Color 35,105,190 Else Color 235,145,35
        EndIf
        Plot x,y
    Next:Next:SetBuffer BackBuffer():Return texture
End Function
