Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Aidiakapi · Sep 06, 2014 at 10:29 PM · shaderbugfragment

Transparent grid shader issue

I have 2 planes (default mesh filter) with a custom shader to render. The shader should render it as a transparent grid, and generally it does, but it bugs out.

When looking at them from the side, they look just fine. However, if I slightly tilt the camera, the front pane becomes (semi?)opaque and back pane loses it's horizontal stripes.

2 grids correct

This is my first attempt at shaders, so I'm most likely doing something incredibly stupid, this is what happens.

alt text

In play mode, even without having a back panel, it's fully opaque no matter the settings.

This is my shader code:

 Shader "Custom/GridShader" {
     Properties {
         _CellSize ("Grid cell size", Float) = 0.5
         _LineWidth ("Grid line width", Float) = 0.1
         _GridColor ("Color", Color) = (1, 1, 1, 1)
     }
     SubShader {
         Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
         pass
         {
             Cull Back
             ZWrite Off
             Blend SrcAlpha OneMinusSrcAlpha
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
  
             struct VertIn
             {
                 float4 vertex : POSITION;
             };
  
             struct VertOut
             {
                 float4 position : POSITION;
                 float3 locpos : TEXCOORD0;
             };
 
             float _CellSize;
             float _LineWidth;
             float4 _GridColor;
  
             VertOut vert(VertIn input)
             {
                 VertOut output;
                 output.position = mul(UNITY_MATRIX_MVP, input.vertex);
                 output.locpos = input.vertex.xyz;
                 return output;
             }
 
             float getGridFact(float pos)
             {
                 float snapPos = round(pos / _CellSize) * _CellSize;
                 float dist = abs(snapPos - pos);
                 return 1 - min(1.f, dist * 2.f / _LineWidth);
             }
 
             float4 frag(VertOut i) : COLOR
             {
                 float factX = getGridFact(i.locpos.x);
                 float factZ = getGridFact(i.locpos.z);
                 return _GridColor * float4(1.f, 1.f, 1.f, factX + factZ - (factX * factZ));
             }
             ENDCG
         }
     } 
     FallBack "Diffuse"
 }

Does anybody know what's going on, and why this is happening?

untitled.png (441.1 kB)
untitled.png (222.3 kB)
Comment
Add comment · Show 1
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 pamz3d · Aug 12, 2016 at 11:38 AM 0
Share

Thanks @Aidiakapi for the code. I have been searching for four long frustrating days and finally i got to see your code.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Jun 08, 2016 at 06:56 PM

Best I can tell, the z-coordinate is getting messed up somewhere between the vertex and fragment shaders. But I have no idea why... That code should work, to the best of my knowledge.

Maybe try storing it as a float2, and passing input.vertex.xz in the vertex shader and modify the fragment shader to use xy instead of xz?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Very simple 2D Shader renders black 0 Answers

Shader renders differently in editor and in exported app 0 Answers

Fog shader 2 Answers

Alternative Android Shader for Fragment? 0 Answers

Transerent Shader renders black on android afetr updating unity 1 Answer


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