3SideProjDiffuse

From Unify Community Wiki
Jump to: navigation, search


Author: Nils Daumann.

Contents

Description

A model using this shader

The shader projects the texture from three sides at the mesh it is applied to. This works especially very good for simple, kinda organic objects. The object it is applied to will only be effected by per pixel lights. This shader basicly removes the need for a decent uv map. The texture will be tiled.

Works on fragment program capable cards (Radeon 9500+, GeForce FX+, Intel 9xx). Supports fog and shadows.


Usage

Typical setup

  • Color is the general color of the material.
  • Texture is for color (Tiling and Offset have no effect).
  • Tile factor specifies how much the texture is tiled.

Download

Media:WorldTextureDiffuse.zip


ShaderLab - WorldTextureDiffuse.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 "World Texture Diffuse"
{
	Properties
	{
		_Color ("Main Color", Color) = (1,1,1,1)
		_MainTex("Texture", 2D) = "white" {}
		_TileFac("Tile factor", Float) = 5
	}
	
	SubShader
	{
		Pass {
			Name "BASE"
			Tags {"LightMode" = "None"}
			Blend AppSrcAdd AppDstAdd
			Fog { Color [_AddFog] }
			
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				
				#pragma multi_compile_builtin
				#pragma fragmentoption ARB_fog_exp2
				#pragma fragmentoption ARB_precision_hint_fastest
				
				#include "UnityCG.cginc"
				#include "AutoLight.cginc"
				
				uniform sampler2D _MainTex;
				uniform float _TileFac;
				
				struct v2f
				{
					V2F_POS_FOG;
					float3 worldnormal;
					float3 worldpos;
				}; 

				v2f vert(appdata_base v)
				{
					v2f o;
					
					PositionFog( v.vertex, o.pos, o.fog );
					
					o.worldnormal = mul(_Object2World, float4(v.normal, 0.0f)).xyz;
					o.worldpos = mul(_Object2World, v.vertex);
					
					return o;
				}
			   
				float4 frag(v2f i) : COLOR
				{
					float2 s = float2(_TileFac, -_TileFac);
					
					float2 tex0 = i.worldpos.xy/s;
					float2 tex1 = i.worldpos.zx/s;
					float2 tex2 = i.worldpos.zy/s;
					
					float4 color0_ = tex2D(_MainTex, tex0);
					float4 color1_ = tex2D(_MainTex, tex1);
					float4 color2_ = tex2D(_MainTex, tex2);
					
					i.worldnormal = normalize(i.worldnormal);
					float3 projnormal = saturate(pow(i.worldnormal*1.5, 4));
					
					float3 color = lerp(color1_, color0_, projnormal.z);
					color = lerp(color, color2_, projnormal.x);
					
					return float4(color.rgb*_PPLAmbient, 1.0);
				}
			ENDCG
		}
		Pass {
			Name "BASE"
			Tags {"LightMode" = "VertexOrPixel"}
			Blend AppSrcAdd AppDstAdd
			Fog { Color [_AddFog] }
			
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				
				#pragma multi_compile_builtin
				#pragma fragmentoption ARB_fog_exp2
				#pragma fragmentoption ARB_precision_hint_fastest
				
				#include "UnityCG.cginc"
				#include "AutoLight.cginc"
				
				uniform sampler2D _MainTex;
				uniform float _TileFac;
				
				struct v2f
				{
					V2F_POS_FOG;
					float3 worldnormal;
					float3 worldpos;
				}; 

				v2f vert(appdata_base v)
				{
					v2f o;
					
					PositionFog( v.vertex, o.pos, o.fog );
					
					o.worldnormal = mul(_Object2World, float4(v.normal, 0.0f)).xyz;
					o.worldpos = mul(_Object2World, v.vertex);
					
					return o;
				}
			   
				float4 frag(v2f i) : COLOR
				{
					float2 s = float2(_TileFac, -_TileFac);
					
					float2 tex0 = i.worldpos.xy/s;
					float2 tex1 = i.worldpos.zx/s;
					float2 tex2 = i.worldpos.zy/s;
					
					float4 color0_ = tex2D(_MainTex, tex0);
					float4 color1_ = tex2D(_MainTex, tex1);
					float4 color2_ = tex2D(_MainTex, tex2);
					
					i.worldnormal = normalize(i.worldnormal);
					float3 projnormal = saturate(pow(i.worldnormal*1.5, 4));
					
					float3 color = lerp(color1_, color0_, projnormal.z);
					color = lerp(color, color2_, projnormal.x);
					
					return float4(color.rgb*_PPLAmbient, 1.0);
				}
			ENDCG
		}
		Pass
		{
			Name "PPL"
			Tags { "LightMode" = "Pixel" }
			
			Blend AppSrcAdd AppDstAdd
			Fog { Color [_AddFog] }
		
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				
				#pragma multi_compile_builtin
				#pragma fragmentoption ARB_fog_exp2
				#pragma fragmentoption ARB_precision_hint_fastest
				
				#include "UnityCG.cginc"
				#include "AutoLight.cginc"
				
				uniform sampler2D _MainTex;
				uniform float _TileFac;
				
				struct v2f
				{
					V2F_POS_FOG;
					LIGHTING_COORDS
					float3 normal;
					float3 worldnormal;
					float3 worldpos;
					float3 lightDir;
				}; 

				v2f vert(appdata_base v)
				{
					v2f o;
					
					PositionFog( v.vertex, o.pos, o.fog );
					o.normal = v.normal;
					
					o.worldnormal = mul(_Object2World, float4(v.normal, 0.0f)).xyz;
					o.worldpos = mul(_Object2World, v.vertex);
					
					o.lightDir = ObjSpaceLightDir(v.vertex);
					TRANSFER_VERTEX_TO_FRAGMENT(o);
					
					return o;
				}
			   
				float4 frag(v2f i) : COLOR
				{
					float2 s = float2(_TileFac, -_TileFac);
					
					float2 tex0 = i.worldpos.xy/s;
					float2 tex1 = i.worldpos.zx/s;
					float2 tex2 = i.worldpos.zy/s;
					
					float4 color0_ = tex2D(_MainTex, tex0);
					float4 color1_ = tex2D(_MainTex, tex1);
					float4 color2_ = tex2D(_MainTex, tex2);
					
					i.normal = normalize(i.normal);
					
					i.worldnormal = normalize(i.worldnormal);
					float3 projnormal = saturate(pow(i.worldnormal*1.5, 4));
					
					float3 color = lerp(color1_, color0_, projnormal.z);
					color = lerp(color, color2_, projnormal.x);
					
					return DiffuseLight(i.lightDir, i.normal, float4(color.rgb, 1.0), LIGHT_ATTENUATION(i));
				}
			ENDCG
		}
	}
	
	FallBack "Diffuse"
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools