LayerShader

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</shaderlab>" to "</syntaxhighlight>")
 
(19 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Author: Bill Vinton (Sync1B)
+
Original Author: Bill Vinton (Sync1B)<BR>
 +
Updated version for 2-texture cards, and doubled lighting version: Jessy
 
==Description==
 
==Description==
 
[[Image:LayerShader.jpg|thumb|100px|Terrain using the shader]]
 
[[Image:LayerShader.jpg|thumb|100px|Terrain using the shader]]
This shader is good for making terrains where you need to tile textures but still want paths.
+
This shader is good for making terrains where you need to tile textures but still want paths. Basically it blends between two textures using alpha from the third (Path Mask) texture.
 +
 
 +
This shader requires a 4-texture video card (GeForce3 and up, Radeon 8500 and up) to be rendered in one pass.
 +
 
 +
See also: [[TerrainFourLayer]], which blends four terrain textures, and [[TerrainTwoLayerBumped]] shader, which adds bumpmapping.
  
  
Line 8: Line 13:
 
Terrains, Tile masking
 
Terrains, Tile masking
  
 +
Path Mask texture's alpha channel is used to blend between Main and Path textures.
  
==ShaderLab - VegetationVertexLit.shader==
+
This first, original version, does not use "doubled" lighting.  (Doubling the lighting seems to be the standard with built-in Unity shaders).  Because the strength of the Ambient light in the Render Setting is halved, you will only be able to achieve half the brightness of your textures with ambient light alone, unless you script the Ambient light to have color components above 1.
<shaderlab>
+
 
Shader "MyShaders/Terrain" {
+
The second shader below doubles the lighting calculation, which means that if you set Ambient light to be white, and have no other lights in your scene, the terrain (or whatever else you use this on) will show up with the textures exactly as you painted them.  It is also possible to achieve overblown highlights with this latter version.
Properties {
+
 
_Color ("Main Color", Color) = (1,1,1,1)
+
 
_MainTex ("Main Texture (RGB)", 2D) = "white"
+
[[Media:Two Layer Terrain.zip|Click here for a zip file that includes these two shaders.]]
_PathTex ("Path Texture (RGB)", 2D) = "white"
+
 
_PathMask ("Path Mask (Grey)", 2D) = "white"
+
==ShaderLab - TerrainTwoLayer.shader==
 +
<syntaxhighlight lang="shaderlab">Shader "MyShaders/TerrainTwoLayer" {
 +
 
 +
 
 +
Properties
 +
{
 +
_Color ("Main Color", Color) = (1,1,1)
 +
_MainTex ("Main Texture (RGB)", 2D) = ""
 +
_PathTex ("Path Texture (RGB)", 2D) = ""
 +
_PathMask ("Path Mask (A)", 2D) = ""
 +
}
 +
 
 +
SubShader
 +
{
 +
Lighting On
 +
 +
Material
 +
{
 +
Ambient [_Color]
 +
Diffuse [_Color]
 +
}
 +
 
 +
Pass
 +
{
 +
SetTexture [_MainTex]
 +
 +
SetTexture [_PathMask]
 +
{
 +
combine previous, texture
 +
}
 +
 +
SetTexture [_PathTex]
 +
{
 +
combine texture lerp(previous) previous
 +
}
 +
 +
SetTexture [_MainTex]
 +
{
 +
combine previous * primary
 +
}
 
}
 
}
SubShader {
 
  Pass {
 
      ZWrite On
 
      Lighting On
 
      Cull Back
 
     
 
      Material {
 
          Diffuse [_Color]
 
          Ambient [_Color]
 
      }
 
      SetTexture [_PathTex] {
 
          combine texture
 
      }
 
      SetTexture [_PathMask] {
 
          combine previous, previous-texture
 
      }
 
      SetTexture [_MainTex] {
 
          combine previous lerp (previous) texture
 
      }
 
      SetTexture [_MainTex] {
 
          combine previous * primary
 
      }
 
  }
 
    }
 
FallBack " Diffuse", 1
 
 
}
 
}
</shaderlab>
+
 
 +
SubShader
 +
{
 +
Lighting On
 +
 +
Material
 +
{
 +
Ambient [_Color]
 +
Diffuse [_Color]
 +
}
 +
 
 +
Pass
 +
{
 +
SetTexture [_MainTex]
 +
{
 +
combine texture * primary
 +
}
 +
}
 +
 +
Pass
 +
{
 +
Blend SrcAlpha OneMinusSrcAlpha
 +
 +
SetTexture [_PathTex]
 +
{
 +
combine texture * primary
 +
}
 +
 +
SetTexture [_PathMask]
 +
{
 +
combine previous, texture
 +
}
 +
}
 +
}
 +
 
 +
Fallback "Diffuse"
 +
 
 +
 +
}</syntaxhighlight>
 +
 
 +
==ShaderLab - Two Layer Terrain DOUBLE.shader==
 +
<syntaxhighlight lang="shaderlab">Shader "Two Layer Terrain DOUBLE" {
 +
 
 +
 
 +
Properties
 +
{
 +
_Color ("Main Color", Color) = (1,1,1)
 +
_MainTex ("Main Texture (RGB)", 2D) = ""
 +
_PathTex ("Path Texture (RGB)", 2D) = ""
 +
_PathMask ("Path Mask (A)", 2D) = ""
 +
}
 +
 
 +
SubShader
 +
{
 +
Lighting On
 +
 +
Material
 +
{
 +
Ambient [_Color]
 +
Diffuse [_Color]
 +
}
 +
 
 +
Pass
 +
{
 +
SetTexture [_MainTex]
 +
 +
SetTexture [_PathMask]
 +
{
 +
combine previous, texture
 +
}
 +
 +
SetTexture [_PathTex]
 +
{
 +
combine texture lerp(previous) previous
 +
}
 +
 +
SetTexture [_MainTex]
 +
{
 +
combine previous * primary DOUBLE
 +
}
 +
}
 +
}
 +
 
 +
SubShader
 +
{
 +
Lighting On
 +
 +
Material
 +
{
 +
Ambient [_Color]
 +
Diffuse [_Color]
 +
}
 +
 +
Blend AppSrcAdd AppDstAdd
 +
 
 +
Pass
 +
{
 +
SetTexture [_MainTex]
 +
{
 +
combine texture * primary
 +
}
 +
 +
SetTexture [_PathMask]
 +
{
 +
combine previous * one - texture alpha DOUBLE
 +
}
 +
}
 +
 +
Pass
 +
{
 +
SetTexture [_PathTex]
 +
{
 +
combine texture * primary
 +
}
 +
 +
SetTexture [_PathMask]
 +
{
 +
combine previous * texture alpha DOUBLE
 +
}
 +
}
 +
}
 +
 
 +
Fallback "Diffuse"
 +
 
 +
 +
}</syntaxhighlight>

Latest revision as of 21:56, 17 January 2012

Original Author: Bill Vinton (Sync1B)
Updated version for 2-texture cards, and doubled lighting version: Jessy

Contents

[edit] Description

Terrain using the shader

This shader is good for making terrains where you need to tile textures but still want paths. Basically it blends between two textures using alpha from the third (Path Mask) texture.

This shader requires a 4-texture video card (GeForce3 and up, Radeon 8500 and up) to be rendered in one pass.

See also: TerrainFourLayer, which blends four terrain textures, and TerrainTwoLayerBumped shader, which adds bumpmapping.


[edit] Usage

Terrains, Tile masking

Path Mask texture's alpha channel is used to blend between Main and Path textures.

This first, original version, does not use "doubled" lighting. (Doubling the lighting seems to be the standard with built-in Unity shaders). Because the strength of the Ambient light in the Render Setting is halved, you will only be able to achieve half the brightness of your textures with ambient light alone, unless you script the Ambient light to have color components above 1.

The second shader below doubles the lighting calculation, which means that if you set Ambient light to be white, and have no other lights in your scene, the terrain (or whatever else you use this on) will show up with the textures exactly as you painted them. It is also possible to achieve overblown highlights with this latter version.


Click here for a zip file that includes these two shaders.

[edit] ShaderLab - TerrainTwoLayer.shader

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


Shader "MyShaders/TerrainTwoLayer" {


Properties
{
	_Color ("Main Color", Color) = (1,1,1)
	_MainTex ("Main Texture (RGB)", 2D) = ""
	_PathTex ("Path Texture (RGB)", 2D) = ""
	_PathMask ("Path Mask (A)", 2D) = ""
}

SubShader 
{
	Lighting On
	
	Material 
	{
		Ambient [_Color]
		Diffuse [_Color]
	}

	Pass
	{		
		SetTexture [_MainTex]
		
		SetTexture [_PathMask]
		{
			combine previous, texture
		}
				
		SetTexture [_PathTex]
		{
			combine texture lerp(previous) previous
		}
		
		SetTexture [_MainTex]
		{
			combine previous * primary	
		}
	}
}

SubShader 
{
	Lighting On
	
	Material 
	{
		Ambient [_Color]
		Diffuse [_Color]
	}

	Pass
	{	
		SetTexture [_MainTex]
		{
			combine texture * primary
		}		
	}
	
	Pass
	{
		Blend SrcAlpha OneMinusSrcAlpha
				
		SetTexture [_PathTex]
		{
			combine texture * primary
		}
		
		SetTexture [_PathMask]
		{
			combine previous, texture
		}
	}
}

Fallback "Diffuse"

 
}

[edit] ShaderLab - Two Layer Terrain DOUBLE.shader

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


Shader "Two Layer Terrain DOUBLE" {


Properties
{
	_Color ("Main Color", Color) = (1,1,1)
	_MainTex ("Main Texture (RGB)", 2D) = ""
	_PathTex ("Path Texture (RGB)", 2D) = ""
	_PathMask ("Path Mask (A)", 2D) = ""
}

SubShader 
{
	Lighting On
	
	Material 
	{
		Ambient [_Color]
		Diffuse [_Color]
	}

	Pass
	{		
		SetTexture [_MainTex]
		
		SetTexture [_PathMask]
		{
			combine previous, texture
		}
				
		SetTexture [_PathTex]
		{
			combine texture lerp(previous) previous
		}
		
		SetTexture [_MainTex]
		{
			combine previous * primary DOUBLE
		}
	}
}

SubShader 
{
	Lighting On
	
	Material 
	{
		Ambient [_Color]
		Diffuse [_Color]
	}
	
	Blend AppSrcAdd AppDstAdd

	Pass
	{	
		SetTexture [_MainTex]
		{
			combine texture * primary
		}
		
		SetTexture [_PathMask]
		{
			combine previous * one - texture alpha DOUBLE
		}
	}
	
	Pass
	{			
		SetTexture [_PathTex]
		{
			combine texture * primary
		}
		
		SetTexture [_PathMask]
		{
			combine previous * texture alpha DOUBLE
		}
	}
}

Fallback "Diffuse"

 
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox