一些新东西学习 - Texture3D,Texture2DArray
Texture3D
Texture3D需要先在脚本中创建3D材质,然后赋予shader。
需要DX11支持,和材质采样一样,3D维度上可以被repleat和插值
参考文章:http://blog.csdn.net/wolf96/article/details/46239557
脚本:
using UnityEngine; public class Texture3DTest : MonoBehaviour
{
public Renderer target;
public int size = ; void Start()
{
var tex = new Texture3D(size, size, size, TextureFormat.RGBA32, false);
var colors = new Color[size * size * size];
var k = ; for (int z = ; z < size; z++)
{
for (int y = ; y < size; y++)
{
for (int x = ; x < size; x++, k++)
{
if (z == )
colors[k] = Color.blue;
else
colors[k] = Color.red;
}
}
}
tex.wrapMode = TextureWrapMode.Repeat;
tex.SetPixels(colors);
tex.Apply();
target.material.SetTexture("_MainTexture", tex);
}
}
shader:
Shader "Test/Texture3D"
{
Properties
{
_MainTexture("Texture", 3D) = "" {}
_Z("Z",float)=
} SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc" struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
}; v2f vert(appdata_tan v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
return o;
} sampler3D _MainTexture;
float _Z; float4 frag(v2f i) : COLOR
{
return tex3D(_MainTexture, fixed3(i.uv.x, i.uv.y, _Z));
} ENDCG
}
}
}
shader中类型声明为'3D'
这里只绘制了蓝色和红色两种颜色,最后会被插值:

Texture2DArray
最早在Adam的demo里,地形中用到了这个东西。需要DX10支持
顾名思义,这个可以存放Texture2D的数组
参考:
https://docs.unity3d.com/Manual/SL-TextureArrays.html
https://github.com/keijiro/Texture2DArrayTest
脚本:
using UnityEngine; public class Texture2DArrayTest : MonoBehaviour
{
public Material material;
Texture2DArray mTexture; void Start()
{
mTexture = new Texture2DArray(, , , TextureFormat.RGBA32, false, true); var temp = new Texture2D(, , TextureFormat.RGBA32, false);
for (int x = ; x < temp.width; x++)
for (int y = ; y < temp.height; y++)
temp.SetPixel(x, y, Color.red); mTexture.SetPixels(temp.GetPixels(), ); for (int x = ; x < temp.width; x++)
for (int y = ; y < temp.height; y++)
temp.SetPixel(x, y, Color.blue); mTexture.SetPixels(temp.GetPixels(), ); mTexture.Apply(); material.SetTexture("_TextureArray", mTexture);
}
}
Shader:
Shader "Unlit/NewUnlitShader"
{
Properties
{
_TextureArray("TexArray", 2DArray) = "" {}
_Index("Index",float)=
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#pragma target 3.5 #include "UnityCG.cginc" struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
}; struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
}; sampler2D _MainTex;
float4 _MainTex_ST;
float _Index; UNITY_DECLARE_TEX2DARRAY(_TextureArray); v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
} fixed4 frag (v2f i) : SV_Target
{
fixed4 r = UNITY_SAMPLE_TEX2DARRAY(_TextureArray, float3(i.uv.x, i.uv.y, _Index));
return r;
}
ENDCG
}
}
}
shader中类型声明为2DArray
这里也是放了红蓝两种颜色的图片,分别放在两个索引当中
最终效果:

一些新东西学习 - Texture3D,Texture2DArray的更多相关文章
- ES6 有什么新东西
ES6 有什么新东西? 你可能已经听说过 ECMAScript 6 (简称 ES6)了.ES6 是 Javascript 的下一个版本,它有很多很棒的新特性.这些特性复杂程度各不相同,但对于简单的脚本 ...
- 新Android学习计划
最近,在学习Android Design Support Library提供的新控件过程中,我感受到了原来的学习方式的缺点: 学习内容过于随意,在工作过程中碰到的新问题都想去掌握,心血来潮就想写一篇相 ...
- 微软Ignite2018——微软宣布新的学习平台:Microsoft Learn
Ignite 2018 首日感受 头一次参加美国的微软 Ignite 大会,确实规模比国内的大不少.23日是 MVP & RD 的 Pre Day(MVP即Most Valuable Prof ...
- 什么新东西值得学「GitHub 热点速览 v.22.29」
上周 18k+ 的项目 bun 这周又获得 7k+ star,是时候了解下它背后的编程语言 zig 了,它并不是一门新的语言,伴随着 bun 的风靡,zig 本周也上了 GitHub 热榜.同样,可以 ...
- JavaScript ES6 数组新方法 学习随笔
JavaScript ES6 数组新方法 学习随笔 新建数组 var arr = [1, 2, 2, 3, 4] includes 方法 includes 查找数组有无该参数 有返回true var ...
- 《新年Flag》2019年“新年Flag” - 新目标 学习计划
<新年Flag>2019年"新年Flag" - 新学期 新目标 学习计划 达叔终于等到你了~ 先做个自我介绍: [达叔小生:往后余生,唯独有你]小程序 -> 后端 ...
- Java8 新特性学习 Lambda表达式 和 Stream 用法案例
Java8 新特性学习 Lambda表达式 和 Stream 用法案例 学习参考文章: https://www.cnblogs.com/coprince/p/8692972.html 1.使用lamb ...
- java8 新特性学习笔记
Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...
- [翻译] 预览 C# 10 的新东西
原文: [Introducing C# 10] 作者: Ken Bonny 本周早些时候(译注:原文发表于5月1日),我关注了 Mads Torgersen 在 DotNet SouthWest ...
随机推荐
- length-of-last-word 最后一个单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the le ...
- JNI 引用问题梳理(转)
局部引用: JNI 函数内部创建的 jobject 对象及其子类( jclass . jstring . jarray 等) 对象都是局部引用,它们在 JNI 函数返回后无效: 一般情况下,我们应该依 ...
- Laravel中pluck的使用——返回指定的字段值信息列表
$model = self::where(['is_delete' => 0, 'is_on_sale' => 1]) ->whereIn('goods.cat_id', Goods ...
- 使用Oracle Data Integrator Studio创建资料档案库
一.Creating the Database Schema /*第1步:创建临时表空间 */ create temporary tablespace user_temp tempfile 'C:\a ...
- JUC-Condition和Lock实践-线程按序交替执行
编写一个程序,开启 3 个线程,这三个线程的 ID 分别为 A.B.C,每个线程将自己的 ID 在屏幕上打印 10 遍,要求输出的结果必须按顺序显示.如:ABCABCABC…… 依次递归 这里只使用c ...
- Linux 系统 fstab错误导致系统无法启动的修复
fstab错误的修复 vim /etc/fstab/dev/sda6 /mnt xfs defaults 0 0重启后系统无法启动,等待一段时间后输入root的密码可进入单用户模式,修改fstab后可 ...
- 使用mapreduce来分析网站的log日志
近日,有人和我说分析log日志. 之前,就写过,但是忘了总结了,找了半天也没有找到,看了以后要将东西整理了. 无奈,在网上收拾,看到这个人写的,索性,就搬过来,待我找到我写的,在一块补充一下! 所有网 ...
- Easyui入门视频教程 第05集---Easyui复杂布局
目录 ----------------------- Easyui入门视频教程 第09集---登录完善 图标自定义 Easyui入门视频教程 第08集---登录实现 ajax button的使用 ...
- 【DeepLearning】Exercise:Convolution and Pooling
Exercise:Convolution and Pooling 习题链接:Exercise:Convolution and Pooling cnnExercise.m %% CS294A/CS294 ...
- Spring使用内存数据库
有时候为了做些测试需要创建数据库及相关表,安装MySQL等轻量数据库虽然简单但还是有点麻烦?而且用于自己临时测试的数据库对象一般不会被多次使用,还是浪费?内存数据库结合ORM可以很好解决这个问题. H ...