if/if else/else clause in amplify shader editor

Node-based Shader Editor

if/if else/else clause in amplify shader editor

Postby BitteWenden » Thu May 11, 2017 9:36 pm

Hey!
How would I realize an if, if else, else clause in Amplify Shader Editor? A simple if clause is easily doable with a..well 'if' node, but I'm not sure how I'd do the if/else if.

For example something like:

Code: Select all
if(test > 0.5)
{
   //something
}
else if(test < 0.1)
{
   //something else
}
else
{
   //and, surprise, something
}
BitteWenden
 
Posts: 5
Joined: Thu May 11, 2017 9:29 pm

Re: if/if else/else clause in amplify shader editor

Postby Ricardo Teixeira » Thu May 11, 2017 10:10 pm

BitteWenden wrote:Hey!
How would I realize an if, if else, else clause in Amplify Shader Editor? A simple if clause is easily doable with a..well 'if' node, but I'm not sure how I'd do the if/else if.

For example something like:

Code: Select all
if(test > 0.5)
{
   //something
}
else if(test < 0.1)
{
   //something else
}
else
{
   //and, surprise, something
}


Hello,

Thank you for using Amplify Shader editor, we really appreciate it. It really depends on what you need to build, I recommend starting by checking the available logical operators.

Logical Operator Nodes

Be sure to let us know if you have any additional questions, we would be happy to help.

Thanks!
Sales & Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Ricardo Teixeira
 
Posts: 954
Joined: Fri Aug 09, 2013 2:26 pm

Re: if/if else/else clause in amplify shader editor

Postby BitteWenden » Thu May 11, 2017 10:32 pm

In my case I wanted to do something like in the given example code with using different values of the world position for an operation in each clause (at the end I want to get a world space texture). I tried to use two compare statements, but if the first value is true, there is no way to "avoid" running the second compare and then using the new (in this case wrong value). After these two statements I'm checking for that exact case and if it's true I'll use the first value instead of the second one. But I figured that there might be an easier way to accomplish it.

By the way great tool so far, it's one of the big things I am missing in Unity.
BitteWenden
 
Posts: 5
Joined: Thu May 11, 2017 9:29 pm

Re: if/if else/else clause in amplify shader editor

Postby BitteWenden » Fri May 12, 2017 10:10 am

Also, (probably a bug a I guess) putting a float value like 0,5 in a vector2 (and all other nodes like color and vector4) creates an error since it creates the code with 0,5 and not 0.5 so the constructor input is wrong.

Image

Image

(I fixed it by adding the following to the OnEnable() method of the AmplifyShaderEditorWindowClass)
Code: Select all
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
BitteWenden
 
Posts: 5
Joined: Thu May 11, 2017 9:29 pm

Re: if/if else/else clause in amplify shader editor

Postby Ricardo Teixeira » Fri May 12, 2017 12:33 pm

BitteWenden wrote:Also, (probably a bug a I guess) putting a float value like 0,5 in a vector2 (and all other nodes like color and vector4) creates an error since it creates the code with 0,5 and not 0.5 so the constructor input is wrong.

Image

Image

(I fixed it by adding the following to the OnEnable() method of the AmplifyShaderEditorWindowClass)
Code: Select all
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");


Hello,

Which Unity and ASE version are you currently using? (Windows > Amplify Shader Editor > About)
Be sure to update your ASE package if you are using an older version.

Current version v0.7.2.004 – 10th May 2017 - Download

Thanks!
Sales & Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Ricardo Teixeira
 
Posts: 954
Joined: Fri Aug 09, 2013 2:26 pm

Re: if/if else/else clause in amplify shader editor

Postby Ricardo Teixeira » Fri May 12, 2017 1:20 pm

BitteWenden wrote:In my case I wanted to do something like in the given example code with using different values of the world position for an operation in each clause (at the end I want to get a world space texture). I tried to use two compare statements, but if the first value is true, there is no way to "avoid" running the second compare and then using the new (in this case wrong value). After these two statements I'm checking for that exact case and if it's true I'll use the first value instead of the second one. But I figured that there might be an easier way to accomplish it.
By the way great tool so far, it's one of the big things I am missing in Unity.


