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

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);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Unity3D shader