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
0
Question by deprofundiis · Oct 21, 2014 at 03:06 PM · 2dshaderspriteshaders

Double sided pass?

Hi everyone,

I have this shader but - mainly due to my limited knowledge of shader programming - don't know why it is not working as "double sided".

Currently, whenever I flip the sprite object, the reverse side is unlit. To my knowledge Cull Off should do it, but it is not working. Is is possible at all with the current code?

 Shader "Sprites/Diffuse"
 {
     Properties
     {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         _Color ("Tint", Color) = (1,1,1,1)
         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
         _Cutoff ("Shadow alpha cutoff", Range(0,1)) = 0.5
     }
 
     SubShader
     {
         Tags
         { 
             "Queue"="Transparent" 
             "IgnoreProjector"="True" 
             "RenderType"="Transparent" 
             "PreviewType"="Plane"
             "CanUseSpriteAtlas"="True"
         }
 
         Cull Off
         Lighting Off
         ZWrite Off
         Fog { Mode Off }
         Blend One OneMinusSrcAlpha
 
         CGPROGRAM
         #pragma surface surf Lambert alpha addshadow vertex:vert
         #pragma multi_compile DUMMY PIXELSNAP_ON
 
         sampler2D _MainTex;
         fixed4 _Color;
 
         struct Input
         {
             float2 uv_MainTex;
             fixed4 color;
         };
         
         void vert (inout appdata_full v, out Input o)
         {
             #if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
             v.vertex = UnityPixelSnap (v.vertex);
             #endif
             v.normal = float3(0,0,-1);
             
             UNITY_INITIALIZE_OUTPUT(Input, o);
             o.color = v.color * _Color;
         }
 
         void surf (Input IN, inout SurfaceOutput o)
         {
             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
             o.Albedo = c.rgb * c.a;
             o.Alpha = c.a;
         }
         ENDCG
     }
 
 Fallback "Transparent/Cutout/VertexLit"
 }
 

Comment
Add comment · Show 2
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 deprofundiis · Oct 21, 2014 at 03:15 PM 0
Share

Argh... Culling is working, I just need to light the back side of the sprite - it is currently unlit!

avatar image Sudarmin-Then · Nov 01, 2015 at 10:00 PM 0
Share

Hi, so did you successfully make the back lit? I also have the same problem, where I want my sprite to be double sided and both can lit by light and have shadows, would you $$anonymous$$d to share your shader here?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Oct 21, 2014 at 03:34 PM

When you disable culling it will render both sides, however the vertex attributes exits only once, so your vertex normals can only point in one direction. That means they are wrong for the other side (usually the back-side)

A common way to write a double sided shader is to implement it as two-pass shader and do back face culling in one pass and front-face culling in the second pass. The second pass would of course use the inverted normal for lighting.

Another way is to use the camera-to-object vector and the objects normal vector to figure out which side is visible and invert the normal if it's pointing in the same direction as your cam-to-obj vector.

Usually it's way easier and more versatile to simply add the back face in the mesh. So if it's a quad you would use 2 quads one that faces the front and one that faces the back each with correct normals. That's why "cull off" shaders are usually unlit shaders like in particle systems.

Comment
Add comment · Show 2 · 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 deprofundiis · Oct 21, 2014 at 04:59 PM 0
Share

Since I posted this I have been learning a lot about shaders and you basically validate everything I learned (yey). So now my question is: in terms of performance, is it better to do the 2 quads or do it dynamically in the shader through cam-to-obj?

avatar image deprofundiis · Oct 21, 2014 at 04:59 PM 0
Share

Oh and thanks for answering :) much appreciated

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why Sprites-Default shader hasn't any fallback? 1 Answer

Allow 2D sprite to receive light from any direction and show on both sides 0 Answers

How to do 2D graphics like this ? 1 Answer

ShaderGraph Position from Texture Sample Issue 0 Answers

Shader sprite overlay other sprites 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