Skip to content

Commit

Permalink
Fixed compilation issue on Unity 2021
Browse files Browse the repository at this point in the history
  • Loading branch information
ltmx committed Apr 10, 2024
1 parent 38393b3 commit 74ddd75
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 100 deletions.
196 changes: 98 additions & 98 deletions Runtime/Noise/Hashx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,26 @@ public static partial class mathx
// }


private static readonly float4 primef = new(73856093, 19349663, 83492791, 16835253);
private static readonly int4 prime = new(73856093, 19349663, 83492791, 16835253);

private static float hash01(this float value) => (value + primef.x) % primef.y / primef.y;
private static float hash01(this float2 coord) => coord.dot(primef.xy) % primef.y / primef.y;
private static float hash01(this float3 coord) => coord.dot(primef.xyz) % primef.y / primef.y;
private static float hash01(this float4 coord) => coord.dot(primef) % primef.y / primef.y;
private static float hash01(this int value) => (value + primef.x) % primef.y / primef.y;
private static float hash01(this int2 coord) => coord.dot(prime.xy) % primef.y / primef.y;
private static float hash01(this int3 coord) => coord.dot(prime.xyz) % primef.y / primef.y;
private static float hash01(this int4 coord) => coord.dot(prime) % primef.y / primef.y;

public static float hashnp01(this float value) => value.hash01().m2n1();
// public static float hashnp01(this f2 coord) => coord.hash01().m2n1();
public static float hashnp01(this float3 coord) => coord.hash01().m2n1();
public static float hashnp01(this float4 coord) => coord.hash01().m2n1();
public static float hashnp01(this int value) => value.hash01().m2n1();
public static float hashnp01(this int2 coord) => coord.hash01().m2n1();
public static float hashnp01(this int3 coord) => coord.hash01().m2n1();
public static float hashnp01(this int4 coord) => coord.hash01().m2n1();
// private static readonly float4 primef = new(73856093, 19349663, 83492791, 16835253);
// private static readonly int4 prime = new(73856093, 19349663, 83492791, 16835253);
//
// private static float hash01(this float value) => (value + primef.x) % primef.y / primef.y;
// private static float hash01(this float2 coord) => coord.dot(primef.xy) % primef.y / primef.y;
// private static float hash01(this float3 coord) => coord.dot(primef.xyz) % primef.y / primef.y;
// private static float hash01(this float4 coord) => coord.dot(primef) % primef.y / primef.y;
// private static float hash01(this int value) => (value + primef.x) % primef.y / primef.y;
// private static float hash01(this int2 coord) => coord.dot(prime.xy) % primef.y / primef.y;
// private static float hash01(this int3 coord) => coord.dot(prime.xyz) % primef.y / primef.y;
// private static float hash01(this int4 coord) => coord.dot(prime) % primef.y / primef.y;
//
// public static float hashnp01(this float value) => value.hash01().m2n1();
// // public static float hashnp01(this f2 coord) => coord.hash01().m2n1();
// public static float hashnp01(this float3 coord) => coord.hash01().m2n1();
// public static float hashnp01(this float4 coord) => coord.hash01().m2n1();
// public static float hashnp01(this int value) => value.hash01().m2n1();
// public static float hashnp01(this int2 coord) => coord.hash01().m2n1();
// public static float hashnp01(this int3 coord) => coord.hash01().m2n1();
// public static float hashnp01(this int4 coord) => coord.hash01().m2n1();

// public static float hashnp01(this f2 coord)
// {
Expand All @@ -125,83 +125,83 @@ public static partial class mathx
// hash = (hash << 13) ^ hash;
// return (float)(hash & 0x7fffffff) / (float)0x7fffffff;
// }

const float F1 = 0.3176f;
const float F2 = 0.7393f;
const float F3 = 0.2847f;
private static float Hash(float3 coord)
{
float3 n = frac(coord * F1);
n += dot(n, n.yxz + 19.19f) + F2;
n = frac(n * F3);
n += dot(n, n.yxz + 57.57f) + F1;
return frac(n.cmul() * n.csum());
}

public static float Hash(this float2 coord) {
return coord.seedrand() * 2 -1;
// float2 n = frac(coord * F1);
// n += dot(n, n.yx + 19.19f) + F2;
// n = frac(n * F3);
// n += dot(n, n.yx + 57.57f) + F1;
// return frac(n.cmul() * n.csum());
}
private static float Hash(float coord)
{
float n = frac(coord * F1);
n += n * (n + 19.19f) + F2;
n = frac(n * F3);
n += n * (n + 57.57f) + F1;
return frac(n * n);
}


public static float GradientNoise(float3 position)
{
int3 cell = (int3)math.floor(position);
float3 frac = position - cell;

float3x4 corners = new float3x4(
GenerateGradient(cell + int3(0, 0, 0)),
GenerateGradient(cell + int3(1, 0, 0)),
GenerateGradient(cell + int3(0, 1, 0)),
GenerateGradient(cell + int3(1, 1, 0))
);

float3x4 corners_z1 = new float3x4(
GenerateGradient(cell + int3(0, 0, 1)),
GenerateGradient(cell + int3(1, 0, 1)),
GenerateGradient(cell + int3(0, 1, 1)),
GenerateGradient(cell + int3(1, 1, 1))
);

float3x4 blend_x = frac.z.lerp(corners, corners_z1);
float3x2 blend_xy = frac.y.lerp(new float3x2(blend_x.c0, blend_x.c1), new float3x2(blend_x.c2, blend_x.c3));
float3 blend_xyz = frac.x.lerp(blend_xy.c0, blend_xy.c1);

return blend_xyz.dot(0.25f);
}

