<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZLibrary Comment="HSV Library">
      <Source>
<![CDATA[void ZgeVizGetFileNames(string path, string pattern, string[] list) { }

vec3 hsv(float h, float s, float v)
{
  s = clamp(s/100, 0, 1);
  v = clamp(v/100, 0, 1);

  if(!s)return vector3(v, v, v);

  h = h < 0 ? frac(1-abs(frac(h/360)))*6 : frac(h/360)*6;

  float c, f, p, q, t;

  c = floor(h);
  f = h-c;

  p = v*(1-s);
  q = v*(1-s*f);
  t = v*(1-s*(1-f));

  switch(c)
  {
    case 0: return vector3(v, t, p);
    case 1: return vector3(q, v, p);
    case 2: return vector3(p, v, t);
    case 3: return vector3(p, q, v);
    case 4: return vector3(t, p, v);
    case 5: return vector3(v, p, q);
  }
}]]>
      </Source>
    </ZLibrary>
    <ZLibrary>
      <Source>
<![CDATA[//Optional: Move  to Constants to ZLibrary before release.
const int
  ALPHA = 0,
  HUE = 1,
  SATURATION = 2,
  LIGHTNESS = 3,
  SPEED = 4,
  USE_BEAT = 5,
  FOV = 6,
  PLANE_HEIGHT = 7,
  ANGLE = 8,
  ORIGIN_X = 9,
  SCALE_X = 10,
  SCALE_Y = 11,
  TILING_X = 12,
  PAUSE_TIME = 13,
  PHASE_OFFSET = 14,
  SMOOTHING = 15,
  FOG_DEPTH = 16,
  FOG_FADE = 17,
  FOG_OPAQUE = 18,
  FOG_ALPHA = 19,
  FOG_HUE = 20,
  FOG_SATURATION = 21,
  FOG_LIGHTNESS = 22;

void setValues() {
  float useBeat=round(Parameters[USE_BEAT]);
  vec3 c=hsv(Parameters[HUE]*360,Parameters[SATURATION]*100,(1-Parameters[LIGHTNESS])*100);
  uColor=vector4(c.x,c.y,c.z,1.0-Parameters[ALPHA]);
  //float speed=1.0;
  float Speed=(Parameters[4]-.5)*4.0; //uncomment, editindex to use as a parameter
  float delta=app.DeltaTime*Speed; //comment to use other time options
  totalTime+=delta;
  if (useBeat) {
    uTime = SongPositionInBeats * Speed / 2.0;
  } else {
    uTime = totalTime;
  }

  //Most used ShaderToy uniform variables
  uResolution=vector2(app.ViewportWidth,app.ViewportHeight);
  uViewport=vector2(app.ViewportX,app.ViewportY);
  uMouse=vector4(0.0,0.0,0.0,0.0);

  uFov = Parameters[FOV];
  uPlaneHeight = Parameters[PLANE_HEIGHT];
  uAngle = Parameters[ANGLE];
  uOriginX = Parameters[ORIGIN_X];
  uScaleX = Parameters[SCALE_X];
  uScaleY = Parameters[SCALE_Y];
  uTilingX = Parameters[TILING_X];
  uPauseTime = Parameters[PAUSE_TIME];
  uPhaseOffset = Parameters[PHASE_OFFSET];
  uSmoothing = Parameters[SMOOTHING];
  uFogDepth = Parameters[FOG_DEPTH];
  uFogFade = Parameters[FOG_FADE];
  uFogOpaque = Parameters[FOG_OPAQUE];
  uFogAlpha = Parameters[FOG_ALPHA];

  vec3 cFog=hsv(Parameters[FOG_HUE]*360,Parameters[FOG_SATURATION]*100,(1-Parameters[FOG_LIGHTNESS])*100);
  uFogColor=vector4(cFog.x,cFog.y,cFog.z,1.0-Parameters[FOG_ALPHA]);
}]]>
      </Source>
    </ZLibrary>
    <ZExpression Expression="setValues();"/>
  </OnLoaded>
  <OnUpdate>
    <ZExpression Expression="setValues();"/>
  </OnUpdate>
  <OnRender>
    <UseMaterial Material="mCanvas"/>
    <RenderSprite/>
  </OnRender>
  <Content>
    <Shader Name="ConveyorPlane">
      <VertexShaderSource>
<![CDATA[#version 120

void main(){
  vec4 vertex = gl_Vertex;
  vertex.xy *= 2.0;
  gl_Position = vertex;
}]]>
      </VertexShaderSource>
      <FragmentShaderSource>
