Advertisement
Coguelin

Simple Cubemap Frag Shader

Oct 21st, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. Shader "FX/FragCubemap"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
  6.         _EnvTex ("CubeMap", CUBE) = "black" {}
  7.         _MainTint ("Diffuse Tint", Color) = (1,1,1,1)
  8.         _ReflAmount ("Reflection Amount", Range(0.01, 1)) = 0.5
  9.     }
  10.    
  11.     SubShader
  12.     {
  13.         Tags { "RenderType"="Opaque" "LightMode"="ForwardBase"}
  14.         LOD 200
  15.        
  16.         CGINCLUDE
  17.         #include "UnityCG.cginc"
  18.        
  19.         sampler2D _MainTex;
  20.         fixed4 _MainTex_ST;
  21.        
  22.         samplerCUBE _EnvTex;
  23.        
  24.         #ifndef LIGHTMAP_OFF
  25.         fixed4 unity_LightmapST;
  26.         sampler2D unity_Lightmap;
  27.         #endif
  28.        
  29.         fixed4 _MainTint;
  30.         fixed _ReflAmount;
  31.        
  32.         struct v2f
  33.         {
  34.             fixed4 pos : SV_POSITION;
  35.             fixed2 uv : TEXCOORD0;
  36.            
  37.             #ifndef LIGHTMAP_OFF
  38.             fixed2 uvLM : TEXCOORD1;
  39.             #endif
  40.            
  41.             fixed3 refl : TEXCOORD2;
  42.         };
  43.         ENDCG
  44.    
  45.         Pass
  46.         {
  47.             CGPROGRAM
  48.             #pragma vertex vert
  49.             #pragma fragment frag
  50.             #pragma fragmentoption ARB_precision_hint_fastest
  51.            
  52.             v2f vert (appdata_full v)
  53.             {
  54.                 v2f o;
  55.                
  56.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  57.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  58.                
  59.                 #ifndef LIGHTMAP_OFF
  60.                 o.uvLM = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  61.                 #endif
  62.                
  63.                 fixed3 worldNormal = mul((fixed3x3)_Object2World, v.normal);
  64.                 fixed3 worldViewDir = normalize(WorldSpaceViewDir(v.vertex));
  65.                
  66.                 o.refl = reflect(normalize(-worldViewDir.xyz), normalize(worldNormal));
  67.                
  68.                 return o;
  69.             }
  70.            
  71.             fixed4 frag (v2f i) : COLOR
  72.             {
  73.                 fixed4 rFactor = texCUBE(_EnvTex, (i.refl)) * _ReflAmount;
  74.            
  75.                 fixed4 tex = tex2D(_MainTex, i.uv.xy) * _MainTint;
  76.                
  77.                 tex += rFactor * tex.a;
  78.                
  79.                 #ifndef LIGHTMAP_OFF
  80.                 fixed3 lm = DecodeLightmap (tex2D(unity_Lightmap, i.uvLM));
  81.                 tex.xyz *= lm;
  82.                 #endif
  83.                
  84.                 return tex;
  85.             }
  86.             ENDCG
  87.         }  
  88.     }
  89.    
  90. FallBack "MADFINGER/Environment/Cube env map (Supports Lightmap)"
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement