Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Texture Atlas Shader

Discussion in 'Scripting' started by Shiikarii, Apr 26, 2016.

  1. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    Hello, it's me again..

    i thought that today i can solve my uv atlas problem, but it seems that im not the best coder in the GLSL/HLSL languages... so my question.. can somebody help/show me or give me an example for texture altases.

    A full shader example would be nice.. as i said im not the best shader coder...

    Here are my greedy voxel chunks:
    greedy_nrm.PNG greedy_wire.PNG uvs.PNG

    As u can see the uvs r correct.. but i can only display one texture and i don't want to use submeshes because of performance issues...

    I've found a solution for texture atlases with greedy meshing:
    https://0fps.net/2013/07/09/texture-atlases-wrapping-and-mip-mapping/

    Thanks to everyone who can help me solving this problem :)

    MR Michael
     
  2. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    ok i found a solution.. but i works not perfectly..

    here is the uv error:
    greedy0.PNG greedy_texture.PNG

    can somebody explain why the textures r stretched?

    this is my shader:
    Code (CSharp):
    1. Shader "BioGen/Block" {
    2.     Properties{
    3.         _MainTex("Atlas", 2D) = "gray" {}
    4.     }
    5.         SubShader{
    6.         Tags{ "RenderType" = "Opaque" }
    7.         LOD 200
    8.  
    9.         CGPROGRAM
    10. #pragma surface surf Standard fullforwardshadows
    11. #pragma target 3.0
    12.  
    13.         sampler2D _MainTex;
    14.  
    15.     struct Input {
    16.         float4 color : COLOR;
    17.         float3 worldNormal;
    18.         float3 worldPos;
    19.     };
    20.  
    21.     void surf(Input i, inout SurfaceOutputStandard o)
    22.     {
    23.         const float gridSize = 8;
    24.         const float margin = (1 / gridSize) / 4; //For 16x16 textures, margin of 1px, 32x32 => 2px etc.
    25.         const float cellSize = (1 / gridSize) - (margin * 2);
    26.         float blockId = int(i.color.a * gridSize * gridSize) / gridSize;
    27.  
    28.         float2 uv = float2(
    29.             i.worldPos.x * (1 - i.worldNormal.x) + i.worldPos.z * i.worldNormal.x,
    30.             i.worldPos.y * (1 - i.worldNormal.y) + i.worldPos.z * i.worldNormal.y);
    31.  
    32.         fixed4 c = tex2Dgrad(_MainTex, frac(uv) * cellSize + float2(blockId + margin, floor(blockId) / gridSize + margin), ddx(uv * cellSize), ddy(uv * cellSize));
    33.         o.Albedo = c.rgb;
    34.     }
    35.     ENDCG
    36.     }
    37.         FallBack "Diffuse"
    38. }
    and here is my texture atlas, it is 32x32px and i supports 8 textures for now...
    Atlas.png