Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Unlit/pbr_smoke"
- {
- Properties
- {
- // Consider this our "rawLightMap" in the example
- _rawLightMap ("Tangent Light Maps (RGBA)", 2D) = "white" {}
- _auxMap("Color Key (R) and Alpha (G)", 2D) = "white" {}
- _lightColor("Light Color", Color) = (1,1,1,1)
- _ambientColor("Ambient Color", Color) = (1,1,1,1)
- }
- SubShader
- {
- Tags { "RenderType" = "Opaque" }
- LOD 100
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #include "UnityCG.cginc"
- // Probably a bunch of redundant information in vertex to fragment (v2f) as I only pass along appdata.
- struct appdata
- {
- float4 vertex : POSITION;
- float2 uv : TEXCOORD0;
- float4 tangent : TANGENT;
- float3 normal : NORMAL;
- };
- struct v2f
- {
- float2 uv : TEXCOORD0;
- float4 vertex : SV_POSITION;
- float4 tangent : TANGENT;
- float3 normal : NORMAL;
- };
- sampler2D _rawLightMap;
- float4 _rawLightMap_ST; // Apparently used by TRANSFORM_TEX macro
- sampler2D _auxMap;
- float4 _auxMap_ST;
- float3 _ambientColor;
- float3 _lightColor;
- v2f vert (appdata v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
- o.uv = TRANSFORM_TEX(v.uv, _rawLightMap);
- o.tangent = v.tangent;
- o.normal = UnityObjectToWorldNormal(v.normal);
- return o;
- }
- fixed4 frag (v2f i) : SV_Target
- {
- // Normal, Tangent and Binormal
- float3 N = i.normal; // Already cast into worldspace
- float3 T = mul((float3x3)unity_ObjectToWorld, i.tangent.xyz); // Casting the tangent vector to world, ie the vector pointing in X direction from the objects perspective.
- float3 B = cross(N, T) * i.tangent.w; // Binormal is the cross of Normal and Tangent. Giving us an orthogonal set of vectors.
- // Compose into a matrix, where the Normal of the object is Z, Tangent X and Binormal Y
- float3x3 worldToTangent = float3x3(T, B, N);
- // Normalized vector for lightDirection, apparently Unity already allows getting the vector for light, should be possible in Unreal as well.
- float3 lightDirection = _WorldSpaceLightPos0.xyz;
- // Cast lightDirection vector into tangentspace of current object.
- float3 tangentLightDirection = mul(worldToTangent, lightDirection); // Inverted light vector to match the tutorial
- // Just define the variables
- float hMap;
- float vMap;
- float dMap;
- float lightMap;
- // Sample and combine the value for pixel into a float for front map
- float frontMap = 0.25f * ( tex2D(_rawLightMap, i.uv).r + tex2D(_rawLightMap, i.uv).g + tex2D(_rawLightMap, i.uv).b + tex2D(_rawLightMap, i.uv).a );
- frontMap = pow(frontMap, 0.625);
- float backMap = 1.0f - frontMap;
- // Assuming the normal used is worldspace?, and backMap * .. four times is pow, not sure why that wasn't used.
- //backMap = saturate(0.25f * (1.0f - N.x) + 0.5f * pow(backMap, 4));
- // It wasn't, seem like object space normal was needed.
- float3 N2 = normalize(mul((float3x3)unity_WorldToObject, N));
- backMap = saturate(0.25f * (1.0f - N2.x) + 0.5f * pow(backMap, 4));
- // Light shining from right-left will be red channel 1 -> -1
- if (tangentLightDirection.x > 0.0f)
- {
- hMap = tex2D(_rawLightMap, i.uv).r; // Right
- }
- else {
- hMap = tex2D(_rawLightMap, i.uv).g; // Left
- }
- // Light shining from above-below will be green channel
- if (tangentLightDirection.y > 0.0f)
- {
- vMap = tex2D(_rawLightMap, i.uv).b; // Above
- }
- else {
- vMap = tex2D(_rawLightMap, i.uv).a; // Below
- }
- // Light shining straight at the face will be black, shining through the object will be blue.
- if (tangentLightDirection.z > 0.0f)
- {
- dMap = frontMap; // At
- }
- else {
- dMap = backMap; // Through
- }
- // The pythagorial combo, map direction times light direction squared for all directions
- lightMap = hMap * tangentLightDirection.x * tangentLightDirection.x + vMap * tangentLightDirection.y * tangentLightDirection.y + dMap * tangentLightDirection.z * tangentLightDirection.z;
- // Fragment output has to be of type fixed4
- fixed4 col;
- // Combine the light map as specified on the post.
- float combinedLight = _lightColor * lightMap + _ambientColor;
- // Set final color and alpha
- col.rgb = float3(T);
- col.a = tex2D(_auxMap, i.uv);
- return col;
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement