Skip to content

Commit 7455f43

Browse files
committed
Implemented bounding sphere culling.
1 parent 1acc517 commit 7455f43

File tree

5 files changed

+128
-67
lines changed

5 files changed

+128
-67
lines changed

UnityProject/Assets/Scripts/AddToPathEngine.cs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,13 @@ private void OnEnable()
4646
emissionColor = Vector3.Normalize(emissionColor) ;
4747
emission = emissionStrength * emissionColor;
4848

49-
RayTracing.RegisterObject(this);
50-
49+
5150

51+
RayTracing.RegisterObject(this);
5252
}
5353

5454
private void OnDisable()
5555
{
5656
RayTracing.UnregisterObject(this);
5757
}
58-
59-
Vector3 meshAverage(Mesh mesh)
60-
{
61-
Vector3[] vertexList = mesh.vertices;
62-
63-
Vector3 average = Vector3.zero;
64-
int count = 0;
65-
66-
foreach (Vector3 vertex in vertexList)
67-
{
68-
average += vertex;
69-
count++;
70-
}
71-
72-
return average / count;
73-
}
7458
}

UnityProject/Assets/Scripts/RayGeometry.hlsl

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void GroundPlaneRayIntersection(Ray ray, inout RayCollision collision)
2121
}
2222

2323
}
24-
24+
/*
2525
void SphereRayIntersection(Ray ray, inout RayCollision collision, Sphere sphere)//, float3 sphereposition, float sphereradius)
2626
{
2727
float3 spherePosition = sphere.position;
@@ -63,6 +63,51 @@ void SphereRayIntersection(Ray ray, inout RayCollision collision, Sphere sphere)
6363
collision.emission = sphere.emission;
6464
}
6565
}
66+
*/
67+
bool SphereRayIntersection(Ray ray, Sphere sphere)
68+
{
69+
float3 spherePosition = sphere.position;
70+
float sphereRadius = sphere.radius;
71+
72+
// Vector pointing from the Origin to the center of the sphere.
73+
float3 distanceVector = ray.origin - spherePosition;
74+
75+
// p1 defines the point where the ray enters the sphere, the dot product is correlated with the overlap between the distance vector and the ray direction.
76+
// Large overlap -> ray direction and distance vector close to the same direction.
77+
float p1 = -dot(ray.direction, distanceVector);
78+
79+
// Check whether the ray hits the sphere.
80+
float p2Squared = p1 * p1 - dot(distanceVector, distanceVector) + sphereRadius * sphereRadius;
81+
82+
if (p2Squared < 0)
83+
return false;
84+
85+
return true;
86+
/*
87+
// Find the point where the ray exits the sphere.
88+
float p2 = sqrt(p2Squared);
89+
90+
// Find the distance between the ray origin and the entry (or exit) point of the sphere.
91+
float distance;
92+
93+
if (p1 - p2 > 0)
94+
distance = p1 - p2;
95+
else
96+
distance = p1 + p2;
97+
98+
if (distance > 0.0 && distance < collision.distance)
99+
{
100+
collision.distance = distance;
101+
collision.position = ray.origin + distance * ray.direction;
102+
// Find the normal vector.
103+
collision.positionNormal = normalize(collision.position - spherePosition);
104+
105+
collision.albedo = sphere.albedo;
106+
collision.specular = sphere.specular;
107+
collision.emission = sphere.emission;
108+
}
109+
*/
110+
}
66111

67112
struct TriangleCollisionPoint
68113
{

UnityProject/Assets/Scripts/RayTracing.compute

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ StructuredBuffer<float3> _normals;
3737

3838

3939
// Set from RayTracing.cs ////////
40-
//StructuredBuffer<Sphere> _SphereBuffer;
40+
StructuredBuffer<Sphere> _SphereBuffer;
4141
//////////////////////////////////
4242

4343

@@ -99,7 +99,8 @@ RayCollision TraceRays(Ray ray)
9999

100100
for (int j = 0; j < count; j++)
101101
{
102-
MeshRayIntersection(ray, collision, _meshObjects[j]);
102+
if (SphereRayIntersection(ray, _SphereBuffer[j] ))
103+
MeshRayIntersection(ray, collision, _meshObjects[j] );
103104
}
104105

105106

UnityProject/Assets/Scripts/RayTracing.cs

Lines changed: 74 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public class RayTracing : MonoBehaviour
2828
//float lightIntensity;
2929

3030
Material addMaterial;
31+
3132
ComputeBuffer sphereBuffer;
33+
private static List<Sphere> spheres = new List<Sphere>();
3234

3335
private static List<MeshObject> meshObjects = new List<MeshObject>();
3436
private static List<Vector3> vertices = new List<Vector3>();
@@ -79,6 +81,7 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)
7981
}
8082

8183
//SetUpGeometryBuffer();
84+
//BuildSphereBuffer();
8285
BuildMeshBuffer();
8386

8487
// Send stuff to the computer shader.
@@ -88,7 +91,7 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)
8891
rayTracer.SetVector("_cameraRotation", camera.transform.eulerAngles);
8992
//rayTracer.SetVector("_LightVector", new Vector4(lightDirection.x, lightDirection.y, lightDirection.z, lightIntensity));
9093

91-
//rayTracer.SetBuffer(0, "_SphereBuffer", sphereBuffer);
94+
rayTracer.SetBuffer(0, "_SphereBuffer", sphereBuffer);
9295
rayTracer.SetBuffer(0, "_meshObjects", meshObjectBuffer);
9396
rayTracer.SetBuffer(0, "_vertices", vertexBuffer);
9497
rayTracer.SetBuffer(0, "_indices", indexBuffer);
@@ -124,55 +127,17 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)
124127

