-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,855 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace DataStructures | ||
{ | ||
public class BVHGameObjectAdapter : IBVHNodeAdapter<GameObject> | ||
{ | ||
private BVH<GameObject> _bvh; | ||
Dictionary<GameObject, BVHNode<GameObject>> gameObjectToLeafMap = new Dictionary<GameObject, BVHNode<GameObject>>(); | ||
private event Action<GameObject> _onPositionOrSizeChanged; | ||
|
||
BVH<GameObject> IBVHNodeAdapter<GameObject>.BVH | ||
{ | ||
get | ||
{ | ||
return _bvh; | ||
} | ||
set | ||
{ | ||
_bvh = value; | ||
} | ||
} | ||
|
||
//TODO: this is not used? | ||
public void CheckMap(GameObject obj) | ||
{ | ||
if (!gameObjectToLeafMap.ContainsKey(obj)) | ||
{ | ||
throw new Exception("missing map for shuffled child"); | ||
} | ||
} | ||
|
||
public BVHNode<GameObject> GetLeaf(GameObject obj) | ||
{ | ||
return gameObjectToLeafMap[obj]; | ||
} | ||
|
||
public Vector3 GetObjectPos(GameObject obj) | ||
{ | ||
return obj.transform.position; | ||
} | ||
|
||
public float GetRadius(GameObject obj) | ||
{ | ||
Bounds encapsulatingBounds = GetBounds(obj); | ||
return Mathf.Max(Mathf.Max(encapsulatingBounds.extents.x, encapsulatingBounds.extents.y), encapsulatingBounds.extents.z); | ||
} | ||
|
||
private Bounds GetBounds(GameObject obj) | ||
{ | ||
Renderer[] renderers = obj.GetComponentsInChildren<Renderer>(); | ||
Bounds encapsulatingBounds = new Bounds(GetObjectPos(obj), Vector3.zero); | ||
foreach (Renderer renderer in renderers) | ||
{ | ||
encapsulatingBounds.Encapsulate(renderer.bounds.max); | ||
encapsulatingBounds.Encapsulate(renderer.bounds.min); | ||
} | ||
return encapsulatingBounds; | ||
} | ||
|
||
public void MapObjectToBVHLeaf(GameObject obj, BVHNode<GameObject> leaf) | ||
{ | ||
gameObjectToLeafMap[obj] = leaf; | ||
} | ||
|
||
// this allows us to be notified when an object moves, so we can adjust the BVH | ||
public void OnPositionOrSizeChanged(GameObject changed) | ||
{ | ||
// the SSObject has changed, so notify the BVH leaf to refit for the object | ||
gameObjectToLeafMap[changed].RefitObjectChanged(this, changed); | ||
} | ||
|
||
public void UnmapObject(GameObject obj) | ||
{ | ||
gameObjectToLeafMap.Remove(obj); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace DataStructures | ||
{ | ||
public interface ISphere | ||
{ | ||
Vector3 Position | ||
{ | ||
get; | ||
} | ||
|
||
float Radius | ||
{ | ||
get; | ||
} | ||
} | ||
|
||
public class BVHParticleDataAdapter : IBVHNodeAdapter<ISphere> | ||
{ | ||
private BVH<ISphere> _bvh; | ||
Dictionary<ISphere, BVHNode<ISphere>> gameObjectToLeafMap = new Dictionary<ISphere, BVHNode<ISphere>>(); | ||
private event Action<ISphere> _onPositionOrSizeChanged; | ||
|
||
BVH<ISphere> IBVHNodeAdapter<ISphere>.BVH | ||
{ | ||
get | ||
{ | ||
return _bvh; | ||
} | ||
set | ||
{ | ||
_bvh = value; | ||
} | ||
} | ||
|
||
//TODO: this is not used? | ||
public void CheckMap(ISphere particle) | ||
{ | ||
if (!gameObjectToLeafMap.ContainsKey(particle)) | ||
{ | ||
throw new Exception("missing map for shuffled child"); | ||
} | ||
} | ||
|
||
public BVHNode<ISphere> GetLeaf(ISphere particle) | ||
{ | ||
return gameObjectToLeafMap[particle]; | ||
} | ||
|
||
public Vector3 GetObjectPos(ISphere particle) | ||
{ | ||
return particle.Position; | ||
} | ||
|
||
public float GetRadius(ISphere particle) | ||
{ | ||
return particle.Radius; | ||
} | ||
|
||
private Bounds GetBounds(ISphere particle) | ||
{ | ||
Bounds bounds = new Bounds | ||
{ | ||
min = new Vector3(particle.Position.x - particle.Radius, particle.Position.y - particle.Radius, particle.Position.z - particle.Radius), | ||
max = new Vector3(particle.Position.x + particle.Radius, particle.Position.y + particle.Radius, particle.Position.z + particle.Radius) | ||
}; | ||
return bounds; | ||
} | ||
|
||
public void MapObjectToBVHLeaf(ISphere particle, BVHNode<ISphere> leaf) | ||
{ | ||
gameObjectToLeafMap[particle] = leaf; | ||
} | ||
|
||
// this allows us to be notified when an object moves, so we can adjust the BVH | ||
public void OnPositionOrSizeChanged(ISphere changed) | ||
{ | ||
// the SSObject has changed, so notify the BVH leaf to refit for the object | ||
gameObjectToLeafMap[changed].RefitObjectChanged(this, changed); | ||
} | ||
|
||
public void UnmapObject(ISphere particle) | ||
{ | ||
gameObjectToLeafMap.Remove(particle); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using UnityEngine; | ||
|
||
namespace DataStructures | ||
{ | ||
public interface IBVHNodeAdapter<T> | ||
{ | ||
BVH<T> BVH { get; set; } | ||
Vector3 GetObjectPos(T obj); | ||
float GetRadius(T obj); | ||
void MapObjectToBVHLeaf(T obj, BVHNode<T> leaf); | ||
void OnPositionOrSizeChanged(T changed); | ||
void UnmapObject(T obj); | ||
void CheckMap(T obj); | ||
BVHNode<T> GetLeaf(T obj); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.