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 Maizer · Jan 19, 2017 at 09:09 PM · unity 5shadershadersvertexfragment

How can I make shadows pixelated in fragment function?

Hello! I'm pretty novice when it comes to graphics programming, so I'd love some advice on the best way to solve this problem. Let me give you some context.

Take a look at this image:

Alt Text

The above image represents how I light a square mesh. The values at the corners of the square represent a lighting value. 1 being fully shadowed, 0 being fully lit. Don't worry about how I get this value. I put this value into the uv2 channel of the mesh component. So, the bottom left vertex value gets stored as (0.0, 1.0f). I'm not using the x, just y in this case.

On to the shader:

Okay, so, I'm currently using this value in my frag function as a light multiplier. uv *= uv2.y (Remember, I'm only using the y-axis from uv2, it's the light value!) This creates a nice light blend, as the 1 light value lerps to 0.

Unfortunately, I'm using a pixel art texture that harshly contrasts the lighting gradient. See below:

Alt Text

It may be hard to see, but the pixel art is most apparent at the top right of the image. I want the shadow to have that same look, if possible.

Would it be possible to use the uv coord to snap this light value to a pixel grid?

Here is my current code:

 CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             struct app_data
             {
                 float4 vertex : POSITION;
                 float2 uv : TEXCOORD0;
                 float2 uv2 : TEXCOORD1;
             };
 
             struct v2f
             {
                 float4 vertex : SV_POSITION;
                 float2 uv : TEXCOORD0;
                 float2 uv2 : TEXCOORD1;
             };
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
               
             v2f vert (app_data v)
             {
                 v2f o;
                 o.vertex = UnityObjectToClipPos(v.vertex);
                 o.uv = v.uv;
                 o.uv2 = v.uv2;
 
                 return o;
             }
             
             fixed4 frag (v2f input) : SV_Target
             {
                 fixed4 col = tex2D(_MainTex, input.uv);
                 col.rgb *= input.uv2.y;
                 return col;
             }
             ENDCG


Thanks for any feedback!

Comment
Add comment · Show 4
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 SunnyChow · Jan 20, 2017 at 02:54 AM 0
Share

excuse me, may you upload the images again?

avatar image Maizer SunnyChow · Jan 20, 2017 at 05:24 PM 0
Share

Yes, sorry about that. Does it work now?

avatar image Cherno · Jan 20, 2017 at 05:58 PM 0
Share

Is it the same as this question I asked a while ago?

Pixels per Unit / Pixel Perfect Shadows, possible?

alt text

64761-pixelshadow-sample.png (487.1 kB)
avatar image Maizer Cherno · Jan 20, 2017 at 06:13 PM 0
Share

Yes and no. I am laso trying to lower the resolution of the shadow so it has a 'pixely' look, but I'm not using any directional lights to calculate shadows. Actually, looking at your post, I am calculating light the same way $$anonymous$$inecraft does. Heh.

The heart of the issue is that there are many light values per one pixel grid square. So, the question is, how does one average out the light in that one grid cell? And to make it even more difficult, how do you do that from the shader? :O From what I understand of shaders (not a lot), you are unaware of neighboring pixel values, so how can you average them out?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SunnyChow · Jan 20, 2017 at 02:59 AM

I am not sure what you want, because i can't see your image. But to manually render shadow, you need to use LIGHT_ATTENUATION() in AutoLight.cginc. I guess you can make it pixelated by working on the value _ShadowCoord

Edit

Ok i think i get what you want. You are not talking about the shadow of unity lighting system. You are talking about some light data baked in to vertex. Try something like this:

  col.rgb *= round(input.uv2.y*4)/4;

it doesn't have to be 4

Comment
Add comment · Show 2 · 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 Maizer · Jan 20, 2017 at 05:55 PM 0
Share

$$anonymous$$y apologies for that.

LIGHT_ATTENUATION uses a lookup texture, right? If so, I'd prefer not to use lookup textures because I'm changing the light so frequently. It's much quicker for me to change the uv2.y value when generating my mesh rather than generate textures.

Do you know if it's possible to look up the uv values at specific vertices from within the frag function? If so, I believe I could simply use that to manually interpolate my light values.

avatar image SunnyChow Maizer · Jan 23, 2017 at 04:37 AM 0
Share

if it's a real time shadow, the shadow texture is already being generated in every frame.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Combine Vertex / Fragment and Surface Shader 0 Answers

Prevent ColorMask obscuring parts of an object's mesh 0 Answers

Why doesn't this vertex+fragment shader work? 0 Answers

pass value from vertex to fragment without interpolation ?! 2 Answers

Standard Shader not correctly working on Android 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