-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFrustumPlanesTests.cs
127 lines (111 loc) · 4.98 KB
/
FrustumPlanesTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using NUnit.Framework;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Rendering;
using Plane = UnityEngine.Plane;
using GameObject = UnityEngine.GameObject;
using Camera = UnityEngine.Camera;
using Vector3 = UnityEngine.Vector3;
using static Unity.Mathematics.math;
using System;
namespace Unity.Entities.Tests
{
public class FrustumPlanesTests
{
static readonly Plane[] Planes =
{
new Plane(new Vector3(1.0f, 0.0f, 0.0f), -1.0f),
new Plane(new Vector3(-1.0f, 0.0f, 0.0f), 1.0f),
new Plane(new Vector3(0.0f, 1.0f, 0.0f), -1.0f),
new Plane(new Vector3(0.0f, -1.0f, 0.0f), 1.0f),
new Plane(new Vector3(0.0f, -1.0f, 1.0f), 1.0f),
new Plane(new Vector3(0.0f, -2.0f, 0.0f), 12.0f),
new Plane(new Vector3(1.0f, -1.0f, -7.0f), -12.0f),
new Plane(new Vector3(0.0f, 183.0f, -7.0f), -12.0f),
new Plane(new Vector3(0.9933293f, 0.01911314f, 0.113717f), -409.9551f),
};
static readonly AABB[] boxes =
{
new AABB { Center = new float3(0.0f, 0.0f, 0.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(-1.0f, 0.0f, 0.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(-2.0f, 0.0f, 0.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(0.0f, -2.0f, 0.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(0.0f, -1.0f, 0.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(0.0f, 1.0f, 0.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(0.0f, 2.0f, 0.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(0.0f, 0.0f, -2.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(0.0f, 0.0f, -1.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(0.0f, 0.0f, 1.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(0.0f, 0.0f, 2.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(1.0f, -1.0f, 1.0f), Extents = new float3(0.5f, 0.5f, 0.5f) },
new AABB { Center = new float3(0.0f, 0.0f, 0.0f), Extents = new float3(16384.0f, 16384.0f, 16384.0f) },
new AABB { Center = new float3(-325.303f, 391.993f, 1053.86f), Extents = new float3(22.32453f, 18.56214f, 23.49754f) },
};
static NativeArray<Plane> CreatePlanes(int n)
{
var result = new NativeArray<Plane>(n, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
for (int i = 0; i < n; ++i)
{
result[i] = Planes[i];
}
return result;
}
[Test]
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
[TestCase(3)]
[TestCase(4)]
[TestCase(5)]
[TestCase(6)]
[TestCase(7)]
[TestCase(8)]
[TestCase(9)]
public void MultiPlaneTest(int planeCount)
{
using (var par = CreatePlanes(planeCount))
using (var soap = FrustumPlanes.BuildSOAPlanePackets(par, Allocator.Temp))
{
foreach (var box in boxes)
{
Assert.AreEqual(ReferenceTest(par, box), FrustumPlanes.Intersect2(soap, box));
}
}
}
private FrustumPlanes.IntersectResult ReferenceTest(NativeArray<Plane> par, AABB box)
{
FrustumPlanes.IntersectResult result;
var temp = new NativeArray<float4>(par.Length, Allocator.Temp);
for (int i = 0; i < par.Length; ++i)
{
temp[i] = new float4(par[i].normal, par[i].distance);
}
result = FrustumPlanes.Intersect(temp, box);
temp.Dispose();
return result;
}
[Test]
[TestCase(0f)]
[TestCase(1e5f)]
[TestCase(-1e5f)]
public void FrustumFromCamera_PlaneDistance(float zPosition)
{
var gameObject = new GameObject();
var camera = gameObject.AddComponent<Camera>();
var nearClipPlane = 0.3f;
var farClipPlane = 1000f;
camera.nearClipPlane = nearClipPlane;
camera.farClipPlane = farClipPlane;
gameObject.transform.position = new float3(0, 0, zPosition);
using (var planes = new NativeArray<float4>(6, Allocator.Temp))
{
FrustumPlanes.FromCamera(camera, planes);
var nearPlane = planes[4];
var farPlane = planes[5];
Assert.That(nearPlane.w, Is.EqualTo(-nearClipPlane - zPosition).Within(1e-3f));
Assert.That(farPlane.w, Is.EqualTo(farClipPlane + zPosition).Within(1e-3f));
}
UnityEngine.Object.DestroyImmediate(gameObject);
}
}
}