125128
ReleaseBuffer();
126129
}
127-
/*
130+
128131
struct Sphere
129132
{
130133
public Vector3 Position;
131134
public float Radius;
132-
public Vector3 Specular;
133-
public Vector3 Albedo;
134-
public Vector3 Emission;
135135
};
136-
137-
void SetUpGeometryBuffer()
138-
{
139-
List<Sphere> spheres = new List<Sphere>();
140-
141-
Sphere sphere;
142-
sphere.Position = new Vector3(0, .5f, 0);
143-
sphere.Radius = .5f;
144-
sphere.Specular = new Vector3(0f, 0f, 0f);
145-
sphere.Albedo = new Vector3(1f, 1f, 1f);
146-
sphere.Emission = new Vector3(1f,1f,1f);
147-
148-
spheres.Add(sphere);
149-
150-
sphere.Position = new Vector3(4f, 2f, 1f);
151-
sphere.Radius = 1f;
152-
sphere.Specular = new Vector3(1f, 1f, 1f);
153-
sphere.Albedo = new Vector3(0f, 0f, 0f);
154-
sphere.Emission = new Vector3(0f, 0f, 0f);
155-
156-
157-
spheres.Add(sphere);
158-
159-
sphere.Position = new Vector3(1f, 2f, 1f);
160-
sphere.Radius = 1f;
161-
sphere.Specular = new Vector3(0f, 0f, 0f);
162-
sphere.Albedo = new Vector3(1f, 0f, 0f);
163-
sphere.Emission = new Vector3(0f, 0f, 0f);
164-
165-
166-
spheres.Add(sphere);
167-
168-
sphereBuffer = new ComputeBuffer(spheres.Count, 52);
169-
sphereBuffer.SetData(spheres);
170-
}
171-
*/
136+
172137
void OnEnable()
173138
{
174139
sampleNumber = 0;
175-
//BuildMeshBuffer();
140+
BuildMeshBuffer();
176141
}
177142

178143
void Update()
@@ -323,6 +288,8 @@ private void BuildMeshBuffer()
323288

324289
RayTracing.indices.AddRange(indicesOffset);
325290

291+
Matrix4x4 localToWorld = obj.gameObject.transform.localToWorldMatrix;
292+
326293
meshObjects.Add(new MeshObject()
327294
{
328295
localToWorldMatrix = obj.transform.localToWorldMatrix,
@@ -333,6 +300,12 @@ private void BuildMeshBuffer()
333300
emission = obj.emission,
334301
alpha = obj.alpha
335302
});
303+
304+
spheres.Add(new Sphere(){
305+
Position = meshAverage(mesh, localToWorld),
306+
Radius = 100000f
307+
//Radius = meshRadius(mesh, localToWorld)
308+
});
336309
}
337310

338311
// Set-up the buffers.
@@ -348,5 +321,63 @@ private void BuildMeshBuffer()
348321
normalBuffer = new ComputeBuffer(normals.Count, 12);
349322
normalBuffer.SetData(normals);
350323

324+
sphereBuffer = new ComputeBuffer(spheres.Count, 16);
325+
sphereBuffer.SetData(spheres);
326+
327+
}
328+
329+
330+
331+
Vector3 meshAverage(Mesh mesh, Matrix4x4 localToWorld)
332+
{
333+
Vector3[] vertexList = mesh.vertices;
334+
335+
Vector3 average = Vector3.zero;
336+
int count = 0;
337+
338+
foreach (Vector3 vertex in vertexList)
339+
{
340+
//print(vertex);
341+
//localToWorld.MultiplyPoint3x4(mf.mesh.vertices[i]);
342+
average += localToWorld.MultiplyPoint3x4(vertex);
343+
count++;
344+
345+
}
346+
//print(average);
347+
return average / count;
348+
}
349+
350+
float meshRadius(Mesh mesh, Matrix4x4 localToWorld)
351+
{
352+
Vector3[] vertexList = mesh.vertices;
353+
354+
float minX = 100000000000000;
355+
float minY = 100000000000000;
356+
float minZ = 100000000000000;
357+
float maxX = 0;
358+
float maxY = 0;
359+
float maxZ = 0;
360+
361+
foreach (Vector3 vertexIt in vertexList)
362+
{
363+
Vector3 vertex = localToWorld.MultiplyPoint3x4(vertexIt);
364+
365+
if (vertex.x < minX) { minX = vertex.x; }
366+
if (vertex.y < minY) { minY = vertex.y; }
367+
if (vertex.z < minZ) { minY = vertex.z; }
368+
369+
if (vertex.x > maxX) { maxX = vertex.x; }
370+
if (vertex.y > maxY) { maxY = vertex.y; }
371+
if (vertex.z > maxZ) { maxZ = vertex.z; }
372+
}
373+
374+
float radius = 0;
375+
376+
if (maxX - minX > radius) {radius = maxX - minX; }
377+
if (maxY - minY > radius) {radius = maxY - minY; }
378+
if (maxZ - minZ > radius) {radius = maxZ - minZ; }
379+
380+
return Mathf.Abs(radius);
351381
}
352-
}
382+
}
383+

UnityProject/Assets/Scripts/SceneItems.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ struct Sphere
77
{
88
float3 position;
99
float radius;
10-
float3 specular;
11-
float3 albedo;
12-
float3 emission;
10+
//float3 specular;
11+
//float3 albedo;
12+
//float3 emission;
1313
};
1414

1515
struct MeshObject

0 commit comments

Comments
 (0)