<![CDATA[#version 120

uniform vec2 iResolution,iViewport;
uniform float iTime;
uniform vec4 iColor;
uniform sampler2D tex1; //ZGE material texture uniforn
#define iChannel0 tex1  //Define shadertoy variable to elimnate code editing.

uniform float iFov;
uniform float iPlaneHeight;
uniform float iAngle;
uniform float iOriginX;
uniform float iScaleX;
uniform float iScaleY;
uniform float iTilingX;
uniform float iPauseTime;
uniform float iPhaseOffset;
uniform float iSmoothing;
uniform float iFogDepth;
uniform float iFogFade;
uniform float iFogOpaque;
uniform float iFogAlpha;
uniform vec4 iFogColor;

vec3 rgb2hsv(vec3 c)
{
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

vec3 hsv2rgb(vec3 c)
{
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

vec4 applyColorMod(vec4 inCol, vec4 modCol) {
  vec3 inHsv = rgb2hsv(inCol.rgb);
  vec3 modHsv = rgb2hsv(modCol.rgb);
  inHsv.r+=modHsv.r;
  inHsv.gb*=modHsv.gb;
  vec3 resCol = hsv2rgb(inHsv);

  return vec4(resCol, inCol.a) * modCol.a;
}

float pi() {
    return atan(1.0, 1.0) * 4.0;
}

vec3 getXYD(vec3 rayDirection) {
    float planeHeight = (iPlaneHeight * 2.0) - 1.0;
    float angle = iAngle * 2.0 * pi();
    float scaleX = pow(2.0, (iScaleX - 0.5) * 10.0);
    float scaleY = pow(2.0, (iScaleY - 0.5) * 10.0);
    float tilingX = pow(2.0, (iTilingX - 0.5) * 20.0) / 2.0;

    float time = mod(iTime + iPhaseOffset, 1.0);
    if (iPauseTime == 1.0) {
        time = 0.0;
    } else {
        if (iPauseTime > 0.0) {
            time = clamp((time) / (1.0 - iPauseTime) - iPauseTime, 0.0, 1.0);
        }
        if (iSmoothing > 0.0) {
            time = time * (1.0 - iSmoothing) + iSmoothing * (sin((time - 0.5) * pi()) / 2.0 + 0.5);
        }
    }

    if (planeHeight == 0.0 || (rayDirection.x == 0.0 && rayDirection.y == 0.0)) {
        return vec3(-1.0);
    }
    float currentAngle = atan(rayDirection.x, rayDirection.y) - angle;
    float currentDistance = distance(rayDirection.xy, vec2(0.0));
    vec2 coords = vec2(sin(currentAngle), cos(currentAngle)) * currentDistance;
    float fov = max(10e-10, rayDirection.z);
    float z = (fov / coords.y) * -planeHeight;
    if (z <= 0.0) {
        return vec3(-1.0);
    }
    float x = (coords.x / fov) * z;
    if (iTilingX < 1.0 && abs(x) > tilingX) {
        return vec3(-1.0);
    }
    vec3 retCoords = vec3(
        mod(x / scaleX - iOriginX, 1.0),
        mod(z / scaleY + time, 1.0),
        abs(-coords.y)
    );
    return retCoords;

}

//copy-paste ShaderToy code here.
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = fragCoord/iResolution.xy;

    vec2 screenCoords = uv - 0.5;
    screenCoords.x*= iResolution.x/iResolution.y;

    vec3 rayDirection = vec3(screenCoords.x, screenCoords.y, iFov * 100.0 + 1.0);

    vec4 col = vec4(0.0);

    vec3 planeCoords = getXYD(rayDirection);

    if (planeCoords.x >= 0.0 && planeCoords.y >= 0.0) {
        float minEdge = iFogDepth * (1.0 - iFogFade);
        float fog = smoothstep(minEdge, iFogDepth, planeCoords.z);
        col = texture2D(iChannel0, planeCoords.xy);
        col.rgb*= fog * iFogOpaque + (1.0 - iFogOpaque);
        col.rgb+= iFogColor.rgb * (1.0 - fog) * iFogOpaque;
        col.a*= fog * iFogAlpha + (1.0 - iFogAlpha);
    }

    // Output to screen
    fragColor = applyColorMod(col, iColor);
}
//copy-paste ShaderToy code here.

void main(){
    //ZGE does not use mainImage( out vec4 fragColor, in vec2 fragCoord )
    //Rededefined fragCoord as gl_FragCoord.xy-iViewport(dynamic window)
    mainImage(gl_FragColor,gl_FragCoord.xy-iViewport);
}]]>
      </FragmentShaderSource>
      <UniformVariables>
        <ShaderVariable VariableName="iResolution" VariableRef="uResolution"/>
        <ShaderVariable VariableName="iViewport" VariableRef="uViewport"/>
        <ShaderVariable VariableName="iTime" VariableRef="uTime"/>
        <ShaderVariable VariableName="iColor" VariableRef="uColor"/>
        <ShaderVariable VariableName="iFov" VariableRef="uFov"/>
        <ShaderVariable VariableName="iPlaneHeight" VariableRef="uPlaneHeight"/>
        <ShaderVariable VariableName="iAngle" VariableRef="uAngle"/>
        <ShaderVariable VariableName="iOriginX" VariableRef="uOriginX"/>
        <ShaderVariable VariableName="iScaleX" VariableRef="uScaleX"/>
        <ShaderVariable VariableName="iScaleY" VariableRef="uScaleY"/>
        <ShaderVariable VariableName="iTilingX" VariableRef="uTilingX"/>
        <ShaderVariable VariableName="iPauseTime" VariableRef="uPauseTime"/>
        <ShaderVariable VariableName="iPhaseOffset" VariableRef="uPhaseOffset"/>
        <ShaderVariable VariableName="iSmoothing" VariableRef="uSmoothing"/>
        <ShaderVariable VariableName="iFogDepth" VariableRef="uFogDepth"/>
        <ShaderVariable VariableName="iFogFade" VariableRef="uFogFade"/>
        <ShaderVariable VariableName="iFogOpaque" VariableRef="uFogOpaque"/>
        <ShaderVariable VariableName="iFogAlpha" VariableRef="uFogAlpha"/>
        <ShaderVariable VariableName="iFogColor" VariableRef="uFogColor"/>
      </UniformVariables>
    </Shader>
    <Group Comment="Default ShaderToy Uniform Variable Inputs">
      <Children>
        <Variable Name="uResolution" Type="6"/>
        <Variable Name="uTime"/>
        <Variable Name="uTimeDelta"/>
        <Variable Name="uFrame" Type="1"/>
        <Variable Name="uFrameRate"/>
        <Variable Name="uMouse" Type="8"/>
        <Variable Name="uDate" Type="8"/>
      </Children>
    </Group>
    <Group Comment="FL Studio Variables">
      <Children>
        <Array Name="Parameters" SizeDim1="23" Persistent="255">
          <Values>
<![CDATA[789C63608081067B541A041C606C7B348C0CEC217A1AD0C41BEC01E5C90576]]>
          </Values>
        </Array>
        <Constant Name="ParamHelpConst" Type="2">
          <StringValue>
<![CDATA[Alpha
Hue
Saturation
Lightness
Speed
Use Beat @checkbox
FOV
Plane Height
Angle
X Origin
X Scale
Y Scale
X Tiling
Pause Time
Phase Offset
Smoothing
Fog Depth
Fog Fade
Fog Opacity
Fog Alpha Fade
Fog Hue
Fog Saturation
Fog Lightness]]>
          </StringValue>
        </Constant>
        <Constant Name="AuthorInfo" Type="2">
          <StringValue>
<![CDATA[GaryCXJk
https://area91.garycxjk.com/]]>
          </StringValue>
        </Constant>
        <Variable Name="SongPositionInBeats"/>
      </Children>
    </Group>
    <Group Comment="Unique Uniform Variable Inputs">
      <Children>
        <Variable Name="uViewport" Type="6"/>
        <Variable Name="uColor" Type="8"/>
        <Variable Name="uFov"/>
        <Variable Name="uPlaneHeight"/>
        <Variable Name="uAngle"/>
        <Variable Name="uOriginX"/>
        <Variable Name="uScaleX"/>
        <Variable Name="uScaleY"/>
        <Variable Name="uTilingX"/>
        <Variable Name="uPauseTime"/>
        <Variable Name="uPhaseOffset"/>
        <Variable Name="uSmoothing"/>
        <Variable Name="uFogDepth"/>
        <Variable Name="uFogOpaque"/>
        <Variable Name="uFogAlpha"/>
        <Variable Name="uFogColor" Type="8"/>
        <Variable Name="uFogFade"/>
      </Children>
    </Group>
    <Group Comment="Misc. Variables">
      <Children>
        <Variable Name="totalTime"/>
      </Children>
    </Group>
    <Group Comment="Materials and Textures">
      <Children>
        <Material Name="mCanvas" Blend="1" Shader="ConveyorPlane">
          <Textures>
            <MaterialTexture Name="mtl" Texture="mTexture" TexCoords="1"/>
          </Textures>
        </Material>
        <Bitmap Name="mTexture" Width="256" Height="256">
          <Producers>
            <BitmapExpression>
              <Expression>
<![CDATA[//X,Y : current coordinate (0..1)
//Pixel : current color (rgb)
//Sample expression: Pixel.R=abs(sin(X*16));
if (x > 0.5 / 256.0 && x < 255.5 / 256.0) {
  float multi = 5.0;
  float doubleY = y * multi;
  doubleY = doubleY - floor(doubleY);
  float max = 256.0 / multi;
  if (doubleY > 6.0 / max) {
    Pixel=(doubleY / 8.0) + 0.125;
    Pixel.R*= 0.8;
    Pixel.G*= 0.9;
  } else {
    Pixel=(doubleY / 8.0) + 0.0625;
    Pixel.R*= 0.8;
    Pixel.G*= 0.9;
  }
} else {
  Pixel.R = 0.08;
  Pixel.G = 0.09;
  Pixel.B = 0.1;
}
Pixel.A = 1;]]>
              </Expression>
            </BitmapExpression>
          </Producers>
        </Bitmap>
      </Children>
    </Group>
  </Content>
</ZApplication>
