Terrain bump mapped + detail texture for 1 splat (Caustics)

Using the base shader found here:
http://forum.unity3d.com/threads/63282-Bump-maps-for-built-in-terrain

below is the replacement shader:

/* 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"

struct 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 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 = true;
			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 :slight_smile:

461765--16208--$Untitled-2.jpg

could you show me what it looks like when playing. I cant get mine to look very good

Im at work now, ill pack a small web player for you tonight.

thanks alot man

You can find the simple web player here to see how it looks like in realtime.
i tried to attach it to this post but it didnt work.

That is pretty cool. Nice work!

You know it can be used for anything like lava, caustics or anything you want detail mapped.

I never thought about lava. Perfect for that. Thanks Aubergine.

Excellent!
I will try to modify it to enhance seashore waves.

You can do it with an additional alpha map for the 4th splat uvs and multiply the detail with this alpha map. But why bother, you will be using an extra texture anyhow soo, just use the 4th texture as your seashore and another texture for the deep parts.

Below versions are slightly better and easier to use.

Shader:

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) = (1.0, 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

struct Input {
	float2 uv_BumpMap;
	float2 uv_Control : TEXCOORD0;
	float2 uv_Splat0 : TEXCOORD1;
	float2 uv_Splat1 : TEXCOORD2;
	float2 uv_Splat2 : TEXCOORD3;
	float2 uv_Splat3 : TEXCOORD4;
};

void vert (inout appdata_full v) {
	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 _Splat0, _Splat1, _Splat2, _Splat3;
sampler2D _Bump00, _Bump01, _Bump02, _Bump03;
sampler2D _DetailMap;

float _SpecPow00, _SpecPow01, _SpecPow02, _SpecPow03;
float _TileX00, _TileX01, _TileX02, _TileX03, _TileZ00, _TileZ01, _TileZ02, _TileZ03;
float _TerX, _TerZ;
half4 _SpecCol;

void surf (Input IN, inout SurfaceOutput o) {
	half4 splat_control = tex2D (_Control, IN.uv_Control);
	half3 col;
	_SpecColor = _SpecCol;
	
	col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
	o.Normal = splat_control.r * UnpackNormal (tex2D (_Bump00, float2(IN.uv_Control.x * (_TerX/_TileX00), IN.uv_Control.y * (_TerZ/_TileZ00))));
	o.Gloss = _SpecPow00 * splat_control.r;
	o.Specular = _SpecPow00 * splat_control.r;
	
	col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
	o.Normal += splat_control.g * UnpackNormal (tex2D (_Bump01, float2(IN.uv_Control.x * (_TerX/_TileX01), IN.uv_Control.y * (_TerZ/_TileZ01))));
	o.Gloss += _SpecPow01 * splat_control.g;
	o.Specular += _SpecPow01 * splat_control.g;
	
	col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
	o.Normal += splat_control.b * UnpackNormal (tex2D (_Bump02, float2(IN.uv_Control.x * (_TerX/_TileX02), IN.uv_Control.y * (_TerZ/_TileZ02))));
	o.Gloss += _SpecPow02 * splat_control.b;
	o.Specular += _SpecPow02 * splat_control.b;
	
	col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
	col += splat_control.a * tex2D (_DetailMap, IN.uv_Splat3).rgb;
	o.Normal += splat_control.a * UnpackNormal (tex2D (_Bump03, float2(IN.uv_Control.x * (_TerX/_TileX03), IN.uv_Control.y * (_TerZ/_TileZ03))));
	o.Gloss += _SpecPow03 * splat_control.a;
	o.Specular += _SpecPow03 * splat_control.a;
	o.Albedo = col;
	o.Alpha = 0.0;
}
ENDCG  
}

// Fallback to Diffuse
Fallback "Diffuse"
}

Script:

using UnityEngine;
using System.Collections;

public class Terrain_Bump : MonoBehaviour {
	
	public Texture2D[] bumpMaps;
	public float[] specularPowers;
	public Color specularColor = Color.grey;
	public Texture2D[] detailMaps;
	public float detailFPS = 15.0F;
	
	void Awake () {
		Terrain terrain = (Terrain)GetComponent(typeof(Terrain));
		int splatCount = terrain.terrainData.alphamapLayers;
		
		float[] tileSizeX = new float[splatCount];
		float[] tileSizeZ = new float[splatCount];
		for (int i = 0; i < splatCount; i++) {
			tileSizeX[i] = terrain.terrainData.splatPrototypes[i].tileSize.x;
			tileSizeZ[i] = terrain.terrainData.splatPrototypes[i].tileSize.y;
			string tileX = "_TileX0"+i.ToString();
			string tileZ = "_TileZ0"+i.ToString();
			Shader.SetGlobalFloat (tileX, tileSizeX[i]);
			Shader.SetGlobalFloat (tileZ, tileSizeZ[i]);
		}
		
		for (int i = 0; i < bumpMaps.Length; i++) {
			if (bumpMaps[i] != null) {
				string bump = "_Bump0"+i.ToString();
				Shader.SetGlobalTexture(bump, bumpMaps[i]);
			}
		}
		
		for (int i = 0; i < specularPowers.Length; i++) {
			string spec = "_SpecPow0"+i.ToString();
			Shader.SetGlobalFloat(spec, specularPowers[i]);
		}
		Shader.SetGlobalColor("_SpecCol", specularColor);
		Shader.SetGlobalFloat("_TerX", terrain.terrainData.size.x);
		Shader.SetGlobalFloat("_TerZ", terrain.terrainData.size.z);
	}

	void Update () {
		float i = Time.time * detailFPS;
		i = i % detailMaps.Length;
		//DetailTX = detailMaps[(int)i];
		Shader.SetGlobalTexture("_DetailMap", detailMaps[(int)i]);
	}
}

Could you maybe post a package? looks really cool but I have a mac and non of those apps work for me :frowning:

Here is the package. Just import and try the lousy test scene.

http://hotfile.com/dl/93937069/3cdedfe/TerrainBumpDetail.unitypackage.html

This file is either removed due to copyright claim or is deleted by the uploader.

has some one can post another package? thx