一区二区三区电影_国产伦精品一区二区三区视频免费_亚洲欧美国产精品va在线观看_国产精品一二三四

聯(lián)系我們 - 廣告服務(wù) - 聯(lián)系電話:
您的當(dāng)前位置: > 關(guān)注 > > 正文

世界報道:世界空間中的著色器 從對象空間到世界空間的轉(zhuǎn)換

來源:CSDN 時間:2023-02-02 09:45:36

在《在著色器中調(diào)試》章節(jié)中提到,頂點輸入?yún)?shù)使用語義詞 POSITION 指定對象的坐標(biāo),即本地對象(模型)的網(wǎng)格空間坐標(biāo),對象空間(對象的坐標(biāo)系統(tǒng))是特定于每個游戲?qū)ο蟮?,然而,所有的游戲?qū)ο髸晦D(zhuǎn)換到一個共同的坐標(biāo)系中(世界空間)。

如果一個對象被直接放入世界空間中,游戲?qū)ο髸ㄟ^轉(zhuǎn)換組件直接將對象的坐標(biāo)轉(zhuǎn)成世界坐標(biāo)。你可以在 Scene 視圖或者 Hierarchy 視圖中選中一個對象,然后在 Inspector 視圖中查看 Transform 組件。在 Transform 組件中包括 Position、Rotation 和 Scale 屬性,這個組件用來指定從本地坐標(biāo)到世界坐標(biāo)的頂點轉(zhuǎn)換(如果一個游戲?qū)ο髶碛懈讣墝ο螅敲崔D(zhuǎn)換組件只轉(zhuǎn)換父級對象的坐標(biāo))。在《Vertex Transformations》章節(jié)中,我們將討論頂點的轉(zhuǎn)換、旋轉(zhuǎn)、縮放以及 4 x 4 矩陣組合變換的細(xì)節(jié)。

回到我們的例子中:從對象空間到世界空間的轉(zhuǎn)換是通過 4 x 4 矩陣來轉(zhuǎn)換的,也叫“模型矩陣”,這個矩陣在 Unity 中通過 uniform 參數(shù) _Object2World 已經(jīng)聲明了:


(資料圖)

Shader "Custom/World Space"

{ SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag //uniform flaot4*4 Object3World //automatic definition of a Unity-specific uniform parameter 自動定義統(tǒng)一值是unity的一個特性 struct vertexOutput  { float4 pos: SV_POSITION; float4 position_in_world_space : TEXCOORD0; }; vertexOutput vert(float4 vertex:POSITION)  { vertexOutput output; output.pos = mul(UNITY_MATRIX_MVP,vertex); output.position_in_world_space = mul(_Object2World,vertex);//物體坐標(biāo)轉(zhuǎn)化為世界坐標(biāo) return output; } float4 frag(vertexOutput input) :COLOR { float dist = distance(input.position_in_world_space,float4(0.0,0.0,0.0,1.0));  //計算片段位置與原點距離(第四個坐標(biāo)永遠(yuǎn)是1) if (dist < 5.0) { return float4(0.0, 1.0, 0.0, 1.0);   //接近 } else return float4(0.3,0.3,0.3,1.0);//遠(yuǎn)離原點 } ENDCG } }

}

更多的 Unity Uniforms

在 Unity 中,定義了幾個和 _Object2World 一樣的 float4x4  矩陣,下面是在系列教程中使用的 uniforms 簡短列表:

uniform float4 _Time, _SinTime, _CosTime; // time valuesuniform float4 _ProjectionParams;// x = 1 or -1 (-1 if projection is flipped)// y = near plane; z = far plane; w = 1/far planeuniform float4 _ScreenParams; // x = width; y = height; z = 1 + 1/width; w = 1 + 1/heightuniform float4 unity_Scale; // w = 1/scale; see _World2Objectuniform float3 _WorldSpaceCameraPos;uniform float4x4 _Object2World; // model matrixuniform float4x4 _World2Object; // inverse model matrix // (all but the bottom-right element have to be scaled // with unity_Scale.w if scaling is important) uniform float4 _LightPositionRange; // xyz = pos, w = 1/rangeuniform float4 _WorldSpaceLightPos0; // position or direction of light sourceuniform float4x4 UNITY_MATRIX_MVP; // model view projection matrix uniform float4x4 UNITY_MATRIX_MV; // model view matrixuniform float4x4 UNITY_MATRIX_V; // view matrixuniform float4x4 UNITY_MATRIX_P; // projection matrixuniform float4x4 UNITY_MATRIX_VP; // view projection matrixuniform float4x4 UNITY_MATRIX_T_MV; // transpose of model view matrixuniform float4x4 UNITY_MATRIX_IT_MV; // transpose of the inverse model view matrixuniform float4x4 UNITY_MATRIX_TEXTURE0; // texture matrixuniform float4x4 UNITY_MATRIX_TEXTURE1; // texture matrixuniform float4x4 UNITY_MATRIX_TEXTURE2; // texture matrixuniform float4x4 UNITY_MATRIX_TEXTURE3; // texture matrixuniform float4 UNITY_LIGHTMODEL_AMBIENT; // ambient color

