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 lancer · Sep 17, 2016 at 02:19 AM · shadersglsl

Lerping in a shader

I'm trying to create a basic shader that just lerps between two base colors. The lerping works if I adjust the blend knob, but now I want to make it so it flashes by it's self.

I was able to find examples that used a shader along side a script that passes the blend value, but I want to do it all in the shader.

I though I programmed it correctly, but it doesn't appear to be fading between the two colors.

My shader:

 Shader "Game/Solid/SolidFlash"
 {
     Properties
     {
         _Blend("Blend", Range(0, 1)) = 0.5
         _BlendSpeed("Blend Speed", Range(0, 50)) = 0.5
         _Color("Main Color", Color) = (1, 1, 1, 1)
         _Color2("Flash Color", Color) = (1, 1, 1, 1)
         _MainTex("Texture 1", 2D) = "white" {}
     }
         SubShader
     {
         Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
         LOD 300
         Pass
     {
         SetTexture[_MainTex]
 
     }
 
         CGPROGRAM
     #pragma surface surf Lambert
 
     sampler2D _MainTex;
 
     fixed4 _Color;
     fixed4 _Color2;
     float _Blend;
     float _BlendSpeed;
 
     int forward = 1;
 
     struct Input
     {
         float2 uv_MainTex;
         float2 uv_Texture2;
     };
 
     void surf(Input IN, inout SurfaceOutput o)
     {
 
         if (forward == 1) {
             _Blend += unity_DeltaTime.x / _BlendSpeed;
             if (_Blend >= 1)
                 forward = 0;
         }
         else {
             _Blend -= unity_DeltaTime.x / _BlendSpeed;
             if (_Blend <= 0)
                 forward = 1;
         }
 
         fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
         o.Albedo = (lerp(_Color, _Color2, _Blend)+ tex * tex.a); // lerp(t1, t2, _Blend);
     }
     ENDCG
     }
         FallBack "Diffuse"
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by tanoshimi · Sep 17, 2016 at 07:24 AM

While valid syntax, using if statements in a shader is considered a big no-no that should be absolutely avoided if possible. The GPU is a pipeline processor that can run a fixed set of instructions against a massive number of fragments very efficiently. But as soon as you implement branching logic, that pipeline must be rebuilt - defeating the parallelism at which GPUs excel.

A better solution would be to perform calculations in a C# script on the CPU and just pass that value to the shader, as in the examples you said you've already found.

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 Clounity · Feb 13, 2017 at 02:04 PM

Something like this @lancer ?

 void surf(Input IN, inout SurfaceOutput o)
     {
         _Blend = (1+_SinTime.w) / 2;
 
         fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
         o.Albedo = lerp(_Color, _Color2, _Blend) * tex;
     }
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

56 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

Related Questions

Can one write glsl shaders in Unity 2017? I cant make this shader work. The expected color should be red but I get black. 0 Answers

Can this shader run in unity? 1 Answer

what would you choose to write unity shaders GLSL, HLSL/CG or Surface lang? 1 Answer

How to use Unity3d shader to mask another two-pass shader? 0 Answers

Compile Shader Graph to GLSL 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