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
1
Question by Tdebel · Apr 20, 2019 at 11:23 AM · material colorsprite renderer

Changing the Sprite Renderer's Color property has no effect in my Custom Shader

I have a material with custom surface shader of which I want to change the "Color" in the Sprite Renderer. However, changing the values seems to do nothing. Changing the "Tint" in the shader itself works fine, but that changes the color for all my Sprites using this material. Is there a way to propagate the Sprite Renderer's Color value like it works in the "Default-Sprite" material? The shader I'm using:

 Shader "Sprites/Diffuse Flash"
 {
     Properties
     {
         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
         //_SelfIllum ("Self Illumination",Range(0.0,1.0)) = 0.0
         [PerRendererData]_FlashAmount ("Flash Amount",Range(0.0,1.0)) = 0.0
         _Color ("Tint", Color) = (1,1,1,1)
         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
     }
 
     SubShader
     {
         Tags
         {
             "Queue" = "Transparent"
             "IgnoreProjector" = "True"
             "RenderType" = "Transparent"
             "PreviewType" = "Plane"
             "CanUseSpriteAtlas" = "True"
         }
 
         Cull Off
         Lighting Off
         ZWrite Off
         Blend SrcAlpha OneMinusSrcAlpha
 
         CGPROGRAM
 
         #pragma surface surf Lambert alpha:fade vertex:vert
         #pragma multi_compile DUMMY PIXELSNAP_ON
 
         sampler2D _MainTex;
         fixed4 _Color;
         float _FlashAmount,_SelfIllum;
 
         struct Input
         {
             float2 uv_MainTex;
             fixed4 color;
         };
 
         void vert(inout appdata_full v, out Input o)
         {
             #if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
             v.vertex = UnityPixelSnap(v.vertex);
             #endif
             v.normal = float3(0,0,-1);
 
             UNITY_INITIALIZE_OUTPUT(Input, o);
             o.color = _Color;
         }
 
         void surf(Input IN, inout SurfaceOutput o)
         {
             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
             o.Albedo = lerp(c.rgb,float3(1.0,1.0,1.0),_FlashAmount);
             //o.Emission = lerp(c.rgb,float3(1.0,1.0,1.0),_FlashAmount) * _SelfIllum;
             o.Alpha = c.a;
         }
     ENDCG
     }
 }

Where I want to be able to change the Color value: alt text

I'm not too well versed in shaders, but I already scoured the web and could find any solutions. Thanks in advance!

capture.png (62.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

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by nhanc18 · Nov 23, 2020 at 04:07 PM

Just read the line 57 of this shader: https://github.com/nubick/unity-utils/blob/master/sources/Assets/Scripts/Shaders/Sprites-Default.shader

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 Deivore · Nov 19, 2019 at 02:44 AM

Tdebel, what you are doing here in the vertex shader is actually using the color of the material tint, rather than the color of the SpriteRenderer.


appdata_full should have a field "appdata_full.color" that needs to be fed into the "Input" struct's "color" field, instead of using the "_Color" argument. This will use the SpriteRenderer's color as applied to the vertices.

Comment
Add comment · Show 1 · 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 seandanger · Feb 14, 2020 at 12:02 AM 0
Share

I don't think that's quite the case. Using the color property as passed into appdata_full like so has no effect:

 v2f vert (appdata_full IN)
 {
     v2f OUT;
     OUT.vertex = UnityObjectToClipPos(IN.vertex);
     OUT.texcoord = IN.texcoord;
     OUT.color = IN.color;
     return OUT;
 }

You need to grab the Instanced Property from the shader. I think I have a solution, I'll post a separate answer.

avatar image
0

Answer by HuldaGnodima · Jan 01, 2020 at 07:54 AM

I'm having the same problem, did you manage to solve this Tdebel?

EDIT: I managed to solve it! I'm posting it here in case anyone's stuck like me.

My code looks a little different to Tdebel's above, but maybe the same principle applies? In my code I had tried to access the standard color in my Sprite Renderer (on an object with a custom shader) a little something like this:

 //A renderer I pulled into the code through the editor
 public SpriteRenderer myRenderer;
     
     
 private void ChangeColor()  {
         myRenderer.material.color = Color.blue
 }

This didn't change the color or alpha for me. What worked instead was if I removed the material-call and just went straight for color like this:

 private void ChangeColor()  {
         myRenderer.color = Color.blue
 }

Hope this helps someone in the future!

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 seandanger · Feb 14, 2020 at 12:27 AM

The color property on SpriteRenderer is what's called an Instanced Property, which are changed using a MaterialPropertyBlock, which allows the Material's Instanced Properties to be changed without requiring a new SetPass call / batch. This page from the docs references that:


A Sprite Renderer uses the texture supplied in the Sprite property but uses the shader and other properties from the Material property (this is actually accomplished using a MaterialPropertyBlock behind the scenes). This means that you can use the same material to render different sprites without worrying about which texture is assigned on the material.


If you're unfamiliar with MaterialPropertyBlock or Instanced Properties in shaders, then I recommend this excellent tutorial. I'm still learning myself, and it's a confusing landscape out there with Unity having different information in various places, some of it outdated, much of it incomplete, etc.


Anyway, looking at the way Unity does it in their shader (I'm not sure how up to date that code is, but it can at least give hints), it looks like they're referencing a reserved variable named unity_SpriteRendererColorArray, so it appears you need to tap into that.


Adding/modifying this code to my shader worked for me (goes inside the CGPROGRAM, see the comments):

 // setup instanced property
 UNITY_INSTANCING_BUFFER_START(MyProps)
     UNITY_DEFINE_INSTANCED_PROP(fixed4, unity_SpriteRendererColorArray)
 UNITY_INSTANCING_BUFFER_END(MyProps)
 
 struct appdata
 {
     float4 vertex : POSITION;
     fixed4 color : COLOR; // apparently must be fixed4 to work with instancing
     float2 uv : TEXCOORD0;
     UNITY_VERTEX_INPUT_INSTANCE_ID // store instanced ID
 };
 
 struct v2f
 {
     float4 vertex : SV_POSITION;
     fixed4 color : COLOR;
     float2 uv : TEXCOORD0;
 };
 
 v2f vert (appdata v)
 {
     v2f o;
     UNITY_SETUP_INSTANCE_ID(v); // setup instanced id
     o.vertex = UnityObjectToClipPos(v.vertex);
     o.uv = v.uv;
     // get the instanced property
     o.color = v.color * UNITY_ACCESS_INSTANCED_PROP(MyProps, unity_SpriteRendererColorArray);
 
     return o;
 }


p.s. (meta) I'm a newb and can't apparently figure out how to break text into paragraphs without resorting to a horizontal rule. Anyone have a solution for that?

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 Tdebel · Feb 14, 2020 at 11:59 AM 0
Share

Hi seandanger, thanks for your answer! It has been quite a while since I asked this and gave up on it shortly after. I will do my due diligence and try out your answer when I have the time!

avatar image seandanger Tdebel · Feb 14, 2020 at 04:31 PM 0
Share

Hey, no problem! I figured you had moved on, but I was having the same issue and since this came up on search results, I wanted to add the answer once I figured it out. :)

avatar image
0

Answer by Houtamelo · Jul 28, 2021 at 08:22 PM

The answer of @seandanger did not work me, instead, i looked at the source code that @nhanc18 provided (his answer is not correct as the line 57 uses the tint which is a property on the material, not on the sprite renderer). This is the correct and simple way of accessing the SpriteRenderer's color field:

             struct appdata
             {
                 float4 vertex : POSITION;
                 fixed4 color : COLOR;
                 float2 texcoord  : TEXCOORD0;
             };
 
             struct v2f
             {
                 float4 vertex : SV_POSITION;
                 float4 color : COLOR;
                 float2 texcoord : TEXCORD0;
             };
 
             v2f vert (appdata IN)
             {
                 v2f OUT;
                 OUT.vertex = UnityObjectToClipPos(IN.vertex);
                 OUT.texcoord = IN.texcoord;
                 OUT.color = IN.color;
                 return OUT;
             }

The "color" field on the appdata struct is the SpriteRenderer's color field, so all you have to do is set the OUT variable's color to the IN variable's color.

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

110 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

Related Questions

Help with simple scripting? 1 Answer

Errors Cannot change albedo color. NullReferenceException 2 Answers

Why colors are being mixed after I try to change them? 1 Answer

Simple Color Shader for 3D Models 0 Answers

Set Specific Child's Material 2 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