用戶指定 Uniforms:著色器屬性

還有一個更重要的 uniform  參數(shù):用戶自定義的 uniforms。實際上,這是 Unity 的屬性,你可以認(rèn)為他們是著色器的用戶自定義 uniform 參數(shù)。通常一個著色器不帶參數(shù)只能由編寫的程序員在一些特定的程序中使用,但是如果一個著色器擁有參數(shù)并且還帶有描述性的說明,那么這個著色器就可以被其他人使用,另外,如果你打算出售你的著色器,為著色器的提供參數(shù)會大大增加它的價值。

因為在 Unity 的 ShaderLab  中使用《description of shader properties 》非常不錯,通過下面的例子我們來了解如何使用著色器屬性,首先,我們聲明一個屬性,然后我們再定義一個與屬性名稱相同、類型相同的 uniforms 。

Shader "Cg shading in world space" {   Properties    {      _Point ("a point in world space", Vector) = (0., 0., 0., 1.0)      _DistanceNear ("threshold distance", Float) = 5.0      _ColorNear ("color near to point", Color) = (0.0, 1.0, 0.0, 1.0)      _ColorFar ("color far from point", Color) = (0.3, 0.3, 0.3, 1.0)   }    SubShader    {      Pass       {         CGPROGRAM          #pragma vertex vert           #pragma fragment frag           #include "UnityCG.cginc"          // defines _Object2World and _World2Object         // uniforms corresponding to properties         uniform float4 _Point;         uniform float _DistanceNear;         uniform float4 _ColorNear;         uniform float4 _ColorFar;          struct vertexInput          {            float4 vertex : POSITION;         };         struct vertexOutput          {            float4 pos : SV_POSITION;            float4 position_in_world_space : TEXCOORD0;         };          vertexOutput vert(vertexInput input)          {            vertexOutput output;              output.pos =  mul(UNITY_MATRIX_MVP, input.vertex);            output.position_in_world_space = mul(_Object2World, input.vertex);            return output;         }          float4 frag(vertexOutput input) : COLOR          {            float dist = distance(input.position_in_world_space, _Point);            // computes the distance between the fragment position             // and the position _Point.            if (dist < _DistanceNear)            {               return _ColorNear;             }            else            {               return _ColorFar;             }         }         ENDCG        }   }}

使用 sharedMaterial 我們可以改變所有使用了這個材質(zhì)的對象的參數(shù),如果你只希望改變一個并使用了這個材質(zhì)的對象的參數(shù),那么你應(yīng)該使用 material 。假如你設(shè)置 _Point 屬性為另一個對象的位置信息,這樣你只需要在 Unity 中移動這個對象就可以查看效果了,你可以復(fù)制/粘貼 下面的代碼到一個 C# 腳本中:

using UnityEngine;using System.Collections;[ExecuteInEditMode]public class NewBehaviourScript : MonoBehaviour {public GameObject other;void Update(){if(other != null){GetComponent().sharedMaterial.SetVector("_Point", other.transform.position);}}}

然后我們只需要移動另一個對象(Sphere)就可以看到(Cube)顏色的變化,當(dāng)小球離原點近時,如圖:

當(dāng)小球離原點遠(yuǎn)時,如圖:

責(zé)任編輯:

標(biāo)簽:

相關(guān)推薦:

精彩放送:

新聞聚焦
Top 主站蜘蛛池模板: 金乡县| 梧州市| 乌恰县| 安图县| 鸡西市| 桓仁| 防城港市| 化州市| 延津县| 湘潭县| 调兵山市| 孝昌县| 山东省| 乐业县| 达尔| 当涂县| 大连市| 海宁市| 卢龙县| 闻喜县| 石景山区| 和龙市| 永修县| 高邑县| 陆川县| 平遥县| 五华县| 浦城县| 瑞丽市| 伊春市| 和硕县| 林芝县| 玉田县| 富源县| 洛宁县| 佳木斯市| 夏津县| 滁州市| 岳西县| 出国| 浪卡子县|