I.はじめに
キューブテクスチャマッピングは、実装環境、立方体集合組織で、6つのイメージで構成されています。
サンプリングは、6つのテクスチャを発生交差すると、立方体の外側中央に拡張することによって開始され、結果が交差点にサンプルから計算されます。
キューブテクスチャの利点:シンプルかつ高速な、新鮮なフルーツが良い、短所は次のとおりです。シーンの変更は、キューブテクスチャを再生成する必要がある、と私たちは、凸体を利用するために必要があるので、自己反射することができないとき。
第二に、スカイボックス
スカイボックスの使用は、キューブテクスチャマッピングです。
ゲームの背景をシミュレートするための方法、テクスチャラップモード6つのニーズが不一致の現象を防止するクランプ、境界値を傍受する必要性を、設定する6つのテクスチャを使用して、そのスカイボックス材料、。
全体的な色を制御するための色合いの色材、空の明るさを調整するための露出ボックス、+のy軸方向の回転角を調整するための回転スカイボックス:3つの属性があります。
スカイボックスは、すべての不透明なオブジェクト、キューブまたは球セグメントの使用の背後にあるグリッドの後にレンダリングされます。
第三に、キューブ環境テクスチャマッピングを作成します
キューブテクスチャマップは、通常、金属材料の環境をシミュレートするために使用されます。
3通りの方法で、キューブ環境テクスチャマッピング方法を作成します。
テクスチャから直接特殊なレイアウトを作成します。1.
その後、我々は、特別なテクスチャレイアウトを提供し、このような立方体としてクロスフロアプラン、パノラマレイアウトを拡張する必要があります。その後、我々はテクスチャタイプは、物理ベースのレンダリングでは、我々は通常、高品質なキューブマップを生成するHDR画像を使用することができますキューブマップに設定されているテクスチャ。
公式には、それがエッジ補正、滑らかで反射、HDRや他の機能のためのテクスチャデータ、およびサポートを圧縮することができ、この方法を使用することをお勧めします。
2.キューブマップリソースのマニュアルを作成し、その後、6図は彼に割り当てられました
スクリプトによって生成された3。
public class RenderCubemapWizard : ScriptableWizard {
public Transform renderFromPosition; //在这个指定的位置动态创建摄像机
public Cubemap cubemap; //获取得出的cubemap
void OnWizardUpdate () { //编辑器更新时调用
helpString = "Select transform to render from and cubemap to render into"; //提示
isValid = (renderFromPosition != null) && (cubemap != null); //设置按钮是否可以点击
}
void OnWizardCreate () { //当向导被打开或只要用户在向导改变了某些东西时被调用。
// create temporary camera for rendering
GameObject go = new GameObject( "CubemapCamera");
go.AddComponent<Camera>();
// place it on the object
go.transform.position = renderFromPosition.position;
// render into cubemap
go.GetComponent<Camera>().RenderToCubemap(cubemap); //渲染出cubemap
// destroy temporary camera
DestroyImmediate( go );//销毁摄像机
}
[MenuItem("GameObject/Render into Cubemap")]
static void RenderCubemap () {
ScriptableWizard.DisplayWizard<RenderCubemapWizard>(
"Render cubemap", "Render!");
}
}
我々は、シーン内のゲームオブジェクト、およびrenderFromPositionインタフェースメニューバー項目を思い付くために割り当てられたコードを作成した後。
そして、(>レガシー>キューブマップを作成します)キューブマップキューブテクスチャを作成し、スムーズなスクリプトは、キューブテクスチャに画像をレンダリングすることができます可能にするために、我々はReadableオプションをチェックする必要があります。
使用した顔の大きさより大きなオプションは、より大きな立方体テクスチャ解像度専用メモリをレンダリングし、得られたテクスチャレンダリングウィンドウコード手段、上記。
第四に、反射
金属反射コーティングは、おそらく方向とこの方向の相関演算におけるテクスチャサンプルの方向を与えるために入射光源の法線方向を表示する反射率を得る考え、同様の効果と同様とすることができます。
Shader "Unity Shaders Book/Chapter 10/Reflection" {
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_ReflectColor ("Reflection Color", Color) = (1, 1, 1, 1) //控制反射颜色
_ReflectAmount ("Reflect Amount", Range(0, 1)) = 1 //控制材质的反射程度
_Cubemap ("Reflection Cubemap", Cube) = "_Skybox" {} //环境映射纹理,就是那个cubemap立方体纹理,在材质中进行替换赋值
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry"} //用于不透明的几何物体。。。
Pass {
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "AutoLight.cginc"
fixed4 _Color;
fixed4 _ReflectColor;
fixed _ReflectAmount;
samplerCUBE _Cubemap;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
fixed3 worldNormal : TEXCOORD1;
fixed3 worldViewDir : TEXCOORD2;
fixed3 worldRefl : TEXCOORD3;
SHADOW_COORDS(4) //用于衰减和阴影计算,三剑客之一
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos);
// Compute the reflect dir in world space
o.worldRefl = reflect(-o.worldViewDir, o.worldNormal); //使用视角方向的反射方向计算光方向
TRANSFER_SHADOW(o); //三剑客之二
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 worldViewDir = normalize(i.worldViewDir);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * max(0, dot(worldNormal, worldLightDir));
// Use the reflect dir in world space to access the cubemap
fixed3 reflection = texCUBE(_Cubemap, i.worldRefl).rgb * _ReflectColor.rgb; //这里i.worldRefl不需要及逆行归一化,因为只取方向
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos); //统一管理那个,算是三剑客之三。。
// Mix the diffuse color with the reflected color
fixed3 color = ambient + lerp(diffuse, reflection, _ReflectAmount) * atten; //这里使用_ReflectAmount进行取值(混合漫发射颜色和反射颜色)
return fixed4(color, 1.0);
}
ENDCG
}
}
FallBack "Reflective/VertexLit"
}
その後、キューブマップキューブテクスチャ後の反射キューブマップでのシェーダ材料における当社のセットの上に。
第五に、屈折
屈折は環境地図のための別の一般的なアプリケーションです。
-------屈折反射角を計算するために、スネルの法則------ N1sinθ1= n2のsinθ2、(角が法線に対して角度であり、N2は、二つの媒質の屈折率n1と、 1真空)は、ガラスが1.5であります
Shader "Unity Shaders Book/Chapter 10/Refraction" {
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_RefractColor ("Refraction Color", Color) = (1, 1, 1, 1) //折射颜色设置
_RefractAmount ("Refraction Amount", Range(0, 1)) = 1 //透射程度
_RefractRatio ("Refraction Ratio", Range(0.1, 1)) = 0.5 //透射比
_Cubemap ("Refraction Cubemap", Cube) = "_Skybox" {} //立方体纹理
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry"} //用于不透明的几何物体。。。
Pass {
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "AutoLight.cginc"
fixed4 _Color;
fixed4 _RefractColor;
float _RefractAmount;
fixed _RefractRatio;
samplerCUBE _Cubemap;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
fixed3 worldNormal : TEXCOORD1;
fixed3 worldViewDir : TEXCOORD2;
fixed3 worldRefr : TEXCOORD3;
SHADOW_COORDS(4)
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos);
// Compute the refract dir in world space
o.worldRefr = refract(-normalize(o.worldViewDir), normalize(o.worldNormal), _RefractRatio);
//计算折射方向(第一个参数入射光线方向,第二个是表面法线,这两个都要归一化,第三个参数是入射光线所在介质的折射率和折射光线所在介质的折射率的比值)
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 worldViewDir = normalize(i.worldViewDir);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * max(0, dot(worldNormal, worldLightDir));
// Use the refract dir in world space to access the cubemap
fixed3 refraction = texCUBE(_Cubemap, i.worldRefr).rgb * _RefractColor.rgb; //使用折射方向对立方体纹理进行采样获取折射颜色
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
// Mix the diffuse color with the refract color
fixed3 color = ambient + lerp(diffuse, refraction, _RefractAmount) * atten; //使用_RefractAmount透射程度对漫反射和折射颜色进行混合取值
return fixed4(color, 1.0);
}
ENDCG
}
}
FallBack "Reflective/VertexLit"
}
その後、キューブマップキューブにおける屈折キューブマップでのシェーダ材料における当社のセットの上にテクスチャ(書籍や間違ったHHH〜)の後に。
第六に、フレネル反射
フレネル反射は、光学現象を発生させることができる反射観察方向の程度に応じて制御することができる
:(立っ湖、水の足はほぼ透明であり、そして距離だけ表面反射現象を見ることができる現象の一例)
と、光、すなわち物体表面に照射したとき、屈折部分の一部が反射または散乱され、の比である反射光の屈折との間の一定の比率、あるフレネル方程式計算。
フレネルの式:F(V、N)= F0 +(1-F0)[(1-V・N) 番目5]
これを使用してアナログと反射光強度の屈折された光強度との境界で変化することができる/乱反射し、水で多くの塗料および他の材料で、それがしばしば使用されます。
Shader "Unity Shaders Book/Chapter 10/Fresnel" {
Properties {
_Color ("Color Tint", Color) = (1, 1, 1, 1)
_FresnelScale ("Fresnel Scale", Range(0, 1)) = 0.5 //调整影响大小的
_Cubemap ("Reflection Cubemap", Cube) = "_Skybox" {}
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry"}
Pass {
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
#include "AutoLight.cginc"
fixed4 _Color;
fixed _FresnelScale;
samplerCUBE _Cubemap;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
float3 worldPos : TEXCOORD0;
fixed3 worldNormal : TEXCOORD1;
fixed3 worldViewDir : TEXCOORD2;
fixed3 worldRefl : TEXCOORD3;
SHADOW_COORDS(4)
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos);
o.worldRefl = reflect(-o.worldViewDir, o.worldNormal); //求出反射方向
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
fixed3 worldViewDir = normalize(i.worldViewDir);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
fixed3 reflection = texCUBE(_Cubemap, i.worldRefl).rgb; //使用反射方向采样
fixed fresnel = _FresnelScale + (1 - _FresnelScale) * pow(1 - dot(worldViewDir, worldNormal), 5); //计算菲涅尔反射公式。
fixed3 diffuse = _LightColor0.rgb * _Color.rgb * max(0, dot(worldNormal, worldLightDir));
fixed3 color = ambient + lerp(diffuse, reflection, saturate(fresnel)) * atten; //使用菲涅尔公式计算出的fresnel对漫反射和反射进行混合取值
return fixed4(color, 1.0);
}
ENDCG
}
}
FallBack "Reflective/VertexLit"
一部も達成直接フレネル反射光と拡散反射光は、アナログエッジ照明効果に重畳乗算します。
拡散反射フレネルスケール物体、0であるエッジ効果を有する光であり、画像が全体的に立方体集合組織で反射され、1です。