Hi, i have a simple shader-coding question. I dont understand why i dont get the desired Color and just a grey tone when i pass the RGBA values in seperated manner. -> fixed4 mainColor = _MainColor; fixed4 col = (mainColor.r, mainColor.g, mainColor.b, mainColor.a); If i would return only mainColor is would get my desired Color but if i return col i just get grey. Whats the problem? Code (HLSL): Shader "Unlit/test" { Properties { _MainColor("MainColor", Color) = (1,1,1,1) _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag // make fog work #pragma multi_compile_fog #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; UNITY_FOG_COORDS(1) float4 vertex : SV_POSITION; }; float4 _MainColor; sampler2D _MainTex; float4 _MainTex_ST; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); UNITY_TRANSFER_FOG(o,o.vertex); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 mainColor = _MainColor; // sample the texture fixed4 texColor = tex2D(_MainTex, i.uv); fixed4 col = (mainColor.r,mainColor.g,mainColor.b,mainColor.a); // apply fog UNITY_APPLY_FOG(i.fogCoord, col); return col; } ENDCG } } }
Hi Chris, Your syntax is just slightly wrong. Use: Code (HLSL): float4(mainColor.r,mainColor.g,mainColor.b,mainColor.a); and you can use also this kind of variable declaration, similar to setting values to an array: Code (HLSL): fixed4 col = { mainColor.r,mainColor.g,mainColor.b,mainColor.a }; I suggest you open the Microsoft HLSL reference. It's a dry but good source for exact information. Helps a lot when you run into this kind of things especially when starting with shaders.
Yeah but not so simple when you don't get an error . Happens to me a lot from time to time with different things when trying to write shaders/HLSL. P.S. also notice that you're mixing two data types there, fixed and float.
This is orthogonal to what the OP asked, but avoid using custom names for things unless you have a specific reason. For example using _MainColor instead of the more standard _Color might cause problems, since there are things in Unity that rely on things being named a certain way.
Or, you know, just fixed4 col = mainColor; Specifically material.color = myColor; expects the shader to be using _Color, as it's just an alias for material.SetColor("_Color", myColor);. Really, just don't ever use material.color and it won't be a problem. The other place it's expected is with fallback shadowcaster passes and replacement shaders. This is really only an issue with an alpha tested shader, where it's expecting the alpha of the _MainTex is multiplied by _Color.a before doing the clip(). If you're not using alpha testing, or not using a fallback for the shadowcaster and not using the _CameraDepthNormalsTexture, this can also be safely ignored.
Also lightmapping expects certain things to be named in certain ways, although if you are writing completely custom Lightmapped shaders, you might as well write a Meta pass too.
thanks everybody... i just wanted to understand why it didnt work when i seperate the RGBA and make a new float. all good now
@bgolus I thought there might be something going to happen with those values after that specific issue was resolved by the author, so I tried to suggest a fix it with minimal changes. But of course you're absolutely correct.