五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Unity-3D 紋理

2021-03-28 12:01 作者:unity_某某師_高錦錦  | 我要投稿

3D 紋理是位圖圖像,其中包含三維信息,而不是標準的二維信息。3D 紋理通常用于仿真諸如霧或煙的體積效果,模擬體積 3D 網(wǎng)格,或存儲動畫紋理并在這些動畫紋理之間平滑混合。

在 Unity 項目中,Unity Editor 將 3D 紋理表示為紋理資源。要配置紋理資源的導入設置,可選擇該紋理資源并使用?Inspector,或者編寫一個使用?TextureImporter?API 的腳本。

在 Unity 引擎中,Unity 使用?Texture3D?類來表示 3D 紋理。使用此類可以在 C# 腳本中與 3D 紋理進行交互。

3D 紋理大小

3D 紋理的最大分辨率為 2048 x 2048 x 2048。

請注意,隨著分辨率的提高,3D 紋理在內存中和磁盤上的大小會迅速增加。一個沒有 Mipmap 且分辨率為 16 x 16 x 16 的 RGBA32 3D 紋理具有 128KB 的大小,而分辨率為 256 x 256 x 256 時則具有 512MB 的大小。

創(chuàng)建 3D 紋理

要在項目中創(chuàng)建 3D 紋理,必須使用腳本。

下面的示例是一個 Editor 腳本,該腳本創(chuàng)建一個 Texture3D 類的實例,用顏色數(shù)據(jù)填充該實例,然后作為紋理資源保存到項目中。

using UnityEditor; 
using UnityEngine; 
public class ExampleEditorScript : MonoBehaviour { ? ?
 ?[MenuItem("CreateExamples/3DTexture")] ? ?
 ?static void CreateTexture3D(){ ? ? ? ?
 ? ?// 配置紋理 ? ? ? ?
 ? ?int size = 32; ? ? ? ?
 ? ?TextureFormat format = TextureFormat.RGBA32; ? ? ? ?
 ? ?TextureWrapMode wrapMode = ?TextureWrapMode.Clamp; ? ? ? ?
 ? ?// 創(chuàng)建紋理并應用配置 ? ? ? ?
 ? ?Texture3D texture = new Texture3D(size, size, size, format, false);
 ? ?texture.wrapMode = wrapMode; ? ? ? ?
 ? ?// 創(chuàng)建 3 維數(shù)組以存儲顏色數(shù)據(jù) ? ? ? ?
 ? ?Color[] colors = new Color[size * size * size]; ? ? ? ?
 ? ?// 填充數(shù)組,使紋理的 x、y 和 z 值映射為紅色、藍色和綠色 ? ? ? ?
 ? ?float inverseResolution = 1.0f / (size - 1.0f); ? ? ? ?
 ? ?for (int z = 0; z < size; z++){
 ? ? ?int zOffset = z * size * size;
 ? ? ?for (int y = 0; y < size; y++)
 ? ? ?{
 ? ? ? ?int yOffset = y * size;
 ? ? ? ?for (int x = 0; x < size; x++){
 ? ? ? ? ?colors[x + yOffset + zOffset] = new Color(x * inverseResolution, ? ? ? ? ? ? ?y * inverseResolution, z * inverseResolution, 1.0f);
 ? ? ? ?} ? ? ? ? ? ?
 ? ? ?} ? ? ? ?
 ? ?} ? ? ? ?
 ? ?// 將顏色值復制到紋理
 ? ?texture.SetPixels(colors);
 ? ?// 將更改應用到紋理,然后將更新的紋理上傳到 GPU
 ? ?texture.Apply();
 ? ?// 將紋理保存到 Unity 項目
 ? ?AssetDatabase.CreateAsset(texture, "Assets/Example3DTexture.asset"); ? ?
 ?} 
}

在著色器中使用 3D 紋理

下面是一個使用 3D 紋理來可視化體積的簡單光線追蹤 (Raymarching) 著色器示例。

