This commit is contained in:
CortexCore
2025-03-14 21:04:19 +08:00
parent ff8670c453
commit 757ffe79ee
1282 changed files with 104378 additions and 3 deletions

View File

@@ -0,0 +1,123 @@
#if GRIFFIN
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Mathematics;
namespace Pinwheel.Griffin
{
public class GJobCommon
{
public static Color GetColorBilinear(GTextureNativeDataDescriptor<Color32> tex, Vector2 uv)
{
return GetColorBilinear(tex, ref uv);
}
public static Color GetColorBilinear(GTextureNativeDataDescriptor<Color32> tex, ref Vector2 uv)
{
Vector2 pixelCoord = new Vector2(
Mathf.Lerp(0, tex.width - 1, uv.x),
Mathf.Lerp(0, tex.height - 1, uv.y));
//apply a bilinear filter
int xFloor = Mathf.FloorToInt(pixelCoord.x);
int xCeil = Mathf.CeilToInt(pixelCoord.x);
int yFloor = Mathf.FloorToInt(pixelCoord.y);
int yCeil = Mathf.CeilToInt(pixelCoord.y);
Color f00 = tex.data[To1DIndex(ref xFloor, ref yFloor, ref tex.width)];
Color f01 = tex.data[To1DIndex(ref xFloor, ref yCeil, ref tex.width)];
Color f10 = tex.data[To1DIndex(ref xCeil, ref yFloor, ref tex.width)];
Color f11 = tex.data[To1DIndex(ref xCeil, ref yCeil, ref tex.width)];
Vector2 unitCoord = new Vector2(
pixelCoord.x - xFloor,
pixelCoord.y - yFloor);
Color color =
f00 * (1 - unitCoord.x) * (1 - unitCoord.y) +
f01 * (1 - unitCoord.x) * unitCoord.y +
f10 * unitCoord.x * (1 - unitCoord.y) +
f11 * unitCoord.x * unitCoord.y;
return color;
}
public static Color GetColorPoint(GTextureNativeDataDescriptor<Color32> tex, Vector2 uv)
{
Vector2 pixelCoord = new Vector2(
Mathf.Lerp(0, tex.width - 1, uv.x),
Mathf.Lerp(0, tex.height - 1, uv.y));
int xFloor = Mathf.FloorToInt(pixelCoord.x);
int yFloor = Mathf.FloorToInt(pixelCoord.y);
return tex.data[To1DIndex(ref xFloor, ref yFloor, ref tex.width)];
}
public static int To1DIndex(ref int x, ref int y, ref int width)
{
return y * width + x;
}
public static bool IsOverlap(Rect rect, GQuad2D quad)
{
if (rect.Contains(quad.p0) || rect.Contains(quad.p1) || rect.Contains(quad.p2) || rect.Contains(quad.p3))
{
return true;
}
float2 r0 = new float2(rect.min.x, rect.min.y);
float2 r1 = new float2(rect.min.x, rect.max.y);
float2 r2 = new float2(rect.max.x, rect.max.y);
float2 r3 = new float2(rect.max.x, rect.min.y);
if (quad.Contains(r0) || quad.Contains(r1) || quad.Contains(r2) || quad.Contains(r3))
{
return true;
}
float2 q0 = quad.p0;
float2 q1 = quad.p1;
float2 q2 = quad.p2;
float2 q3 = quad.p3;
if (IsIntersect(r0, r1, q0, q1, q2, q3))
return true;
if (IsIntersect(r1, r2, q0, q1, q2, q3))
return true;
if (IsIntersect(r2, r3, q0, q1, q2, q3))
return true;
if (IsIntersect(r3, r0, q0, q1, q2, q3))
return true;
return false;
}
private static bool IsIntersect(float2 r0, float2 r1, float2 q0, float2 q1, float2 q2, float2 q3)
{
Line2D l1 = new Line2D(r0, r1);
Line2D l2;
float2 point;
l2 = new Line2D(q0, q1);
if (Line2D.Intersect(l1, l2, out point))
return true;
l2 = new Line2D(q1, q2);
if (Line2D.Intersect(l1, l2, out point))
return true;
l2 = new Line2D(q2, q3);
if (Line2D.Intersect(l1, l2, out point))
return true;
l2 = new Line2D(q3, q0);
if (Line2D.Intersect(l1, l2, out point))
return true;
return false;
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c64ebebc538d7614ba6d63c7fda5744a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,50 @@
#if GRIFFIN
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using Random = Unity.Mathematics.Random;
namespace Pinwheel.Griffin
{
#if GRIFFIN_BURST
[BurstCompile(CompileSynchronously = false)]
#endif
public struct GMaskCullingJob : IJobParallelFor
{
[ReadOnly]
public NativeArray<Vector2> positions;
[ReadOnly]
public GTextureNativeDataDescriptor<Color32> mask;
[WriteOnly]
public NativeArray<bool> result;
public void Execute(int index)
{
Vector2 pos = positions[index];
Color c = GJobCommon.GetColorBilinear(mask, pos);
float value = c.r;
Random rand = Random.CreateFromIndex((uint)index);
if (rand.NextFloat() < value)
{
result[index] = true;
}
else
{
result[index] = false;
}
}
}
public struct GMaskCullingDataHolder
{
public NativeArray<Vector2> positionsNA;
public NativeArray<bool> resultNA;
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 30b26fa6c135db54c86e6f961d60c092
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,29 @@
#if GRIFFIN
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Burst;
using Unity.Mathematics;
using Unity.Jobs;
using static Unity.Mathematics.math;
namespace Pinwheel.Griffin
{
public struct GQuadOverlapTestJob : IJobParallelFor
{
[ReadOnly]
public NativeArray<Rect> rectsToTest;
public GQuad2D quad;
[WriteOnly]
public NativeArray<bool> result;
public void Execute(int index)
{
Rect r = rectsToTest[index];
result[index] = GJobCommon.IsOverlap(r, quad);
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bd4669e986f2fae4a9c8e9a8d795831c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,57 @@
#if GRIFFIN
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using Unity.Burst;
using Unity.Jobs;
namespace Pinwheel.Griffin
{
#if GRIFFIN_BURST
[BurstCompile(CompileSynchronously = false)]
#endif
public struct GSampleInstanceJob : IJobParallelFor
{
[WriteOnly]
public NativeArray<bool> cullResult;
[WriteOnly]
public NativeArray<GPrototypeInstanceInfo> instanceInfo;
[ReadOnly]
public GTextureNativeDataDescriptor<Color32> mask;
[ReadOnly]
public NativeArray<int> selectedPrototypeIndices;
public float minRotation;
public float maxRotation;
public Vector3 minScale;
public Vector3 maxScale;
public int seed;
public void Execute(int index)
{
Unity.Mathematics.Random rand = Unity.Mathematics.Random.CreateFromIndex((uint)(index^seed));
Vector2 uv = new Vector2(rand.NextFloat(), rand.NextFloat());
float maskValue = GJobCommon.GetColorBilinear(mask, uv).r;
if (rand.NextFloat() > maskValue)
{
cullResult[index] = false;
}
else
{
cullResult[index] = true;
GPrototypeInstanceInfo info = new GPrototypeInstanceInfo();
info.prototypeIndex = selectedPrototypeIndices[rand.NextInt(0, selectedPrototypeIndices.Length)];
info.position = new Vector3(uv.x, 0, uv.y);
info.rotation = Quaternion.Euler(0, rand.NextFloat(minRotation, maxRotation), 0);
info.scale = Vector3.Lerp(minScale, maxScale, rand.NextFloat());
instanceInfo[index] = info;
}
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f498db2eea051394ba08e09338427392
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: