Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ghostboxer · May 28, 2016 at 05:23 AM · shaderproceduraltexture atlas

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...alt text

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"
 }

ssedit.png (384.2 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cjdev · May 28, 2016 at 06:17 AM 0
Share

Shouldn't it be IN.color.a * 255 ?

avatar image ghostboxer cjdev · May 28, 2016 at 01:00 PM 0
Share

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.

avatar image cjdev ghostboxer · May 28, 2016 at 03:28 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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"
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

62 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Strange shading on procedural mesh 0 Answers

How to create texture in shader 1 Answer

Create Shader from string 0 Answers

Skybox color pls help! 0 Answers

Understanding the Procedural Skybox 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges