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 Timze · Oct 25, 2017 at 07:39 PM · materialshaderscolor

Shader Color

Hey, I have this waving sprite shader here and I would like to add color to wave objects and this already kind a works after I added those 3 lines there (commented ones) but after I press play button to see it in game it resets to default white. I can also edit it in game and can see the changes but again it resets as soon as I stop play mode. So, how can I get to keep that color I set? I'm new with shaders still... But I read many topics saying it shouldn't be that hard. (but seems bit too hard) Thanks in advance.

 Shader "Custom/WavySprite"{
     Properties{
         _Color("Color", Color) = (1,1,1,1)     // Edit: Added this line here
         _MainTex("Texture",2D)="white"{}
         _WaveDirection("Wave Direction",range(0,1))=0
         _StaticSide("Static Side",range(0,4))=3
         _WaveFrequency("Wave Frequency",float)=10
         _WaveForce("Wave Force",float)=0.1
         _WaveSpeed("Wave Speed",float)=1
     }
     SubShader
     {
         Tags{
             "Queue"="Transparent" 
             "IgnoreProjector"="True"
             "RenderType"="Transparent" 
             "PreviewType"="Plane"
         }
         ZWrite Off 
         Lighting Off 
         Cull Off 
         Fog { Mode Off } 
         Blend One Zero
         Pass{
             Blend SrcAlpha OneMinusSrcAlpha
             CGPROGRAM
 
             #pragma exclude_renderers xbox360 ps3 flash
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
 
             struct appdata{
                 float4 pos:POSITION;
                 float2 uv:TEXCOORD0;
             };
 
             struct v2f{
                 float4 pos:SV_POSITION;
                 float2 uv:TEXCOORD0;
             };
 
             fixed4 _Color;    // Edit: Added this line here
             sampler2D _MainTex;
             float _WaveDirection;
             float _StaticSide;
             float _WaveFrequency;
             float _WaveForce;
             float _WaveSpeed;
 
             v2f vert(appdata v){
                 v2f o;
                 float3 unit=UnityObjectToClipPos(float3(1,1,0))-UnityObjectToClipPos(float3(0,0,0));
                 float3 wpos=mul(unity_ObjectToWorld,v.pos).xyz;
                 o.pos=UnityObjectToClipPos(v.pos);
                 //Decide a static side
                 float multiplier=0;
                 if(_StaticSide==1) multiplier=1-v.uv.y; //Top
                 if(_StaticSide==2) multiplier=1-v.uv.x; //Right
                 if(_StaticSide==3) multiplier=v.uv.y; //Bottom
                 if(_StaticSide==4) multiplier=v.uv.x; //Left
                 if(_StaticSide==0) multiplier=1; //None
                 //Horizontal waves
                 if(_WaveDirection==0) o.pos.x+=sin((wpos.y*_WaveFrequency)-(_Time.a*_WaveSpeed))*(_WaveForce*unit.x*multiplier);
                 //Vertical waves
                 else o.pos.y+=sin((wpos.x*_WaveFrequency)-(_Time.a*_WaveSpeed))*(_WaveForce*unit.y*multiplier);
 
                 o.uv=v.uv;
                 return o;
             }
             
             fixed4 frag (v2f i):SV_Target{
                 fixed4 col=tex2D(_MainTex,i.uv);
                 col *= _Color;     // Edit: Added this line here
                 return col; 
             }
             ENDCG
         }
     }
 }
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
0
Best Answer

Answer by TSI25 · Oct 25, 2017 at 08:09 PM

I'm a beginner with all the shader logic but if you check out this link and scroll down to the Final Color Modifier section it has a pattern that will apply the color tint to whatever the final calculated coloring is, and might show a more reliable way of setting that color

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 Timze · Oct 25, 2017 at 09:19 PM 0
Share

I tried working with that but no success so far, while I continue trying it, other suggestions are appreciated.

avatar image TSI25 Timze · Oct 26, 2017 at 01:59 PM 0
Share

when i bring your shader into a new project and apply it to a sprite and change the color on the material it actually works just fine. are you changing the color from the material or from the hierarchy? and are you sure you arent just changing the color of the sprite by accident? it does seem like it overrides that color

EDIT as a side note you can also go to the scene view and enable animated material previews from this little drop down to get a better idea of what that material will look like at run time.

avatar image Timze TSI25 · Oct 26, 2017 at 03:43 PM 0
Share

I got it all working. The color was indeed overridden in script that makes the object wave. (I didn't provide it earlier, sorry, didn't expect it to be cause) I edited that script and now it all works fine. $$anonymous$$ade small changes to the shader too. Thanks for help.

                 fixed4 frag (v2f i):SV_Target{
                     fixed4 col =tex2D(_$$anonymous$$ainTex,i.uv);
                     return col * _Color;
                 }


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

91 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

White material, which is affected by colored light 0 Answers

Unity 5 - New project, default shaders are pink? 8 Answers

Any idea on how to do a FallOff Shader...? 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