Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by darkerparker7 · Dec 10, 2017 at 05:41 AM · shadershadersshader programmingshaderlabshader writing

Translucent Transparent Diffuse Sprite Shader Edit

I have no experience with shaders, and I am in a bit of a pickle. I found the shader below in order to allow light to illuminate the front of a sprite no matter where light is coming from. This effect is needed because I am mixing 2D sprites in a 3D environment and am having some issues when light is behind the sprite (sprite turns black). The attached shader works great, but with two small issues that I have tried but could not fix. The first being that the shader is not transparent and creates a black box around my sprites, something that other transparent shaders do not do. The second is that the attached shader does not diffuse light. It creates a bright spot where the light reflects. Diffuse shaders that I have tried look much better.

In short, I need help converting the attached shader to a transparent diffuse shader without losing the translucent effect that I need.

(credit to http://farfarer.com/blog/2012/09/11/translucent-shader-unity3d/)

 Shader "Custom/Translucent" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _BumpMap ("Normal (Normal)", 2D) = "bump" {}
         _Color ("Main Color", Color) = (1,1,1,1)
         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
         _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
 
         //_Thickness = Thickness texture (invert normals, bake AO).
         //_Power = "Sharpness" of translucent glow.
         //_Distortion = Subsurface distortion, shifts surface normal, effectively a refractive index.
         //_Scale = Multiplier for translucent glow - should be per-light, really.
         //_SubColor = Subsurface colour.
         _Thickness ("Thickness (R)", 2D) = "bump" {}
         _Power ("Subsurface Power", Float) = 1.0
         _Distortion ("Subsurface Distortion", Float) = 0.0
         _Scale ("Subsurface Scale", Float) = 0.5
         _SubColor ("Subsurface Color", Color) = (1.0, 1.0, 1.0, 1.0)
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
 
         CGPROGRAM
         #pragma surface surf Translucent
         #pragma exclude_renderers flash
 
         sampler2D _MainTex, _BumpMap, _Thickness;
         float _Scale, _Power, _Distortion;
         fixed4 _Color, _SubColor;
         half _Shininess;
 
         struct Input {
             float2 uv_MainTex;
         };
 
         void surf (Input IN, inout SurfaceOutput o) {
             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
             o.Albedo = tex.rgb * _Color.rgb;
             o.Alpha = tex2D(_Thickness, IN.uv_MainTex).r;
             o.Gloss = tex.a;
             o.Specular = _Shininess;
             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
         }
 
         inline fixed4 LightingTranslucent (SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten)
         {        
             // You can remove these two lines,
             // to save some instructions. They're just
             // here for visual fidelity.
             viewDir = normalize ( viewDir );
             lightDir = normalize ( lightDir );
 
             // Translucency.
             half3 transLightDir = lightDir + s.Normal * _Distortion;
             float transDot = pow ( max (0, dot ( viewDir, -transLightDir ) ), _Power ) * _Scale;
             fixed3 transLight = (atten * 2) * ( transDot ) * s.Alpha * _SubColor.rgb;
             fixed3 transAlbedo = s.Albedo * _LightColor0.rgb * transLight;
 
             // Regular BlinnPhong.
             half3 h = normalize (lightDir + viewDir);
             fixed diff = max (0, dot (s.Normal, lightDir));
             float nh = max (0, dot (s.Normal, h));
             float spec = pow (nh, s.Specular*128.0) * s.Gloss;
             fixed3 diffAlbedo = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten * 2);
 
             // Add the two together.
             fixed4 c;
             c.rgb = diffAlbedo + transAlbedo;
             c.a = _LightColor0.a * _SpecColor.a * spec * atten;
             return c;
         }
 
         ENDCG
     }
     FallBack "Bumped Diffuse"
 }

lightbehind.jpg (19.6 kB)
lightfront.jpg (19.1 kB)
Comment
Add comment
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

1 Reply

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

Answer by darkerparker7 · Dec 10, 2017 at 10:48 PM

I fixed my issue. Here is the code.

 Shader "Custom/Translucent" {
     Properties{
         _MainTex("Base (RGB) Trans(A)", 2D) = "white" {}
         _BumpMap("Normal (Normal)", 2D) = "bump" {}
         _Color("Main Color", Color) = (1,1,1,1)
         _SpecColor("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
         _Shininess("Shininess", Range(0.03, 1)) = 0.078125
 
         //_Thickness = Thickness texture (invert normals, bake AO).
         //_Power = "Sharpness" of translucent glow.
         //_Distortion = Subsurface distortion, shifts surface normal, effectively a refractive index.
         //_Scale = Multiplier for translucent glow - should be per-light, really.
         //_SubColor = Subsurface colour.
         _Thickness("Thickness (R)", 2D) = "bump" {}
         _Power("Subsurface Power", Float) = 1.0
         _Distortion("Subsurface Distortion", Float) = 0.0
         _Scale("Subsurface Scale", Float) = 0.5
         _SubColor("Subsurface Color", Color) = (1.0, 1.0, 1.0, 1.0)
     }
         SubShader{
         Tags{ "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
         Cull Off
         LOD 200
 
         CGPROGRAM
 #pragma surface surf Translucent alpha
 //#pragma surface surf Lambert alpha
 
     sampler2D _MainTex, _BumpMap, _Thickness;
     float _Scale, _Power, _Distortion;
     fixed4 _Color, _SubColor;
     half _Shininess;
 
     struct Input {
         float2 uv_MainTex;
     };
 
     void surf(Input IN, inout SurfaceOutput o) {
         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
         o.Albedo = c.rgb;
         o.Alpha = c.a;
     }
 
     inline fixed4 LightingTranslucent(SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten)
     {
         half NdotL = dot(s.Normal, lightDir);
         half INdotL = dot(-s.Normal, lightDir);
         // Figure out if we should use the inverse normal or the regular normal based on light direction.
         half diff = (NdotL < 0) ? INdotL : NdotL;
         half4 c;
         c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten);
         c.a = s.Alpha;
 
         return c;
     }
 
     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

166 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 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 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

_Time in Image Effects 0 Answers

Operate the shader, rotate the material 90 degrees 0 Answers

Fog not working in my own Vertext Shader 2 Answers

Is it possible to set the 'ForceNoShadowCasting' SubShader Tag via shader properties or a custom material editor? 1 Answer

Is there any way to do multiple passes on a texture / in a fragment shader? 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