Search Unity

(Help!) Vertex Displacement with Collision

Discussion in 'Physics' started by TheCGMaster, Jun 3, 2019.

  1. TheCGMaster

    TheCGMaster

    Joined:
    May 11, 2016
    Posts:
    21
    Hello! I'm creating a water simulation and I'm having trouble getting the position of the boat to follow the waves. Because I'm using Vertex Displacement the mesh itself doesn't change, neither does the collider. So getting that Vertex position is a bit harder then I thought.

    Currently the boat stays in one place and the waves animate to make it seem like I'm moving, when really, I'm not. I need the boat to stay afloat in that one position.

    Right now, I see doing this in two ways. Either copying the math used to create the waves and then apply that to the height of the boat. Or by constantly updating a mesh collider. I dont know how to go about doing either of these things. I found the math controlling the waves but have no clue how to transfer it into a different script. And I'm not sure how to convert that "illusion" of a mesh into an actual mesh.
     
    DonPuno likes this.
  2. TheCGMaster

    TheCGMaster

    Joined:
    May 11, 2016
    Posts:
    21
    Here is the math used. They created their own "cginc" script and used that.
    Code (CSharp):
    1.  
    2. float snoise(float3 v)
    3. {
    4.     const float2 C = float2(
    5.         0.166666666666666667, // 1/6
    6.         0.333333333333333333  // 1/3
    7.     );
    8.     const float4 D = float4(0.0, 0.5, 1.0, 2.0);
    9.    
    10. // First corner
    11.     float3 i = floor( v + dot(v, C.yyy) );
    12.     float3 x0 = v - i + dot(i, C.xxx);
    13.    
    14. // Other corners
    15.     float3 g = step(x0.yzx, x0.xyz);
    16.     float3 l = 1 - g;
    17.     float3 i1 = min(g.xyz, l.zxy);
    18.     float3 i2 = max(g.xyz, l.zxy);
    19.    
    20.     float3 x1 = x0 - i1 + C.xxx;
    21.     float3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
    22.     float3 x3 = x0 - D.yyy;      // -1.0+3.0*C.x = -0.5 = -D.y
    23.    
    24. // Permutations
    25.     i = mod289(i);
    26.     float4 p = permute(
    27.         permute(
    28.             permute(
    29.                     i.z + float4(0.0, i1.z, i2.z, 1.0 )
    30.             ) + i.y + float4(0.0, i1.y, i2.y, 1.0 )
    31.         )     + i.x + float4(0.0, i1.x, i2.x, 1.0 )
    32.     );
    33.    
    34. // Gradients: 7x7 points over a square, mapped onto an octahedron.
    35. // The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
    36.     float n_ = 0.142857142857; // 1/7
    37.     float3 ns = n_ * D.wyz - D.xzx;
    38.    
    39.     float4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
    40.    
    41.     float4 x_ = floor(j * ns.z);
    42.     float4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
    43.    
    44.     float4 x = x_ *ns.x + ns.yyyy;
    45.     float4 y = y_ *ns.x + ns.yyyy;
    46.     float4 h = 1.0 - abs(x) - abs(y);
    47.    
    48.     float4 b0 = float4( x.xy, y.xy );
    49.     float4 b1 = float4( x.zw, y.zw );
    50.    
    51.     //float4 s0 = float4(lessThan(b0,0.0))*2.0 - 1.0;
    52.     //float4 s1 = float4(lessThan(b1,0.0))*2.0 - 1.0;
    53.     float4 s0 = floor(b0)*2.0 + 1.0;
    54.     float4 s1 = floor(b1)*2.0 + 1.0;
    55.     float4 sh = -step(h, 0.0);
    56.    
    57.     float4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
    58.     float4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
    59.    
    60.     float3 p0 = float3(a0.xy,h.x);
    61.     float3 p1 = float3(a0.zw,h.y);
    62.     float3 p2 = float3(a1.xy,h.z);
    63.     float3 p3 = float3(a1.zw,h.w);
    64.    
    65. //Normalise gradients
    66.     float4 norm = taylorInvSqrt(float4(
    67.         dot(p0, p0),
    68.         dot(p1, p1),
    69.         dot(p2, p2),
    70.         dot(p3, p3)
    71.     ));
    72.     p0 *= norm.x;
    73.     p1 *= norm.y;
    74.     p2 *= norm.z;
    75.     p3 *= norm.w;
    76.    
    77. // Mix final noise value
    78.     float4 m = max(
    79.         0.6 - float4(
    80.             dot(x0, x0),
    81.             dot(x1, x1),
    82.             dot(x2, x2),
    83.             dot(x3, x3)
    84.         ),
    85.         0.0
    86.     );
    87.     m = m * m;
    88.     return 42.0 * dot(
    89.         m*m,
    90.         float4(
    91.             dot(p0, x0),
    92.             dot(p1, x1),
    93.             dot(p2, x2),
    94.             dot(p3, x3)
    95.         )
    96.     );
    97. }
    98.  
     
    DonPuno likes this.