texture artifacts on voxel terrain

Started by
1 comment, last by coffeecup 9 years, 2 months ago

I am working on a voxel engine and I'm trying to texture my terrain but I get very strange artifacts.

I lookup the texture coords depending on the worldposition, the relevant line of my shader code is


vec3 vtex =  mod(my_vWorldPosition.xzy,64.0)/64.0;

It seems whenever worldpos.xyz/64.0 is 0 the artifacts appear, if I offset the position by 0.0001 they are gone.

I think its because of a precisson loss or something, how can I overcome this?

Here is a pic how it looks like:

5k5GHjn.jpg

Advertisement

Can you post the rest of your shader code? My first guess would be that you have a divide-by-zero (or similarly undefined mathematical construct) later in the same shader.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

vertexshader


attribute vec3 position;
uniform sampler2D texture_0;
varying vec3  my_vWorldPosition;
void main() {
  vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  gl_Position = projectionMatrix * mvPosition;
  my_vWorldPosition = (modelMatrix * vec4( position, 1.0 )).xyz;     
}

fragmentshader


uniform sampler2D texture_0;
varying vec3 my_vWorldPosition;


vec2 computeSliceOffset(float slice, float slicesPerRow, vec2 sliceSize) {
  return sliceSize * vec2(mod(slice, slicesPerRow),floor(slice / slicesPerRow));                  
}                                                                        

vec4 sampleAs3DTexture(sampler2D tex, vec3 texCoord, float size, float numRows, float slicesPerRow) {
  float slice   = texCoord.z * size;                                             
  float sliceZ  = floor(slice);                                  
  float zOffset = fract(slice);                          
                                                                                 
  vec2 sliceSize = vec2(1.0 / slicesPerRow,             // u space of 1 slice    
                        1.0 / numRows);                 // v space of 1 slice    
                                                                                 
  vec2 slice0Offset = computeSliceOffset(sliceZ, slicesPerRow, sliceSize);       
  vec2 slice1Offset = computeSliceOffset(sliceZ + 1.0, slicesPerRow, sliceSize); 
                                                                                 
  vec2 slicePixelSize = sliceSize / size;               // space of 1 pixel      
  vec2 sliceInnerSize = slicePixelSize * (size - 1.0);  // space of size pixels  
                                                                                 
  vec2 uv = slicePixelSize * 0.5 + texCoord.xy * sliceInnerSize;                 
  vec4 slice0Color = texture2D(tex, slice0Offset + uv);                          
  vec4 slice1Color = texture2D(tex, slice1Offset + uv);                          
  //return mix(slice0Color, slice1Color, zOffset);                               
  return slice0Color;                                                            
}                                                                              
void main() {                                                                    
  vec3 vtex =  mod(my_vWorldPosition.xzy,64.0)/64.0;
  gl_FragColor = sampleAs3DTexture(texture_0,vtex,64.0,8.0,8.0);
}

This topic is closed to new replies.

Advertisement