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 FedoraDaEsplorer · Nov 07, 2019 at 11:08 PM · shaderlightingsightfalloff

How to have a point light with no falloff in Unity 3D?

Does anyone know of a good strategy to make point lights with no falloff, eg. anything in the radius of the light will be 100% illumination while anything outside the radius will be 0% illumination.

I want to use this as a sort of 3D line of sight drawing. The line of sight is a big sphere originating from the character's head that I thought would be a clever idea to have that represented as a point light, however with the falloff it doesn't correctly show where the edge of the line of site is.

I've tried a few solutions, such as a custom falloff package (doesn't seem to have the options I want) and a few shaders, but I am terrible at shaders so it's difficult for me to debug. I'll post the shader here in case somebody can tell me if I'm being dumb, otherwise if someone can point me to a different solution that would be great. Thanks!

Here's a shader I tried, but it just turns everything invisible and it's not immediately obvious to me why:

 Shader "Custom/No Falloff v2"
 {
     Properties
     {
         _Color("Color", Color) = (1,1,1,1)
         _MainTex("Texture", 2D) = "white" {}
     }
         SubShader
     {
         Tags{ "Queue" = "Transparent" }
 
         Pass
     {
         Blend SrcAlpha OneMinusSrcAlpha
 
         Tags{ "LightMode" = "ForwardAdd" }
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #include "UnityCG.cginc"
         #include "Lighting.cginc"
 
         // compile shader into multiple variants, with and without shadows
         #pragma multi_compile_fwdadd_fullshadows
         // shadow helper functions and macros
         #include "AutoLight.cginc"
 
         sampler2D _MainTex;
     float4 _MainTex_ST;
 
     fixed4 _Color;
 
     struct v2f
     {
         float2 uv : TEXCOORD0;
         SHADOW_COORDS(1) // put shadows data into TEXCOORD1
             float4 pos : SV_POSITION;
     };
 
     v2f vert(appdata_base v)
     {
         v2f o;
         o.pos = UnityObjectToClipPos(v.vertex);
         o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
         TRANSFER_SHADOW(o)
             return o;
     }
 
     fixed4 frag(v2f i) : SV_Target
     {
         fixed4 col = tex2D(_MainTex, i.uv) * _Color;
 
     // compute shadow attenuation (1.0 = fully lit, 0.0 = fully shadowed)
     fixed shadow = SHADOW_ATTENUATION(i);
 
     if (shadow < 0.5)
     {
         return fixed4(0.0156862745, 0, 0.23529411764,1.0); //or any other color for shadow
     }
 
     return col;
     }
         ENDCG
     }
 
         // shadow casting support
         UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
     }
 }
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

Answer by xibanya · Dec 24, 2019 at 10:36 AM

This'll do it. The key part is in the custom lighting function,

 #ifdef POINT
      atten = step(_PointThreshold, atten);
 #endif

step returns either 0 or 1. if the point light is stronger than the threshold, it'll become full strength. If less, it'll become 0. Other lights are unaffected, so you can still have normal shading from a directional light.

 Shader "Xibanya/HardPointLight"
 {
     Properties
     {
         _Color("Color", Color) = (1,1,1,1)
         _MainTex("Albedo (RGB)", 2D) = "white" {}
         _BumpMap("Normal", 2D) = "bump" {}
         _BumpScale("Normal Strength", float) = 1
         _Threshold("Shadow Threshold", Range(0,2)) = 1
         _ShadowSoftness("Shadow Smoothness", Range(0.5, 1)) = 0.6
         _ShadowColor("Shadow Color", Color) = (0,0,0,1)
         _PointThreshold("Point Threshold", Range(0, 1)) = 0.5
     }
         SubShader
         {
             Tags { "RenderType" = "Opaque" }
             LOD 200
 
             CGPROGRAM
             #pragma surface surf Toon
 
             half        _Threshold;
             half        _ShadowSoftness;
             half3        _ShadowColor;
 
             sampler2D    _MainTex;
             sampler2D    _BumpMap;
 
             struct Input
             {
                 float2 uv_MainTex;
             };
 
             half4        _Color;
             float        _BumpScale;
             half        _PointThreshold;
 
             inline half4 LightingToon(SurfaceOutput s, half3 lightDir, half atten)
             {
             #ifndef USING_DIRECTIONAL_LIGHT
                 lightDir = normalize(lightDir);
             #endif
             #ifdef POINT
                 atten = step(_PointThreshold, atten);
             #endif
                 half shadowDot = pow(dot(s.Normal, lightDir) * 0.5 + 0.5, _Threshold);
                 float threshold = smoothstep(0.5, _ShadowSoftness, shadowDot);
                 half3 diffuseTerm = threshold * atten;
                 half3 diffuse = lerp(_ShadowColor, _LightColor0.rgb, diffuseTerm);
                 return half4(s.Albedo * diffuse, 1);
             }
 
             void surf(Input IN, inout SurfaceOutput o)
             {
                 o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb * _Color;
                 o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale);
             }
             ENDCG
         }
         FallBack "Diffuse"
 }

here's a screenshot of this shader in action

alt text


nopointlightfalloff.jpg (259.4 kB)
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

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

What is wrog with Unity Deferred Shading? 0 Answers

How would I exclued parts of a texture from the lighting system? 0 Answers

I want to create a billowing fog/cloud/dry-ice effect, possibly constrained 0 Answers

apply lighting to custom shader made in shader graph? 0 Answers

day night cycle on building 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