Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "318/BumpMapping" // shader name
- {
- Properties
- {
- _MainTex("MainTex", 2D) = "white" {}
- _BumpTex("BumpMap", 2D) = "white" {}
- _farbeF("Farbe", Color) = (0.1,0.7,1,0.8)
- _SpecColor("SpeckColor",Color) = (1,1,1,1)
- _Shiny("Shiny",Float) = 10
- }
- CGINCLUDE
- //common code for all passes
- #include "UnityCG.cginc" //hat mehr Globale constant values aus Unity
- struct VertexInput
- {
- float4 pos : POSITION;
- float3 normal : NORMAL;
- float4 tex : TEXCOORD0;
- float4 tan : TANGENT;
- };
- struct VertexOutput
- {
- float4 pos : SV_POSITION;
- float4 posWorld : TEXCOORD0;
- float4 tex : TEXCOORD1;
- float3 tanWorld : TEXCOORD2;
- float3 normalWorld : TEXCOORD3;
- float3 binormalWorld : TEXOORD4;
- };
- uniform float4 _LightColor0;
- uniform sampler2D _MainTex;
- uniform float4 _MainTex_ST; //tillling and offset parameters
- uniform sampler2D _BumpTex;
- uniform float4 _BumpTex_ST; //tillling and offset parameters
- uniform float4 _farbeF;
- uniform float4 _SpecColor;
- uniform float _Shiny;
- //VERTEX shader
- VertexOutput VS(VertexInput vi)
- {
- VertexOutput vo;
- float4x4 modelMatrix = unity_ObjectToWorld;
- float4x4 modelMatrixInvers = unity_WorldToObject;
- vo.tanWorld = normalize(
- mul(modelMatrix, float4(vi.tan.xyz, 0)).xyz);
- vo.normalWorld = normalize(
- mul(float4(vi.normal, 0), modelMatrixInvers).xyz);
- vo.binormalWorld = normalize(
- cross(vo.normalWorld, vo.tanWorld)
- * vi.tan.w); // tan.w is nur wegen Unity....
- vo.posWorld = mul(modelMatrix, vi.pos);
- vo.pos = UnityObjectToClipPos(vi.pos);
- vo.tex = vi.tex;
- return vo;
- }
- float4 PS_A(VertexOutput input) : COLOR
- { //mit Ambient
- float4 encodedNormal = tex2D(_BumpTex,
- _BumpTex_ST.xy * input.tex.xy + _BumpTex_ST.zw);
- float3 localCoords = float3(
- 2.0 *encodedNormal.a - 1,
- 2.0 *encodedNormal.g - 1,
- 0);
- localCoords.z = sqrt(1.0 - dot(localCoords, localCoords));
- //approximation von localCoords.z = 1 -0.5 * dot(localCoords,localCoords)
- float3x3 local2WorldTranpose = float3x3(
- input.tanWorld,
- input.binormalWorld,
- input.normalWorld);
- //das hier ist der grosse Unterschied!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- float3 normalDir = normalize(mul(localCoords, local2WorldTranpose));
- float3 viewDir = normalize(_WorldSpaceCameraPos - input.posWorld.xyz);
- float3 lightDir;
- float attenuation;
- //wenn in der .w eine 0 steht -> dannn directional light
- if (0.0 == _WorldSpaceLightPos0.w)
- {
- attenuation = 1;
- lightDir = normalize(_WorldSpaceLightPos0.xyz);
- }
- else
- { //spot or point light
- float3 vertexToLight = _WorldSpaceLightPos0.xyz
- - input.posWorld.xyz;
- float distance = length(vertexToLight);
- attenuation = 1.0 / distance;
- lightDir = normalize(vertexToLight);
- }
- float3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb * _farbeF;
- float3 diffRefl = attenuation * _LightColor0.rgb * _farbeF.rgb
- *max(0.0, dot(normalDir, lightDir));
- float3 specRefl;
- if (dot(normalDir, lightDir) < 0)
- {
- //licht auf falscher seite?
- specRefl.rgb = 0;
- }
- else
- {
- specRefl = attenuation * _LightColor0.rgb * _SpecColor.rgb *
- pow(max(0.0, dot(reflect(-lightDir, normalDir), viewDir)), _Shiny);
- }
- float4 diff = tex2D(_MainTex, _MainTex_ST.xy * input.tex.xy + _MainTex_ST.zw);
- return float4(diff * (ambient + diffRefl + specRefl), 1.0);
- }
- float4 PS(VertexOutput input) : COLOR
- { //mit Ambient
- float4 encodedNormal = tex2D(_BumpTex,
- _BumpTex_ST.xy * input.tex.xy + _BumpTex_ST.zw);
- float3 localCoords = float3(
- 2.0 *encodedNormal.a - 1,
- 2.0 *encodedNormal.g - 1,
- 0);
- localCoords.z = sqrt(1.0 - dot(localCoords, localCoords));
- //approximation von localCoords.z = 1 -0.5 * dot(localCoords,localCoords)
- float3x3 local2WorldTranpose = float3x3(
- input.tanWorld,
- input.binormalWorld,
- input.normalWorld);
- //das hier ist der grosse Unterschied!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- float3 normalDir = normalize(mul(localCoords, local2WorldTranpose));
- float3 viewDir = normalize(_WorldSpaceCameraPos - input.posWorld.xyz);
- float3 lightDir;
- float attenuation;
- //wenn in der .w eine 0 steht -> dannn directional light
- if (0.0 == _WorldSpaceLightPos0.w)
- {
- attenuation = 1;
- lightDir = normalize(_WorldSpaceLightPos0.xyz);
- }
- else
- { //spot or point light
- float3 vertexToLight = _WorldSpaceLightPos0.xyz
- - input.posWorld.xyz;
- float distance = length(vertexToLight);
- attenuation = 1.0 / distance;
- lightDir = normalize(vertexToLight);
- }
- float3 diffRefl = attenuation * _LightColor0.rgb * _farbeF.rgb
- *max(0.0, dot(normalDir, lightDir));
- float3 specRefl;
- if (dot(normalDir, lightDir) < 0)
- {
- //licht auf falscher seite?
- specRefl.rgb = 0;
- }
- else
- {
- specRefl = attenuation * _LightColor0.rgb * _SpecColor.rgb *
- pow(max(0.0, dot(reflect(-lightDir, normalDir), viewDir)), _Shiny);
- }
- float4 diff = tex2D(_MainTex, _MainTex_ST.xy * input.tex.xy + _MainTex_ST.zw);
- return float4(diff * (diffRefl + specRefl), 1.0);
- }
- ENDCG // -------------END CG
- SubShader
- {
- Pass
- {
- Tags{"LightMode" = "ForwardBase"}
- ///pass for ambient
- CGPROGRAM
- #pragma vertex VS
- #pragma fragment PS_A
- ENDCG
- }
- Pass
- {
- Tags{"LightMode" = "ForwardAdd"}
- ///pass for ambient
- CGPROGRAM
- #pragma vertex VS
- #pragma fragment PS
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement