【发布时间】:2017-06-08 19:35:51
【问题描述】:
我的表面着色器的主要纹理是一个谷歌地图图像瓦片,类似于这样: .
我想用单独纹理中的像素替换接近指定颜色的像素。现在的工作如下:
Shader "MyShader"
{
Properties
{
_MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
_GrassTexture("Grass Texture", 2D) = "white" {}
_RoadTexture("Road Texture", 2D) = "white" {}
_WaterTexture("Water Texture", 2D) = "white" {}
}
SubShader
{
Tags{ "Queue" = "Transparent-1" "IgnoreProjector" = "True" "ForceNoShadowCasting" = "True" "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert alpha approxview halfasview noforwardadd nometa
uniform sampler2D _MainTex;
uniform sampler2D _GrassTexture;
uniform sampler2D _RoadTexture;
uniform sampler2D _WaterTexture;
struct Input
{
float2 uv_MainTex;
};
void surf(Input IN, inout SurfaceOutput o)
{
fixed4 ct = tex2D(_MainTex, IN.uv_MainTex);
// if the red (or blue) channel of the pixel is within a
// specific range, get either a 1 or a 0 (true/false).
int grassCond = int(ct.r >= 0.45) * int(0.46 >= ct.r);
int waterCond = int(ct.r >= 0.14) * int(0.15 >= ct.r);
int roadCond = int(ct.b >= 0.23) * int(0.24 >= ct.b);
// if none of the above conditions is a 1, then we want to keep our
// current pixel's color:
half defaultCond = 1 - grassCond - waterCond - roadCond;
// get the pixel from each texture, multiple by their check condition
// to get:
// fixed4(0,0,0,0) if this isn't the right texture for this pixel
// or fixed4(r,g,b,1) from the texture if it is the right pixel
fixed4 grass = grassCond * tex2D(_GrassTexture, IN.uv_MainTex);
fixed4 water = waterCond * tex2D(_WaterTexture, IN.uv_MainTex);
fixed4 road = roadCond * tex2D(_RoadTexture, IN.uv_MainTex);
fixed4 def = defaultCond * ct; // just used the MainTex pixel
// then use the found pixels as the Albedo
o.Albedo = (grass + road + water + def).rgb;
o.Alpha = 1;
}
ENDCG
}
Fallback "None"
}
这是我编写的第一个着色器,它的性能可能不是很好。对我来说,在每个纹理上为每个像素调用 tex2D 以丢弃这些数据似乎违反直觉,但如果没有 if/else(我读到这对 GPU 不利),我想不出更好的方法来做到这一点。
这是一个 Unity 表面着色器,而不是片段/顶点着色器。我知道幕后有一个步骤会为我生成片段/顶点着色器(添加场景的照明、雾等)。此着色器应用于 100 个 256x256 像素的地图图块(总共 2560x2560 像素)。草/道路/水的纹理也都是 256x256 像素。
我的问题是:有没有更好、更高效的方式来完成我在这里所做的事情?该游戏可在 Android 和 iOS 上运行。
【问题讨论】:
-
您是将 256x256 源图块转换为 256x256 目标图块,还是结果应该很大?
-
结果为 256x256
标签: android ios unity3d opengl-es shader