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 Eonwulf · Oct 16, 2013 at 10:44 PM · shaderlightingnormalsvertex

How to calculate light from behind a quad? (and other light positions)

So here's the deal:

I am using Quads to render sprites into the 3D world. I am NOT trying to create a 2D game. For a reference of something similar to what I am doing, think of Paper Mario.

So here's the problem:

I want the sprites to be lit up by lights, and they are. However, they are only lit up from in front of the quad. Any light behind it or perfectly to the side, above and below will not apply any light to the quad.

So here's what I think I need to do:

I think that I need to calculate the normal 6 times, 1 for each direction. Either that or calculate the light per vertex based solely on how far away the light source is without normals. The problem with the latter is that directional lights won't work, which is sorta ok, I can probably find a work around to that. However I don't know how to do either of these things in a shader.

So here's the shader I have made so far:

This shader takes 2 textures, 1 is the base sprite and the other is an overlay of details such as a stripe texture or a checker texture. Both of these can be colored individually.

 Shader "Custom/Simple" {
     Properties {
         _MainColor ("Texture Tint", Color) = (1,1,1,1)
         _Main ("Main", 2D) = "white" {}
         _DetailColor ("Texture Tint", Color) = (1,1,1,1)
         _Detail ("Detail", 2D) = "white" {}
     }
     SubShader {
         Tags {"RenderType" = "Transparent"}
         
         CGPROGRAM
         #pragma surface surf Lambert alpha
 
         struct Input {
             float2 uv_Main;
             float2 uv_Detail;
         };
         
         sampler2D _Main;
         sampler2D _Detail;
         fixed4 _MainColor;
         fixed4 _DetailColor;
 
         void surf (Input IN, inout SurfaceOutput o) {
             fixed4 c = tex2D(_Main, IN.uv_Main) * _MainColor;
             fixed4 d = tex2D (_Detail, IN.uv_Detail) * _DetailColor;
             if (d.a > 0)
                 o.Albedo = c.rgb * d.rgb;
             else
                 o.Albedo = c.rgb;
             o.Alpha = c.a;
             
         }
         ENDCG
     } 
     FallBack "Transparent/Diffuse"
 }

If you know the solution to my problem or can point me in the right direction, then I will be very grateful.

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
1
Best Answer

Answer by Eonwulf · Oct 17, 2013 at 09:50 PM

So I started putting sprites into my scene and noticed a lot of problems with them being drawn in the wrong order so I had to do some research. Apparently the only thing that does a proper order is the Transparency Cutoff shaders, so I downloaded them and modified the diffuse one. I learned about controlling the lighting of a surface shader and finally came up with a solution that doesn't have any problems that I can see for now.

 Shader "Custom/Sprite2" {
 Properties {
     _MainCol ("Main Tint", Color) = (1,1,1,1)
     _MainTex ("Main Texture", 2D) = "white" {}
     _DetailCol ("Detail Tint", Color) = (1,1,1,1)
     _DetailTex ("Detail Texture", 2D) = "white" {}
     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
 }
 
 SubShader {
     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
     Cull off
     LOD 200
     
 CGPROGRAM
 #pragma surface surf SimpleLambert alphatest:_Cutoff
 
 sampler2D _MainTex;
 fixed4 _MainCol;
 sampler2D _DetailTex;
 fixed4 _DetailCol;
 
 half4 LightingSimpleLambert (SurfaceOutput s, half3 lightDir, half atten) {
           half4 c;
           c.rgb = s.Albedo * _LightColor0.rgb * (atten);
           c.a = s.Alpha;
           return c;
       }
 
 struct Input {
     float2 uv_MainTex;
     float2 uv_DetailTex;
 };
 
 void surf (Input IN, inout SurfaceOutput o) {
     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _MainCol;
     fixed4 d = tex2D(_DetailTex, IN.uv_DetailTex) * _DetailCol;
     o.Albedo = lerp(c.rgb, d.rgb, d.a);
     o.Alpha = c.a;
 }
 ENDCG
 }
 
 Fallback "Transparent/Cutout/VertexLit"
 }
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

14 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

Related Questions

Implementing Custom Vertex Lighting Shader 1 Answer

Assigning vertex normal in Surface program to o.Normal brakes lighting? 0 Answers

Simple vertex color + light shader 1 Answer

shader for light replacement blending, instead of additive blending? 0 Answers

Lighting and lightmapping - OpenGL ES 1.1? 3 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