Shader "Unlit/VolumeShader" { ? ?
 ?Properties{ ? ? ? ?
 ? ?_MainTex ("Texture", 3D) = "white" {}
 ? ?_Alpha ("Alpha", float) = 0.02
 ? ?_StepSize ("Step Size", float) = 0.01
 ?}
 ?SubShader{
 ? ?Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
 ? ?Blend One OneMinusSrcAlpha
 ? ?LOD 100
 ? ?Pass{
 ? ? ?CGPROGRAM
 ? ? ?#pragma vertex vert
 ? ? ?#pragma fragment frag
 ? ? ?#include "UnityCG.cginc"
 ? ? ?// 最大光線追蹤樣本數(shù)
 ? ? ?#define MAX_STEP_COUNT 128
 ? ? ?// 允許的浮點數(shù)誤差
 ? ? ?#define EPSILON 0.00001f
 ? ? ?struct appdata{
 ? ? ? ?float4 vertex : POSITION;
 ? ? ?}; 
 ? ? ?struct v2f{
 ? ? ? ?float4 vertex : SV_POSITION;
 ? ? ? ?float3 objectVertex : TEXCOORD0;
 ? ? ? ?float3 vectorToSurface : TEXCOORD1;
 ? ? ?};
 ? ? ?sampler3D _MainTex;
 ? ? ?float4 _MainTex_ST;
 ? ? ?float _Alpha;
 ? ? ?float _StepSize;
 ? ? ?v2f vert (appdata v){
 ? ? ? ?v2f o;
 ? ? ? ?// 對象空間中的頂點將成為光線追蹤的起點
 ? ? ? ?o.objectVertex = v.vertex;
 ? ? ? ?// 計算世界空間中從攝像機到頂點的矢量
 ? ? ? ?float3 worldVertex = mul(unity_ObjectToWorld, v.vertex).xyz;
 ? ? ? ?o.vectorToSurface = worldVertex - _WorldSpaceCameraPos;
 ? ? ? ?o.vertex = UnityObjectToClipPos(v.vertex);
 ? ? ? ?return o;
 ? ? ?}
 ? ? ?float4 BlendUnder(float4 color, float4 newColor){
 ? ? ? ?color.rgb += (1.0 - color.a) * newColor.a * newColor.rgb;
 ? ? ? ?color.a += (1.0 - color.a) * newColor.a;
 ? ? ? ?return color;
 ? ? ?}
 ? ? ?fixed4 frag(v2f i) : SV_Target{
 ? ? ? ?// 開始在對象的正面進行光線追蹤
 ? ? ? ?float3 rayOrigin = i.objectVertex;
 ? ? ? ?// 使用攝像機到對象表面的矢量獲取射線方向
 ? ? ? ?float3 rayDirection = mul(unity_WorldToObject, float4(normalize(i.vectorToSurface), 1));
 ? ? ? ?float4 color = float4(0, 0, 0, 0);
 ? ? ? ?float3 samplePosition = rayOrigin;
 ? ? ? ?// 穿過對象空間進行光線追蹤
 ? ? ? ?for (int i = 0; i < MAX_STEP_COUNT; i++){
 ? ? ? ? ?// 僅在單位立方體邊界內累積顏色
 ? ? ? ? ?if(max(abs(samplePosition.x), max(abs(samplePosition.y), abs(samplePosition.z))) < 0.5f + EPSILON)
 ? ? ? ? ? ?sampledColor = tex3D(_MainTex, samplePosition + float3(0.5f, 0.5f, 0.5f))
 ? ? ? ? ?sampledColor.a *= _Alpha;
 ? ? ? ? ?color = BlendUnder(color, sampledColor);
 ? ? ? ? ?samplePosition += rayDirection * _StepSize;
 ? ? ? ?}
 ? ? ?}
 ? ? ?return color;
 ? ?}
 ? ?ENDCG
 ?}
}
}

如果將此著色器用于在頁面頂部的示例中創(chuàng)建的 3D 紋理,則結果將如下所示:


Unity-3D 紋理的評論 (共 條)

分享到微博請遵守國家法律
五台县| 峨眉山市| 荔波县| 磐石市| 衡东县| 龙泉市| 东乌珠穆沁旗| 吉木乃县| 吉水县| 滁州市| 甘德县| 简阳市| 阳东县| 汪清县| 六安市| 安化县| 东辽县| 重庆市| 敖汉旗| 浑源县| 竹溪县| 碌曲县| 雅安市| 遵义市| 乳山市| 大埔县| 柘荣县| 平陆县| 万安县| 罗田县| 灵台县| 行唐县| 沁水县| 垦利县| 昌吉市| 平原县| 定兴县| 蒲江县| 沾益县| 博爱县| 新化县|