- Home /
Help with triplanar atlas shader.
So I have written a shader that uses a 32x32x256 Texture3D as a texture atlas, to texture a marching cubes terrain. In the voxel data, the type of voxel, and therefore the texture tile used, is indicated to the shader by the alpha channel of the vertex color of the mesh.
All of this seems to work nicely, except that there is, what I assume to call "bleeding", on any voxel that is adjacent to a different voxel type. All the other tiles of the atlas are 'squished', for lack of a better term, into that voxel's texture.
Like so...
I havent the slightest clue as to how to approach a solution to these artifacts. Can anyone help me, or point me towards some helpful reading material?
Here is the shader
Properties {
_Tint("Tint", Color) = (0.25, 0.25, 0.25, 1)
_Atlas("Atlas 3D", 3D) = "clear" {}
_Side("Side", 2D) = "white" {}
_Top("Top", 2D) = "white" {}
_Bottom("Bottom", 2D) = "white" {}
_SideScale("Side Scale", Float) = 2
_TopScale("Top Scale", Float) = 2
_BottomScale ("Bottom Scale", Float) = 2
_Rows ("Row Count", Float) = 1
_Clms ("Column Count", FLoat) = 1
}
SubShader {
Tags {
"Queue"="Geometry"
"IgnoreProjector"="False"
"RenderType"="Opaque"
}
Cull Back
ZWrite On
CGPROGRAM
#pragma surface surf Lambert vertex:vert fullforwardshadows
#pragma exclude_renderers flash
#include "UnityCG.cginc"
sampler3D _Atlas;
sampler2D _Side, _Top, _Bottom;
float _SideScale, _TopScale, _BottomScale, _Rows, _Clms;
float4 _Tint;
struct Input {
float3 worldPos;
float3 worldNormal;
float4 color;
float3 tileUV;
};
void vert(inout appdata_full v, out Input o){
UNITY_INITIALIZE_OUTPUT (Input, o);
o.color = v.color;
o.tileUV = v.vertex.zxy + 0.5f ;
}
void surf (Input IN, inout SurfaceOutput o) {
float3 projNormal = saturate(pow(IN.worldNormal * 1.4, 4));
// SIDE X
float3 x = tex3D(_Atlas, frac(float3(IN.tileUV.x,IN.tileUV.z, IN.color.a * 256 * _SideScale ))) * abs(IN.worldNormal.x);
// TOP / BOTTOM
float3 y = 0;
if (IN.worldNormal.y > 0) {
y = tex3D(_Atlas, frac(float3(IN.tileUV.x,IN.tileUV.y, IN.color.a * 256 * _TopScale ))) * abs(IN.worldNormal.y);
} else {
y = tex3D(_Atlas, frac(float3(IN.tileUV.x,IN.tileUV.y, IN.color.a * 256 * _BottomScale ))) * abs(IN.worldNormal.y);
}
// SIDE Z
float3 z = tex3D(_Atlas, frac(float3(IN.tileUV.y,IN.tileUV.z, IN.color.a * 256 * _SideScale ))) * abs(IN.worldNormal.z);
// o.Albedo = z * 0.925f;
o.Albedo = _Tint * lerp(o.Albedo, z, projNormal.z * 0.925f);
o.Albedo = _Tint * lerp(o.Albedo, x, projNormal.x);
o.Albedo = _Tint * lerp(o.Albedo, y, projNormal.y);
}
ENDCG
}
Fallback "Diffuse"
}
it should be, but when i use 255 the textures scramble together and I cant match the indexes to the right tiles. Though I'm not entirely sure why that is.
If I had to guess I'd say it's floating point imprecision in the abs of the world normals. If they're slightly off from 1 or -1 it would skew the frac return, possibly enough to affect the texture lookup.
Answer by Dray · Feb 05, 2018 at 09:18 AM
Hey man, I'm not sure if you are still looking for an answer to this but I'm just working on something quite similar and stumbled upon this so I'm just gonna add my two cents. So basically the reason this will not work the way you want is that between the vertices, all values like colors or uvs are interpolated for the fragment shader, meaning that if you put your alpha to 50 on one vertex and to 0 on the next one, it's going to run trough all the values from 50 to 0 when rendering the fragments in between, outputting all those 50 textures on its way.
Ways around this are either seperating the mesh into multiple submeshes which will render you unable to do any blending or to use different channels for each material (e.g. red for rock and green for grass), which will limit the number of materials you can add a lot but keep the ability to blend and use an atlas.
Answer by ghostboxer · May 29, 2016 at 04:26 PM
[Update]
I have since simplified the shader, and added bump mapping to it, but the problem of 'squished' textures remains.
Shader "Tri-Planar World" {
Properties {
_Tint("Tint", Color) = (1, 1, 1, 1)
_Atlas("Atlas 3D", 3D) = "clear" {}
_Bump("Bump Atlas 3D", 3D) = "clear" {}
_BumpMultX ("Bump Multiplier X", range(-0.3,0.3)) = 0
_BumpMultY ("Bump Multiplier Y", range(-0.3,0.3)) = 0
_BumpMultZ ("Bump Multiplier Z", range(-0.3,0.3)) = 0
}
SubShader {
Tags {
"Queue"="Geometry"
"IgnoreProjector"="False"
"RenderType"="Opaque"
}
Cull Back
ZWrite On
CGPROGRAM
#pragma target 3.0
#pragma surface surf Lambert vertex:vert fullforwardshadows
#pragma exclude_renderers flash
#pragma debug
#include "UnityCG.cginc"
sampler3D _Atlas, _Bump;
float4 _Tint;
float _BumpMultX, _BumpMultY, _BumpMultZ;
struct Input {
float3 worldNormal;
float4 color;
};
void vert(inout appdata_full v, out Input o){
UNITY_INITIALIZE_OUTPUT (Input, o);
o.color = v.color;
o.color.r = v.vertex.z + 0.5f;
o.color.g = v.vertex.x + 0.5f;
o.color.b = v.vertex.y + 0.5f;
}
void surf (Input IN, inout SurfaceOutput o) {
float3 projNormal = saturate(pow(IN.worldNormal * 1.4, 4));
// SIDE X
float3 x = tex3D(_Atlas, IN.color.rba) * abs(IN.worldNormal.x);
float3 xb = tex3D(_Bump, IN.color.rba) * abs(IN.worldNormal.x);
// TOP / BOTTOM
float3 y = 0;
float3 yb = 0;
if (IN.worldNormal.y > 0) {
y = tex3D(_Atlas, IN.color.rga) * abs(IN.worldNormal.y);
yb = tex3D(_Bump, IN.color.rga) * abs(IN.worldNormal.y);
} else {
y = tex3D(_Atlas, IN.color.rga) * abs(IN.worldNormal.y);
yb = tex3D(_Bump, IN.color.rga) * abs(IN.worldNormal.y);
}
// SIDE Z
float3 z = tex3D(_Atlas, IN.color.gba) * abs(IN.worldNormal.z);
float3 zb = tex3D(_Bump, IN.color.gba) * abs(IN.worldNormal.z);
o.Albedo = _Tint * lerp(o.Albedo, z * lerp(z,zb,_BumpMultZ), projNormal.z);
o.Albedo = _Tint * lerp(o.Albedo, x * lerp(x,xb,_BumpMultX), projNormal.x);
o.Albedo = _Tint * lerp(o.Albedo, y * lerp(y,yb,_BumpMultY), projNormal.y);
}
ENDCG
}
Fallback "Diffuse"
}
Your answer
Follow this Question
Related Questions
Is it possible to fake Global Illumination within textures? 1 Answer
Procedural Shader Cost/Draw Calls 1 Answer
is there a way to make a gradient material, white to black, in Unity3D? 2 Answers
How to create an procedural texture in shader graph for many objects, and save it? 0 Answers
Procedural heightmap shader - Normals and tangents 0 Answers