Shader "Custom/UnwrapShader" { Properties { _MainTex("Albedo", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } Pass { Cull Off Lighting Off ZWrite Off ZTest Off Fog { Mode Off } CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct a2v { float4 pos : POSITION; // The vertex position in model space. float3 normal : NORMAL; // The vertex normal in model space. float2 txcoord : TEXCOORD0; }; struct v2f { float4 hpos : SV_POSITION; float2 txcoord : TEXCOORD0; float4 col : COLOR0; float4 wpos : TEXCOORD1; float4 wnormal : TEXCOORD2; }; float4 _eyeWPOS; float4 _lightWPOS; float4x4 lightMatrix; sampler2D _MainTex; int _materialNum; int _materialNo; int _objectNo; float _UseRim; v2f vert( a2v v) { v2f OUT; float4x4 modelMatrix = _Object2World; float4x4 modelMatrixInverse = _World2Object; // convert texture coordinates to NDC position [-1, 1] OUT.hpos.xy = float2(2.0*v.txcoord.x-1.0, -(2.0*v.txcoord.y-1.0)); OUT.hpos.z = 0; OUT.hpos.w = 1; // ワールド座標系での頂点・法線 OUT.wpos = mul(modelMatrix, v.pos); float3 wNormal = mul((float3x3)modelMatrix, v.normal); OUT.wnormal.xyz = 0.5+0.5*(normalize(wNormal)); // 念のため0~1へ // diffuse lighting float3 N = normalize(mul((float3x3) lightMatrix, v.normal)); float3 L = normalize(-mul(lightMatrix, v.pos).xyz); float diffuse = max(0, dot(N, L)); OUT.col = diffuse; OUT.txcoord = v.txcoord; return OUT; } float4 frag( v2f input) : COLOR { // オブジェクト番号でマスクする場合 // if (_objectNo != 2) discard; // float4 color = (float)(_materialNo+1)/(float)_materialNum; // マテリアル番号でマスクする場合 // if (_materialNo != 3) discard; // rim lighting float3 V = normalize(_eyeWPOS.xyz - input.wpos.xyz/input.wpos.w); float3 N = normalize(2.0*input.wnormal.xyz-1.0); float dotVN = 1.0-dot(V, N); // diffuse色の取得 float4 color = tex2D(_MainTex, input.txcoord.xy); // リムライティングを使用する場合 if (_UseRim > 0.5) { return color*(input.col+dotVN); } return color*(input.col); } ENDCG } } }