您的位置:首页 > 移动开发 > Unity3D

[shader]Unity3D地形

2015-12-03 10:20 232 查看
高度细分shader

Shader "QQ/TerrainTessEdge" {
Properties{
_Color("Color", Color) = (1,1,1,1)
_HeightMap("高度图",2D) = "black"{}
_HeightAmount("高度比例",Range(0.0,5.0)) = 0.5
_EdgaLength("细分程度",Range(0.1,100)) = 50
_MainTex("底层图",2D) = "white"{}
_RTex("红色贴图", 2D) = "white" {}
_GTex("绿色贴图", 2D) = "white" {}
_BTex("蓝色贴图", 2D) = "white" {}
_ATex("Alpha贴图", 2D) = "white" {}
_RGBATex("通道图", 2D) = "white" {}
}
SubShader{
Tags { "RenderType" = "Opaque" }
LOD 300
CGPROGRAM
#pragma surface surf BlinnPhong addshadow vertex:disp tessellate:tessEdge//定义了点方法和细分方法
#include "Tessellation.cginc"//unity的细分API
#pragma target 4.0//3.0的贴图有限
struct appdata {
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
float _HeightAmount;
float _EdgaLength;
float4 tessEdge(appdata v0, appdata v1,appdata v2)
{
return UnityEdgeLengthBasedTessCull(v0.vertex, v1.vertex, v2.vertex, _EdgaLength, _HeightAmount * 1.5f);//Unity Tessellation中的方法
}
sampler2D _HeightMap;
void disp(inout appdata v)//绘制顶点
{
float d = tex2Dlod(_HeightMap, float4(v.texcoord.xy, 0, 0)).a * _HeightAmount;
v.vertex.xyz += v.normal*d;
}

sampler2D _MainTex;
sampler2D _RGBATex;
sampler2D _RTex;
sampler2D _GTex;
sampler2D _BTex;
sampler2D _ATex;
struct Input {
float2 uv_MainTex;
float2 uv_RGBATex;
float2 uv_RTex;
float2 uv_GTex;
float2 uv_BTex;
float2 uv_ATex;
};
fixed4 _Color;
void surf(Input IN, inout SurfaceOutput o) {//贴图融合
fixed4 main = tex2D(_MainTex,IN.uv_MainTex);
fixed4 blend = tex2D(_RGBATex, IN.uv_RGBATex);
fixed4 final = main;
final = lerp(final, tex2D(_RTex, IN.uv_RTex), blend.r);
final = lerp(final, tex2D(_GTex, IN.uv_GTex), blend.g);
final = lerp(final, tex2D(_BTex, IN.uv_BTex), blend.b);
final = lerp(final, tex2D(_ATex, IN.uv_ATex), blend.a);
o.Albedo = final.rgb*_Color;
o.Alpha = final.a;
}
ENDCG
}
FallBack "Diffuse"
}


高度置换shader

Shader "QQ/TerrainShaderDisplace" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_HeightMap("高度图",2D) = "black"{}
_HeightAmount("高度比例",Range(0.0,5.0)) = 0.5
_MainTex("底层图",2D) = "white"{}
_RTex("红色贴图", 2D) = "white" {}
_GTex("绿色贴图", 2D) = "white" {}
_BTex("蓝色贴图", 2D) = "white" {}
_ATex("Alpha贴图", 2D) = "white" {}
_RGBATex("通道图", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 300
CGPROGRAM
#pragma surface surf BlinnPhong addshadow vertex:disp nolightmap
//#include "Tessellation.cginc"
#pragma target 4.0
struct appdata {
float4 vertex : POSITION;
float4 tangent : TANGENT;
float3 normal : NORMAL;
float2 texcoord : TEXCOORD0;
};
sampler2D _HeightMap;
float _HeightAmount;
void disp(inout appdata v)
{
float d = tex2Dlod(_HeightMap, float4(v.texcoord.xy, 0, 0)).a * _HeightAmount;
v.vertex.xyz += v.normal*d;
}

sampler2D _MainTex;
sampler2D _RGBATex;
sampler2D _RTex;
sampler2D _GTex;
sampler2D _BTex;
sampler2D _ATex;
struct Input {
float2 uv_MainTex;
float2 uv_RGBATex;
float2 uv_RTex;
float2 uv_GTex;
float2 uv_BTex;
float2 uv_ATex;
};
fixed4 _Color;

void surf (Input IN, inout SurfaceOutput o) {
fixed4 main = tex2D(_MainTex,IN.uv_MainTex);
fixed4 blend = tex2D(_RGBATex, IN.uv_RGBATex);
fixed4 a = tex2D(_ATex, IN.uv_ATex);
fixed4 final= main;
final = lerp(final, tex2D(_RTex, IN.uv_RTex), blend.r);
final = lerp(final, tex2D(_GTex, IN.uv_GTex), blend.g);
final = lerp(final, tex2D(_BTex, IN.uv_BTex), blend.b);
final = lerp(final, tex2D(_ATex, IN.uv_ATex), blend.a);
o.Albedo = final.rgb*_Color;
o.Alpha = final.a;
}
ENDCG
}
FallBack "Diffuse"
}


没什么太难的,参考官方的例子和网上的例子 合成的shader,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  U3DShader