|
9 | 9 | #include <metal_stdlib>
|
10 | 10 | using namespace metal;
|
11 | 11 |
|
| 12 | +kernel void deformNormal(const device float3 *inVerts [[ buffer(0) ]], |
| 13 | + device float3 *outVerts [[ buffer(1) ]], |
| 14 | + device float3 *outNormals [[ buffer(2) ]], |
| 15 | + uint id [[ thread_position_in_grid ]]) |
| 16 | +{ |
| 17 | + |
| 18 | + if (id % 3 == 0) { |
| 19 | + |
| 20 | + const float3 v1 = inVerts[id]; |
| 21 | + const float3 v2 = inVerts[id + 1]; |
| 22 | + const float3 v3 = inVerts[id + 2]; |
| 23 | + |
| 24 | + const float3 v12 = v2 - v1; |
| 25 | + const float3 v13 = v3 - v1; |
| 26 | + |
| 27 | + const float3 normal = fast::normalize(cross(v12, v13)); |
| 28 | + |
| 29 | + outVerts[id] = v1; |
| 30 | + outVerts[id + 1] = v2; |
| 31 | + outVerts[id + 2] = v3; |
| 32 | + |
| 33 | + outNormals[id] = normal; |
| 34 | + outNormals[id + 1] = normal; |
| 35 | + outNormals[id + 2] = normal; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +struct DeformData { |
| 41 | + float3 location; |
| 42 | +}; |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +static float3 mod289(float3 x) { |
| 49 | + return x - floor(x * (1.0 / 289.0)) * 289.0; |
| 50 | +} |
| 51 | + |
| 52 | +static float2 mod289(float2 x) { |
| 53 | + return x - floor(x * (1.0 / 289.0)) * 289.0; |
| 54 | +} |
| 55 | + |
| 56 | +static float3 permute(float3 x) { |
| 57 | + return mod289(((x*34.0)+1.0)*x); |
| 58 | +} |
| 59 | + |
| 60 | +static float snoise(float2 v) |
| 61 | +{ |
| 62 | + const float4 C = float4(0.211324865405187, // (3.0-sqrt(3.0))/6.0 |
| 63 | + 0.366025403784439, // 0.5*(sqrt(3.0)-1.0) |
| 64 | + -0.577350269189626, // -1.0 + 2.0 * C.x |
| 65 | + 0.024390243902439); // 1.0 / 41.0 |
| 66 | + // First corner |
| 67 | + float2 i = floor(v + dot(v, C.yy) ); |
| 68 | + float2 x0 = v - i + dot(i, C.xx); |
| 69 | + |
| 70 | + // Other corners |
| 71 | + float2 i1; |
| 72 | + //i1.x = step( x0.y, x0.x ); // x0.x > x0.y ? 1.0 : 0.0 |
| 73 | + //i1.y = 1.0 - i1.x; |
| 74 | + i1 = (x0.x > x0.y) ? float2(1.0, 0.0) : float2(0.0, 1.0); |
| 75 | + // x0 = x0 - 0.0 + 0.0 * C.xx ; |
| 76 | + // x1 = x0 - i1 + 1.0 * C.xx ; |
| 77 | + // x2 = x0 - 1.0 + 2.0 * C.xx ; |
| 78 | + float4 x12 = x0.xyxy + C.xxzz; |
| 79 | + x12.xy -= i1; |
| 80 | + |
| 81 | + // Permutations |
| 82 | + i = mod289(i); // Avoid truncation effects in permutation |
| 83 | + float3 p = permute( permute( i.y + float3(0.0, i1.y, 1.0 )) |
| 84 | + + i.x + float3(0.0, i1.x, 1.0 )); |
| 85 | + |
| 86 | + float3 m = max(0.5 - float3(dot(x0,x0), dot(x12.xy,x12.xy), dot(x12.zw,x12.zw)), 0.0); |
| 87 | + m = m*m ; |
| 88 | + m = m*m ; |
| 89 | + |
| 90 | + // Gradients: 41 points uniformly over a line, mapped onto a diamond. |
| 91 | + // The ring size 17*17 = 289 is close to a multiple of 41 (41*7 = 287) |
| 92 | + |
| 93 | + float3 x = 2.0 * fract(p * C.www) - 1.0; |
| 94 | + float3 h = abs(x) - 0.5; |
| 95 | + float3 ox = floor(x + 0.5); |
| 96 | + float3 a0 = x - ox; |
| 97 | + |
| 98 | + // Normalise gradients implicitly by scaling m |
| 99 | + // Approximation of: m *= inversesqrt( a0*a0 + h*h ); |
| 100 | + m *= 1.79284291400159 - 0.85373472095314 * ( a0*a0 + h*h ); |
| 101 | + |
| 102 | + // Compute final noise value at P |
| 103 | + float3 g; |
| 104 | + g.x = a0.x * x0.x + h.x * x0.y; |
| 105 | + g.yz = a0.yz * x12.xz + h.yz * x12.yw; |
| 106 | + return 130.0 * dot(m, g); |
| 107 | +} |
| 108 | + |
| 109 | +static float noisy(float2 x) { |
| 110 | + return snoise(float2(snoise(x),snoise(x))); |
| 111 | +} |
| 112 | + |
| 113 | +kernel void deformVertexNoise(const device float3 *inVerts [[ buffer(0) ]], |
| 114 | + device float3 *outVerts [[ buffer(1) ]], |
| 115 | + constant DeformData &deformD [[ buffer(2)]], |
| 116 | + uint id [[ thread_position_in_grid ]]) |
| 117 | +{ |
| 118 | + |
| 119 | + |
| 120 | + const float3 inVert = inVerts[id]; |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + // const float3 outVert = float3(inVert.x,noisy(((inVert.xy*inVert.yz)/30.0*cos(deformD.location.x))+deformD.location.x),inVert.z); |
| 125 | + const float3 outVert = float3(inVert.x,inVert.y,inVert.z); |
| 126 | + |
| 127 | + outVerts[id] = outVert; |
| 128 | +} |
12 | 129 |
|
0 commit comments