Unity3D TerrainSurface Shader
2015-09-01 14:33
393 查看
Shader "TerrainSurface"
{
Properties
{
_Mask ("Mask", 2D) = "black" {}
_MainTex ("Albedo 0", 2D) = "white" {}
_MainTex2 ("Albedo 1", 2D) = "white" {}
_NormalMap ("NormalMap 0", 2D) = "bump" {}
_NormalMap2 ("NormalMap 1", 2D) = "bump" {}
[LM_Specular] [LM_Glossiness] _SpecGlossMap("Specular 0", 2D) = "white" {}
[LM_Specular] [LM_Glossiness] _SpecGlossMap2("Specular 1", 2D) = "white" {}
}
// SM3+
SubShader
{
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf StandardSpecular fullforwardshadows addshadow
// until texCubeLOD is solved
#pragma exclude_renderers gles
#pragma target 3.0
struct Input
{
float4 color;
float2 uv_MainTex;
float2 uv_MainTex2;
float2 uv_Mask;
};
sampler2D _MainTex;
sampler2D _MainTex2;
sampler2D _NormalMap;
sampler2D _NormalMap2;
sampler2D _SpecGlossMap;
sampler2D _SpecGlossMap2;
sampler2D _Mask;
void surf (Input IN, inout SurfaceOutputStandardSpecular o)
{
fixed blend = tex2D(_Mask, IN.uv_Mask).a;
fixed4 albedo1 = tex2D(_MainTex, IN.uv_MainTex);
fixed4 spec1
= tex2D(_SpecGlossMap, IN.uv_MainTex);
fixed3 normal1 = UnpackNormal (tex2D (_NormalMap, IN.uv_MainTex));
fixed4 albedo2 = tex2D(_MainTex2, IN.uv_MainTex2);
fixed4 spec2 = tex2D(_SpecGlossMap2, IN.uv_MainTex2);
fixed3 normal2 = UnpackNormal (tex2D (_NormalMap2, IN.uv_MainTex2));
fixed4 specGloss = lerp (spec1, spec2, blend);
o.Albedo = lerp (albedo1, albedo2, blend);
o.Specular
= specGloss.rgb;
o.Smoothness = specGloss.a;
o.Normal
= lerp (normal1, normal2, blend);
}
ENDCG
}
CustomEditor "BlendShaderGUI"
}
//--------------------------------- Shader GUI-------------------------------------------------------------------------------------------------------------------------------------------
using UnityEngine;
using UnityEditor;
using Object = UnityEngine.Object;
using System;
internal class BlendShaderGUI : ShaderGUI
{
private static class Styles
{
public static GUIContent albedo = new GUIContent("Albedo", "Albedo (RGB) Emissive (A)");
public static GUIContent specular = new GUIContent("Specular", "Specular (RGB) and Smoothness (A)");
public static GUIContent normal = new GUIContent("Normal", "Normal Map");
public static GUIContent blendMask = new GUIContent("Mask", "Mask (A) -> blend");
public static string material0Header = "Primary Maps";
public static string material1Header = "Secondary Maps";
public static string maskHeader = "Blend : Mask";
}
MaterialProperty blendMask = null;
MaterialProperty albedoMap = null;
MaterialProperty specularMap = null;
MaterialProperty bumpMap = null;
MaterialProperty albedoMap2 = null;
MaterialProperty specularMap2 = null;
MaterialProperty bumpMap2 = null;
const int kSecondLevelIndentOffset = 2;
const float kVerticalSpacing = 2f;
public void FindProperties (MaterialProperty[] props)
{
blendMask = FindProperty ("_Mask", props);
albedoMap = FindProperty ("_MainTex", props);
albedoMap2 = FindProperty ("_MainTex2", props);
specularMap = FindProperty ("_SpecGlossMap", props);
specularMap2 = FindProperty ("_SpecGlossMap2", props);
bumpMap = FindProperty ("_NormalMap", props);
bumpMap2 = FindProperty ("_NormalMap2", props);
}
public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] props)
{
FindProperties (props); // MaterialProperties can be animated so we do not cache them but fetch them every event to ensure animated values are updated correctly
// Use default labelWidth
EditorGUIUtility.labelWidth = 0f;
// Detect any changes to the material
EditorGUI.BeginChangeCheck();
{
GUILayout.Label (Styles.material0Header, EditorStyles.boldLabel);
// Texture
materialEditor.TexturePropertySingleLine (Styles.albedo, albedoMap);
materialEditor.TexturePropertySingleLine (Styles.specular, specularMap);
materialEditor.TexturePropertySingleLine (Styles.normal, bumpMap);
materialEditor.TextureScaleOffsetProperty (albedoMap);
GUILayout.Label (Styles.maskHeader, EditorStyles.boldLabel);
materialEditor.TexturePropertySingleLine (Styles.blendMask, blendMask);
materialEditor.TextureScaleOffsetProperty (blendMask);
GUILayout.Label (Styles.material1Header, EditorStyles.boldLabel);
materialEditor.TexturePropertySingleLine (Styles.albedo, albedoMap2);
materialEditor.TexturePropertySingleLine (Styles.specular, specularMap2);
materialEditor.TexturePropertySingleLine (Styles.normal, bumpMap2);
materialEditor.TextureScaleOffsetProperty (albedoMap2);
}
}
}
{
Properties
{
_Mask ("Mask", 2D) = "black" {}
_MainTex ("Albedo 0", 2D) = "white" {}
_MainTex2 ("Albedo 1", 2D) = "white" {}
_NormalMap ("NormalMap 0", 2D) = "bump" {}
_NormalMap2 ("NormalMap 1", 2D) = "bump" {}
[LM_Specular] [LM_Glossiness] _SpecGlossMap("Specular 0", 2D) = "white" {}
[LM_Specular] [LM_Glossiness] _SpecGlossMap2("Specular 1", 2D) = "white" {}
}
// SM3+
SubShader
{
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf StandardSpecular fullforwardshadows addshadow
// until texCubeLOD is solved
#pragma exclude_renderers gles
#pragma target 3.0
struct Input
{
float4 color;
float2 uv_MainTex;
float2 uv_MainTex2;
float2 uv_Mask;
};
sampler2D _MainTex;
sampler2D _MainTex2;
sampler2D _NormalMap;
sampler2D _NormalMap2;
sampler2D _SpecGlossMap;
sampler2D _SpecGlossMap2;
sampler2D _Mask;
void surf (Input IN, inout SurfaceOutputStandardSpecular o)
{
fixed blend = tex2D(_Mask, IN.uv_Mask).a;
fixed4 albedo1 = tex2D(_MainTex, IN.uv_MainTex);
fixed4 spec1
= tex2D(_SpecGlossMap, IN.uv_MainTex);
fixed3 normal1 = UnpackNormal (tex2D (_NormalMap, IN.uv_MainTex));
fixed4 albedo2 = tex2D(_MainTex2, IN.uv_MainTex2);
fixed4 spec2 = tex2D(_SpecGlossMap2, IN.uv_MainTex2);
fixed3 normal2 = UnpackNormal (tex2D (_NormalMap2, IN.uv_MainTex2));
fixed4 specGloss = lerp (spec1, spec2, blend);
o.Albedo = lerp (albedo1, albedo2, blend);
o.Specular
= specGloss.rgb;
o.Smoothness = specGloss.a;
o.Normal
= lerp (normal1, normal2, blend);
}
ENDCG
}
CustomEditor "BlendShaderGUI"
}
//--------------------------------- Shader GUI-------------------------------------------------------------------------------------------------------------------------------------------
using UnityEngine;
using UnityEditor;
using Object = UnityEngine.Object;
using System;
internal class BlendShaderGUI : ShaderGUI
{
private static class Styles
{
public static GUIContent albedo = new GUIContent("Albedo", "Albedo (RGB) Emissive (A)");
public static GUIContent specular = new GUIContent("Specular", "Specular (RGB) and Smoothness (A)");
public static GUIContent normal = new GUIContent("Normal", "Normal Map");
public static GUIContent blendMask = new GUIContent("Mask", "Mask (A) -> blend");
public static string material0Header = "Primary Maps";
public static string material1Header = "Secondary Maps";
public static string maskHeader = "Blend : Mask";
}
MaterialProperty blendMask = null;
MaterialProperty albedoMap = null;
MaterialProperty specularMap = null;
MaterialProperty bumpMap = null;
MaterialProperty albedoMap2 = null;
MaterialProperty specularMap2 = null;
MaterialProperty bumpMap2 = null;
const int kSecondLevelIndentOffset = 2;
const float kVerticalSpacing = 2f;
public void FindProperties (MaterialProperty[] props)
{
blendMask = FindProperty ("_Mask", props);
albedoMap = FindProperty ("_MainTex", props);
albedoMap2 = FindProperty ("_MainTex2", props);
specularMap = FindProperty ("_SpecGlossMap", props);
specularMap2 = FindProperty ("_SpecGlossMap2", props);
bumpMap = FindProperty ("_NormalMap", props);
bumpMap2 = FindProperty ("_NormalMap2", props);
}
public override void OnGUI (MaterialEditor materialEditor, MaterialProperty[] props)
{
FindProperties (props); // MaterialProperties can be animated so we do not cache them but fetch them every event to ensure animated values are updated correctly
// Use default labelWidth
EditorGUIUtility.labelWidth = 0f;
// Detect any changes to the material
EditorGUI.BeginChangeCheck();
{
GUILayout.Label (Styles.material0Header, EditorStyles.boldLabel);
// Texture
materialEditor.TexturePropertySingleLine (Styles.albedo, albedoMap);
materialEditor.TexturePropertySingleLine (Styles.specular, specularMap);
materialEditor.TexturePropertySingleLine (Styles.normal, bumpMap);
materialEditor.TextureScaleOffsetProperty (albedoMap);
GUILayout.Label (Styles.maskHeader, EditorStyles.boldLabel);
materialEditor.TexturePropertySingleLine (Styles.blendMask, blendMask);
materialEditor.TextureScaleOffsetProperty (blendMask);
GUILayout.Label (Styles.material1Header, EditorStyles.boldLabel);
materialEditor.TexturePropertySingleLine (Styles.albedo, albedoMap2);
materialEditor.TexturePropertySingleLine (Styles.specular, specularMap2);
materialEditor.TexturePropertySingleLine (Styles.normal, bumpMap2);
materialEditor.TextureScaleOffsetProperty (albedoMap2);
}
}
}
相关文章推荐
- Kinect结合Unity3D引擎开发体感游戏(一)
- Unity3D中脚本的执行顺序和编译顺序
- Unity3D动态对象优化代码分享
- unity3d发布apk在android虚拟机中运行的详细步骤(unity3d导出android apk)
- Unity3D游戏引擎实现在Android中打开WebView的实例
- unity3d调用手机或电脑摄像头
- 分享一个开源的网络游戏服务器架构—HouHai
- Unity3D插件详细评测及教学下载
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关
- Unity3d TransformPoint 函数研究
- Unity3d 动态加载fbx模型文件
- unity3d地形系统总结