Creating color from 4 floats

Node-based Shader Editor
Post Reply
Thayoost
Posts: 3
Joined: Wed Jun 21, 2017 8:33 pm

Creating color from 4 floats

Post by Thayoost »

Hello,

Amplify has so far proven to be a good basis for building up some shader skills, thanks for providing this product. I've now hit a bump I was hoping you could help me with.

I am using the RGBA channels of different textures and running separate mathematical functions on each channel using shader functions. These functions output a float (per channel). I want to re-assemble these channels into a color that goes to the Albedo, but I cannot seem to find a node that takes in 4 floats and outputs a "Color" or "float4".

Here's what I'm trying to do in pseudocode:

Code: Select all

float4 assembledAlbedo = float4 (0, 0, 0, 0); 
assembledAlbedo.r = SomeShaderFunctionR(text1.r, text2.r); 
assembledAlbedo.g = SomeShaderFunctionG(text1.g, text2.g); 
assembledAlbedo.b = SomeShaderFunctionB(text1.b, text2.b); 
assembledAlbedo.a = SomeShaderFunctionA(text1.a, text2.a); 
Does such a node exist? Or am I misunderstanding the variable type that goes into the albedo node?

Thanks for helping me out..
TY
User avatar
Ricardo Teixeira
Posts: 954
Joined: Fri Aug 09, 2013 2:26 pm

Re: Creating color from 4 floats

Post by Ricardo Teixeira »

Hello,

That sounds great, happy to hear it.

Have you tried the Append node? Let us know if you are looking to use it in a specific way not covered by that node, we would be happy to elaborate.

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!
Thayoost
Posts: 3
Joined: Wed Jun 21, 2017 8:33 pm

Re: Creating color from 4 floats

Post by Thayoost »

That's exactly what I needed. Missed that for some reason..
It doesn't work as expected, but good chance that's in my shader functions.. I'll check those out tomorrow..

Thanks for the quick reply! :D
TY
Thayoost
Posts: 3
Joined: Wed Jun 21, 2017 8:33 pm

Re: Creating color from 4 floats

Post by Thayoost »

I couldn't resist testing a bit more before hitting the sack. I may have found a bug, or I'm just using the system wrong.

When I use the same shader function 3 times, the generated code creates duplicate variables which gets me the error:
declaration of "ifLocalVar16" conflicts with : d3d9 49

Here's the image of my node graph:
https://drive.google.com/file/d/0B99Nb8 ... sp=sharing

Here's the generated code:

Code: Select all

void surf( Input i , inout SurfaceOutputStandard o )
		{
			float4 screenPos1 = i.screenPos;
			#if UNITY_UV_STARTS_AT_TOP
			float scale1 = -1.0;
			#else
			float scale1 = 1.0;
			#endif
			float halfPosW1 = screenPos1.w * 0.5;
			screenPos1.y = ( screenPos1.y - halfPosW1 ) * _ProjectionParams.x* scale1 + halfPosW1;
			screenPos1.w += 0.00000000001;
			screenPos1.xyzw /= screenPos1.w;
			float4 tex2DNode1 = tex2Dproj( GrabScreen0, UNITY_PROJ_COORD( screenPos1 ) );
			float temp_output_13_0_g10 = ( 1.0 - ( 2.0 * ( ( 1.0 - tex2DNode1.r ) * ( 1.0 - _BaseColor.r ) ) ) );
			float ifLocalVar16 = 0;
			if( tex2DNode1.r > 0.5 )
				ifLocalVar16 = temp_output_13_0_g10;
			else if( tex2DNode1.r == 0.5 )
				ifLocalVar16 = temp_output_13_0_g10;
			else if( tex2DNode1.r < 0.5 )
				ifLocalVar16 = ( tex2DNode1.r * ( 2.0 * _BaseColor.r ) );
			float temp_output_13_0_g9 = ( 1.0 - ( 2.0 * ( ( 1.0 - tex2DNode1.g ) * ( 1.0 - _BaseColor.g ) ) ) );
			float ifLocalVar16 = 0;
			if( tex2DNode1.g > 0.5 )
				ifLocalVar16 = temp_output_13_0_g9;
			else if( tex2DNode1.g == 0.5 )
				ifLocalVar16 = temp_output_13_0_g9;
			else if( tex2DNode1.g < 0.5 )
				ifLocalVar16 = ( tex2DNode1.g * ( 2.0 * _BaseColor.g ) );
			float temp_output_13_0_g11 = ( 1.0 - ( 2.0 * ( ( 1.0 - tex2DNode1.b ) * ( 1.0 - _BaseColor.b ) ) ) );
			float ifLocalVar16 = 0;
			if( tex2DNode1.b > 0.5 )
				ifLocalVar16 = temp_output_13_0_g11;
			else if( tex2DNode1.b == 0.5 )
				ifLocalVar16 = temp_output_13_0_g11;
			else if( tex2DNode1.b < 0.5 )
				ifLocalVar16 = ( tex2DNode1.b * ( 2.0 * _BaseColor.b ) );
			float4 appendResult41 = float4( ifLocalVar16 , ifLocalVar16 , ifLocalVar16 , _BaseColor.a );
			o.Albedo = appendResult41.xyz;
			o.Alpha = 1;
		}
Notice how it uses the same variable name 3 times. I changed the variable names to be unique which solved the errors. Any other info you may need?
User avatar
Ricardo Teixeira
Posts: 954
Joined: Fri Aug 09, 2013 2:26 pm

Re: Creating color from 4 floats

Post by Ricardo Teixeira »

Thayoost wrote:That's exactly what I needed. Missed that for some reason..
It doesn't work as expected, but good chance that's in my shader functions.. I'll check those out tomorrow..

Thanks for the quick reply! :D
TY
Awesome! No problem, we would be happy to help debug the issue.
Thayoost wrote:I couldn't resist testing a bit more before hitting the sack. I may have found a bug, or I'm just using the system wrong.

When I use the same shader function 3 times, the generated code creates duplicate variables which gets me the error:
declaration of "ifLocalVar16" conflicts with : d3d9 49

Here's the image of my node graph:
https://drive.google.com/file/d/0B99Nb8 ... sp=sharing

Here's the generated code:

Code: Select all

void surf( Input i , inout SurfaceOutputStandard o )
		{
			float4 screenPos1 = i.screenPos;
			#if UNITY_UV_STARTS_AT_TOP
			float scale1 = -1.0;
			#else
			float scale1 = 1.0;
			#endif
			float halfPosW1 = screenPos1.w * 0.5;
			screenPos1.y = ( screenPos1.y - halfPosW1 ) * _ProjectionParams.x* scale1 + halfPosW1;
			screenPos1.w += 0.00000000001;
			screenPos1.xyzw /= screenPos1.w;
			float4 tex2DNode1 = tex2Dproj( GrabScreen0, UNITY_PROJ_COORD( screenPos1 ) );
			float temp_output_13_0_g10 = ( 1.0 - ( 2.0 * ( ( 1.0 - tex2DNode1.r ) * ( 1.0 - _BaseColor.r ) ) ) );
			float ifLocalVar16 = 0;
			if( tex2DNode1.r > 0.5 )
				ifLocalVar16 = temp_output_13_0_g10;
			else if( tex2DNode1.r == 0.5 )
				ifLocalVar16 = temp_output_13_0_g10;
			else if( tex2DNode1.r < 0.5 )
				ifLocalVar16 = ( tex2DNode1.r * ( 2.0 * _BaseColor.r ) );
			float temp_output_13_0_g9 = ( 1.0 - ( 2.0 * ( ( 1.0 - tex2DNode1.g ) * ( 1.0 - _BaseColor.g ) ) ) );
			float ifLocalVar16 = 0;
			if( tex2DNode1.g > 0.5 )
				ifLocalVar16 = temp_output_13_0_g9;
			else if( tex2DNode1.g == 0.5 )
				ifLocalVar16 = temp_output_13_0_g9;
			else if( tex2DNode1.g < 0.5 )
				ifLocalVar16 = ( tex2DNode1.g * ( 2.0 * _BaseColor.g ) );
			float temp_output_13_0_g11 = ( 1.0 - ( 2.0 * ( ( 1.0 - tex2DNode1.b ) * ( 1.0 - _BaseColor.b ) ) ) );
			float ifLocalVar16 = 0;
			if( tex2DNode1.b > 0.5 )
				ifLocalVar16 = temp_output_13_0_g11;
			else if( tex2DNode1.b == 0.5 )
				ifLocalVar16 = temp_output_13_0_g11;
			else if( tex2DNode1.b < 0.5 )
				ifLocalVar16 = ( tex2DNode1.b * ( 2.0 * _BaseColor.b ) );
			float4 appendResult41 = float4( ifLocalVar16 , ifLocalVar16 , ifLocalVar16 , _BaseColor.a );
			o.Albedo = appendResult41.xyz;
			o.Alpha = 1;
		}
Notice how it uses the same variable name 3 times. I changed the variable names to be unique which solved the errors. Any other info you may need?
Thank you for the additional information. What's your current ASE version? Be sure to pickup the latest package directly from our site.

If possible, please share the Shader and Shader Function, we are available via support@amplify pt.

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!
Post Reply