-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBVHSphereObject.cs
More file actions
65 lines (57 loc) · 1.78 KB
/
BVHSphereObject.cs
File metadata and controls
65 lines (57 loc) · 1.78 KB
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
/********************************************************************************
** All rights reserved
** Auth: kay.yang
** Date: 6/12/2017 9:12:46 AM
** Version: v1.0.0
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace BVH
{
public class BVHSphereObject : BVHObject
{
public Vector3 mCenter;
public float mRadius, mRadius2; // Radius, Radius^2
public BVHSphereObject(Vector3 center, float r)
{
mCenter = center;
mRadius = r;
mRadius2 = mRadius * mRadius;
}
override
public bool GetIntersection(ref BVHRay ray, ref BVHIntersectionInfo intersection)
{
Vector3 s = mCenter - ray.mOrigin;
float sd = Vector3.Dot(s, ray.mDirection);
float ss = s.magnitude * s.magnitude;
float disc = sd * sd + mRadius2 - ss;
if (disc < 0.0f)
{
return false;
}
intersection.mObject = this;
intersection.mLength = sd - Mathf.Sqrt(disc);
return true;
}
override
public Vector3 GetNormal(ref BVHIntersectionInfo i)
{
Vector3 nor = i.mHitPoint - mCenter;
nor.Normalize();
return nor;
}
override
public BVHBox GetBBox()
{
return new BVHBox(mCenter - new Vector3(mRadius, mRadius, mRadius), mCenter + new Vector3(mRadius, mRadius, mRadius)); ;
}
override
public Vector3 GetCentroid()
{
return mCenter;
}
}
}