質感と素材は切っても切り離せない関係にあります
このセクションの知識構造
実践: モデルにテクスチャを貼り付けるだけです
まず、関連する属性を属性に追加します。
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Main Tex", 2D) = "white" {}//加入纹理
_Specular ("Specular", Color) = (1, 1, 1, 1)
_Gloss ("Gloss", Range(8.0, 256)) = 20
}
次に、SubShaderでそれを参照します
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Specular;
float _Gloss;
このうち、テクスチャ名_STはUVのスケーリングとオフセットを格納するために使用されます。
ここでのUVのスケーリングとオフセットは自分で変更できます。
変更前の効果は以下の通り
ちょっと嫌ですね(
次に、入力構造と出力構造を定義します。
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;//获得纹理模型纹理的纹理坐标
};
struct v2f {
float4 pos : SV_POSITION;
float3 worldNormal : TEXCOORD0;
float3 worldPos : TEXCOORD1;
float2 uv : TEXCOORD2;//传递uv坐标
};
頂点シェーダーの最も重要なコード部分
o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
テクスチャ座標 (2 次元) にテクスチャのスケール + テクスチャの移動値を乗算します。
組み込みのマクロコマンドも使用できます
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
フラグメント シェーダーのテクスチャに関係するコードの最も重要な部分
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
モデルの UV 座標に基づいてテクスチャ上のピクセルの色を取得し、それを環境光で乗算します。
テクスチャのプロパティ
・テクスチャ タイプ、テクスチャをマークするモード。デフォルト、法線マップ、GUI、2DSprite としてマークできます
現在、私にとって最も一般的に使用されているのは、デフォルト、ノーマル、ライト ベイクド マップ、シャドウ マスク、およびシングル チャネルです。
タグの目的は、Unity が特定のことを自動的に処理できるようにすることかもしれません...
・ラップモード
テクスチャ座標が [0,1] の範囲を超えた場合にどのようにタイル化するかを決定します。この本では 2 つのモードについてのみ説明しています
1.繰り返し、テクスチャ座標が1を超える場合、その整数部分は破棄され、小数部分が直接サンプリングされ、結果が連続的に繰り返されます。
2. クランプ、境界ピクセルに従って余分な部分が切り詰められ、サンプリングされます。
・フィルターモード
ポイント では最近傍フィルタリングが使用されます。ズームインまたはズームアウトする場合、サンプリング ピクセルの数は 1 つだけであるため、画像にはピクセル スタイルの効果が生じます。
双線形、 は線形フィルタリングを使用します。ピクセルごとに 4 つの隣接ピクセルがサンプリングされ、線形差分が混合されて最終ピクセルが得られます
見た目も少し良くなりました。
トリリニア フィルタリングは、このオプションがミキシングにミップマップ テクノロジーを使用する点を除いて、Biliear とほぼ同じです。テクスチャにミップマップがない場合、結果は Biliear と同じになります。 。
最大サイズ、テクスチャ サイズとテクスチャの品質はプラットフォームによって異なります。インポートされたテクスチャ サイズが最大を超える場合、Unity はこの最大解像度にスケールします (テクスチャを使用してみてください)解像度サイズが 2 の累乗である場合、スペースとパフォーマンスに影響します)。
形式 は、Unity がテクスチャを保存するために内部的に使用する形式を決定します。あまり高精度のテクスチャを必要とせず、圧縮形式を使用しようとするものもあります。
バンプマッピング。
・質感が高い(使用感が少ない)
利点はより直感的であることですが、欠点は法線マップをグレースケール画像から計算する必要があるため、計算がより複雑になることです。
・通常のテクスチャー
通常、通常のテクスチャは青色です
これは、法線方向の成分範囲が [-1,1] で、ピクセルの成分範囲が [0,1] であるため、式に従ってマッピングする必要があるためです。
このマップは、各点の空間が異なり、法線の大部分が画面の外側を指しているため、それぞれの接線空間内の各点の法線摂動方向を記録します。マッピング関係に従って、0,0 の法線方向など、 1 、マッピング後は RGB(0.5,0.5,1) 水色になります。
Bチャンネルの通常のテクスチャが削除される場合もあります。
この利点は、ストレージ容量を削減できることと、ノーマルの B チャネルを直接計算できることです。
ワールド空間の通常のテクスチャは実際にはほとんど使用されません。
この本では、両者の長所と短所も比較しています。
通常テクスチャの貼り方の練習
・照明などの情報を接空間に伝えて計算を行う
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv.zw = v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
// Compute the matrix that transform directions from tangent space to world space
// Put the world position in w component for optimization
o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
return o;
·
fixed4 frag(v2f i) : SV_Target {
// Get the position in world space
float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
// Compute the light and view dir in world space
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));
// Get the normal in tangent space
fixed3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
bump.xy *= _BumpScale;
bump.z = sqrt(1.0 - saturate(dot(bump.xy, bump.xy)));
// Transform the narmal from tangent space to world space
bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(bump, lightDir));
fixed3 halfDir = normalize(lightDir + viewDir);
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(bump, halfDir)), _Gloss);
return fixed4(ambient + diffuse + specular, 1.0);
}
・接線空間の法線テクスチャをワールド空間に変換して計算します
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv.zw = v.texcoord.xy * _BumpMap_ST.xy + _BumpMap_ST.zw;
///
/// Note that the code below can handle both uniform and non-uniform scales
///
// Construct a matrix that transforms a point/vector from tangent space to world space
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
/*
float4x4 tangentToWorld = float4x4(worldTangent.x, worldBinormal.x, worldNormal.x, 0.0,
worldTangent.y, worldBinormal.y, worldNormal.y, 0.0,
worldTangent.z, worldBinormal.z, worldNormal.z, 0.0,
0.0, 0.0, 0.0, 1.0);
// The matrix that transforms from world space to tangent space is inverse of tangentToWorld
//实际上将法线从切线空间变换到世界空间应该是空间变换矩阵的转置矩阵的逆矩阵,因为切线副切线
//法线是正交的,因此直接用这三个坐标轴构成的矩阵作为变换矩阵是可行的
float3x3 worldToTangent = inverse(tangentToWorld);
*/
//wToT = the inverse of tToW = the transpose of tToW as long as tToW is an orthogonal matrix.
float3x3 worldToTangent = float3x3(worldTangent, worldBinormal, worldNormal);
// Transform the light and view dir from world space to tangent space
o.lightDir = mul(worldToTangent, WorldSpaceLightDir(v.vertex));
o.viewDir = mul(worldToTangent, WorldSpaceViewDir(v.vertex));
///
/// Note that the code below can only handle uniform scales, not including non-uniform scales
///
// Compute the binormal
// float3 binormal = cross( normalize(v.normal), normalize(v.tangent.xyz) ) * v.tangent.w;
// // Construct a matrix which transform vectors from object space to tangent space
// float3x3 rotation = float3x3(v.tangent.xyz, binormal, v.normal);
// Or just use the built-in macro
// TANGENT_SPACE_ROTATION;
//
// // Transform the light direction from object space to tangent space
// o.lightDir = mul(rotation, normalize(ObjSpaceLightDir(v.vertex))).xyz;
// // Transform the view direction from object space to tangent space
// o.viewDir = mul(rotation, normalize(ObjSpaceViewDir(v.vertex))).xyz;
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);
// Get the texel in the normal map
fixed4 packedNormal = tex2D(_BumpMap, i.uv.zw);
fixed3 tangentNormal;
// If the texture is not marked as "Normal map"
// tangentNormal.xy = (packedNormal.xy * 2 - 1) * _BumpScale;
// tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
// Or mark the texture as "Normal map", and use the built-in funciton
tangentNormal = UnpackNormal(packedNormal);
tangentNormal.xy *= _BumpScale;
tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));//法线切线的z可以通过xy计算出来,因为我们方向进行了归一化,其模长为1
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, tangentLightDir));
fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(tangentNormal, halfDir)), _Gloss);
return fixed4(ambient + diffuse + specular, 1.0);
}
法線テクスチャを使用する場合は、テクスチャ タイプを法線マップに設定してください
設定後、Unity はさまざまなプラットフォームに応じてテクスチャを圧縮します。
グラデーションテクスチャ
NPR でよく使用されます。このテクスチャは、法線と照明方向の相対関係に従ってテクスチャ上でサンプリングして、照明と影の全体的なカラー スタイルを制御することができ、NPR で影のカラー ブロックをシミュレートするために使用されます。
プロパティでインポートする
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_RampTex ("Ramp Tex", 2D) = "white" {}
_Specular ("Specular", Color) = (1, 1, 1, 1)
_Gloss ("Gloss", Range(8.0, 256)) = 20
}
sampler2D _RampTex;
float4 _RampTex_ST;
頂点シェーダーが UV 座標を取得します
o.uv = TRANSFORM_TEX(v.texcoord, _RampTex);
フラグメント シェーダは、拡散ライティング情報を使用して 2 次元座標を構築し、Ramp で対応するピクセル カラーを見つけます。
fixed4 frag(v2f i) : SV_Target {
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
// Use the texture to sample the diffuse color
fixed halfLambert = 0.5 * dot(worldNormal, worldLightDir) + 0.5;
fixed3 diffuseColor = tex2D(_RampTex, fixed2(halfLambert, halfLambert)).rgb*_Color.rgb;
fixed3 diffuse = _LightColor0.rgb * diffuseColor;
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
fixed3 halfDir = normalize(worldLightDir + viewDir);
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(worldNormal, halfDir)), _Gloss);
return fixed4(ambient + diffuse + specular, 1.0);
}
マスクのテクスチャです。
マスク テクスチャには、環境光マスク、ハイライト マスクなど、さまざまな種類があります。
マスキングの原理は、ソース ピクセルとマスク ピクセルの積として一文に要約できます。
この本ではハイライトマスキングについて詳しく説明しています。
まずは属性です
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_MainTex ("Main Tex", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump" {}
_BumpScale("Bump Scale", Float) = 1.0
_SpecularMask ("Specular Mask", 2D) = "white" {}
_SpecularScale ("Specular Scale", Float) = 1.0
_Specular ("Specular", Color) = (1, 1, 1, 1)
_Gloss ("Gloss", Range(8.0, 256)) = 20
}
SubShader のプロパティを参照する
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float _BumpScale;
sampler2D _SpecularMask;
float _SpecularScale;
fixed4 _Specular;
float _Gloss;
ここでは _MainTex_ST を 1 つだけ定義していますが、メイン テクスチャのオフセットとスケーリングを変更すると、同時に 3 つのテクスチャのサンプリングに影響します。頂点シェーダで使用するデルタレジスタの数を減らすことができます。これは、テクスチャのスケーリングと変換を行う必要がない場合、または多くのテクスチャで同じスケーリングと変換を使用できる場合、つまり、複数のテクスチャを使用して同じテクスチャを変更する場合に実行できます。
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
TANGENT_SPACE_ROTATION;
o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz;
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);
fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uv));
tangentNormal.xy *= _BumpScale;
tangentNormal.z = sqrt(1.0 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, =tangentLightDir));
fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
// Get the mask value
fixed specularMask = tex2D(_SpecularMask, i.uv).r * _SpecularScale;
//不少游戏会将多种纹理(非颜色主纹理)放在多个通道中,因此使用的时候只需要取一个通道的值就可以了,但要知道每个通道具体的含义
// Compute specular term with the specular mask
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(tangentNormal, halfDir)), _Gloss) * specularMask;
return fixed4(ambient + diffuse + specular, 1.0);
}