Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by stefanplc · Mar 23, 2017 at 11:20 PM · shadershadersmobileprojectorprojectors

Need help with projector shader

Hey guys,

I'm pretty clueless when it comes to shader coding and I'm wondering if someone could help me fix what I currently have. I want to build a shader that I'll use with my projector and it'll just project the image exactly as it is in terms of colors and opacity, nothing more, nothing less. I've been searching all over the internet for some examples to see if I could find one that I could just tweak but haven't been all that lucky. I did however find one that's pretty close. Btw, these will be used on a mobile game so I'm trying to make them as simple as possible without any extra features to save performance. I'm trying to project a circle that indicates a "hero unit" and a shadow in the middle of it. Bellow is what it currently looks like:

alt text

The image in the corner is what I'd like to project. Here's my current code:

 Shader "Projector/AdditiveTint" {
     Properties {
         _Attenuation ("Falloff", Range(0.0, 1.0)) = 1.0
         _ShadowTex ("Cookie", 2D) = "gray" {}
     }
     Subshader {
         Tags {"Queue"="Transparent"}
         Pass {
             ZWrite Off
             ColorMask RGB
             Blend SrcAlpha One // Additive blending
             Offset -1, -1
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
             
             struct v2f {
                 float4 uvShadow : TEXCOORD0;
                 float4 pos : SV_POSITION;
             };
             
             float4x4 unity_Projector;
             float4x4 unity_ProjectorClip;
             
             v2f vert (float4 vertex : POSITION)
             {
                 v2f o;
                 o.pos = mul (UNITY_MATRIX_MVP, vertex);
                 o.uvShadow = mul (unity_Projector, vertex);
                 return o;
             }
             
             sampler2D _ShadowTex;
             fixed4 _Color;
             float _Attenuation;
             
             fixed4 frag (v2f i) : SV_Target
             {
                 // Apply tint & alpha mask
                 fixed4 texCookie = tex2Dproj (_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
                 fixed4 outColor = texCookie.a;
                 // Distance attenuation
                 float depth = i.uvShadow.z; // [-1(near), 1(far)]
                 return outColor * clamp(1.0 - abs(depth) + _Attenuation, 0.0, 1.0);
             }
             ENDCG
         }
     }
 }

I'm guessing for someone who can code shaders this isn't anything difficult but to me it's all Chinese. If anyone could help me, I would really appreciate it. I'd also like to remove that extra functionality at the end with the "Distance attenuation". What it does is it makes it so based on distance the image fades out. Since I'm trying to make it as simple as possible that's not really needed. Thanks in advance!

ss.jpg (131.0 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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by lmoro · Jan 15, 2018 at 08:39 AM

I think your main change is to have changed the Blending mode (Blend OneMinusSrcAlpha SrcAlpha), then you removed the distance attenuation and tint to keep the texture with the saem color as it comes.


The image you posted was white because you were using additive blending.

https://docs.unity3d.com/Manual/SL-Blend.html

https://en.wikipedia.org/wiki/Blend_modes

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
avatar image
0

Answer by stefanplc · Mar 24, 2017 at 12:11 AM

I ended up finding one which after some "random" edits to see what happens turned out to do what I was looking for. Unfortunately I'm not entirely sure what the code does or how I could simplify it even more, but it seems to do the job for now. If anyone knows how this code could be simplified even more, that would be awesome!

 // Upgrade NOTE: replaced '_Projector' with 'unity_Projector'
 
 Shader "Projector/TatooProjector"
 {
     Properties
     {
         _ShadowTex ("Cookie", 2D) = "white" { TexGen ObjectLinear }
     }
    
     Subshader
     {
         Tags { "RenderType"="Transparent"  "Queue"="Transparent+100"}
         Pass
         {
             ZWrite Off
             Offset -1, -1
    
             Fog { Mode Off }
    
             ColorMask RGB
             Blend OneMinusSrcAlpha SrcAlpha
 
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma fragmentoption ARB_fog_exp2
             #pragma fragmentoption ARB_precision_hint_fastest
             #include "UnityCG.cginc"
            
             struct v2f
             {
                 float4 pos      : SV_POSITION;
                 float4 uv       : TEXCOORD0;
             };
            
                 sampler2D _ShadowTex;
                 float4x4 unity_Projector;
                 float4 _Color;
            
             v2f vert(appdata_tan v)
             {
                 v2f o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.uv = mul (unity_Projector, v.vertex);
                 return o;
             }
            
             half4 frag (v2f i) : COLOR
             {
                 half4 tex = tex2Dproj(_ShadowTex, i.uv);
                 tex.a = 1-tex.a;
                 if (i.uv.w < 0)
                 {
                     tex = float4(0,0,0,1);
                 }
                 return tex;
             }
             ENDCG
        
         }
     }
 }

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
avatar image
0

Answer by NearAutomata · May 27, 2018 at 04:41 PM

@stefanplc I can't thank you enough for your posted shader as I have been searching the internet and asking around and noone was able to help on that so awesome work!

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
avatar image
0

Answer by JollyTheory · May 18, 2020 at 08:00 AM

Here's an additive colored projector shader that respects it's nearClipPlane and farClipPlane (at least in orthographic mode)

 Shader "Custom/ProjectorAdditiveTint" {
     Properties{
         _Color("Tint Color", Color) = (1,1,1,1)
         _ShadowTex("Cookie", 2D) = "gray" {}
     }
     Subshader{
         Tags {"Queue" = "Transparent"}
         Pass {
             ZWrite Off
             Offset -1, -1
             ColorMask RGB
             Blend SrcAlpha One // Additive blending
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             struct v2f {
                 float4 uvShadow : TEXCOORD0;
                 float4 pos : SV_POSITION;
             };
 
             float4x4 unity_Projector;
             float4x4 unity_ProjectorClip;
 
             v2f vert(float4 vertex : POSITION)
             {
                 v2f o;
                 o.pos = UnityObjectToClipPos(vertex);
                 o.uvShadow = mul(unity_Projector, vertex);
                 return o;
             }
 
             sampler2D _ShadowTex;
             fixed4 _Color;
 
             fixed4 frag(v2f i) : SV_Target
             {
                 fixed4 texCookie = tex2Dproj(_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
                 clip(1 + i.uvShadow.z);
                 clip(1 - i.uvShadow.z);
                 return _Color * texCookie.a;
             }
             ENDCG
         }
     }
 }

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

142 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

Related Questions

Projector and Shader with modified Vertices 0 Answers

problem w/ projector 0 Answers

Making clamped texture render parts outside of the uvs transparent 1 Answer

Creating a 3D portal effect using stencil buffer with a deferred shading setup? 1 Answer

LWRP Shader Graph material fades away with distance. 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