Search Unity

Problems creating a 3D/2D mix game

Discussion in 'General Graphics' started by RafaelDeJota, Jun 19, 2019.

  1. RafaelDeJota

    RafaelDeJota

    Joined:
    Sep 21, 2018
    Posts:
    4
    Hey,

    I am a game art student and I usually don't have to get very deep into unity to get my part of projects done. However, I decided to challenge myself to make a game in unity by myself. Game play wise things have been smooth, but graphics, ironically have become a problem.
    I want to recreate or emulate, the graphic style of games like "The Last Light" and "Octopath Traveller", mixing 3D environments and pixel art. My problem is with the characters, they use sprite shaders by default, which don't react to light, so i had to change it to something else. But nothing seems to work 100%.

    If I use other sprite shaders, the depth of field will affect them in weird way. The parts of the sprite that are over far away objects will blur even if the sprite itself is in a distance that the effect should not happen.

    If I try with a "standard material" with cut out, the deph of field works ok but the sprite cant be flipped, since the normal's now point backwards, so I cant play the animations to the other direction.

    The closest I've come to a solution was a double sided shader, but the ones that I've come across have a pretty fatal flaw, the lighting from the behind becomes the one in the front. I think the normals in the back face are inverted and I dont know almost anything about shaders to be able to fix this.

    Any help would be great!
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    How are you flipping these? If you have a quad and rotate 180 it won't work, but if you scale the quad by (-1,1,1) then the quad will be reversed, but the normal will still face the same direction.
     
  3. RafaelDeJota

    RafaelDeJota

    Joined:
    Sep 21, 2018
    Posts:
    4
    I have scaled it by (-1,1,1), that is the weird part. The lighting from the back part, with the yellow tint is showing in the front.
    You can see it here:
    there is a yellow light above and behind these two objects. The one on the right has been scaled by (-1,1,1)
    upload_2019-6-19_11-43-37.png
    And here is the back view with the inverted one on the left, not being affected by the light right next to it
    upload_2019-6-19_11-45-18.png
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Are you using normal maps?
     
  5. RafaelDeJota

    RafaelDeJota

    Joined:
    Sep 21, 2018
    Posts:
    4
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Ah, I didn't realize that at first. I don't think normal maps work flipped. You might have to make a separate set of sprites for each direction.
     
  7. RafaelDeJota

    RafaelDeJota

    Joined:
    Sep 21, 2018
    Posts:
    4
    Yeah, its the only option apparently, the resolution of the characters is so small that this will not have much impact on size, but it sure will be annoying having to duplicate them on the sprite sheet.
    Thanks for the help anyway.

    If else anyone come across this, this is the double sided shader i was using, it can be freely downloaded here https://assetstore.unity.com/packages/vfx/shaders/double-sided-shaders-23087

    If anyone finds whats making this happen it would make my life a quite bit easier.

    Code (CSharp):
    1. Shader "Ciconia Studio/Double Sided/Transparent/Diffuse Bump Cutout" {
    2.     Properties {
    3.         _Color ("Diffuse Color", Color) = (1,1,1,1)
    4.         _MainTex ("Diffuse map (Cutout A)", 2D) = "white" {}
    5.         _BumpMap ("Normal map", 2D) = "bump" {}
    6.         _NormalIntensity ("Normal Intensity", Range(0, 2)) = 1
    7.         _SpecGlossMap ("Specular map", 2D) = "white" {}
    8.         _SpecColor ("Specular Color", Color) = (1,1,1,1)
    9.         _SpecularIntensity ("Specular Intensity", Range(0, 2)) = 0.2
    10.         _Glossiness ("Glossiness", Range(0, 1)) = 0.5
    11.         [HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    12.     }
    13.     SubShader {
    14.         Tags {
    15.             "Queue"="AlphaTest"
    16.             "RenderType"="TransparentCutout"
    17.         }
    18.         Pass {
    19.             Name "FORWARD"
    20.             Tags {
    21.                 "LightMode"="ForwardBase"
    22.             }
    23.             Cull Off
    24.            
    25.            
    26.             CGPROGRAM
    27.             #pragma vertex vert
    28.             #pragma fragment frag
    29.             #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
    30.             #define _GLOSSYENV 1
    31.             #include "UnityCG.cginc"
    32.             #include "AutoLight.cginc"
    33.             #include "Lighting.cginc"
    34.             #include "UnityPBSLighting.cginc"
    35.             #include "UnityStandardBRDF.cginc"
    36.             #pragma multi_compile_fwdbase_fullshadows
    37.             #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
    38.             #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
    39.             #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
    40.             #pragma multi_compile_fog
    41.             #pragma only_renderers d3d9 d3d11 glcore gles gles3 metal d3d11_9x xboxone ps4 psp2 n3ds wiiu
    42.             #pragma target 3.0
    43.             uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
    44.             uniform float4 _Color;
    45.             uniform sampler2D _BumpMap; uniform float4 _BumpMap_ST;
    46.             uniform float _NormalIntensity;
    47.             uniform sampler2D _SpecGlossMap; uniform float4 _SpecGlossMap_ST;
    48.             uniform float _SpecularIntensity;
    49.             uniform float _Glossiness;
    50.             struct VertexInput {
    51.                 float4 vertex : POSITION;
    52.                 float3 normal : NORMAL;
    53.                 float4 tangent : TANGENT;
    54.                 float2 texcoord0 : TEXCOORD0;
    55.                 float2 texcoord1 : TEXCOORD1;
    56.                 float2 texcoord2 : TEXCOORD2;
    57.             };
    58.             struct VertexOutput {
    59.                 float4 pos : SV_POSITION;
    60.                 float2 uv0 : TEXCOORD0;
    61.                 float2 uv1 : TEXCOORD1;
    62.                 float2 uv2 : TEXCOORD2;
    63.                 float4 posWorld : TEXCOORD3;
    64.                 float3 normalDir : TEXCOORD4;
    65.                 float3 tangentDir : TEXCOORD5;
    66.                 float3 bitangentDir : TEXCOORD6;
    67.                 LIGHTING_COORDS(7,8)
    68.                 UNITY_FOG_COORDS(9)
    69.                 #if defined(LIGHTMAP_ON) || defined(UNITY_SHOULD_SAMPLE_SH)
    70.                     float4 ambientOrLightmapUV : TEXCOORD10;
    71.                 #endif
    72.             };
    73.             VertexOutput vert (VertexInput v) {
    74.                 VertexOutput o = (VertexOutput)0;
    75.                 o.uv0 = v.texcoord0;
    76.                 o.uv1 = v.texcoord1;
    77.                 o.uv2 = v.texcoord2;
    78.                 #ifdef LIGHTMAP_ON
    79.                     o.ambientOrLightmapUV.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    80.                     o.ambientOrLightmapUV.zw = 0;
    81.                 #endif
    82.                 #ifdef DYNAMICLIGHTMAP_ON
    83.                     o.ambientOrLightmapUV.zw = v.texcoord2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
    84.                 #endif
    85.                 o.normalDir = UnityObjectToWorldNormal(v.normal);
    86.                 o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );
    87.                 o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
    88.                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
    89.                 float3 lightColor = _LightColor0.rgb;
    90.                 o.pos = UnityObjectToClipPos( v.vertex );
    91.                 UNITY_TRANSFER_FOG(o,o.pos);
    92.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
    93.                 return o;
    94.             }
    95.             float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
    96.                 float isFrontFace = ( facing >= 0 ? 1 : 0 );
    97.                 float faceSign = ( facing >= 0 ? 1 : -1 );
    98.                 i.normalDir = normalize(i.normalDir);
    99.                 i.normalDir *= faceSign;
    100.                 float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
    101.                 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
    102.                 float3 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap)));
    103.                 float3 normalLocal = lerp(float3(0,0,1),_BumpMap_var.rgb,_NormalIntensity);
    104.                 float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
    105.                 float3 viewReflectDirection = reflect( -viewDirection, normalDirection );
    106.                 float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
    107.                 clip(_MainTex_var.a - 0.5);
    108.                 float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    109.                 float3 lightColor = _LightColor0.rgb;
    110.                 float3 halfDirection = normalize(viewDirection+lightDirection);
    111. ////// Lighting:
    112.                 UNITY_LIGHT_ATTENUATION(attenuation,i, i.posWorld.xyz);
    113.                 float3 attenColor = attenuation * _LightColor0.xyz;
    114.                 float Pi = 3.141592654;
    115.                 float InvPi = 0.31830988618;
    116. ///////// Gloss:
    117.                 float gloss = _Glossiness;
    118.                 float perceptualRoughness = 1.0 - _Glossiness;
    119.                 float roughness = perceptualRoughness * perceptualRoughness;
    120.                 float specPow = exp2( gloss * 10.0 + 1.0 );
    121. /////// GI Data:
    122.                 UnityLight light;
    123.                 #ifdef LIGHTMAP_OFF
    124.                     light.color = lightColor;
    125.                     light.dir = lightDirection;
    126.                     light.ndotl = LambertTerm (normalDirection, light.dir);
    127.                 #else
    128.                     light.color = half3(0.f, 0.f, 0.f);
    129.                     light.ndotl = 0.0f;
    130.                     light.dir = half3(0.f, 0.f, 0.f);
    131.                 #endif
    132.                 UnityGIInput d;
    133.                 d.light = light;
    134.                 d.worldPos = i.posWorld.xyz;
    135.                 d.worldViewDir = viewDirection;
    136.                 d.atten = attenuation;
    137.                 #if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)
    138.                     d.ambient = 0;
    139.                     d.lightmapUV = i.ambientOrLightmapUV;
    140.                 #else
    141.                     d.ambient = i.ambientOrLightmapUV;
    142.                 #endif
    143.                 #if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTION
    144.                     d.boxMin[0] = unity_SpecCube0_BoxMin;
    145.                     d.boxMin[1] = unity_SpecCube1_BoxMin;
    146.                 #endif
    147.                 #if UNITY_SPECCUBE_BOX_PROJECTION
    148.                     d.boxMax[0] = unity_SpecCube0_BoxMax;
    149.                     d.boxMax[1] = unity_SpecCube1_BoxMax;
    150.                     d.probePosition[0] = unity_SpecCube0_ProbePosition;
    151.                     d.probePosition[1] = unity_SpecCube1_ProbePosition;
    152.                 #endif
    153.                 d.probeHDR[0] = unity_SpecCube0_HDR;
    154.                 d.probeHDR[1] = unity_SpecCube1_HDR;
    155.                 Unity_GlossyEnvironmentData ugls_en_data;
    156.                 ugls_en_data.roughness = 1.0 - gloss;
    157.                 ugls_en_data.reflUVW = viewReflectDirection;
    158.                 UnityGI gi = UnityGlobalIllumination(d, 1, normalDirection, ugls_en_data );
    159.                 lightDirection = gi.light.dir;
    160.                 lightColor = gi.light.color;
    161. ////// Specular:
    162.                 float NdotL = saturate(dot( normalDirection, lightDirection ));
    163.                 float LdotH = saturate(dot(lightDirection, halfDirection));
    164.                 float4 _SpecGlossMap_var = tex2D(_SpecGlossMap,TRANSFORM_TEX(i.uv0, _SpecGlossMap));
    165.                 float3 specularColor = ((_SpecGlossMap_var.rgb*_SpecularIntensity)*_SpecColor.rgb);
    166.                 float specularMonochrome;
    167.                 float3 diffuseColor = (_MainTex_var.rgb*_Color.rgb); // Need this for specular when using metallic
    168.                 diffuseColor = EnergyConservationBetweenDiffuseAndSpecular(diffuseColor, specularColor, specularMonochrome);
    169.                 specularMonochrome = 1.0-specularMonochrome;
    170.                 float NdotV = abs(dot( normalDirection, viewDirection ));
    171.                 float NdotH = saturate(dot( normalDirection, halfDirection ));
    172.                 float VdotH = saturate(dot( viewDirection, halfDirection ));
    173.                 float visTerm = SmithJointGGXVisibilityTerm( NdotL, NdotV, roughness );
    174.                 float normTerm = GGXTerm(NdotH, roughness);
    175.                 float specularPBL = (visTerm*normTerm) * UNITY_PI;
    176.                 #ifdef UNITY_COLORSPACE_GAMMA
    177.                     specularPBL = sqrt(max(1e-4h, specularPBL));
    178.                 #endif
    179.                 specularPBL = max(0, specularPBL * NdotL);
    180.                 #if defined(_SPECULARHIGHLIGHTS_OFF)
    181.                     specularPBL = 0.0;
    182.                 #endif
    183.                 half surfaceReduction;
    184.                 #ifdef UNITY_COLORSPACE_GAMMA
    185.                     surfaceReduction = 1.0-0.28*roughness*perceptualRoughness;
    186.                 #else
    187.                     surfaceReduction = 1.0/(roughness*roughness + 1.0);
    188.                 #endif
    189.                 specularPBL *= any(specularColor) ? 1.0 : 0.0;
    190.                 float3 directSpecular = attenColor*specularPBL*FresnelTerm(specularColor, LdotH);
    191.                 half grazingTerm = saturate( gloss + specularMonochrome );
    192.                 float3 indirectSpecular = (gi.indirect.specular);
    193.                 indirectSpecular *= FresnelLerp (specularColor, grazingTerm, NdotV);
    194.                 indirectSpecular *= surfaceReduction;
    195.                 float3 specular = (directSpecular + indirectSpecular);
    196. /////// Diffuse:
    197.                 NdotL = max(0.0,dot( normalDirection, lightDirection ));
    198.                 half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);
    199.                 float nlPow5 = Pow5(1-NdotL);
    200.                 float nvPow5 = Pow5(1-NdotV);
    201.                 float3 directDiffuse = ((1 +(fd90 - 1)*nlPow5) * (1 + (fd90 - 1)*nvPow5) * NdotL) * attenColor;
    202.                 float3 indirectDiffuse = float3(0,0,0);
    203.                 indirectDiffuse += gi.indirect.diffuse;
    204.                 diffuseColor *= 1-specularMonochrome;
    205.                 float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
    206. /// Final Color:
    207.                 float3 finalColor = diffuse + specular;
    208.                 fixed4 finalRGBA = fixed4(finalColor,1);
    209.                 UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
    210.                 return finalRGBA;
    211.             }
    212.             ENDCG
    213.         }
    214.         Pass {
    215.             Name "FORWARD_DELTA"
    216.             Tags {
    217.                 "LightMode"="ForwardAdd"
    218.             }
    219.             Blend One One
    220.             Cull Off
    221.            
    222.            
    223.             CGPROGRAM
    224.             #pragma vertex vert
    225.             #pragma fragment frag
    226.             #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
    227.             #define _GLOSSYENV 1
    228.             #include "UnityCG.cginc"
    229.             #include "AutoLight.cginc"
    230.             #include "Lighting.cginc"
    231.             #include "UnityPBSLighting.cginc"
    232.             #include "UnityStandardBRDF.cginc"
    233.             #pragma multi_compile_fwdadd_fullshadows
    234.             #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
    235.             #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
    236.             #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
    237.             #pragma multi_compile_fog
    238.             #pragma only_renderers d3d9 d3d11 glcore gles gles3 metal d3d11_9x xboxone ps4 psp2 n3ds wiiu
    239.             #pragma target 3.0
    240.             uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
    241.             uniform float4 _Color;
    242.             uniform sampler2D _BumpMap; uniform float4 _BumpMap_ST;
    243.             uniform float _NormalIntensity;
    244.             uniform sampler2D _SpecGlossMap; uniform float4 _SpecGlossMap_ST;
    245.             uniform float _SpecularIntensity;
    246.             uniform float _Glossiness;
    247.             struct VertexInput {
    248.                 float4 vertex : POSITION;
    249.                 float3 normal : NORMAL;
    250.                 float4 tangent : TANGENT;
    251.                 float2 texcoord0 : TEXCOORD0;
    252.                 float2 texcoord1 : TEXCOORD1;
    253.                 float2 texcoord2 : TEXCOORD2;
    254.             };
    255.             struct VertexOutput {
    256.                 float4 pos : SV_POSITION;
    257.                 float2 uv0 : TEXCOORD0;
    258.                 float2 uv1 : TEXCOORD1;
    259.                 float2 uv2 : TEXCOORD2;
    260.                 float4 posWorld : TEXCOORD3;
    261.                 float3 normalDir : TEXCOORD4;
    262.                 float3 tangentDir : TEXCOORD5;
    263.                 float3 bitangentDir : TEXCOORD6;
    264.                 LIGHTING_COORDS(7,8)
    265.                 UNITY_FOG_COORDS(9)
    266.             };
    267.             VertexOutput vert (VertexInput v) {
    268.                 VertexOutput o = (VertexOutput)0;
    269.                 o.uv0 = v.texcoord0;
    270.                 o.uv1 = v.texcoord1;
    271.                 o.uv2 = v.texcoord2;
    272.                 o.normalDir = UnityObjectToWorldNormal(v.normal);
    273.                 o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );
    274.                 o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);
    275.                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
    276.                 float3 lightColor = _LightColor0.rgb;
    277.                 o.pos = UnityObjectToClipPos( v.vertex );
    278.                 UNITY_TRANSFER_FOG(o,o.pos);
    279.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
    280.                 return o;
    281.             }
    282.             float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
    283.                 float isFrontFace = ( facing >= 0 ? 1 : 0 );
    284.                 float faceSign = ( facing >= 0 ? 1 : -1 );
    285.                 i.normalDir = normalize(i.normalDir);
    286.                 i.normalDir *= faceSign;
    287.                 float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
    288.                 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
    289.                 float3 _BumpMap_var = UnpackNormal(tex2D(_BumpMap,TRANSFORM_TEX(i.uv0, _BumpMap)));
    290.                 float3 normalLocal = lerp(float3(0,0,1),_BumpMap_var.rgb,_NormalIntensity);
    291.                 float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
    292.                 float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
    293.                 clip(_MainTex_var.a - 0.5);
    294.                 float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));
    295.                 float3 lightColor = _LightColor0.rgb;
    296.                 float3 halfDirection = normalize(viewDirection+lightDirection);
    297. ////// Lighting:
    298.                 UNITY_LIGHT_ATTENUATION(attenuation,i, i.posWorld.xyz);
    299.                 float3 attenColor = attenuation * _LightColor0.xyz;
    300.                 float Pi = 3.141592654;
    301.                 float InvPi = 0.31830988618;
    302. ///////// Gloss:
    303.                 float gloss = _Glossiness;
    304.                 float perceptualRoughness = 1.0 - _Glossiness;
    305.                 float roughness = perceptualRoughness * perceptualRoughness;
    306.                 float specPow = exp2( gloss * 10.0 + 1.0 );
    307. ////// Specular:
    308.                 float NdotL = saturate(dot( normalDirection, lightDirection ));
    309.                 float LdotH = saturate(dot(lightDirection, halfDirection));
    310.                 float4 _SpecGlossMap_var = tex2D(_SpecGlossMap,TRANSFORM_TEX(i.uv0, _SpecGlossMap));
    311.                 float3 specularColor = ((_SpecGlossMap_var.rgb*_SpecularIntensity)*_SpecColor.rgb);
    312.                 float specularMonochrome;
    313.                 float3 diffuseColor = (_MainTex_var.rgb*_Color.rgb); // Need this for specular when using metallic
    314.                 diffuseColor = EnergyConservationBetweenDiffuseAndSpecular(diffuseColor, specularColor, specularMonochrome);
    315.                 specularMonochrome = 1.0-specularMonochrome;
    316.                 float NdotV = abs(dot( normalDirection, viewDirection ));
    317.                 float NdotH = saturate(dot( normalDirection, halfDirection ));
    318.                 float VdotH = saturate(dot( viewDirection, halfDirection ));
    319.                 float visTerm = SmithJointGGXVisibilityTerm( NdotL, NdotV, roughness );
    320.                 float normTerm = GGXTerm(NdotH, roughness);
    321.                 float specularPBL = (visTerm*normTerm) * UNITY_PI;
    322.                 #ifdef UNITY_COLORSPACE_GAMMA
    323.                     specularPBL = sqrt(max(1e-4h, specularPBL));
    324.                 #endif
    325.                 specularPBL = max(0, specularPBL * NdotL);
    326.                 #if defined(_SPECULARHIGHLIGHTS_OFF)
    327.                     specularPBL = 0.0;
    328.                 #endif
    329.                 specularPBL *= any(specularColor) ? 1.0 : 0.0;
    330.                 float3 directSpecular = attenColor*specularPBL*FresnelTerm(specularColor, LdotH);
    331.                 float3 specular = directSpecular;
    332. /////// Diffuse:
    333.                 NdotL = max(0.0,dot( normalDirection, lightDirection ));
    334.                 half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);
    335.                 float nlPow5 = Pow5(1-NdotL);
    336.                 float nvPow5 = Pow5(1-NdotV);
    337.                 float3 directDiffuse = ((1 +(fd90 - 1)*nlPow5) * (1 + (fd90 - 1)*nvPow5) * NdotL) * attenColor;
    338.                 diffuseColor *= 1-specularMonochrome;
    339.                 float3 diffuse = directDiffuse * diffuseColor;
    340. /// Final Color:
    341.                 float3 finalColor = diffuse + specular;
    342.                 fixed4 finalRGBA = fixed4(finalColor * 1,0);
    343.                 UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
    344.                 return finalRGBA;
    345.             }
    346.             ENDCG
    347.         }
    348.         Pass {
    349.             Name "ShadowCaster"
    350.             Tags {
    351.                 "LightMode"="ShadowCaster"
    352.             }
    353.             Offset 1, 1
    354.             Cull Off
    355.            
    356.             CGPROGRAM
    357.             #pragma vertex vert
    358.             #pragma fragment frag
    359.             #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
    360.             #define _GLOSSYENV 1
    361.             #include "UnityCG.cginc"
    362.             #include "Lighting.cginc"
    363.             #include "UnityPBSLighting.cginc"
    364.             #include "UnityStandardBRDF.cginc"
    365.             #pragma fragmentoption ARB_precision_hint_fastest
    366.             #pragma multi_compile_shadowcaster
    367.             #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
    368.             #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
    369.             #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
    370.             #pragma multi_compile_fog
    371.             #pragma only_renderers d3d9 d3d11 glcore gles gles3 metal d3d11_9x xboxone ps4 psp2 n3ds wiiu
    372.             #pragma target 3.0
    373.             uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
    374.             struct VertexInput {
    375.                 float4 vertex : POSITION;
    376.                 float2 texcoord0 : TEXCOORD0;
    377.                 float2 texcoord1 : TEXCOORD1;
    378.                 float2 texcoord2 : TEXCOORD2;
    379.             };
    380.             struct VertexOutput {
    381.                 V2F_SHADOW_CASTER;
    382.                 float2 uv0 : TEXCOORD1;
    383.                 float2 uv1 : TEXCOORD2;
    384.                 float2 uv2 : TEXCOORD3;
    385.                 float4 posWorld : TEXCOORD4;
    386.             };
    387.             VertexOutput vert (VertexInput v) {
    388.                 VertexOutput o = (VertexOutput)0;
    389.                 o.uv0 = v.texcoord0;
    390.                 o.uv1 = v.texcoord1;
    391.                 o.uv2 = v.texcoord2;
    392.                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
    393.                 o.pos = UnityObjectToClipPos( v.vertex );
    394.                 TRANSFER_SHADOW_CASTER(o)
    395.                 return o;
    396.             }
    397.             float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
    398.                 float isFrontFace = ( facing >= 0 ? 1 : 0 );
    399.                 float faceSign = ( facing >= 0 ? 1 : -1 );
    400.                 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
    401.                 float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
    402.                 clip(_MainTex_var.a - 0.5);
    403.                 SHADOW_CASTER_FRAGMENT(i)
    404.             }
    405.             ENDCG
    406.         }
    407.         Pass {
    408.             Name "Meta"
    409.             Tags {
    410.                 "LightMode"="Meta"
    411.             }
    412.             Cull Off
    413.            
    414.             CGPROGRAM
    415.             #pragma vertex vert
    416.             #pragma fragment frag
    417.             #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )
    418.             #define _GLOSSYENV 1
    419.             #include "UnityCG.cginc"
    420.             #include "Lighting.cginc"
    421.             #include "UnityPBSLighting.cginc"
    422.             #include "UnityStandardBRDF.cginc"
    423.             #include "UnityMetaPass.cginc"
    424.             #pragma fragmentoption ARB_precision_hint_fastest
    425.             #pragma multi_compile_shadowcaster
    426.             #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
    427.             #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE
    428.             #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON
    429.             #pragma multi_compile_fog
    430.             #pragma only_renderers d3d9 d3d11 glcore gles gles3 metal d3d11_9x xboxone ps4 psp2 n3ds wiiu
    431.             #pragma target 3.0
    432.             uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
    433.             uniform float4 _Color;
    434.             uniform sampler2D _SpecGlossMap; uniform float4 _SpecGlossMap_ST;
    435.             uniform float _SpecularIntensity;
    436.             uniform float _Glossiness;
    437.             struct VertexInput {
    438.                 float4 vertex : POSITION;
    439.                 float2 texcoord0 : TEXCOORD0;
    440.                 float2 texcoord1 : TEXCOORD1;
    441.                 float2 texcoord2 : TEXCOORD2;
    442.             };
    443.             struct VertexOutput {
    444.                 float4 pos : SV_POSITION;
    445.                 float2 uv0 : TEXCOORD0;
    446.                 float2 uv1 : TEXCOORD1;
    447.                 float2 uv2 : TEXCOORD2;
    448.                 float4 posWorld : TEXCOORD3;
    449.             };
    450.             VertexOutput vert (VertexInput v) {
    451.                 VertexOutput o = (VertexOutput)0;
    452.                 o.uv0 = v.texcoord0;
    453.                 o.uv1 = v.texcoord1;
    454.                 o.uv2 = v.texcoord2;
    455.                 o.posWorld = mul(unity_ObjectToWorld, v.vertex);
    456.                 o.pos = UnityMetaVertexPosition(v.vertex, v.texcoord1.xy, v.texcoord2.xy, unity_LightmapST, unity_DynamicLightmapST );
    457.                 return o;
    458.             }
    459.             float4 frag(VertexOutput i, float facing : VFACE) : SV_Target {
    460.                 float isFrontFace = ( facing >= 0 ? 1 : 0 );
    461.                 float faceSign = ( facing >= 0 ? 1 : -1 );
    462.                 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
    463.                 UnityMetaInput o;
    464.                 UNITY_INITIALIZE_OUTPUT( UnityMetaInput, o );
    465.                
    466.                 o.Emission = 0;
    467.                
    468.                 float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
    469.                 float3 diffColor = (_MainTex_var.rgb*_Color.rgb);
    470.                 float4 _SpecGlossMap_var = tex2D(_SpecGlossMap,TRANSFORM_TEX(i.uv0, _SpecGlossMap));
    471.                 float3 specColor = ((_SpecGlossMap_var.rgb*_SpecularIntensity)*_SpecColor.rgb);
    472.                 float specularMonochrome = max(max(specColor.r, specColor.g),specColor.b);
    473.                 diffColor *= (1.0-specularMonochrome);
    474.                 float roughness = 1.0 - _Glossiness;
    475.                 o.Albedo = diffColor + specColor * roughness * roughness * 0.5;
    476.                
    477.                 return UnityMetaFragment( o );
    478.             }
    479.             ENDCG
    480.         }
    481.     }
    482.     FallBack "Diffuse"
    483. }
     
    Last edited: Jun 19, 2019