Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by yatagarasu · Nov 26, 2013 at 03:01 PM · shaderspriteunity4.3

Apply shader to a sprite.

Is it possible to apply shader to a unity4.3 sprite somehow? Want to make some basic color correction.

Comment
Add comment · Show 1
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 schmidj42 · Jan 31, 2014 at 10:43 PM 0
Share

I am looking to do that also. I've tried to create a new material and bind it a custom shader, but I can't seem to figure out how to write the .shader file exactly for the Sprite Renderer.

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by schmidj42 · Feb 01, 2014 at 12:46 AM

I was finally able to use my own shader with a SpriteRenderer. Here what I did,

I created a new shader (i.e. called TextureBlend), then I edited the shader to have:

 Shader "2D/Texture Blend"
 {  
     Properties
     {
        _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Alpha Color Key", Color) = (0,0,0,1)
     }
     SubShader
     {
         Tags 
         { 
             "RenderType" = "Opaque" 
             "Queue" = "Transparent+1" 
         }
 
         Pass
         {
             ZWrite Off
             Blend SrcAlpha OneMinusSrcAlpha 
  
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile DUMMY PIXELSNAP_ON
  
             sampler2D _MainTex;
             float4 _Color;
 
             struct Vertex
             {
                 float4 vertex : POSITION;
                 float2 uv_MainTex : TEXCOORD0;
                 float2 uv2 : TEXCOORD1;
             };
     
             struct Fragment
             {
                 float4 vertex : POSITION;
                 float2 uv_MainTex : TEXCOORD0;
                 float2 uv2 : TEXCOORD1;
             };
  
             Fragment vert(Vertex v)
             {
                 Fragment o;
     
                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.uv_MainTex = v.uv_MainTex;
                 o.uv2 = v.uv2;
     
                 return o;
             }
                                                     
             float4 frag(Fragment IN) : COLOR
             {
                 float4 o = float4(1, 0, 0, 0.2);
 
                 half4 c = tex2D (_MainTex, IN.uv_MainTex);
                 o.rgb = c.rgb;
                 if(c.r == _Color.r && c.g == _Color.g && c.b == _Color.b)
                 {
                     o.a = 0;
                 }
                 else
                 {
                     o.a = 1;
                 }
                     
                 return o;
             }
 
             ENDCG
         }
     }
 }

Then I created a new Material (i.e. called OverlayMaterial) and attached the above shader to it.

Finally on the SpriteRenderer component I selected the OverlayMaterial for the Material attribute.

The important part that did make it work for me was to set the tags such as

 Tags 
 { 
     "RenderType" = "Opaque"
     "Queue" = "Transparent+1" 
 }

And by defining the vert program input as

     struct Vertex
     {
         float4 vertex : POSITION;
         float2 uv_MainTex : TEXCOORD0;
         float2 uv2 : TEXCOORD1;
     };

After that I was able to apply the effect I wanted. Here a shot of the result for the sprite renderer component alt text


sprite renderer custom material.jpg (39.1 kB)
Comment
Add comment · Show 3 · 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 OP_toss · Feb 03, 2014 at 06:07 PM 0
Share

So what exactly was the parts necessary for sprite renderer to accept it? The vertex struct and the tags?

Thanks for giving a detailed description of your solution! Not often you see that from people :)

avatar image zee_ola05 · Feb 06, 2014 at 04:05 AM 0
Share

Hi. I'm trying to apply your solution. But I can't. Is it possible for you to give me the resources for me to test it? $$anonymous$$aybe, the the texture and the shader file? That would really help me. Thanks!

avatar image yatagarasu · Feb 20, 2014 at 11:06 AM 1
Share

OP_toss, This properties and tags are vital i think

 Properties
 {
     [PerRendererData] _$$anonymous$$ainTex ("Sprite Texture", 2D) = "white" {}
     [$$anonymous$$aterialToggle] PixelSnap ("Pixel snap", Float) = 0
 }

 SubShader
 {
     Tags
     { 
         "Queue"="Transparent" 
         "IgnoreProjector"="True" 
         "RenderType"="Transparent" 
         "PreviewType"="Plane"
         "CanUseSpriteAtlas"="True"
     }

     Cull Off
     Lighting Off
     ZWrite Off
     Fog { $$anonymous$$ode Off }
     Blend SrcAlpha One$$anonymous$$inusSrcAlpha

also you can always check builin shader source for details. You can get them here http://unity3d.com/unity/download/archive

avatar image
0

Answer by supernat · Jan 31, 2014 at 11:30 PM

See this link: http://docs.unity3d.com/Documentation/Components/SL-ShaderReplacement.html

I haven't tried this myself, but look down at the shaders that can be replaced. Maybe you can use Camera.SetReplacementShader(). This functionality may not be built in for 2D sprites yet though.

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 DTek · Mar 31, 2015 at 10:28 PM

This is my solution - a lot simpler and more straight forward :

  • create a material

  • add your sprite to the material ( you can change this later from the sprite itself )

  • add the shader to the material

  • in the sprite properties, drag in the shader.

  • List item

That should do it :) .

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

21 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

Related Questions

2D Sprite Highlight 0 Answers

Is it possible to have multiple stencil reference values in one shader? 0 Answers

Pixel snap for material made with Unlit Shader Graph? 1 Answer

Outline set of tiled sprites 0 Answers

Making shader's first pass light sensitive (2D) 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