- 最后登录
- 2018-6-29
- 注册时间
- 2011-7-1
- 阅读权限
- 20
- 积分
- 359
- 纳金币
- 335582
- 精华
- 0
|
below is the replacement shader:
Code: /* Code provided by Chris Morris of Six Times Nothing (http://www.sixtimesnothing.com) */
/* Free to use and modify */
/* Edited by Aubergine (3 lines :p) */
Shader "Hidden/TerrainEngine/Splatmap/Lightmap-FirstPass" {
Properties {
_Control ("Control (RGBA)", 2D) = "red" {}
_Splat3 ("Layer 3 (A)", 2D) = "white" {}
_Splat2 ("Layer 2 (B)", 2D) = "white" {}
_Splat1 ("Layer 1 (G)", 2D) = "white" {}
_Splat0 ("Layer 0 (R)", 2D) = "white" {}
// used in fallback on old cards
_MainTex ("BaseMap (RGB)", 2D) = "white" {}
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
}
SubShader {
Tags {
"SplatCount" = "4"
"Queue" = "Geometry-100"
"RenderType" = "Opaque"
}
CGPROGRAM
#pragma surface surf BlinnPhong vertex:vert
#pragma target 3.0
#include "UnityCG.cginc"
s***ct Input {
float3 worldPos;
float2 uv_BumpMap;
float2 uv_Control : TEXCOORD0;
float2 uv_Splat0 : TEXCOORD1;
float2 uv_Splat1 : TEXCOORD2;
float2 uv_Splat2 : TEXCOORD3;
float2 uv_Splat3 : TEXCOORD4;
};
// Supply the shader with tangents for the terrain
void vert (inout appdata_full v) {
// A general tangent estimation
float3 T1 = float3(1, 0, 1);
float3 Bi = cross(T1, v.normal);
float3 newTangent = cross(v.normal, Bi);
normalize(newTangent);
v.tangent.xyz = newTangent.xyz;
if (dot(cross(v.normal,newTangent),Bi) < 0)
v.tangent.w = -1.0f;
else
v.tangent.w = 1.0f;
}
sampler2D _Control;
sampler2D _BumpMap0, _BumpMap1, _BumpMap2, _BumpMap3;
sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
sampler2D _DetailTX;
float _Spec0, _Spec1, _Spec2, _Spec3, _Tile0, _Tile1, _Tile2, _Tile3, _TerrainX, _TerrainZ;
float4 _v4CameraPos;
void surf (Input IN, inout SurfaceOutput o) {
half4 splat_control = tex2D (_Control, IN.uv_Control);
half3 col;
half3 detail;
// 4 splats, normals, and specular settings
col = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
o.Normal = splat_control.r * UnpackNormal(tex2D(_BumpMap0, float2(IN.uv_BumpMap.x * (_TerrainX/_Tile0), IN.uv_BumpMap.y * (_TerrainZ/_Tile0))));
o.Gloss = _Spec0 * splat_control.r;
o.Specular = _Spec0 * splat_control.r;
col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
o.Normal += splat_control.g * UnpackNormal(tex2D(_BumpMap1, float2(IN.uv_BumpMap.x * (_TerrainX/_Tile1), IN.uv_BumpMap.y * (_TerrainZ/_Tile1))));
o.Gloss += _Spec1 * splat_control.g;
o.Specular += _Spec1 * splat_control.g;
col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
o.Normal += splat_control.b * UnpackNormal(tex2D(_BumpMap2, float2(IN.uv_BumpMap.x * (_TerrainX/_Tile2), IN.uv_BumpMap.y * (_TerrainZ/_Tile2))));
o.Gloss += _Spec2 * splat_control.b;
o.Specular +=_Spec2 * splat_control.b;
col += splat_control.a * (tex2D (_Splat3, IN.uv_Splat3).rgb + tex2D (_DetailTX, IN.uv_Splat3).rgb);
o.Normal += splat_control.a * UnpackNormal(tex2D(_BumpMap3, float2(IN.uv_BumpMap.x * (_TerrainX/_Tile3), IN.uv_BumpMap.y * (_TerrainZ/_Tile3))));
o.Gloss += _Spec3 * splat_control.a;
o.Specular += _Spec3 * splat_control.a;
o.Albedo = col;
o.Alpha = 0.0;
}
ENDCG
}
// Fallback to Diffuse
Fallback "Diffuse"
}
aand below is the script to attach to the terrain:
Code: /* Code provided by Chris Morris of Six Times Nothing (http://www.sixtimesnothing.com) */
/* Free to use and modify */
/* Edited by Aubergine (more than 3 lines of code:p)*/
using UnityEngine;
using System.Collections;
[AddComponentMenu ("Frameworks/Terrain/TerrainNormalMap")]
public class TerrainNormalMap : MonoBehaviour {
public Texture2D Bump0;
public Texture2D Bump1;
public Texture2D Bump2;
public Texture2D Bump3;
public Texture2D[] DetailTXArray;
private Texture2D DetailTX;
private float img = 0.0F;
private bool canDetail;
private float detailFps = 15.0F;
public float Tile0;
public float Tile1;
public float Tile2;
public float Tile3;
public float Spec0;
public float Spec1;
public float Spec2;
public float Spec3;
public float terrainSizeX;
public float terrainSizeZ;
void Start () {
Terrain terrainComp = (Terrain)GetComponent(typeof(Terrain));
if(Bump0)
Shader.SetGlobalTexture("_BumpMap0", Bump0);
if(Bump1)
Shader.SetGlobalTexture("_BumpMap1", Bump1);
if(Bump2)
Shader.SetGlobalTexture("_BumpMap2", Bump2);
if(Bump3)
Shader.SetGlobalTexture("_BumpMap3", Bump3);
if(DetailTXArray.Length > 0) {
canDetail = ***e;
DetailTX = DetailTXArray[0];
Shader.SetGlobalTexture("_DetailTX", DetailTX);
}
Shader.SetGlobalFloat("_Spec0", Spec0);
Shader.SetGlobalFloat("_Spec1", Spec1);
Shader.SetGlobalFloat("_Spec2", Spec2);
Shader.SetGlobalFloat("_Spec3", Spec3);
Shader.SetGlobalFloat("_Tile0", Tile0);
Shader.SetGlobalFloat("_Tile1", Tile1);
Shader.SetGlobalFloat("_Tile2", Tile2);
Shader.SetGlobalFloat("_Tile3", Tile3);
terrainSizeX = terrainComp.terrainData.size.x;
terrainSizeZ = terrainComp.terrainData.size.z;
Shader.SetGlobalFloat("_TerrainX", terrainSizeX);
Shader.SetGlobalFloat("_TerrainZ", terrainSizeZ);
}
/* Don't need this update unless you're testing during play */
void Update () {
if(Bump0)
Shader.SetGlobalTexture("_BumpMap0", Bump0);
if(Bump1)
Shader.SetGlobalTexture("_BumpMap1", Bump1);
if(Bump2)
Shader.SetGlobalTexture("_BumpMap2", Bump2);
if(Bump3)
Shader.SetGlobalTexture("_BumpMap3", Bump3);
if(canDetail) {
float i = Time.time * detailFPS;
i = i % DetailTXArray.Length;
DetailTX = DetailTXArray[(int)i];
Shader.SetGlobalTexture("_DetailTX", DetailTX);
}
Shader.SetGlobalFloat("_Spec0", Spec0);
Shader.SetGlobalFloat("_Spec1", Spec1);
Shader.SetGlobalFloat("_Spec2", Spec2);
Shader.SetGlobalFloat("_Spec3", Spec3);
Shader.SetGlobalFloat("_Tile0", Tile0);
Shader.SetGlobalFloat("_Tile1", Tile1);
Shader.SetGlobalFloat("_Tile2", Tile2);
Shader.SetGlobalFloat("_Tile3", Tile3);
Shader.SetGlobalFloat("_TerrainX", terrainSizeX);
Shader.SetGlobalFloat("_TerrainZ", terrainSizeZ);
}
}
How to use:
There is a free caustics generator software (search google), using this one export an animated series of textures
Just normap map and do every other thing mentioned in the first link (which is the main base of this shader)
Attach your caustics to the array and you will see the 4th splat is now blending with main texture, normal texture and this detail caustics.
Code may look messy, but sure you can tidy it yourself.
It looks better when animated |
|