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
0
Question by Chimera · Oct 08, 2014 at 09:32 AM · shadertime

Changinge the value of a shader over time

Hi. I am trying to change shader vaules over time on a object.

I Found shader HSV_shader, which enables me to change HueShift, Value and Saturation on texture. But I have no idea how to change those values over time. Can someone please hlep me.

The shader:

     Shader "Custom/HSVShader" {
         Properties {
             _MainTex ("Texture", 2D) = "white" {}
             _HueShift("HueShift", Float ) = 0
             _Sat("Saturation", Float) = 1
             _Val("Value", Float) = 1
         }
         SubShader {
      
             Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType" = "Transparent" }
             ZWrite Off
             Blend SrcAlpha OneMinusSrcAlpha
             Cull Off
      
             Pass
             {
                 CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
                 #pragma target 2.0
      
                 #include "UnityCG.cginc"
      
                 float3 shift_col(float3 RGB, float3 shift)
                 {
                 float3 RESULT = float3(RGB);
                 float VSU = shift.z*shift.y*cos(shift.x*3.14159265/180);
                     float VSW = shift.z*shift.y*sin(shift.x*3.14159265/180);
                    
                     RESULT.x = (.299*shift.z+.701*VSU+.168*VSW)*RGB.x
                             + (.587*shift.z-.587*VSU+.330*VSW)*RGB.y
                             + (.114*shift.z-.114*VSU-.497*VSW)*RGB.z;
                    
                     RESULT.y = (.299*shift.z-.299*VSU-.328*VSW)*RGB.x
                             + (.587*shift.z+.413*VSU+.035*VSW)*RGB.y
                             + (.114*shift.z-.114*VSU+.292*VSW)*RGB.z;
                    
                     RESULT.z = (.299*shift.z-.3*VSU+1.25*VSW)*RGB.x
                             + (.587*shift.z-.588*VSU-1.05*VSW)*RGB.y
                             + (.114*shift.z+.886*VSU-.203*VSW)*RGB.z;
                    
                 return (RESULT);
                 }
      
                 struct v2f {
                     float4  pos : SV_POSITION;
                     float2  uv : TEXCOORD0;
                 };
      
                 float4 _MainTex_ST;
      
                 v2f vert (appdata_base v)
                 {
                     v2f o;
                     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                     return o;
                 }
      
                 sampler2D _MainTex;
                 float _HueShift;
                 float _Sat;
                 float _Val;
      
                 half4 frag(v2f i) : COLOR
                 {
                     half4 col = tex2D(_MainTex, i.uv);
                     float3 shift = float3(_HueShift, _Sat, _Val);
                    
                     return half4( half3(shift_col(col, shift)), col.a);
                 }
                 ENDCG
             }
         }
         Fallback "Particles/Alpha Blended"
     }


I also did the script in which I am trying to call and change those values.

 using UnityEngine;
 using System.Collections;
 
 public class HsValue : MonoBehaviour {
     void Start() {
         renderer.material.shader = Shader.Find("HSVshader");
     }
     void Update() {
         float _Sat = Mathf.PingPong(Time.time, 0.0F);
         renderer.material.SetFloat("Saturation", _Sat);
 
         float _HueShift = Mathf.PingPong(Time.time, 1.0F);
         renderer.material.SetFloat("HueShift", _HueShift);
 
         float _Val = Mathf.PingPong(Time.time, 1.0F);
         renderer.material.SetFloat("Value", _Val);
     }
 }

Thanks for help :)

Comment
Add comment · Show 2
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 Scribe · Oct 08, 2014 at 10:25 AM 0
Share

Tbh I can't see a reason for your hue and value to not be working but I thought I'd point out a few $$anonymous$$or problems. Firstly, saturation will currently always be 0 as you have set the length of the pingpong to 0. Secondly, it is bad practice to be creating 'new variables' every frame which is what happens when you have float _Sat etc, it is preferable to create the variable outside of the function and simply change their values in update (like Evil Tak suggested). Finally (and not really a problem) but in general, na$$anonymous$$g convention means that names like '_Val' are the ones inside your shader and 'Value' would be the name of your float variable but that is just convention, and it appears you are still using the right variable in the right place assu$$anonymous$$g the variable names in your shader are Saturation, HueShift and Value!

avatar image Scribe · Oct 08, 2014 at 10:28 AM 0
Share

Oh just realised, you are not calling the SetFloat method correctly as I sort of mentioned in point 3 of my last comment. I think it should be:

 renderer.material.SetFloat("_Sat", _Sat);

as _Sat is the name of the variable in your shader, similarly for the other 2!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by EvilTak · Oct 08, 2014 at 10:00 AM

You can use something like this:

 public float speed;
 
 float sat, hueShift, val;
 
 void Update(){
     float t += Time.deltaTime * speed;
     t = Mathf.Clamp01(t);
     sat = hueShift = val = t; //If you want the value to be same
 }

If you want the value of hueShift, sat and val to remain same. If not, you could use different values for both, you can use different speed variables for each of them.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Built-in ShaderLab "_Time.y" and "Time.time" are not equal? 5 Answers

Change input values based on Time 0 Answers

Trying to change a range within a shader over time, using a script. 1 Answer

Shader Graph not updating time related nodes in preview 6 Answers

How can i create a time bubble effect ? 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