Page 1 of 1

Artifacts in BitmapExpression

Posted: Wed May 06, 2015 8:17 am
by rrTea
I get some really strange artifacts in the bitmap that shift even when I change a comment of the component. Here is an example:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?> 
<ZApplication Name="App" Caption="ZGameEditor application" ClearColor="0.4902 0.8745 1 1" FrameRateStyle="2" FixedFrameRate="60" Camera="ProjectionFrontal" NoSound="1"> 
  <OnLoaded> 
    <ZLibrary Comment="HSV Library"> 
      <Source> 
<![CDATA[// 

float angle(float X) 
{ 
  if(X >= 0 && X < 360)return X; 
  if(X > 360)return X-floor(X/360)* 360; 
  if(X <   0)return X+floor(X/360)*-360; 
} 

// 

void hsv(float H, float S, float V) 
{ 
  float R,G,B,I,F,P,Q,T; 

  H = angle(H); 
  S = clamp(S,0,100); 
  V = clamp(V,0,100); 

  H /= 60; 
  S /= 100; 
  V /= 100; 

  if(S == 0) 
  { 
    Color[0] = V; 
    Color[1] = V; 
    Color[2] = V; 
    return; 
  } 

  I = floor(H); 
  F = H-I; 

  P = V*(1-S); 
  Q = V*(1-S*F); 
  T = V*(1-S*(1-F)); 

  if(I == 0){R = V; G = T; B = P;} 
  if(I == 1){R = Q; G = V; B = P;} 
  if(I == 2){R = P; G = V; B = T;} 
  if(I == 3){R = P; G = Q; B = V;} 
  if(I == 4){R = T; G = P; B = V;} 
  if(I == 5){R = V; G = P; B = Q;} 

  Color[0] = R; 
  Color[1] = G; 
  Color[2] = B; 
}]]> 
      </Source> 
    </ZLibrary> 
    <ZExpression> 
      <Expression> 
<![CDATA[Stretch.Scale.X = 2f*App.ViewportWidth/App.ViewportHeight; 
Stretch.Scale.Y = 2;]]> 
      </Expression> 
    </ZExpression> 
  </OnLoaded> 
  <OnRender> 
    <UseMaterial Material="ColorRainbow"/> 
    <RenderTransformGroup Name="Stretch"> 
      <Children> 
        <RenderNet XCount="8"/> 
      </Children> 
    </RenderTransformGroup> 
  </OnRender> 
  <Content> 
    <Material Name="ColorRainbow" Shading="1" Color="1 1 1 0.8993" Light="0"> 
      <Textures> 
        <MaterialTexture Texture="Rainbow" TextureWrapMode="2" TexCoords="1"/> 
      </Textures> 
    </Material> 
    <Mesh Name="PlaneRectangle"> 
      <Producers> 
        <MeshBox/> 
      </Producers> 
    </Mesh> 
    <Array Name="Color" SizeDim1="3"/> 
    <Camera Name="ProjectionFrontal" Comment="No rotation, for interface etc" Kind="1" Position="0 0 10"/> 
    <Bitmap Name="Rainbow" Comment="ff" Width="0" Height="0" Filter="1"> 
      <Producers> 
        <BitmapExpression> 
          <Expression> 
<![CDATA[//X,Y : current coordinate (0..1) 
//Pixel : current color (rgb) 
//Sample expression: Pixel.R=abs(sin(X*16)); 

hsv((X+0.5)*360f,100,100); 
Pixel.R = Color[0]; 
Pixel.G = Color[1]; 
Pixel.B = Color[2];]]> 
          </Expression> 
        </BitmapExpression> 
      </Producers> 
    </Bitmap> 
  </Content> 
</ZApplication> 
...try to change the comment (not any code!) of the component and the preview will shift etc.

Posted: Wed May 06, 2015 8:31 am
by VilleK
Hi,

This is because of BitmapExpression is threaded :). It calculates several pixels in multiple threads simultaneously. So it doesn't work well with the hsv function that returns value in global variable. Remove the Color variable and change the hsv function to return a vec3 instead, that will make it work.

Posted: Wed May 06, 2015 8:47 am
by rrTea
I see, that's how it's meant to be used with newer (post-threading) builds. Thanks and sorry for the false alarm :)

Posted: Wed May 06, 2015 9:13 am
by Kjell
:!:

Here you go ( got rid of the angle function too ) ..

Code: Select all

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);
  }
}
K

Posted: Wed May 06, 2015 10:09 am
by VilleK
Thanks Kjell, I'll update library.xml with this version.

Posted: Wed May 06, 2015 10:34 am
by rrTea
Thanks, I also updated the post from earlier today where this library was used (which was unaffected, but just so it's up to date). (1)