Search Unity

Tri-planar Shading on deformable ico-sphere mesh

Discussion in 'Shaders' started by frances_farmer, Aug 29, 2019.

  1. frances_farmer

    frances_farmer

    Joined:
    Aug 29, 2019
    Posts:
    11
    I have an icosahedron that I have subdivided into a sphere and I'm trying to texture it using a tri-planar shader. This works fine until I start deforming the terrain on the sphere by raising and lowering vertices and I end up with triangles that have stretched textures. I assume that this is because the axes of the planes I'm using to project the textures are oriented in the standard way such that Y is up and that I probably need them oriented relative to my sphere? Here is where the projection happens - taken from Martin Palko's site:

    Code (CSharp):
    1. void surf (Input IN, inout SurfaceOutput o)
    2.         {
    3.             // Find our UVs for each axis based on world position of the fragment.
    4.             half2 yUV = IN.worldPos.xz / _TextureScale;
    5.             half2 xUV = IN.worldPos.zy / _TextureScale;
    6.             half2 zUV = IN.worldPos.xy / _TextureScale;
    7.             // Now do texture samples from our diffuse map with each of the 3 UV set's we've just made.
    8.             half3 yDiff = tex2D (_DiffuseMap, yUV);
    9.             half3 xDiff = tex2D (_DiffuseMap, xUV);
    10.             half3 zDiff = tex2D (_DiffuseMap, zUV);
    Could anyone point me in the right direction to orienting my axes correctly? Thanks in advance!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Are you updating the vertex normals post displacement? You have to do this manually, it's not handled by basic displacement alone. If not, then triplanar mapping isn't going to be very effective. It relies on the normal to know which projection direction to use. So if the normal passed in isn't the current surface's normal, it'll likely be showing a projection that's more stretched than you're expecting.
     
  3. frances_farmer

    frances_farmer

    Joined:
    Aug 29, 2019
    Posts:
    11
    Hi bgolus, thank you for your reply. I certainly thought I was updating the normals correctly on the mesh. I will double check the normals to make sure they look correct. Do you think the projections should work without having to modify the axes in any way, using the world position, if I fix my normals?
     
  4. frances_farmer

    frances_farmer

    Joined:
    Aug 29, 2019
    Posts:
    11
    I fixed my normals. They were all pointing outwards from the center of the sphere. Now the texturing works.

    Thank you bgolus!
     
    bgolus likes this.