Hello,

As you may have read, conditional statements (if etc) are not ideal for shaders but there are ways to overcome this limitation. I took the liberty of preparing a simple sample that demonstrates how to achieve the effect that you are looking for. Depending on which values you need to manipulate, you might need to adjust it, or even opt for another method of comparison(threshold or range).

Image

Sample shader - Download

For demonstration purposes, I used several float nodes that can actually be set directly in the Compare parameters; no need to create them, simple input the necessary values.

As you can see from the image, I used a simple setup that uses 1 main input (X) to set the value of 2 lerp nodes. In the first check (x > 0.5), if false = 0, If true = 1; meaning that it will either show the color for < 0.5 condition or it will show the default (else) color. To make sure that we also compare it with < 0.1, we run a second check for x < 0.1, if it's true we show the blue color, if not, we show the default else red color.

I hope that makes sense, I would be happy to answer any questions that you might have. It's also important to keep in mind that you can pass on values to your shader, you could use external conditions via code. Be sure to check the "Highlighted Animation" sample for a specific example.

Looking forward to your feedback.

Thanks!
Sales & Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Ricardo Teixeira
 
Posts: 954
Joined: Fri Aug 09, 2013 2:26 pm

Re: if/if else/else clause in amplify shader editor

Postby BitteWenden » Fri May 12, 2017 1:49 pm

Updated to the latest version from the website (only had the latest asset store version installed). The problem still exists, I'm using the 2017.1.0b1 with the updated C#/.NET version though. So that might be what's causing this.
BitteWenden
 
Posts: 5
Joined: Thu May 11, 2017 9:29 pm

Re: if/if else/else clause in amplify shader editor

Postby Ricardo Teixeira » Fri May 12, 2017 1:52 pm

BitteWenden wrote:Updated to the latest version from the website (only had the latest asset store version installed). The problem still exists, I'm using the 2017.1.0b1 with the updated C#/.NET version though. So that might be what's causing this.


Thank you for the update, and the tips. What's your OS and Keyboard language set to? It might be some sort of Unity issue, we would love to know more.

Thanks!
Sales & Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Ricardo Teixeira
 
Posts: 954
Joined: Fri Aug 09, 2013 2:26 pm

Re: if/if else/else clause in amplify shader editor

Postby BitteWenden » Fri May 12, 2017 2:01 pm

First of all, thanks a lot for the example! It's really helpful and exactly what I was looking for. I guess it takes some time to get used to the workflow.

OS and keyboard language are both German, that's probably why changing the culture info solves the problem. It's worth noting that I haven't tried it with the old 3.5 framework version since it won't let me "downgrade" the project to test it.
BitteWenden
 
Posts: 5
Joined: Thu May 11, 2017 9:29 pm

Re: if/if else/else clause in amplify shader editor

Postby Ricardo Teixeira » Fri May 12, 2017 2:28 pm

BitteWenden wrote:First of all, thanks a lot for the example! It's really helpful and exactly what I was looking for. I guess it takes some time to get used to the workflow.

OS and keyboard language are both German, that's probably why changing the culture info solves the problem. It's worth noting that I haven't tried it with the old 3.5 framework version since it won't let me "downgrade" the project to test it.


Hello,

No problem, happy to help.

Thank you for elaborating, we are aware of some Unity 2017 issues related. Unfortunately, we do not support beta Unity versions, we will register the bug and contact you as soon as we have any new developments.

We do not recommend using ASE with that specific Unity version.

Thanks!
Sales & Customer Relations at Amplify Creations
Learn more about our offering: Amplify Creations Products
Amplify Shader Editor won the Asset Store Best Tool Award - Thank you for your support!
User avatar
Ricardo Teixeira
 
Posts: 954
Joined: Fri Aug 09, 2013 2:26 pm


Return to Amplify Shader Editor

Who is online

Users browsing this forum: No registered users and 11 guests

cron