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 /
avatar image
0
Question by PinkPanter · Sep 10, 2016 at 08:51 AM · shadowprojector

Shadows in projector shader

I am trying to make custom shader for projector component, to make sticker for car. I improve standard ProjectorMultiply shadere to draw only in front of projector and in limited distance.

But I can't use shadows over this. I am trying standard ways, like here Vertex and fragment shader examples, and many other ways from different forums, but all of it not work.

I know that if I blend projection with background texture it will have shadow, but I need clean texture of sticker.

 Shader "Projector/Sticker" {
     Properties {
         _ShadowTex ("Cookie", 2D) = "gray" {}
         _ProjectorForward("Projector Forward", Vector) = (0, 0, 0)
         _ProjectorPosition("Projector Position", Vector) = (0, 0, 0)
         _Distance("Projector Distance", Float) = 5
 
         _Reflection("Reflection Intence", Float) = 1
     }
     Subshader {
         Tags {"Queue"="Transparent"}
         Pass {
             ZWrite Off
             ColorMask RGB
             Blend SrcAlpha OneMinusSrcAlpha
             Offset -1, -1
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
             #include "UnityCG.cginc"
             #include "UnityLightingCommon.cginc"
             #include "AutoLight.cginc"
             
             struct v2f {
                 float4 uvShadow : TEXCOORD0;
                 float3 normal : NORMAL; 
                 float4 pos : SV_POSITION;
                 float3 worldPos : NORMAL1;
                 fixed3 diff : COLOR;
             };
             
             float4x4 unity_Projector;
             float4x4 unity_ProjectorClip;
 
             v2f vert (appdata_base v)
             {
                 v2f o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                 o.uvShadow = mul (unity_Projector, v.vertex);
                 o.normal = mul(unity_ObjectToWorld, v.normal);
 
                 //Light
                 half nl = min(max(0.3, dot(o.normal, _WorldSpaceLightPos0.xyz) * 0.5 + 0.5), 0.8);
                 o.diff = nl * _LightColor0;
 
                 return o;
             }
             
             sampler2D _ShadowTex;
             sampler2D _FalloffTex;
             float3 _ProjectorForward;
             float3 _ProjectorPosition;
             float _Distance;
             float _Reflection;
 
             fixed4 frag(v2f i) : SV_Target
             {
                 fixed4 projectedCord = UNITY_PROJ_COORD(i.uvShadow);
                 clip(projectedCord.z);
 
                 if (distance(_ProjectorPosition, i.worldPos) > _Distance)
                     return float4(0, 0, 0, 0);
                 if (i.uvShadow.x > 1 || i.uvShadow.x < 0)
                     return float4(0, 0, 0, 0);
                 if (i.uvShadow.y > 1 || i.uvShadow.y < 0)
                     return float4(0, 0, 0, 0);
 
                 if (dot(i.normal, _ProjectorForward) < 0.0) // in front of projector?
                 {
                     fixed4 res = tex2Dproj(_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
                     UNITY_APPLY_FOG_COLOR(i.fogCoord, res, fixed4(1, 1, 1, 1));
 
                     return res * fixed4(i.diff, 1);
                 }
                 else // behind projector
                 {
                     return float4(1.0, 1.0, 1.0, 0.0);
                 }
             }
             ENDCG
         }
     }
 }
 

Here is example, black is a strengthened shadow alt text

2016-09-10-00-56-34.png (74.2 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

Answer by Namey5 · Sep 11, 2016 at 07:21 AM

Alpha blended objects cannot receive shadows (technically speaking) in unity, and as such objects in the transparent queue will not receive shadows. Although, the image you are using does not appear to have semitransparent parts, so you should be able to use alpha clipping instead, i.e.

 Shader "Projector/Sticker" {
      Properties {
          _ShadowTex ("Cookie", 2D) = "gray" {}
          _ProjectorForward("Projector Forward", Vector) = (0, 0, 0)
          _ProjectorPosition("Projector Position", Vector) = (0, 0, 0)
          _Distance("Projector Distance", Float) = 5
  
          _Reflection("Reflection Intence", Float) = 1
      }
      Subshader {
          Tags {"Queue"="Geometry"}
          Pass {
              ColorMask RGB
              Offset -1, -1
  
              CGPROGRAM
              #pragma vertex vert
              #pragma fragment frag
              #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
              #include "UnityCG.cginc"
              #include "UnityLightingCommon.cginc"
              #include "AutoLight.cginc"
              
              struct v2f {
                  float4 uvShadow : TEXCOORD0;
                  float3 normal : NORMAL; 
                  float4 pos : SV_POSITION;
                  float3 worldPos : NORMAL1;
                  fixed3 diff : COLOR;
              };
              
              float4x4 unity_Projector;
              float4x4 unity_ProjectorClip;
  
              v2f vert (appdata_base v)
              {
                  v2f o;
                  o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                  o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                  o.uvShadow = mul (unity_Projector, v.vertex);
                  o.normal = mul(unity_ObjectToWorld, v.normal);
  
                  //Light
                  half nl = min(max(0.3, dot(o.normal, _WorldSpaceLightPos0.xyz) * 0.5 + 0.5), 0.8);
                  o.diff = nl * _LightColor0;
  
                  return o;
              }
              
              sampler2D _ShadowTex;
              sampler2D _FalloffTex;
              float3 _ProjectorForward;
              float3 _ProjectorPosition;
              float _Distance;
              float _Reflection;
  
              fixed4 frag(v2f i) : SV_Target
              {
                  fixed4 projectedCord = UNITY_PROJ_COORD(i.uvShadow);
                  clip(projectedCord.z);
  
                  if (distance(_ProjectorPosition, i.worldPos) > _Distance)
                      return float4(0, 0, 0, 0);
                  if (i.uvShadow.x > 1 || i.uvShadow.x < 0)
                      return float4(0, 0, 0, 0);
                  if (i.uvShadow.y > 1 || i.uvShadow.y < 0)
                      return float4(0, 0, 0, 0);
  
                  if (dot(i.normal, _ProjectorForward) < 0.0) // in front of projector?
                  {
                      fixed4 res = tex2Dproj(_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
                      UNITY_APPLY_FOG_COLOR(i.fogCoord, res, fixed4(1, 1, 1, 1));
                      
                      if (res.a < 0.5)
                          discard;
 
                      return res * fixed4(i.diff, 1);
                  }
                  else // behind projector
                  {
                      return float4(1.0, 1.0, 1.0, 0.0);
                  }
              }
              ENDCG
          }
      }
  }
Comment
Add comment · Show 1 · 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 PinkPanter · Sep 15, 2016 at 06:29 PM 0
Share

Without blending object is black. Also, I found unity sample project, that use command buffer to create decals, and now making my own decal system based by it

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

Projector LookAt Player 2 Answers

One-sided texture projection 0 Answers

Need help with projector shader 0 Answers

Blob Shadow Dynamic FarClipPlane Distance 0 Answers

Shadow Projector Component not pointed down 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