Page 1 of 1

Texture fix

Posted: Tue Feb 03, 2015 3:09 am
by rrTea
I'm trying to make a mesh look in a particular way and it all works great, except for one face that renders as in the attached screenshot. This happens as soon as I set the Material's texture to 0.5. Must be something obvious but I just can't see why... How can I fix it?

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application">
  <Content>
    <Material Name="LayeredColors">
      <Textures>
        <MaterialTexture Texture="Stripes" TextureScale="0.5 0.5 0.5"/>
      </Textures>
    </Material>
    <Bitmap Name="Stripes" Filter="1">
      <Producers>
        <BitmapCells UsedMetrics="4" RandomSeed="6" BorderPixels="1"/>
      </Producers>
    </Bitmap>
    <Mesh Name="CubeStuck">
      <Producers>
        <MeshBox Name="Body" Comment="Body" Scale="0.25 0.25 0.25"/>
        <MeshBox Name="Tail" Comment="Tail" Scale="0.5 0.75 0.5"/>
        <MeshTransform Scale="0.25 0.2 0.4" Position="0 -0.5 0"/>
        <MeshCombine/>
      </Producers>
    </Mesh>
    <Model Name="Buzzer">
      <OnRender>
        <UseMaterial Material="LayeredColors"/>
        <RenderMesh Mesh="CubeStuck"/>
      </OnRender>
    </Model>
  </Content>
</ZApplication>

Posted: Tue Feb 03, 2015 11:40 am
by Rado1
This example works without artifacts you shown on my GPU. BTW do you really want to use TexCoords Generated? I had some problems with it on different HW, also I'm not sure it works on Android...

Posted: Tue Feb 03, 2015 1:11 pm
by Kjell
Hi rrTea,

That's not being caused by Z-fighting. The reason why it causes glitches ( on some GPUs ) is because with those settings ( nearest neighbor filtering + resulting texture matrix ) the texture coordinates for the bottom vertices of the large cube are exactly at 0.125 ( exactly between the 8th and 9th pixel-row from the bottom ), which when looked at from a angle causes some fragments to be sampled from row 8 and others from 9.

Image

Either offset the texture matrix ( MaterialTexture.TextureY ) by a very small number ( 0.00001 or something ), or change row 8 of the bitmap so it matches row 9 .. or do this :wink:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application">
  <Content>
    <Material Name="LayeredColors">
      <Textures>
        <MaterialTexture Texture="Stripes" TextureScale="0.5 0.5 0.5" TexCoords="1"/>
      </Textures>
    </Material>
    <Bitmap Name="Stripes" Filter="1">
      <Producers>
        <BitmapCells UsedMetrics="4" RandomSeed="6" BorderPixels="1"/>
      </Producers>
    </Bitmap>
    <Mesh Name="CubeStuck">
      <Producers>
        <MeshBox Name="Body" Comment="Body" Scale="0.25 0.25 0.25"/>
        <MeshExpression AutoNormals="0" HasTexCoords="255">
          <Expression>
<![CDATA[//

TexCoord.X = V.X;

// If the normal points downwards ( which is the case for the bottom face )
// select texture coordinate between pixel 9 and 10 ( instead of 8 and 9 )
// and otherwise use the vertex Y coordinate as usual :-)

TexCoord.Y = N.Y > -0.5 ? V.Y : -7 / 32;]]>
          </Expression>
        </MeshExpression>
        <MeshBox Name="Tail" Comment="Tail" Scale="0.5 0.75 0.5"/>
        <MeshTransform Scale="0.25 0.2 0.4" Position="0 -0.5 0"/>
        <MeshExpression AutoNormals="0" HasTexCoords="255">
          <Expression>
<![CDATA[//

TexCoord.X = V.X;
TexCoord.Y = V.Y;]]>
          </Expression>
        </MeshExpression>
        <MeshCombine/>
      </Producers>
    </Mesh>
    <Model Name="Buzzer">
      <OnRender>
        <UseMaterial Material="LayeredColors"/>
        <RenderMesh Mesh="CubeStuck"/>
      </OnRender>
    </Model>
  </Content>
</ZApplication>
And TexCoords should be / have been ModelDefined by default. Only use Generated when you absolutely need to and you know what you're doing.

K

Posted: Wed Feb 04, 2015 1:04 am
by rrTea
Rado1: Right, I should avoid using that (I was advised against using it in the chat too). It's just that in this particular case using this option made it almost work and I tend to leave the settings I'm not familiar with on default... One wonders why is TexCoords Generated the default option if it's causing problems :)

Kjell: Offsetting the texture sounds like the cheapest "solution" for this particular problem since I don't have that much influence over the details of the texture (other than feeding the BitmapCells a new seed). Fixing normals is maybe a bit too detailed for this situation (thanks for showing me how to do it, maybe I'll need it in the future)...

I'll also change the title of the thread to something that doesn't include "z-fighting" (I named it this because the effect looked similar but turns out it's unrelated).

Posted: Wed Feb 04, 2015 2:11 pm
by Kjell
Hi rrTea,

The OpenGL feature that "TexCoords = Generated" relies on is simply not available in OpenGL ES ( nor modern OpenGL ), and ZGE doesn't have a software fallback implemented ( for ES 1.1 ). But the primary reason you should avoid it is that it generates / calculates the texture coordinates each frame, which is a waste of cycles when you're not using it for a specific effect ( chrome / ghost / lava etc. ).

And something like "texture glitch" would have been a more suitable title ( to prevent confusion between fragmented and fragments .. which are completely unrelated ) ;)

K