地形混合shader
1.四个贴图混合
Shader "Custom/BlendTex_surface" {
Properties {
_RTexture("Red Channel Texture", 2D) = "" {}
_GTexture("Green Channel Texture", 2D) = "" {}
_BTexture("Blue Channel Texture", 2D) = "" {}
_ATexture("Alpha Channel Texture", 2D) = "" {}
_Mask("Mask(RG)",2D) = ""{}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD CGPROGRAM
#pragma surface surf Lambert
#pragma target 4.0 sampler2D _RTexture;
sampler2D _GTexture;
sampler2D _BTexture;
sampler2D _ATexture; sampler2D _Mask; struct Input {
float2 uv_RTexture;
float2 uv_GTexture;
float2 uv_BTexture;
float2 uv_ATexture;
float2 uv_Mask;
}; void surf (Input IN, inout SurfaceOutput o) {
float4 rTexData = tex2D(_RTexture, IN.uv_RTexture);
float4 gTexData = tex2D(_GTexture, IN.uv_GTexture);
float4 bTexData = tex2D(_BTexture, IN.uv_BTexture);
float4 aTexData = tex2D(_ATexture, IN.uv_ATexture);
float4 blendData = tex2D(_Mask, IN.uv_Mask); float4 finalColor;
//根据blendData.g 将 RTexture 和 GTexture 混合
finalColor = lerp(rTexData, gTexData, blendData.g);//原本为g
//根据blendData.b 将 BTexture 混合
finalColor = lerp(finalColor, bTexData, blendData.b);//原本为b
//根据blendData.a 将 ATexture 混合
finalColor = lerp(finalColor, aTexData , blendData.a);//原本为a
finalColor = saturate(finalColor);
o.Albedo = finalColor.rgb;
o.Alpha = finalColor.a;
}
ENDCG
}
FallBack "Diffuse"
}
BlendTex_surface
Shader "Custom/BlendTex_vertfrag"
{
Properties
{
Tex0 ("Layer 0 (R)", 2D) = "white" {}
Tex1 ("Layer 1 (G)", 2D) = "white" {}
Tex2 ("Layer 2 (G)", 2D) = "white" {}
Tex3 ("Layer 3 (A)", 2D) = "white" {}
_Control ("Control (RGBA)", 2D) = "red" {}
_IllumFactor ("Illumin Factor", Range(,)) =
} SubShader
{
Tags{ "Queue" = "Geometry+110" }
Pass
{ Tags{"LightMode" = "ForwardBase" } CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc" sampler2D _Control;
sampler2D Tex0;
sampler2D Tex1;
sampler2D Tex2;
sampler2D Tex3;
half _IllumFactor; half4 Tex0_ST;
half4 Tex1_ST;
half4 Tex2_ST;
half4 Tex3_ST;
half4 _Control_ST; struct v2f
{
float4 pos : SV_POSITION;
half2 uv_Tex0:TEXCOORD0;
half2 uv_Tex1:TEXCOORD1;
half2 uv_Tex2:TEXCOORD2;
half2 uv_Tex3:TEXCOORD3;
half2 uv_Control:TEXCOORD4;
}; struct appdata
{
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
}; v2f vert (appdata v)
{
float4 worldPos = mul(unity_ObjectToWorld, v.vertex); v2f o; o.uv_Control = v.texcoord.xy;
o.uv_Tex0 = TRANSFORM_TEX(v.texcoord ,Tex0);
o.uv_Tex1 = TRANSFORM_TEX(v.texcoord ,Tex1);
o.uv_Tex2 = TRANSFORM_TEX(v.texcoord, Tex2);
o.uv_Tex3 = TRANSFORM_TEX(v.texcoord, Tex3);
o.pos = mul(UNITY_MATRIX_MVP, float4(v.vertex.xyz, )); return o;
} fixed4 frag(v2f i) : COLOR
{
fixed4 splat_control = tex2D (_Control, i.uv_Control.xy); fixed4 albedo = fixed4(,,,);
fixed4 Tex0Color = tex2D(Tex0, i.uv_Tex0);
//albedo.rgb = splat_control.r * Tex0Color.rgb;...用这个会出现不明黑色阴影
fixed4 Tex1Color = tex2D(Tex1, i.uv_Tex1);
//albedo.rgb += splat_control.g * Tex1Color.rgb; fixed4 Tex2Color = tex2D(Tex2, i.uv_Tex2).rgba;
//albedo.rgb += splat_control.b * Tex2Color;
fixed4 Tex3Color = tex2D(Tex3, i.uv_Tex3).rgba;
//albedo.rgb += splat_control.a * Tex3Color; albedo = lerp(Tex0Color,Tex1Color, splat_control.g);
albedo = lerp(albedo,Tex2Color, splat_control.b);
albedo = lerp(albedo,Tex3Color, splat_control.a); return albedo* _IllumFactor;
} ENDCG
}
}
FallBack "Diffuse"
}
BlendTex_vertfrag
2.四个贴图(加法线)混合
Shader "Custom/BlendNormalTex_surface" {
Properties {
_RTexture("Red Channel Texture", 2D) = "" {}
_GTexture("Green Channel Texture", 2D) = "" {}
_BTexture("Blue Channel Texture", 2D) = "" {}
_ATexture("Alpha Channel Texture", 2D) = "" {} _RNormalTex ("RNormal Map", 2D) = "bump" {}
_GNormalTex ("GNormal Map", 2D) = "bump" {}
_BNormalTex ("BNormal Map", 2D) = "bump" {}
_ANormalTex ("ANormal Map", 2D) = "bump" {} _RNormalIntensity ("RNormal Map Intensity", Range(-,)) =
_GNormalIntensity ("GNormal Map Intensity", Range(-,)) =
_BNormalIntensity ("BNormal Map Intensity", Range(-,)) =
_ANormalIntensity ("ANormal Map Intensity", Range(-,)) =
_IllumFactor ("Illumin Factor", Range(,)) = _Mask("Mask(RG)",2D) = ""{}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD CGPROGRAM
#pragma surface surf Lambert
// #pragma surface surf Lambert vertex:vert
#pragma target 4.0 sampler2D _RTexture,_GTexture,_BTexture,_ATexture;
sampler2D _RNormalTex,_GNormalTex,_BNormalTex,_ANormalTex;
float _RNormalIntensity,_GNormalIntensity,_BNormalIntensity,_ANormalIntensity;
float _IllumFactor; sampler2D _Mask; struct Input {
float2 uv_RTexture;
float2 uv_GTexture;
float2 uv_BTexture;
float2 uv_ATexture;
float2 uv_RNormalTex;
float2 uv_GNormalTex;
float2 uv_BNormalTex;
float2 uv_ANormalTex;
float2 uv_Mask;
// float3 m_normal;
};
void surf (Input IN, inout SurfaceOutput o) {
float4 rTexData = tex2D(_RTexture, IN.uv_RTexture);
float4 gTexData = tex2D(_GTexture, IN.uv_GTexture);
float4 bTexData = tex2D(_BTexture, IN.uv_BTexture);
float4 aTexData = tex2D(_ATexture, IN.uv_ATexture);
float4 blendData = tex2D(_Mask, IN.uv_Mask); float4 finalColor;
//根据blendData.g 将 RTexture 和 GTexture 混合
finalColor = lerp(rTexData, gTexData, blendData.g);
//根据blendData.b 将 BTexture 混合
finalColor = lerp(finalColor, bTexData, blendData.b);
//根据blendData.a 将 ATexture 混合
finalColor = lerp(finalColor, aTexData , blendData.a);
finalColor = saturate(finalColor); float3 rNormalMap = UnpackNormal(tex2D(_RNormalTex, IN.uv_RNormalTex));
rNormalMap.xy*=_RNormalIntensity;
float3 gNormalMap = UnpackNormal(tex2D(_GNormalTex, IN.uv_GNormalTex));
gNormalMap.xy*=_GNormalIntensity;
float3 bNormalMap = UnpackNormal(tex2D(_BNormalTex, IN.uv_BNormalTex));
bNormalMap.xy*=_BNormalIntensity;
//bNormalMap.z=sqrt(1.0-saturate(dot(bNormalMap.xy,bNormalMap.xy)));这个似乎并没有效果
float3 aNormalMap = UnpackNormal(tex2D(_ANormalTex, IN.uv_ANormalTex));
aNormalMap.xy*=_ANormalIntensity; float3 finalNormal;
//根据blendData.g 将 RTexture 和 GTexture 混合
finalNormal = lerp(rNormalMap, gNormalMap, blendData.g);
//根据blendData.b 将 BTexture 混合
finalNormal = lerp(finalNormal, bNormalMap, blendData.b);
//根据blendData.a 将 ATexture 混合
finalNormal = lerp(finalNormal, aNormalMap , blendData.a); o.Albedo =finalColor*_IllumFactor;
o.Alpha = finalColor.a;
o.Normal=finalNormal;
//o.Normal=rNormalMap*blendData.r+gNormalMap*blendData.g+bNormalMap*blendData.b+aNormalMap*blendData.a;
}
ENDCG
}
FallBack "Diffuse"
}
BlendNormalTex_surface
Shader "Custom/BlendNormalTex_vertfrag"
{
Properties
{
Tex0 ("Layer 0 (R)", 2D) = "white" {}
Tex1 ("Layer 1 (G)", 2D) = "white" {}
Tex2 ("Layer 2 (G)", 2D) = "white" {}
Tex3 ("Layer 3 (A)", 2D) = "white" {} _Normal0("Normal 0 (A)", 2D) = "bump" {}
_Normal1("Normal 1 (B)", 2D) = "bump" {}
_Normal2("Normal 2 (G)", 2D) = "bump" {}
_Normal3("Normal 3 (R)", 2D) = "bump" {} _BumpScale0("BumpScale 0", Range(-1.0,1.0)) = 1.0
_BumpScale1("BumpScale 1", Range(-1.0, 1.0)) = 1.0
_BumpScale2("BumpScale 2", Range(-1.0, 1.0)) = 1.0
_BumpScale3("BumpScale 3", Range(-1.0, 1.0)) = 1.0
_Control ("Control (RGBA)", 2D) = "red" {}
_IllumFactor ("Illumin Factor", Range(,)) =
} SubShader
{
Tags{ "Queue" = "Geometry+110" }
Pass
{ Tags{"LightMode" = "ForwardBase" } CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc"
#include "Lighting.cginc" sampler2D _Control;
//贴图及纹理
sampler2D Tex0,Tex1,Tex2,Tex3;
half4 Tex0_ST,Tex1_ST,Tex2_ST,Tex3_ST;
//法线贴图及纹理
sampler2D _Normal0,_Normal1,_Normal2,_Normal3;
half4 _Normal0_ST,_Normal1_ST,_Normal2_ST,_Normal3_ST;
half _BumpScale0, _BumpScale1, _BumpScale2, _BumpScale3; half _IllumFactor;
half4 _Control_ST; struct v2f
{
float4 pos : SV_POSITION;
float3 normal:NORMAL;
float4 tangnent:TANGENT;
half4 uv_Tex0:TEXCOORD0;
half4 uv_Tex1:TEXCOORD1;
half4 uv_Tex2:TEXCOORD2;
half4 uv_Tex3:TEXCOORD3;
half2 uv_Control:TEXCOORD4;
half3 lightDir:TEXCOORD5;
half3 viewDir:TEXCOORD6;
}; struct appdata
{
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
float4 tangent:TANGENT;
float3 normal:NORMAL;
}; v2f vert (appdata v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv_Control = v.texcoord.xy;
o.uv_Tex0.xy = TRANSFORM_TEX(v.texcoord ,Tex0);
o.uv_Tex0.zw = TRANSFORM_TEX(v.texcoord ,_Normal0); o.uv_Tex1.xy = TRANSFORM_TEX(v.texcoord ,Tex1);
o.uv_Tex1.zw = TRANSFORM_TEX(v.texcoord ,_Normal1); o.uv_Tex2.xy = TRANSFORM_TEX(v.texcoord, Tex2);
o.uv_Tex2.zw = TRANSFORM_TEX(v.texcoord ,_Normal2); o.uv_Tex3.xy = TRANSFORM_TEX(v.texcoord, Tex3);
o.uv_Tex3.zw = TRANSFORM_TEX(v.texcoord ,_Normal3); TANGENT_SPACE_ROTATION;
o.lightDir=mul(rotation,ObjSpaceLightDir(v.vertex)).xyz;
o.viewDir=mul(rotation,ObjSpaceViewDir(v.vertex)).xyz; return o;
}
fixed4 frag(v2f i):SV_Target
{
fixed3 tangentLightDir=normalize(i.lightDir);
fixed3 tangentViewDir=normalize(i.viewDir);//暂时没用到,用于补充高光 fixed4 splat_control = tex2D (_Control, i.uv_Control.xy); fixed3 tangentNormal0=UnpackNormal(tex2D(_Normal0, i.uv_Tex0.zw));
tangentNormal0.xy*=_BumpScale0; fixed3 tangentNormal1=UnpackNormal(tex2D(_Normal1, i.uv_Tex1.zw));
tangentNormal1.xy*=_BumpScale1; fixed3 tangentNormal2=UnpackNormal(tex2D(_Normal2, i.uv_Tex2.zw));
tangentNormal2.xy*=_BumpScale2; fixed3 tangentNormal3=UnpackNormal(tex2D(_Normal3, i.uv_Tex3.zw));
tangentNormal3.xy*=_BumpScale3; fixed4 albedo = 0.0f; fixed4 Tex0Color = tex2D(Tex0, i.uv_Tex0);
fixed4 Tex1Color = tex2D(Tex1, i.uv_Tex1);
fixed4 Tex2Color = tex2D(Tex2, i.uv_Tex2);
fixed4 Tex3Color = tex2D(Tex3, i.uv_Tex3); albedo = lerp(Tex0Color*max(, dot(tangentNormal1, tangentLightDir)),Tex1Color*max(, dot(tangentNormal1, tangentLightDir)), splat_control.g);
albedo = lerp(albedo,Tex2Color*max(, dot(tangentNormal2, tangentLightDir)), splat_control.b);
albedo = lerp(albedo,Tex3Color*max(, dot(tangentNormal3, tangentLightDir)), splat_control.a); return albedo*_IllumFactor;
}
ENDCG
}
}
FallBack "Diffuse"
}
BlendNormalTex_vertfrag
效果:
albedo = lerp(Tex0Color,Tex1Color, splat_control.g);
//splat_control.g*Tex0Color+(1-splat_control.g)*Tex1Color
工程文件:http://files.cnblogs.com/files/luxishi/BlendShader.rar
地形混合shader的更多相关文章
- Unity3d之Shader编程:子着色器、通道与标签的写法 & 纹理混合
一.子着色器 Unity中的每一个着色器都包含一个subshader的列表,当Unity需要显示一个网格时,它能发现使用的着色器,并提取第一个能运行在当前用户的显示卡上的子着色器. 我们知道,子着色器 ...
- 【浅墨Unity3D Shader编程】之三 光之城堡篇:子着色器、通道与标签的写法 & 纹理混合
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://hpw123.net/a/C__/kongzhitaichengxu/2014/1117/120.html 作者:毛星云 ...
- Unity3d之将terrain转化成mesh
Unity3d中,terrain还是比较耗的,DrawCall数也比较多,为了优化性能,可能需要将terrain转化成mesh. 现提供一工具,思路是根据terrain高度图生成mesh等,可参考: ...
- unity描边效果
这里总结了几种在unity实现描边效果的方法,首先准备一个模型导入在unity中,使用默认shader,上传一张原始图,以便后面实现功能效果的对比 一.边缘光,这里参照官方的一个SurfaceShad ...
- Android群英传笔记——第六章:Android绘图机制与处理技巧
Android群英传笔记--第六章:Android绘图机制与处理技巧 一直在情调,时间都是可以自己调节的,不然世界上哪有这么多牛X的人 今天就开始读第六章了,算日子也刚好一个月了,一个月就读一半,这效 ...
- UE问题分部解决
0.寻找Actor ALandscape *land=nullptr; for (TActorIterator<ALandscape> It(GEditor->GetEditorWo ...
- Unity中实现网格轮廓效果,选中边框效果
问题背景: 最近要实现选中实体的高亮效果,要那种类似于unity中Outline的效果,网格轮廓高亮效果. 效果图: 具体代码: OutlineEffect.cs 实体高亮效果类: 轮廓边总控制类,该 ...
- 添加Google网络地图功能
在MeteoInfo中添加了Google网络地图功能.打开MeteoInfo软件,选中图层管理区的一个Map Frame(New Map Frame),点击鼠标右键,在弹出菜单中点击Add Web L ...
- 解读Unity中的CG编写Shader系列七(不透明度与混合)
转自http://www.itnose.net/detail/6098539.html 1.不透明度 当我们要将两个半透的纹理贴图到一个材质球上的时候就遇到混合的问题,由于前面的知识我们已经知道了片段 ...
随机推荐
- C# html的Table导出到Excel中
C#中导出Excel分为两大类.一类是Winform的,一类是Web.今天说的这一种是Web中的一种,把页面上的Table部分导出到Excel中. Table导出Excel,简单点说,分为以下几步: ...
- java的几个特性
前言 本文主要介绍java语言的三个特性:类型协变和逆变,动态代理和静态代理,注解. 协变和逆变 借用Treant的博文,逆变与协变用来描述类型转换(type transformation)后的继承关 ...
- [置顶]
kubernetes--优雅删除资源对象
当用户请求删除含有pod的资源对象时(如RC.deployment等),K8S为了让应用程序优雅关闭(即让应用程序完成正在处理的请求后,再关闭软件),K8S提供两种信息通知: 1).默认:K8S通知n ...
- 【Hadoop】Hadoop MR 自定义排序
1.概念 2.代码示例 FlowSort package com.ares.hadoop.mr.flowsort; import java.io.IOException; import org.apa ...
- solrCloud分布式检索流程
FROM: http://blog.csdn.net/duck_genuine/article/details/17014991 好久没写solr的文章了,刚好需要在公司作个分享,先总结一些先. 引用 ...
- 理解支持向量机(四)LibSVM工具包的使用
LibSVM是一款简单易用的支持向量机工具包.包括了C和Java的开发源代码.大家能够訪问其官网进行了解和下载相关文件. 这里以其官网的第一个数据集a1a 为例.练习使用多项式核和径向基核来对数据集进 ...
- Node.js 网页爬虫再进阶,cheerio助力
任务还是读取博文标题. 读取app2.js // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // cheerio模块, ...
- node - 关于package.json
2018-8-3(首次更新) 一.关于版本号: 文章来自:https://blog.csdn.net/yancloudfrontend/article/details/72867314 指定版本:比如 ...
- Codeforces Round #262 (Div. 2)解题报告
详见:http://robotcator.logdown.com/posts/221514-codeforces-round-262-div-2 1:A. Vasya and Socks http ...
- react-native Image resizeMode
resizeMode (默认为 cover)该属性用来设置图片的缩放模式,对应值如下 cover 保持图片宽高比,直到宽度和高度都大于等于容器视图的尺寸(参考下图效果)contain 在保持图片宽高比 ...