Skip to content

Commit a0d555a

Browse files
committed
added some stuff that will inevitably be removed. gonna go with BSP for some constructive solid geometry stuff instead of voxels
1 parent c6e4837 commit a0d555a

14 files changed

+1282
-16
lines changed

Diff for: Compute.metal

+117
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,121 @@
99
#include <metal_stdlib>
1010
using namespace metal;
1111

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+
}
12129

Diff for: Extensions.swift

+28
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,31 @@
77
//
88

99
import Foundation
10+
11+
extension Array {
12+
var powerset: [[Element]] {
13+
guard count > 0 else {
14+
return [[]]
15+
}
16+
17+
// tail contains the whole array BUT the first element
18+
let tail = Array(self[1..<endIndex])
19+
20+
// head contains only the first element
21+
let head = self[0]
22+
23+
// computing the tail's powerset
24+
let withoutHead = tail.powerset
25+
26+
// mergin the head with the tail's powerset
27+
let withHead = withoutHead.map { $0 + [head] }
28+
29+
// returning the tail's powerset and the just computed withHead array
30+
return withHead + withoutHead
31+
}
32+
33+
func combinations(_ size: Int) -> [[Element]]{
34+
35+
return powerset.filter { $0.count == size }
36+
}
37+
}

0 commit comments

Comments
 (0)