Search Unity

Object intersection problem, every shader is not working. Please help spent 3 days already.

Discussion in 'Shaders' started by cometa93, Sep 17, 2017.

  1. cometa93

    cometa93

    Joined:
    Mar 5, 2014
    Posts:
    41
    Hello Unity friends,

    I'm struggling with the water shader for my game, actually long time ago it was working (in older versions of unity) and now i do not know what have changed... Can't find out what is happening. What i want to achieve is water shoreline or foam ( Simple geometry intersection). Currently my water is not transparent. It is using grab pass to take what is behind and is making distort effect, First pass is writing only distorted grab texture next pass is responsible for lighting, shoreline and color. Didn't optimize anything yet, because it's not working.

    I've tried to use other intersection shaders for example this:
    (package to download is on github). But still got same problem, and on video it's totally working ( like my water).

    So here is how it was working ( old screen ):



    And what is happening :
    Firstly example scene from the package

    Secondly my scene with debug render of depth texture on second screen:


    Shader Code ( Don't judge already tried everything so it's dirty now):
    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. CGINCLUDE
    4. float3 getVertPosition(float3 vertexXYZW)
    5. {
    6.     float3 worldPosition = mul(unity_ObjectToWorld, vertexXYZW);
    7.     vertexXYZW.z += sin(worldPosition.z + worldPosition.x + _Time[1]) / 10;
    8.     vertexXYZW.x += sin(worldPosition.z + worldPosition.y + _Time[1]) / 10;
    9.     vertexXYZW.y += sin(worldPosition.z + worldPosition.x + _Time[1]) / 10;
    10.     return vertexXYZW;
    11. }
    12. float4 distortUV(float4 uv, float3 position, float strenght)
    13. {
    14.     float3 worldPosition = mul(unity_ObjectToWorld, position);
    15.     uv.x += sin((worldPosition.z + _Time[0]) / (1/strenght*100)) ;
    16.     return uv;
    17. }
    18.  
    19. //HERE IS TUTORIAL FOR THAT MAGIC http://diary.conewars.com/vertex-displacement-shader/
    20. //IT IS CALCULATING NEW NORMAL AFTER VERTEX DISPLACEMENT
    21. void getNormal(float3 position, float3 oldPosition, float3 normal, float3 tangent, out float3 OUTtangent, out float3 OUTnormal)
    22. {
    23.     float3 bitangent = cross(normal, tangent);
    24.     float3 positionAndTangent = getVertPosition(oldPosition.xyz + tangent * 0.1);
    25.     float3 positionAndBitangent = getVertPosition(oldPosition.xyz + bitangent * 0.1);
    26.     float3 newtangent = positionAndTangent - position;
    27.     float3 newbitangent = positionAndBitangent - position;
    28.     float3 newNormal = cross(newtangent, newbitangent);
    29.  
    30.     OUTnormal = newNormal;
    31.     OUTtangent = newtangent;
    32. }
    33.  
    34. ENDCG
    35.  
    36. Shader "Cosmos Quest/LowPolyWater 2"
    37. {
    38.     Properties
    39.     {
    40.         _BumpMap("Normal Map", 2D) = "bump" {}
    41.         _Color("Diffuse Material Color", Color) = (1,1,1,1)
    42.         _ShoreLineColor("ShoreLine Color", Color) = (1,1,1,1)
    43.         _BumpMapSize("Normal Map Size", Range(0.1,10)) = 1
    44.         _LightStrength("Light strength factor", Range(0,10)) = 1
    45.         _WaterDistortionStrenght("Water distortion strenght factor", Range(0.1,100)) = 1
    46.         _Fade("Shore Line offset", Range(0,10000)) = 1
    47.     }
    48.  
    49.         SubShader
    50.         {
    51.             LOD 100
    52.             ZWrite Off
    53.             Tags
    54.             {
    55.                 "RenderType" = "Transparent"
    56.                 "LightMode" = "ForwardBase"
    57.                 "Queue" = "Geometry"
    58.             }
    59.  
    60.  
    61.                 GrabPass
    62.             {
    63.                 "_BackTexture"
    64.             }
    65.  
    66.                 Pass
    67.             {
    68.                 Blend One Zero
    69.     CGPROGRAM
    70.                 #pragma fragment frag
    71.                 #pragma vertex vert
    72.                 #include "UnityCG.cginc"
    73.  
    74.                 uniform sampler2D _CameraDepthTexture;
    75.  
    76.                 sampler2D _BackTexture;
    77.                 float _WaterDistortionStrenght;
    78.  
    79.                 struct vertexIN {
    80.                     float4 pos : SV_POSITION;
    81.                     fixed4 grabUV : TEXCOORD0;
    82.                 };
    83.  
    84.                 vertexIN vert(appdata_base IN)
    85.                 {
    86.                     vertexIN OUT;
    87.                     float3 position = getVertPosition(IN.vertex);
    88.                     OUT.pos = UnityObjectToClipPos(position);
    89.                     OUT.grabUV = ComputeGrabScreenPos(OUT.pos);
    90.                     OUT.grabUV = distortUV(OUT.grabUV, OUT.pos, _WaterDistortionStrenght);
    91.                     return OUT;
    92.                 }
    93.  
    94.                 fixed4 frag(vertexIN IN) : SV_TARGET
    95.                 {
    96.                     fixed4 color = tex2Dproj(_BackTexture, IN.grabUV);
    97.                     color.a = 1;
    98.                     return color;
    99.                 }
    100.     ENDCG
    101.             }
    102.  
    103.  
    104.             Pass
    105.             {
    106.                 Blend SrcAlpha OneMinusSrcAlpha
    107.                
    108.  
    109.                 CGPROGRAM
    110.     #pragma vertex vert
    111.     #pragma fragment frag
    112.  
    113.     #include "UnityCG.cginc"
    114.     #include "AutoLight.cginc"
    115.  
    116.                 //FROM AUTOLIGHT
    117.                 uniform float4 _LightColor0;
    118.  
    119.                 // User-specified properties
    120.                 sampler2D _CameraDepthTexture;
    121.                 sampler2D _BumpMap;
    122.                 float _BumpMapSize;
    123.                 float4 _BumpMap_ST;
    124.                 fixed4 _Color;
    125.                 fixed4 _ShoreLineColor;
    126.                 float _Fade;
    127.                 float _LightStrength;
    128.  
    129.                 struct vertexIN {
    130.                     float4 pos : SV_POSITION;
    131.                     float3    normal : NORMAL;
    132.                     float3 tangent : TANGENT;
    133.                     half2 uv : TEXCOORD0;
    134.                     float3 tangentWorld : TEXCOORD1;
    135.                     float3 normalWorld : TEXCOORD2;
    136.                     float3 binormalWorld : TEXCOORD3;
    137.                     float4 posWorld : TEXCOORD4;
    138.                     float4 projPos : TEXCOORD5;
    139.                 };
    140.  
    141.                 vertexIN vert(appdata_full IN)
    142.                 {
    143.                     vertexIN OUT;
    144.                     float3 position = getVertPosition(IN.vertex);
    145.  
    146.                     float4 projPositionCalc = UnityObjectToClipPos(IN.vertex);
    147.                     OUT.pos = UnityObjectToClipPos(position);
    148.                     OUT.projPos = ComputeScreenPos(OUT.pos);
    149.                     OUT.uv = IN.vertex;
    150.                    
    151.                     getNormal(position, IN.vertex,IN.normal,IN.tangent,OUT.tangent, OUT.normal);
    152.                     half3 worldNormal = UnityObjectToWorldNormal(OUT.normal);
    153.  
    154.                     float4x4 modelMatrix = unity_ObjectToWorld;
    155.                     float4x4 modelMatrixInverse = unity_WorldToObject;
    156.  
    157.                     OUT.posWorld = mul(modelMatrix, position);
    158.                     OUT.tangentWorld = normalize( mul(modelMatrix, float4(OUT.tangent.xyz, 0.0)).xyz);
    159.                     OUT.normalWorld = normalize( mul(float4(OUT.normal, 0.0), modelMatrixInverse).xyz);
    160.                     OUT.binormalWorld = normalize(cross(OUT.normalWorld, OUT.tangentWorld));
    161.  
    162.                     return OUT;
    163.                 }
    164.  
    165.                 fixed4 calculateAndReturnColor(vertexIN IN)
    166.                 {
    167.                     //Normal mapping
    168.                     fixed2 distortedBumpUv =  IN.uv / _BumpMapSize;
    169.                     float4 encodedNormal = tex2D(_BumpMap, distortedBumpUv);
    170.                     float3 localCoords = 2.0 * encodedNormal.rgb - float3(1.0, 1.0, 1.0);
    171.                     float3x3 local2WorldTranspose = float3x3(
    172.                         IN.tangentWorld,
    173.                         IN.binormalWorld,
    174.                         IN.normalWorld);
    175.  
    176.                     float3 normalDirection = normalize(mul(localCoords, local2WorldTranspose));
    177.                     float3 viewDirection = normalize(_WorldSpaceCameraPos - IN.posWorld.xyz);
    178.  
    179.                     float3 lightDirection;
    180.                     float attenuation;
    181.                     if (0.0 == _WorldSpaceLightPos0.w) // directional light?
    182.                     {
    183.                         attenuation = 1.0; // no attenuation
    184.                         lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    185.                     }
    186.                     else // point or spot light
    187.                     {
    188.                         float3 vertexToLightSource =
    189.                             _WorldSpaceLightPos0.xyz - IN.posWorld.xyz;
    190.                         float distance = length(vertexToLightSource);
    191.                         attenuation = 1.0 / distance; // linear attenuation
    192.                         lightDirection = normalize(vertexToLightSource);
    193.                         return fixed4(1, 0, 0, 1);
    194.                     }
    195.  
    196.                     float3 ambientLighting = UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb;
    197.  
    198.                     float3 diffuseReflection = attenuation * _LightColor0.rgb * _Color.rgb * max(0.0, dot(normalDirection, lightDirection));
    199.                     fixed4 color = fixed4(ambientLighting + diffuseReflection, 1.0);
    200.  
    201.                     return color;
    202.                 }
    203.  
    204.                 fixed4 frag(vertexIN IN) : SV_TARGET
    205.                 {
    206.                     fixed4 col = _Color;
    207.                     float depth = LinearEyeDepth(UNITY_SAMPLE_DEPTH(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.projPos))));
    208.                     float currDepth = IN.projPos.z/IN.projPos.w;
    209.  
    210.                     float fade = saturate((depth - currDepth)/_Fade);
    211.                     if (fade < 0.1) {
    212.                         return 1;
    213.                     }
    214.                     //If the two are similar, then there is an object intersecting with our object
    215.                     col.rgb    +=    (_LightStrength * calculateAndReturnColor(IN).rgb);
    216.                     col.a = 0.5;
    217.                     return col;
    218.                 }
    219. ENDCG
    220.             }
    221.         }
    222. }
    223.  
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Unity Technologies implemented in Unity 5.5 an inverted z-buffer. Perhaps the issue you're having is related to this issue?

    The upgrade guide outlines what shader changes are necessary to properly read from a depth texture now:
    https://docs.unity3d.com/Manual/UpgradeGuide55.html
     
  3. cometa93

    cometa93

    Joined:
    Mar 5, 2014
    Posts:
    41
    S*** ... I've missed that ... Thank you will try IT this evening.