Study!シェーダー!unityのやつ! FragmentShaderで色をぬってみる!
*FragmentShader!
前回まで、ちらちらとvertexshaderを触ってましたが、今回は気分を変えてFragmentShaderをいじってみます。
こちらは、色を決めるシェーダーになりますので、カラフルな感じで行きたいですね。(>_<)
前回
ってわけでいきなりコードどーん!
Shader "Unlit/minimini" { Properties { _MainTex ("Texture", 2D) = "white" {} _Size ("ShaderSize", Range (0.1, 1)) = 1 } 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; }; sampler2D _MainTex; float4 _MainTex_ST; float _Size; v2f vert (appdata v) { v.vertex.xyz = v.vertex.xyz * _Size ; 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 { // sample the texture //fixed4 col = tex2D(_MainTex, i.uv); fixed4 col =fixed4(1.0,0,0,1.0); // apply fog UNITY_APPLY_FOG(i.fogCoord, col); return col; } ENDCG } } }
前々回で使いました大きさだけ変わるShaderをベースにしました。あれをベースにした理由は手元にあったからです!(>_<)
Unlit/TextureのShaderをベースにすればよいかと思います。
ちなみにこんな感じになります。
赤い!テクスチャーから拾わずに直接まっかっか!って感じになりました(@_@。
でもってコードの変更点は!
fixed4 frag (v2f i) : SV_Target { // sample the texture //fixed4 col = tex2D(_MainTex, i.uv); fixed4 col =fixed4(1.0,0,0,1.0); // apply fog UNITY_APPLY_FOG(i.fogCoord, col); return col; }
こちらの
//fixed4 col = tex2D(_MainTex, i.uv);
を!
fixed4 col =fixed4(1.0,0,0,1.0);
に変更した感じですね。
元々はテクスチャーから取っていた色を、直接入力しています。
fixed4の中身は、左から R G B にαって感じかな?
Red Green Blue とαで透明さを表してるですね。
0~1.0までの少数で、赤青緑の色の量を決めれるみたいです。
今回は Rだけ1.0にしてるので赤いですね。