I don't have time to debug your code, unfortunatly, but I can give some further pointers. But first, can I just confrm that you are using one of the cubic surface extractors rather than the marching cubes one? If so, have a look at this thread:
http://www.volumesoffun.com/phpBB3/viewtopic.php?f=2&t=152And specifically this shader code:
Code:
float2 uv;
if(worldNormal.x > 0.5)
uv = inWorldPosition.yz;
if(worldNormal.x < -0.5)
uv = inWorldPosition.yz;
if(worldNormal.y > 0.5)//Down
uv = inWorldPosition.xz;
if(worldNormal.y < -0.5)//Up
uv = inWorldPosition.xz;
if(worldNormal.z > 0.5)
uv = inWorldPosition.xy;
if(worldNormal.z < -0.5)
uv = inWorldPosition.xy;
uv = frac(uv / 2 - 0.5);
uv = uv / 4;
result = tex2D(texture, uv);
It's similar in principle to the code you posted yourself, but notice that it does not sample the texture within the if statement. Instead it uses the if statements to set up the texcoords and then samples the texture after all the if statements. This presumably avoids the issue you are encountering, because it appears to scale the texture at the end (the divide by 4).
The only shaderer code which I have for subic surfaces is what's in the Voxeliens demo. I wouldn't really recommend you work from it (it's complex and dependant on setup done on the CPU), but if you want to see it then look at 'materials\VoxelMaterial\ColouredCubicVoxel.hlsl' The important function is buildCoordinateSystem. Note that this function doesn't actually sample the texture but only sets up the texcoords for later use:
Code:
void buildCoordinateSystem(float3 worldPosition, out float2 texCoords, out float3 tangent, out float3 binormal, out float3 normal)
{
//Compute normals from derivitive instrutions
normal = cross(ddy(worldPosition), ddx(worldPosition));
normal = normalize(normal);
//Need to initialise to something, otherwise 45
//degree angles (fireballs) never get texcoords.
texCoords = float2(0.5, 0.5);
//For some reason, in OpenGL the computed normals face the other way.
#if OPENGL
normal *= -1.0f;
#endif
//Small offset, because our cubes are centered on integer position rather than
float3 worldPositionOffset = worldPosition + float3(0.5, 0.5, 0.5);
//Maybe we can improve on this with a x1x1 cubemap lookup texture, to convert
//normal to face index? Then a look up table from face index to other details?
if(normal.x > 0.9)
{
texCoords = float2(-worldPositionOffset.z, worldPositionOffset.y);
tangent = float3(0.0, 0.0,-1.0);
binormal = float3(0.0, 1.0,0.0);
}
else if(normal.y > 0.9)
{
texCoords = float2(worldPositionOffset.x, -worldPositionOffset.z);
tangent = float3(1.0, 0.0, 0.0);
binormal = float3(0.0, 0.0, -1.0);
}
else if(normal.z > 0.9)
{
texCoords = float2(worldPositionOffset.x, worldPositionOffset.y);
tangent = float3(1.0, 0.0, 0.0);
binormal = float3(0.0, 1.0, 0.0);
}
else if(normal.x < -0.9)
{
texCoords = float2(worldPositionOffset.z, worldPositionOffset.y);
tangent = float3(0.0, 0.0,1.0);
binormal = float3(0.0, 1.0,0.0);
}
else if(normal.y < -0.9)
{
texCoords = float2(worldPositionOffset.x, worldPositionOffset.z);
tangent = float3(1.0, 0.0, 0.0);
binormal = float3(0.0, 0.0, 1.0);
}
else if(normal.z < -0.9)
{
texCoords = float2(-worldPositionOffset.x, worldPositionOffset.y);
tangent = float3(-1.0, 0.0, 0.0);
binormal = float3(0.0, 1.0, 0.0);
}
}
gamer8o4 wrote:
i think i can handle the implemantation of a textureatlas myself.
Be aware that texture atlases get pretty complex if you want to make use of linear filtering. You should find some useful code on these forums but I'm not sure anyone got it working 100%. If you just want point filtering (like Minecraft) then it's much easier, but you still might be better off using a different system suc as different texture units or texture arrays (if you have hardware support).