• 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 unoo_ · Oct 25, 2018 at 12:45 PM · shadershadersshader programmingshader writing

my 2D light shader works wrong

I'm making 2d light which limits the sight like the second stage of Nuclear Throne. I made my shader work, but it doesn't do well with multiple lights. The circles surrounding the ones in the middle must have the same alpha but as you can see, the first one in the list is darker, and the last one is brighter.

alt text

Here's a script for a camera.

 using UnityEngine;
 
 [System.Serializable]
 public struct LightSource
 {
     public Vector2 pos;
     public float range;
 }
 
 [ExecuteInEditMode]
 public class LightCamera : MonoBehaviour {
 
     public Material lightMapper;
     public Material lightApplier;
     public LightSource[] lightSources;
 
     Camera cam;
 
     private void OnEnable()
     {
         cam = GetComponent<Camera>();
     }
 
     private void OnRenderImage(RenderTexture source, RenderTexture destination)
     {
         RenderTexture lightMap = new RenderTexture(source.width, source.height, 16);
 
         foreach (LightSource light in lightSources)
         {
             RenderTexture tempMap = new RenderTexture(source.width, source.height, 16);
 
             Vector2 screenPoint = cam.WorldToScreenPoint(light.pos);
 
             lightMapper.SetVector("_LightCoord", new Vector2(screenPoint.x, screenPoint.y));
             lightMapper.SetFloat("_SquaredLightRange", light.range * light.range);
 
             Graphics.Blit(lightMap, tempMap, lightMapper);
             lightMap = tempMap;
         }
 
         lightApplier.SetTexture("_LightMap", lightMap);
         Graphics.Blit(source, destination, lightApplier);
     }
 
 }
 

And shader. It's assigned to the lightMapper above.

 Shader "Custom/LightMapper"
 {
     Properties
     {
         _MainTex ("Texture", 2D) = "white" {}
     }
     SubShader
     {
         Pass
         {
             Blend SrcAlpha     OneMinusSrcAlpha
         
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             #include "UnityCG.cginc"
 
             struct appdata
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             struct v2f
             {
                 float4 vertex : SV_POSITION;
                 float2 uv : TEXCOORD0;
             };
 
             v2f vert(appdata v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.uv;
                 return o;
             }
 
             sampler2D _MainTex;
             float4 _MainTex_TexelSize;
             float2 _LightCoord;
             float _SquaredLightRange;
 
             float Square(float x) {
                 return x * x;
             }
             
             fixed4 frag(v2f i) : SV_Target
             {
                 float squaredDst = Square(_LightCoord.x - i.uv.x * _MainTex_TexelSize.z) + Square(_LightCoord.y - i.uv.y * _MainTex_TexelSize.w);
                 if (squaredDst <= _SquaredLightRange)
                 {
                     if (squaredDst <= _SquaredLightRange / 8 * 3)
                         return float4(1, 1, 1, 1);
 
                     float4 col = tex2D(_MainTex, i.uv);
                     if (col.a >= 1)
                         return col;
 
                     return float4(1, 1, 1, .5);
                 }
 
                 return tex2D(_MainTex, i.uv);
             }
             ENDCG
         }
     }
 }
 

The lightApplier doesn't matter. I tried drawing without that, but the result was the same.

캡처.png (128.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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

221 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

Related Questions

Operate the shader, rotate the material 90 degrees 0 Answers

How much operations does tex2d func take? 0 Answers

Reversed UV Light with 2D PointLight (shader graph) 0 Answers

How do I get the fragment position in world coordinates? 1 Answer

How to write a shader like this? [Screenshots] 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