public static float3 GenerateGradient(int3 cell) => cell.hashnp01();


public static float2 randdir(float2 p) => p.seedrand2() * 2 -1; // haha lol symetry on both axis

public static float unity_gradientNoise(float2 p)
{
float2 ip = floor(p);
float2 fp = frac(p);
float2 d0 = f2(dot(randdir(ip), fp), dot(randdir(ip + rightf2), fp - rightf2));
float2 d1 = f2(dot(randdir(ip + upf2), fp - upf2), dot(randdir(ip + 1), fp - 1));
fp = smooth5(fp);
d1 = fp.y.lerp(d0, d1);
return fp.x.lerp(d1.x, d1.y) + 0.5f;
}

private const float F = 0.61803398875f; // golden ratio
public static float hashx(this float2 p) => p.mult(F).frac().set(out p).add(p.cycle().add(37).dot(p)).set(out p).cmul().mult(p.csum()).frac();
public static float hashx(this float3 p) => p.mult(F).frac().set(out p).add(p.cycle().add(37).dot(p)).set(out p).cmul().mult(p.csum()).frac();
public static float hashx(this float4 p) => p.mult(F).frac().set(out p).add(p.cycle().add(37).dot(p)).set(out p).cmul().mult(p.csum()).frac();
public static float hashx(this float p) => frac(p * F + 0.1f).add(p.sq().mult(34.53f)).set(out p).mult(p + 1).frac();
//
// const float F1 = 0.3176f;
// const float F2 = 0.7393f;
// const float F3 = 0.2847f;
// private static float Hash(float3 coord)
// {
// float3 n = frac(coord * F1);
// n += dot(n, n.yxz + 19.19f) + F2;
// n = frac(n * F3);
// n += dot(n, n.yxz + 57.57f) + F1;
// return frac(n.cmul() * n.csum());
// }
//
// public static float Hash(this float2 coord) {
// return coord.seedrand() * 2 -1;
// // float2 n = frac(coord * F1);
// // n += dot(n, n.yx + 19.19f) + F2;
// // n = frac(n * F3);
// // n += dot(n, n.yx + 57.57f) + F1;
// // return frac(n.cmul() * n.csum());
// }
// private static float Hash(float coord)
// {
// float n = frac(coord * F1);
// n += n * (n + 19.19f) + F2;
// n = frac(n * F3);
// n += n * (n + 57.57f) + F1;
// return frac(n * n);
// }
//
//
// public static float GradientNoise(float3 position)
// {
// int3 cell = (int3)math.floor(position);
// float3 frac = position - cell;
//
// float3x4 corners = new float3x4(
// GenerateGradient(cell + int3(0, 0, 0)),
// GenerateGradient(cell + int3(1, 0, 0)),
// GenerateGradient(cell + int3(0, 1, 0)),
// GenerateGradient(cell + int3(1, 1, 0))
// );
//
// float3x4 corners_z1 = new float3x4(
// GenerateGradient(cell + int3(0, 0, 1)),
// GenerateGradient(cell + int3(1, 0, 1)),
// GenerateGradient(cell + int3(0, 1, 1)),
// GenerateGradient(cell + int3(1, 1, 1))
// );
//
// float3x4 blend_x = frac.z.lerp(corners, corners_z1);
// float3x2 blend_xy = frac.y.lerp(new float3x2(blend_x.c0, blend_x.c1), new float3x2(blend_x.c2, blend_x.c3));
// float3 blend_xyz = frac.x.lerp(blend_xy.c0, blend_xy.c1);
//
// return blend_xyz.dot(0.25f);
// }
//
// public static float3 GenerateGradient(int3 cell) => cell.hashnp01();
//
//
// public static float2 randdir(float2 p) => p.seedrand2() * 2 -1; // haha lol symetry on both axis
//
// public static float unity_gradientNoise(float2 p)
// {
// float2 ip = floor(p);
// float2 fp = frac(p);
// float2 d0 = f2(dot(randdir(ip), fp), dot(randdir(ip + rightf2), fp - rightf2));
// float2 d1 = f2(dot(randdir(ip + upf2), fp - upf2), dot(randdir(ip + 1), fp - 1));
// fp = smooth5(fp);
// d1 = fp.y.lerp(d0, d1);
// return fp.x.lerp(d1.x, d1.y) + 0.5f;
// }
//
// private const float F = 0.61803398875f; // golden ratio
// public static float hashx(this float2 p) => p.mult(F).frac().set(out p).add(p.cycle().add(37).dot(p)).set(out p).cmul().mult(p.csum()).frac();
// public static float hashx(this float3 p) => p.mult(F).frac().set(out p).add(p.cycle().add(37).dot(p)).set(out p).cmul().mult(p.csum()).frac();
// public static float hashx(this float4 p) => p.mult(F).frac().set(out p).add(p.cycle().add(37).dot(p)).set(out p).cmul().mult(p.csum()).frac();
// public static float hashx(this float p) => frac(p * F + 0.1f).add(p.sq().mult(34.53f)).set(out p).mult(p + 1).frac();
}
}
3 changes: 1 addition & 2 deletions Runtime/_Experimental/Jobify/FunctionPointers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

namespace Unity.Mathematics
{
[BurstCompile]
public static partial class FunctionPointers
{
// ** Very important to cache the function pointer for performance reasons
public static readonly f1x3_f1 p_smax_exp = compile<f1x3_f1>(mathx.smax_exp);
// public static readonly f1x3_f1 p_smax_exp = compile<f1x3_f1>(mathx.smax_exp);
}
}

0 comments on commit 74ddd75

Please sign in to comment.