重新导入obi

This commit is contained in:
2026-04-06 11:35:18 +08:00
parent 05fa2d6e5e
commit ae3002a0e2
1643 changed files with 232496 additions and 13 deletions

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 16e8693eb314d42888bd5a08daebe03a
folderAsset: yes
timeCreated: 1501157275
licenseType: Store
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: e56c9feb62e6b4f9ea6517ebb106419b
folderAsset: yes
timeCreated: 1503483313
licenseType: Store
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,47 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiBoxShapeTracker2D : ObiShapeTracker
{
public ObiBoxShapeTracker2D(ObiCollider2D source, BoxCollider2D collider){
this.source = source;
this.collider = collider;
}
public override bool UpdateIfNeeded (){
BoxCollider2D box = collider as BoxCollider2D;
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// update collider:
var shape = world.colliderShapes[index];
shape.is2D = 1;
shape.type = ColliderShape.ShapeType.Box;
shape.filter = source.Filter;
shape.flags = box.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.contactOffset = source.Thickness + box.edgeRadius;
shape.center = box.offset;
shape.size = box.size;
world.colliderShapes[index] = shape;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(box.bounds, shape.contactOffset, true);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(box.transform, true);
world.colliderTransforms[index] = trfm;
return false;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: e3701e56c1528404daa61e704345b750
timeCreated: 1501960568
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,53 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiCapsuleShapeTracker2D : ObiShapeTracker
{
public ObiCapsuleShapeTracker2D(ObiCollider2D source, CapsuleCollider2D collider)
{
this.source = source;
this.collider = collider;
}
public override bool UpdateIfNeeded ()
{
CapsuleCollider2D capsule = collider as CapsuleCollider2D;
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// update collider:
var shape = world.colliderShapes[index];
shape.is2D = 1;
shape.type = ColliderShape.ShapeType.Capsule;
shape.filter = source.Filter;
shape.flags = capsule.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = capsule.offset;
Vector2 size = capsule.size;
shape.size = new Vector4((capsule.direction == CapsuleDirection2D.Horizontal ? size.y : size.x) * 0.5f,
Mathf.Max(size.x, size.y),
capsule.direction == CapsuleDirection2D.Horizontal ? 0 : 1, 0);
world.colliderShapes[index] = shape;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(capsule.bounds, shape.contactOffset,true);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(capsule.transform,true);
world.colliderTransforms[index] = trfm;
return false;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: fe1cd08d83cfe4785b8d5bb8e0bd3dd9
timeCreated: 1501961541
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,52 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiCircleShapeTracker2D : ObiShapeTracker
{
public ObiCircleShapeTracker2D(ObiCollider2D source, CircleCollider2D collider)
{
this.source = source;
this.collider = collider;
}
public override bool UpdateIfNeeded ()
{
CircleCollider2D sphere = collider as CircleCollider2D;
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// update collider:
var shape = world.colliderShapes[index];
shape.is2D = 1;
shape.type = ColliderShape.ShapeType.Sphere;
shape.filter = source.Filter;
shape.flags = sphere.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = sphere.offset;
shape.size = Vector3.one * sphere.radius;
world.colliderShapes[index] = shape;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(sphere.bounds, shape.contactOffset, true);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(sphere.transform,true);
world.colliderTransforms[index] = trfm;
return true;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 4290734bafa8c46d8862202365a9f83b
timeCreated: 1501960324
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,72 @@
using System;
using UnityEngine;
using System.Runtime.InteropServices;
namespace Obi{
public class ObiEdgeShapeTracker2D : ObiShapeTracker
{
ObiEdgeMeshHandle handle;
public ObiEdgeShapeTracker2D(ObiCollider2D source, EdgeCollider2D collider)
{
this.source = source;
this.collider = collider;
}
public void UpdateEdgeData()
{
ObiColliderWorld.GetInstance().DestroyEdgeMesh(handle);
}
public override bool UpdateIfNeeded (){
EdgeCollider2D edgeCollider = collider as EdgeCollider2D;
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// get or create the mesh:
if (handle == null || !handle.isValid)
{
handle = world.GetOrCreateEdgeMesh(edgeCollider);
handle.Reference();
}
// update collider:
var shape = world.colliderShapes[index];
shape.is2D = 1;
shape.type = ColliderShape.ShapeType.EdgeMesh;
shape.filter = source.Filter;
shape.flags = edgeCollider.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.center = edgeCollider.offset;
shape.contactOffset = source.Thickness + edgeCollider.edgeRadius;
shape.dataIndex = handle.index;
world.colliderShapes[index] = shape;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(edgeCollider.bounds, shape.contactOffset, true);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(edgeCollider.transform, true);
world.colliderTransforms[index] = trfm;
return true;
}
public override void Destroy()
{
base.Destroy();
if (handle != null && handle.Dereference())
ObiColliderWorld.GetInstance().DestroyEdgeMesh(handle);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 2d2b0912e5ba74985bb209d365e52f4b
timeCreated: 1502808634
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 3eba36295266249e08782d3327db66cd
folderAsset: yes
timeCreated: 1503483325
licenseType: Store
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,58 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiBoxShapeTracker : ObiShapeTracker
{
public ObiBoxShapeTracker(ObiCollider source, BoxCollider collider)
{
this.source = source;
this.collider = collider;
}
public override bool UpdateIfNeeded (){
BoxCollider box = collider as BoxCollider;
/*if (box != null && (box.size != size || box.center != center)){
size = box.size;
center = box.center;
adaptor.Set(center, size);
Oni.UpdateShape(oniShape,ref adaptor);
return true;
}*/
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// update collider:
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.Box;
shape.filter = source.Filter;
shape.flags = box.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = box.center;
shape.size = box.size;
world.colliderShapes[index] = shape;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(box.bounds, shape.contactOffset);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(box.transform);
world.colliderTransforms[index] = trfm;
return true;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 7f9acb590fcdd47838d4e1929e463712
timeCreated: 1501960568
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,49 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiCapsuleShapeTracker : ObiShapeTracker
{
public ObiCapsuleShapeTracker(ObiCollider source, CapsuleCollider collider){
this.collider = collider;
this.source = source;
}
public override bool UpdateIfNeeded (){
CapsuleCollider capsule = collider as CapsuleCollider;
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// update collider:
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.Capsule;
shape.filter = source.Filter;
shape.flags = capsule.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = capsule.center;
shape.size = new Vector4(capsule.radius, capsule.height, capsule.direction, 0);
world.colliderShapes[index] = shape;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(capsule.bounds, shape.contactOffset);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(capsule.transform);
world.colliderTransforms[index] = trfm;
return true;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: ab81f697ad33c47c0b487129c7ef1761
timeCreated: 1501961541
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,52 @@
using System;
using UnityEngine;
namespace Obi
{
public class ObiCharacterControllerShapeTracker : ObiShapeTracker
{
public ObiCharacterControllerShapeTracker(ObiCollider source, CharacterController collider)
{
this.collider = collider;
this.source = source;
}
public override bool UpdateIfNeeded()
{
CharacterController character = collider as CharacterController;
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// update collider:
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.Capsule;
shape.filter = source.Filter;
shape.flags = character.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = character.center;
shape.size = new Vector4(character.radius, character.height, 1, 0);
world.colliderShapes[index] = shape;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(character.bounds, shape.contactOffset);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(character.transform);
world.colliderTransforms[index] = trfm;
return true;
}
}
}

View File

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

View File

@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
namespace Obi{
public class ObiDistanceFieldShapeTracker : ObiShapeTracker
{
public ObiDistanceField distanceField;
ObiDistanceFieldHandle handle;
public ObiDistanceFieldShapeTracker(ObiCollider source, Component collider, ObiDistanceField distanceField){
this.source = source;
this.collider = collider;
this.distanceField = distanceField;
}
/**
* Forces the tracker to update distance field data during the next call to UpdateIfNeeded().
*/
public void UpdateDistanceFieldData()
{
ObiColliderWorld.GetInstance().DestroyDistanceField(handle);
}
public override bool UpdateIfNeeded ()
{
bool trigger = false;
if (collider is Collider) trigger = ((Collider)collider).isTrigger;
else if (collider is Collider2D) trigger = ((Collider2D)collider).isTrigger;
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// decrease reference count of current handle if the df data it points to is different
// than the df used by the collider:
if (handle != null && handle.owner != distanceField)
{
if (handle.Dereference())
world.DestroyDistanceField(handle);
}
if (handle == null || !handle.isValid)
{
handle = world.GetOrCreateDistanceField(distanceField);
handle.Reference();
}
// update collider:
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.SignedDistanceField;
shape.filter = source.Filter;
shape.flags = trigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.contactOffset = source.Thickness;
shape.dataIndex = handle.index;
world.colliderShapes[index] = shape;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(distanceField.FieldBounds.Transform(source.transform.localToWorldMatrix), shape.contactOffset);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(source.transform);
world.colliderTransforms[index] = trfm;
return true;
}
public override void Destroy()
{
base.Destroy();
if (handle != null && handle.Dereference())
ObiColliderWorld.GetInstance().DestroyDistanceField(handle);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: b9b443b752c21400ca6cfc151526f107
timeCreated: 1504879085
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
namespace Obi{
public class ObiMeshShapeTracker : ObiShapeTracker
{
ObiTriangleMeshHandle handle;
public ObiMeshShapeTracker(ObiCollider source, MeshCollider collider){
this.source = source;
this.collider = collider;
}
/**
* Forces the tracker to update mesh data during the next call to UpdateIfNeeded().
*/
public void UpdateMeshData()
{
ObiColliderWorld.GetInstance().DestroyTriangleMesh(handle);
}
public override bool UpdateIfNeeded ()
{
MeshCollider meshCollider = collider as MeshCollider;
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// decrease reference count of current handle if the mesh data it points to is different
// than the mesh used by the collider:
if (handle != null && handle.owner != meshCollider.sharedMesh)
{
if (handle.Dereference())
world.DestroyTriangleMesh(handle);
}
// get or create the mesh:
if (handle == null || !handle.isValid)
{
handle = world.GetOrCreateTriangleMesh(meshCollider.sharedMesh);
handle.Reference();
}
// update collider:
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.TriangleMesh;
shape.filter = source.Filter;
shape.flags = meshCollider.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.contactOffset = source.Thickness;
shape.dataIndex = handle.index;
world.colliderShapes[index] = shape;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(meshCollider.bounds, shape.contactOffset);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(meshCollider.transform);
world.colliderTransforms[index] = trfm;
return true;
}
public override void Destroy()
{
base.Destroy();
if (handle != null && handle.Dereference())
ObiColliderWorld.GetInstance().DestroyTriangleMesh(handle);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: c955001531e3444fd9615a5272954dea
timeCreated: 1502875710
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,20 @@
using System;
using UnityEngine;
namespace Obi{
public abstract class ObiShapeTracker
{
protected ObiColliderBase source;
protected Component collider;
public virtual void Destroy(){
}
public abstract bool UpdateIfNeeded ();
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5f455a735ac894ee7bddecd2b5a64ca8
timeCreated: 1501960324
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,72 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiSphereShapeTracker : ObiShapeTracker
{
public ObiSphereShapeTracker(ObiCollider source, SphereCollider collider)
{
this.source = source;
this.collider = collider;
}
public override bool UpdateIfNeeded()
{
SphereCollider sphere = collider as SphereCollider;
// TODO: testing for changes here is not needed? all we do is set variables...
//if (sphere != null && (sphere.radius != radius || sphere.center != center))
{
//radius = sphere.radius;
//center = sphere.center;
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// update collider:
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.Sphere;
shape.filter = source.Filter;
shape.flags = sphere.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.contactOffset = source.Thickness;
shape.center = sphere.center;
shape.size = Vector3.one * sphere.radius;
world.colliderShapes[index] = shape;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(sphere.bounds, shape.contactOffset);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(sphere.transform);
world.colliderTransforms[index] = trfm;
/*var shape = source.colliderWorld.colliderShapes[source.shapeHandle.index];
// update the transform
shape.Set(collider as Collider, source.Phase, source.Thickness);
// update the shape:
shape.SetSphere(sphere.center, sphere.radius);
source.colliderWorld.colliderShapes[source.shapeHandle.index] = shape;*/
//adaptor.Set(center, radius);
//Oni.UpdateShape(oniShape,ref adaptor);
return true;
}
//return false;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 221c651500acc4cbfa30ed329aebb822
timeCreated: 1501960324
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,76 @@
using System;
using UnityEngine;
using System.Runtime.InteropServices;
namespace Obi{
public class ObiTerrainShapeTracker : ObiShapeTracker
{
private bool heightmapDataHasChanged = false;
ObiHeightFieldHandle handle;
public ObiTerrainShapeTracker(ObiCollider source, TerrainCollider collider){
this.source = source;
this.collider = collider;
}
public void UpdateHeightData()
{
ObiColliderWorld.GetInstance().DestroyHeightField(handle);
}
public override bool UpdateIfNeeded ()
{
TerrainCollider terrain = collider as TerrainCollider;
// retrieve collision world and index:
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
int resolution = terrain.terrainData.heightmapResolution;
// get or create the heightfield:
if (handle == null || !handle.isValid)
{
handle = world.GetOrCreateHeightField(terrain.terrainData);
handle.Reference();
}
// update collider:
var shape = world.colliderShapes[index];
shape.type = ColliderShape.ShapeType.Heightmap;
shape.filter = source.Filter;
shape.flags = terrain.isTrigger ? 1 : 0;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.contactOffset = source.Thickness;
shape.dataIndex = handle.index;
shape.size = terrain.terrainData.size;
shape.center = new Vector4(resolution, resolution, resolution, resolution);
world.colliderShapes[index] = shape;
// update bounds:
var aabb = world.colliderAabbs[index];
aabb.FromBounds(terrain.bounds, shape.contactOffset);
world.colliderAabbs[index] = aabb;
// update transform:
var trfm = world.colliderTransforms[index];
trfm.FromTransform(terrain.transform);
world.colliderTransforms[index] = trfm;
return true;
}
public override void Destroy()
{
base.Destroy();
if (handle != null && handle.Dereference())
ObiColliderWorld.GetInstance().DestroyHeightField(handle);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 601187ba897f54e20b787637df64e96b
timeCreated: 1502808634
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,118 @@
using UnityEngine;
using UnityEngine.Serialization;
namespace Obi
{
/**
* Add this component to any Collider that you want to be considered by Obi.
*/
[ExecuteInEditMode]
[RequireComponent(typeof(Collider))]
public class ObiCollider : ObiColliderBase
{
[SerializeProperty("sourceCollider")]
[FormerlySerializedAs("SourceCollider")]
[SerializeField] private Collider m_SourceCollider;
/// <summary>
/// The Unity collider that this ObiCollider should mimic.
/// </summary>
/// This is automatically set when you first create the ObiCollider component, but you can override it afterwards.
public Collider sourceCollider
{
set
{
if (value != null && value.gameObject != this.gameObject)
{
Debug.LogError("The Collider component must reside in the same GameObject as ObiCollider.");
return;
}
m_SourceCollider = value;
RemoveCollider();
AddCollider();
}
get { return m_SourceCollider; }
}
[SerializeProperty("distanceField")]
[FormerlySerializedAs("distanceField")]
[SerializeField] private ObiDistanceField m_DistanceField;
/// <summary>
/// The distance field used by this collider.
/// </summary>
/// Setting a distance field will cause the collider to ignore its <see cref="m_SourceCollider"/> and use the distance field instead.
public ObiDistanceField distanceField
{
set
{
if (m_DistanceField != value)
{
m_DistanceField = value;
CreateTracker();
}
}
get { return m_DistanceField; }
}
/**
* Creates an OniColliderTracker of the appropiate type.
*/
protected override void CreateTracker()
{
if (tracker != null)
{
tracker.Destroy();
tracker = null;
}
if (distanceField != null)
tracker = new ObiDistanceFieldShapeTracker(this, m_SourceCollider, distanceField);
else
{
if (m_SourceCollider is SphereCollider)
tracker = new ObiSphereShapeTracker(this, (SphereCollider)m_SourceCollider);
else if (m_SourceCollider is BoxCollider)
tracker = new ObiBoxShapeTracker(this, (BoxCollider)m_SourceCollider);
else if (m_SourceCollider is CapsuleCollider)
tracker = new ObiCapsuleShapeTracker(this, (CapsuleCollider)m_SourceCollider);
else if (m_SourceCollider is CharacterController)
tracker = new ObiCharacterControllerShapeTracker(this, (CharacterController)m_SourceCollider);
else if (m_SourceCollider is TerrainCollider)
tracker = new ObiTerrainShapeTracker(this, (TerrainCollider)m_SourceCollider);
else if (m_SourceCollider is MeshCollider)
tracker = new ObiMeshShapeTracker(this,(MeshCollider)m_SourceCollider);
else
Debug.LogWarning("Collider type not supported by Obi.");
}
}
protected override Component GetUnityCollider(ref bool enabled)
{
if (m_SourceCollider != null)
enabled = m_SourceCollider.enabled;
return m_SourceCollider;
}
protected override void FindSourceCollider()
{
if (sourceCollider == null)
sourceCollider = GetComponent<Collider>();
else
AddCollider();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6757b231cedd544c583ef8aeaf773b2a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 6da22d6bce08a4f3d86542bb903de689, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,82 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Obi
{
/**
* Add this component to any Collider that you want to be considered by Obi.
*/
[ExecuteInEditMode]
[RequireComponent(typeof(Collider2D))]
public class ObiCollider2D : ObiColliderBase
{
[SerializeProperty("SourceCollider")]
[SerializeField] private Collider2D sourceCollider;
public Collider2D SourceCollider
{
set
{
if (value.gameObject != this.gameObject)
{
Debug.LogError("The Collider component must reside in the same GameObject as ObiCollider.");
}
else
{
sourceCollider = value;
RemoveCollider();
AddCollider();
}
}
get { return sourceCollider; }
}
/**
* Creates an OniColliderTracker of the appropiate type.
*/
protected override void CreateTracker()
{
if (tracker != null)
{
tracker.Destroy();
tracker = null;
}
if (sourceCollider is CircleCollider2D)
tracker = new ObiCircleShapeTracker2D(this,(CircleCollider2D)sourceCollider);
else if (sourceCollider is BoxCollider2D)
tracker = new ObiBoxShapeTracker2D(this,(BoxCollider2D)sourceCollider);
else if (sourceCollider is CapsuleCollider2D)
tracker = new ObiCapsuleShapeTracker2D(this,(CapsuleCollider2D)sourceCollider);
else if (sourceCollider is EdgeCollider2D)
tracker = new ObiEdgeShapeTracker2D(this,(EdgeCollider2D)sourceCollider);
else
Debug.LogWarning("Collider2D type not supported by Obi.");
}
protected override Component GetUnityCollider(ref bool enabled)
{
if (sourceCollider != null)
enabled = sourceCollider.enabled;
return sourceCollider;
}
protected override void FindSourceCollider()
{
if (SourceCollider == null)
SourceCollider = GetComponent<Collider2D>();
else
AddCollider();
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 7f6a9934a1a0f44e89ccbb25355461bb
timeCreated: 1503509235
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: e7185d0557058412db2fc499de3a0739, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,206 @@
using UnityEngine;
using System;
namespace Obi
{
/**
* Implements common functionality for ObiCollider and ObiCollider2D.
*/
public abstract class ObiColliderBase : MonoBehaviour
{
[SerializeProperty("Thickness")]
[SerializeField] private float thickness = 0;
[SerializeProperty("CollisionMaterial")]
[SerializeField] private ObiCollisionMaterial material;
[SerializeField] private int filter = ObiUtils.MakeFilter(ObiUtils.CollideWithEverything, 0);
public ObiCollisionMaterial CollisionMaterial
{
set
{
material = value;
}
get { return material; }
}
public int Filter
{
set
{
if (filter != value)
{
filter = value;
dirty = true;
}
}
get { return filter; }
}
public float Thickness
{
set
{
if (!Mathf.Approximately(thickness, value))
{
thickness = value;
dirty = true;
}
}
get { return thickness; }
}
public ObiShapeTracker Tracker
{
get { return tracker; }
}
public ObiColliderHandle Handle
{
get
{
if (shapeHandle == null)
FindSourceCollider();
return shapeHandle;
}
}
public IntPtr OniCollider
{
get
{
if (oniCollider == IntPtr.Zero)
FindSourceCollider();
return oniCollider;
}
}
public ObiRigidbodyBase Rigidbody
{
get { return obiRigidbody; }
}
protected ObiColliderHandle shapeHandle;
protected IntPtr oniCollider;
protected ObiRigidbodyBase obiRigidbody;
protected bool wasUnityColliderEnabled = true;
protected bool dirty = false;
protected ObiShapeTracker tracker; /**< tracker object used to determine when to update the collider's shape*/
/**
* Creates an OniColliderTracker of the appropiate type.
*/
protected abstract void CreateTracker();
protected abstract Component GetUnityCollider(ref bool enabled);
protected abstract void FindSourceCollider();
protected void CreateRigidbody()
{
obiRigidbody = null;
// find the first rigidbody up our hierarchy:
Rigidbody rb = GetComponentInParent<Rigidbody>();
Rigidbody2D rb2D = GetComponentInParent<Rigidbody2D>();
// if we have an rigidbody above us, see if it has a ObiRigidbody component and add one if it doesn't:
if (rb != null)
{
obiRigidbody = rb.GetComponent<ObiRigidbody>();
if (obiRigidbody == null)
obiRigidbody = rb.gameObject.AddComponent<ObiRigidbody>();
}
else if (rb2D != null)
{
obiRigidbody = rb2D.GetComponent<ObiRigidbody2D>();
if (obiRigidbody == null)
obiRigidbody = rb2D.gameObject.AddComponent<ObiRigidbody2D>();
}
}
private void OnTransformParentChanged()
{
CreateRigidbody();
}
protected void AddCollider()
{
Component unityCollider = GetUnityCollider(ref wasUnityColliderEnabled);
if (unityCollider != null && (shapeHandle == null || !shapeHandle.isValid))
{
shapeHandle = ObiColliderWorld.GetInstance().CreateCollider();
shapeHandle.owner = this;
// Create shape tracker:
CreateTracker();
// Create rigidbody if necessary, and link ourselves to it:
CreateRigidbody();
}
}
protected void RemoveCollider()
{
ObiColliderWorld.GetInstance().DestroyCollider(shapeHandle);
// Destroy shape tracker:
if (tracker != null)
{
tracker.Destroy();
tracker = null;
}
}
/**
* Check if the collider transform or its shape have changed any relevant property, and update their Oni counterparts.
*/
public void UpdateIfNeeded()
{
bool unityColliderEnabled = false;
Component unityCollider = GetUnityCollider(ref unityColliderEnabled);
var colliderWorld = ObiColliderWorld.GetInstance();
if (unityCollider != null)
{
// no need to test for changes, all we are doing is setting some variables here.
if (tracker != null)
tracker.UpdateIfNeeded();
}
// If the unity collider is null but its handle is valid, the unity collider has been destroyed.
else if (shapeHandle != null && shapeHandle.isValid)
RemoveCollider();
}
private void OnEnable()
{
// Initialize using the source collider specified by the user (or find an appropiate one).
FindSourceCollider();
}
private void OnDisable()
{
RemoveCollider();
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5a055da07c6fe4debad9680a4b251f7a
timeCreated: 1503477796
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,434 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Obi
{
public class ObiResourceHandle<T> where T : class
{
public T owner = null; /**< reference to the owner instance*/
public int index = -1; /**< index of this resource in the collision world.*/
private int referenceCount = 0; /**< amount of references to this handle. Can be used to clean up any associated resources after it reaches zero.*/
public bool isValid
{
get { return index >= 0; }
}
public void Invalidate()
{
index = -1;
referenceCount = 0;
}
public void Reference()
{
referenceCount++;
}
public bool Dereference()
{
return --referenceCount == 0;
}
public ObiResourceHandle(int index = -1)
{
this.index = index;
owner = null;
}
}
public class ObiColliderHandle : ObiResourceHandle<ObiColliderBase>
{
public ObiColliderHandle(int index = -1) : base(index) { }
}
public class ObiCollisionMaterialHandle : ObiResourceHandle<ObiCollisionMaterial>
{
public ObiCollisionMaterialHandle(int index = -1) : base(index) { }
}
public class ObiRigidbodyHandle : ObiResourceHandle<ObiRigidbodyBase>
{
public ObiRigidbodyHandle(int index = -1) : base(index) { }
}
[ExecuteInEditMode]
public class ObiColliderWorld
{
[NonSerialized] public List<IColliderWorldImpl> implementations;
[NonSerialized] public List<ObiColliderHandle> colliderHandles; // list of collider handles, used by ObiCollider components to retrieve them.
[NonSerialized] public ObiNativeColliderShapeList colliderShapes; // list of collider shapes.
[NonSerialized] public ObiNativeAabbList colliderAabbs; // list of collider bounds.
[NonSerialized] public ObiNativeAffineTransformList colliderTransforms; // list of collider transforms.
[NonSerialized] public List<ObiCollisionMaterialHandle> materialHandles;
[NonSerialized] public ObiNativeCollisionMaterialList collisionMaterials; // list of collision materials.
[NonSerialized] public List<ObiRigidbodyHandle> rigidbodyHandles; // list of rigidbody handles, used by ObiRigidbody components to retrieve them.
[NonSerialized] public ObiNativeRigidbodyList rigidbodies; // list of rigidbodies.
[NonSerialized] public ObiTriangleMeshContainer triangleMeshContainer;
[NonSerialized] public ObiEdgeMeshContainer edgeMeshContainer;
[NonSerialized] public ObiDistanceFieldContainer distanceFieldContainer;
[NonSerialized] public ObiHeightFieldContainer heightFieldContainer;
private static ObiColliderWorld instance;
public static ObiColliderWorld GetInstance()
{
if (instance == null)
{
instance = new ObiColliderWorld();
instance.Initialize();
}
return instance;
}
private void Initialize()
{
// Allocate all lists:
if (implementations == null)
implementations = new List<IColliderWorldImpl>();
if (colliderHandles == null)
colliderHandles = new List<ObiColliderHandle>();
if (colliderShapes == null)
colliderShapes = new ObiNativeColliderShapeList();
if (colliderAabbs == null)
colliderAabbs = new ObiNativeAabbList();
if (colliderTransforms == null)
colliderTransforms = new ObiNativeAffineTransformList();
if (materialHandles == null)
materialHandles = new List<ObiCollisionMaterialHandle>();
if (collisionMaterials == null)
collisionMaterials = new ObiNativeCollisionMaterialList();
if (rigidbodyHandles == null)
rigidbodyHandles = new List<ObiRigidbodyHandle>();
if (rigidbodies == null)
rigidbodies = new ObiNativeRigidbodyList();
if (triangleMeshContainer == null)
triangleMeshContainer = new ObiTriangleMeshContainer();
if (edgeMeshContainer == null)
edgeMeshContainer = new ObiEdgeMeshContainer();
if (distanceFieldContainer == null)
distanceFieldContainer = new ObiDistanceFieldContainer();
if (heightFieldContainer == null)
heightFieldContainer = new ObiHeightFieldContainer();
}
private void Destroy()
{
for (int i = 0; i < implementations.Count; ++i)
{
implementations[i].SetColliders(colliderShapes, colliderAabbs, colliderTransforms, 0);
implementations[i].UpdateWorld(0);
}
// Invalidate all handles:
if (colliderHandles != null)
foreach (var handle in colliderHandles)
handle.Invalidate();
if (rigidbodyHandles != null)
foreach (var handle in rigidbodyHandles)
handle.Invalidate();
if (materialHandles != null)
foreach (var handle in materialHandles)
handle.Invalidate();
// Dispose of all lists:
implementations = null;
colliderHandles = null;
rigidbodyHandles = null;
materialHandles = null;
if (colliderShapes != null)
colliderShapes.Dispose();
if (colliderAabbs != null)
colliderAabbs.Dispose();
if (colliderTransforms != null)
colliderTransforms.Dispose();
if (collisionMaterials != null)
collisionMaterials.Dispose();
if (rigidbodies != null)
rigidbodies.Dispose();
if (triangleMeshContainer != null)
triangleMeshContainer.Dispose();
if (edgeMeshContainer != null)
edgeMeshContainer.Dispose();
if (distanceFieldContainer != null)
distanceFieldContainer.Dispose();
if (heightFieldContainer != null)
heightFieldContainer.Dispose();
instance = null;
}
private void DestroyIfUnused()
{
// when there are no implementations and no colliders, the world gets destroyed.
if (colliderHandles.Count == 0 && rigidbodyHandles.Count == 0 && materialHandles.Count == 0 && implementations.Count == 0)
Destroy();
}
public void RegisterImplementation(IColliderWorldImpl impl)
{
if (!implementations.Contains(impl))
implementations.Add(impl);
}
public void UnregisterImplementation(IColliderWorldImpl impl)
{
implementations.Remove(impl);
DestroyIfUnused();
}
public ObiColliderHandle CreateCollider()
{
var handle = new ObiColliderHandle(colliderHandles.Count);
colliderHandles.Add(handle);
colliderShapes.Add(new ColliderShape() { materialIndex = -1, rigidbodyIndex = -1});
colliderAabbs.Add(new Aabb());
colliderTransforms.Add(new AffineTransform());
return handle;
}
public ObiRigidbodyHandle CreateRigidbody()
{
var handle = new ObiRigidbodyHandle(rigidbodyHandles.Count);
rigidbodyHandles.Add(handle);
rigidbodies.Add(new ColliderRigidbody());
return handle;
}
public ObiCollisionMaterialHandle CreateCollisionMaterial()
{
var handle = new ObiCollisionMaterialHandle(materialHandles.Count);
materialHandles.Add(handle);
collisionMaterials.Add(new CollisionMaterial());
return handle;
}
public ObiTriangleMeshHandle GetOrCreateTriangleMesh(Mesh mesh)
{
return triangleMeshContainer.GetOrCreateTriangleMesh(mesh);
}
public void DestroyTriangleMesh(ObiTriangleMeshHandle meshHandle)
{
triangleMeshContainer.DestroyTriangleMesh(meshHandle);
}
public ObiEdgeMeshHandle GetOrCreateEdgeMesh(EdgeCollider2D collider)
{
return edgeMeshContainer.GetOrCreateEdgeMesh(collider);
}
public void DestroyEdgeMesh(ObiEdgeMeshHandle meshHandle)
{
edgeMeshContainer.DestroyEdgeMesh(meshHandle);
}
public ObiDistanceFieldHandle GetOrCreateDistanceField(ObiDistanceField df)
{
return distanceFieldContainer.GetOrCreateDistanceField(df);
}
public void DestroyDistanceField(ObiDistanceFieldHandle dfHandle)
{
distanceFieldContainer.DestroyDistanceField(dfHandle);
}
public ObiHeightFieldHandle GetOrCreateHeightField(TerrainData hf)
{
return heightFieldContainer.GetOrCreateHeightField(hf);
}
public void DestroyHeightField(ObiHeightFieldHandle hfHandle)
{
heightFieldContainer.DestroyHeightField(hfHandle);
}
public void DestroyCollider(ObiColliderHandle handle)
{
if (colliderShapes != null && handle != null && handle.isValid && handle.index < colliderHandles.Count)
{
int index = handle.index;
int lastIndex = colliderHandles.Count - 1;
// swap all collider info:
colliderHandles.Swap(index, lastIndex);
colliderShapes.Swap(index, lastIndex);
colliderAabbs.Swap(index, lastIndex);
colliderTransforms.Swap(index, lastIndex);
// update the index of the handle we swapped with:
colliderHandles[index].index = index;
// invalidate our handle:
// (after updating the swapped one!
// in case there's just one handle in the array,
// we need to write -1 after 0)
handle.Invalidate();
// remove last index:
colliderHandles.RemoveAt(lastIndex);
colliderShapes.count--;
colliderAabbs.count--;
colliderTransforms.count--;
DestroyIfUnused();
}
}
public void DestroyRigidbody(ObiRigidbodyHandle handle)
{
if (rigidbodies != null && handle != null && handle.isValid && handle.index < rigidbodyHandles.Count)
{
int index = handle.index;
int lastIndex = rigidbodyHandles.Count - 1;
// swap all collider info:
rigidbodyHandles.Swap(index, lastIndex);
rigidbodies.Swap(index, lastIndex);
// update the index of the handle we swapped with:
rigidbodyHandles[index].index = index;
// invalidate our handle:
// (after updating the swapped one!
// in case there's just one handle in the array,
// we need to write -1 after 0)
handle.Invalidate();
// remove last index:
rigidbodyHandles.RemoveAt(lastIndex);
rigidbodies.count--;
DestroyIfUnused();
}
}
public void DestroyCollisionMaterial(ObiCollisionMaterialHandle handle)
{
if (collisionMaterials != null && handle != null && handle.isValid && handle.index < materialHandles.Count)
{
int index = handle.index;
int lastIndex = materialHandles.Count - 1;
// swap all collider info:
materialHandles.Swap(index, lastIndex);
collisionMaterials.Swap(index, lastIndex);
// update the index of the handle we swapped with:
materialHandles[index].index = index;
// invalidate our handle:
// (after updating the swapped one!
// in case there's just one handle in the array,
// we need to write -1 after 0)
handle.Invalidate();
// remove last index:
materialHandles.RemoveAt(lastIndex);
collisionMaterials.count--;
DestroyIfUnused();
}
}
public void UpdateColliders()
{
// update all colliders:
for (int i = 0; i < colliderHandles.Count; ++i)
colliderHandles[i].owner.UpdateIfNeeded();
}
public void UpdateRigidbodies(List<ObiSolver> solvers, float stepTime)
{
// reset all solver's delta buffers to zero:
foreach (ObiSolver solver in solvers)
{
if (solver != null && solver.initialized)
{
solver.EnsureRigidbodyArraysCapacity(rigidbodyHandles.Count);
solver.rigidbodyLinearDeltas.WipeToZero();
solver.rigidbodyAngularDeltas.WipeToZero();
}
}
for (int i = 0; i < rigidbodyHandles.Count; ++i)
rigidbodyHandles[i].owner.UpdateIfNeeded(stepTime);
}
public void UpdateWorld(float deltaTime)
{
for (int i = 0; i < implementations.Count; ++i)
{
if (implementations[i].referenceCount > 0)
{
// set arrays:
implementations[i].SetColliders(colliderShapes, colliderAabbs, colliderTransforms, colliderShapes.count);
implementations[i].SetRigidbodies(rigidbodies);
implementations[i].SetCollisionMaterials(collisionMaterials);
implementations[i].SetTriangleMeshData(triangleMeshContainer.headers, triangleMeshContainer.bihNodes, triangleMeshContainer.triangles, triangleMeshContainer.vertices);
implementations[i].SetEdgeMeshData(edgeMeshContainer.headers, edgeMeshContainer.bihNodes, edgeMeshContainer.edges, edgeMeshContainer.vertices);
implementations[i].SetDistanceFieldData(distanceFieldContainer.headers, distanceFieldContainer.dfNodes);
implementations[i].SetHeightFieldData(heightFieldContainer.headers, heightFieldContainer.samples);
// update world implementation:
implementations[i].UpdateWorld(deltaTime);
}
}
}
public void UpdateRigidbodyVelocities(List<ObiSolver> solvers)
{
int count = 0;
foreach (ObiSolver solver in solvers)
if (solver != null && solver.initialized) count++;
if (count > 0)
{
// we want to average the deltas applied by all solvers, so calculate 1/solverCount.
float rcpCount = 1.0f / count;
for (int i = 0; i < rigidbodyHandles.Count; ++i)
{
Vector4 linearDelta = Vector4.zero;
Vector4 angularDelta = Vector4.zero;
foreach (ObiSolver solver in solvers)
{
if (solver != null && solver.initialized)
{
linearDelta += solver.rigidbodyLinearDeltas[i] * rcpCount;
angularDelta += solver.rigidbodyAngularDeltas[i] * rcpCount;
}
}
// update rigidbody velocities
rigidbodyHandles[i].owner.UpdateVelocities(linearDelta, angularDelta);
}
}
}
}
}

View File

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

View File

@@ -0,0 +1,452 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Obi
{
public class ObiResourceHandle<T> where T : class
{
public T owner = null; /**< reference to the owner instance*/
public int index = -1; /**< index of this resource in the collision world.*/
private int referenceCount = 0; /**< amount of references to this handle. Can be used to clean up any associated resources after it reaches zero.*/
public bool isValid
{
get { return index >= 0; }
}
public void Invalidate()
{
index = -1;
referenceCount = 0;
}
public void Reference()
{
referenceCount++;
}
public bool Dereference()
{
return --referenceCount == 0;
}
public ObiResourceHandle(int index = -1)
{
this.index = index;
owner = null;
}
}
public class ObiColliderHandle : ObiResourceHandle<ObiColliderBase>
{
public ObiColliderHandle(int index = -1) : base(index) { }
}
public class ObiCollisionMaterialHandle : ObiResourceHandle<ObiCollisionMaterial>
{
public ObiCollisionMaterialHandle(int index = -1) : base(index) { }
}
public class ObiRigidbodyHandle : ObiResourceHandle<ObiRigidbodyBase>
{
public ObiRigidbodyHandle(int index = -1) : base(index) { }
}
[ExecuteInEditMode]
public class ObiColliderWorld
{
[NonSerialized] public List<IColliderWorldImpl> implementations;
[NonSerialized] public List<ObiColliderHandle> colliderHandles; // list of collider handles, used by ObiCollider components to retrieve them.
[NonSerialized] public ObiNativeColliderShapeList colliderShapes; // list of collider shapes.
[NonSerialized] public ObiNativeAabbList colliderAabbs; // list of collider bounds.
[NonSerialized] public ObiNativeAffineTransformList colliderTransforms; // list of collider transforms.
[NonSerialized] public List<ObiCollisionMaterialHandle> materialHandles;
[NonSerialized] public ObiNativeCollisionMaterialList collisionMaterials; // list of collision materials.
[NonSerialized] public List<ObiRigidbodyHandle> rigidbodyHandles; // list of rigidbody handles, used by ObiRigidbody components to retrieve them.
[NonSerialized] public ObiNativeRigidbodyList rigidbodies; // list of rigidbodies.
[NonSerialized] public ObiTriangleMeshContainer triangleMeshContainer;
[NonSerialized] public ObiEdgeMeshContainer edgeMeshContainer;
[NonSerialized] public ObiDistanceFieldContainer distanceFieldContainer;
[NonSerialized] public ObiHeightFieldContainer heightFieldContainer;
private static ObiColliderWorld instance;
public static ObiColliderWorld GetInstance()
{
if (instance == null)
{
instance = new ObiColliderWorld();
instance.Initialize();
}
return instance;
}
private void Initialize()
{
// Allocate all lists:
if (implementations == null)
implementations = new List<IColliderWorldImpl>();
if (colliderHandles == null)
colliderHandles = new List<ObiColliderHandle>();
if (colliderShapes == null)
colliderShapes = new ObiNativeColliderShapeList();
if (colliderAabbs == null)
colliderAabbs = new ObiNativeAabbList();
if (colliderTransforms == null)
colliderTransforms = new ObiNativeAffineTransformList();
if (materialHandles == null)
materialHandles = new List<ObiCollisionMaterialHandle>();
if (collisionMaterials == null)
collisionMaterials = new ObiNativeCollisionMaterialList();
if (rigidbodyHandles == null)
rigidbodyHandles = new List<ObiRigidbodyHandle>();
if (rigidbodies == null)
rigidbodies = new ObiNativeRigidbodyList();
if (triangleMeshContainer == null)
triangleMeshContainer = new ObiTriangleMeshContainer();
if (edgeMeshContainer == null)
edgeMeshContainer = new ObiEdgeMeshContainer();
if (distanceFieldContainer == null)
distanceFieldContainer = new ObiDistanceFieldContainer();
if (heightFieldContainer == null)
heightFieldContainer = new ObiHeightFieldContainer();
}
private void Destroy()
{
for (int i = 0; i < implementations.Count; ++i)
{
implementations[i].SetColliders(colliderShapes, colliderAabbs, colliderTransforms, 0);
implementations[i].UpdateWorld(0);
}
// Invalidate all handles:
if (colliderHandles != null)
foreach (var handle in colliderHandles)
handle.Invalidate();
if (rigidbodyHandles != null)
foreach (var handle in rigidbodyHandles)
handle.Invalidate();
if (materialHandles != null)
foreach (var handle in materialHandles)
handle.Invalidate();
// Dispose of all lists:
implementations = null;
colliderHandles = null;
rigidbodyHandles = null;
materialHandles = null;
if (colliderShapes != null)
colliderShapes.Dispose();
if (colliderAabbs != null)
colliderAabbs.Dispose();
if (colliderTransforms != null)
colliderTransforms.Dispose();
if (collisionMaterials != null)
collisionMaterials.Dispose();
if (rigidbodies != null)
rigidbodies.Dispose();
if (triangleMeshContainer != null)
triangleMeshContainer.Dispose();
if (edgeMeshContainer != null)
edgeMeshContainer.Dispose();
if (distanceFieldContainer != null)
distanceFieldContainer.Dispose();
if (heightFieldContainer != null)
heightFieldContainer.Dispose();
instance = null;
}
private void DestroyIfUnused()
{
// when there are no implementations and no colliders, the world gets destroyed.
if (colliderHandles.Count == 0 && rigidbodyHandles.Count == 0 && materialHandles.Count == 0 && implementations.Count == 0)
Destroy();
}
public void RegisterImplementation(IColliderWorldImpl impl)
{
if (!implementations.Contains(impl))
implementations.Add(impl);
}
public void UnregisterImplementation(IColliderWorldImpl impl)
{
implementations.Remove(impl);
DestroyIfUnused();
}
public ObiColliderHandle CreateCollider()
{
var handle = new ObiColliderHandle(colliderHandles.Count);
colliderHandles.Add(handle);
colliderShapes.Add(new ColliderShape());
colliderAabbs.Add(new Aabb());
colliderTransforms.Add(new AffineTransform());
return handle;
}
public ObiRigidbodyHandle CreateRigidbody()
{
var handle = new ObiRigidbodyHandle(rigidbodyHandles.Count);
rigidbodyHandles.Add(handle);
rigidbodies.Add(new ColliderRigidbody());
return handle;
}
public ObiCollisionMaterialHandle CreateCollisionMaterial()
{
var handle = new ObiCollisionMaterialHandle(materialHandles.Count);
materialHandles.Add(handle);
collisionMaterials.Add(new CollisionMaterial());
return handle;
}
public ObiTriangleMeshHandle GetOrCreateTriangleMesh(Mesh mesh)
{
return triangleMeshContainer.GetOrCreateTriangleMesh(mesh);
}
public void DestroyTriangleMesh(ObiTriangleMeshHandle meshHandle)
{
triangleMeshContainer.DestroyTriangleMesh(meshHandle);
}
public ObiEdgeMeshHandle GetOrCreateEdgeMesh(EdgeCollider2D collider)
{
return edgeMeshContainer.GetOrCreateEdgeMesh(collider);
}
public void DestroyEdgeMesh(ObiEdgeMeshHandle meshHandle)
{
edgeMeshContainer.DestroyEdgeMesh(meshHandle);
}
public ObiDistanceFieldHandle GetOrCreateDistanceField(ObiDistanceField df)
{
return distanceFieldContainer.GetOrCreateDistanceField(df);
}
public void DestroyDistanceField(ObiDistanceFieldHandle dfHandle)
{
distanceFieldContainer.DestroyDistanceField(dfHandle);
}
public ObiHeightFieldHandle GetOrCreateHeightField(TerrainData hf)
{
return heightFieldContainer.GetOrCreateHeightField(hf);
}
public void DestroyHeightField(ObiHeightFieldHandle hfHandle)
{
heightFieldContainer.DestroyHeightField(hfHandle);
}
public void DestroyCollider(ObiColliderHandle handle)
{
if (colliderShapes != null && handle != null && handle.isValid && handle.index < colliderHandles.Count)
{
int index = handle.index;
int lastIndex = colliderHandles.Count - 1;
// swap all collider info:
colliderHandles.Swap(index, lastIndex);
colliderShapes.Swap(index, lastIndex);
colliderAabbs.Swap(index, lastIndex);
colliderTransforms.Swap(index, lastIndex);
// update the index of the handle we swapped with:
colliderHandles[index].index = index;
// invalidate our handle:
// (after updating the swapped one!
// in case there's just one handle in the array,
// we need to write -1 after 0)
handle.Invalidate();
// remove last index:
colliderHandles.RemoveAt(lastIndex);
colliderShapes.count--;
colliderAabbs.count--;
colliderTransforms.count--;
DestroyIfUnused();
}
}
public void DestroyRigidbody(ObiRigidbodyHandle handle)
{
if (rigidbodies != null && handle != null && handle.isValid && handle.index < rigidbodyHandles.Count)
{
int index = handle.index;
int lastIndex = rigidbodyHandles.Count - 1;
// swap all collider info:
rigidbodyHandles.Swap(index, lastIndex);
rigidbodies.Swap(index, lastIndex);
// update the index of the handle we swapped with:
rigidbodyHandles[index].index = index;
// invalidate our handle:
// (after updating the swapped one!
// in case there's just one handle in the array,
// we need to write -1 after 0)
handle.Invalidate();
// remove last index:
rigidbodyHandles.RemoveAt(lastIndex);
rigidbodies.count--;
DestroyIfUnused();
}
}
public void DestroyCollisionMaterial(ObiCollisionMaterialHandle handle)
{
if (collisionMaterials != null && handle != null && handle.isValid && handle.index < materialHandles.Count)
{
int index = handle.index;
int lastIndex = materialHandles.Count - 1;
// swap all collider info:
materialHandles.Swap(index, lastIndex);
collisionMaterials.Swap(index, lastIndex);
// update the index of the handle we swapped with:
materialHandles[index].index = index;
// invalidate our handle:
// (after updating the swapped one!
// in case there's just one handle in the array,
// we need to write -1 after 0)
handle.Invalidate();
// remove last index:
materialHandles.RemoveAt(lastIndex);
collisionMaterials.count--;
DestroyIfUnused();
}
}
public void UpdateColliders()
{
// update all colliders:
for (int i = 0; i < colliderHandles.Count; ++i)
colliderHandles[i].owner.UpdateIfNeeded();
<<<<<<< HEAD
=======
for (int i = 0; i < implementations.Count; ++i)
{
if (implementations[i].referenceCount > 0)
{
// set arrays:
implementations[i].SetColliders(colliderShapes, colliderAabbs, colliderTransforms, colliderShapes.count);
implementations[i].SetRigidbodies(rigidbodies);
implementations[i].SetCollisionMaterials(collisionMaterials);
implementations[i].SetTriangleMeshData(triangleMeshContainer.headers, triangleMeshContainer.bihNodes, triangleMeshContainer.triangles, triangleMeshContainer.vertices);
implementations[i].SetEdgeMeshData(edgeMeshContainer.headers, edgeMeshContainer.bihNodes, edgeMeshContainer.edges, edgeMeshContainer.vertices);
implementations[i].SetDistanceFieldData(distanceFieldContainer.headers, distanceFieldContainer.dfNodes);
implementations[i].SetHeightFieldData(heightFieldContainer.headers, heightFieldContainer.samples);
// update world implementation:
implementations[i].UpdateWorld();
}
}
>>>>>>> 56Hotfix
}
public void UpdateRigidbodies(List<ObiSolver> solvers, float stepTime)
{
// reset all solver's delta buffers to zero:
foreach (ObiSolver solver in solvers)
{
if (solver != null)
{
solver.EnsureRigidbodyArraysCapacity(rigidbodyHandles.Count);
solver.rigidbodyLinearDeltas.WipeToZero();
solver.rigidbodyAngularDeltas.WipeToZero();
}
}
for (int i = 0; i < rigidbodyHandles.Count; ++i)
rigidbodyHandles[i].owner.UpdateIfNeeded(stepTime);
}
public void UpdateWorld(float deltaTime)
{
for (int i = 0; i < implementations.Count; ++i)
{
// set arrays:
implementations[i].SetColliders(colliderShapes, colliderAabbs, colliderTransforms, colliderShapes.count);
implementations[i].SetRigidbodies(rigidbodies);
implementations[i].SetCollisionMaterials(collisionMaterials);
implementations[i].SetTriangleMeshData(triangleMeshContainer.headers, triangleMeshContainer.bihNodes, triangleMeshContainer.triangles, triangleMeshContainer.vertices);
implementations[i].SetEdgeMeshData(edgeMeshContainer.headers, edgeMeshContainer.bihNodes, edgeMeshContainer.edges, edgeMeshContainer.vertices);
implementations[i].SetDistanceFieldData(distanceFieldContainer.headers, distanceFieldContainer.dfNodes);
implementations[i].SetHeightFieldData(heightFieldContainer.headers, heightFieldContainer.samples);
// update world implementation:
implementations[i].UpdateWorld(deltaTime);
}
}
public void UpdateRigidbodyVelocities(List<ObiSolver> solvers)
{
int count = 0;
foreach (ObiSolver solver in solvers)
if (solver != null) count++;
if (count > 0)
{
// we want to average the deltas applied by all solvers, so calculate 1/solverCount.
float rcpCount = 1.0f / count;
for (int i = 0; i < rigidbodyHandles.Count; ++i)
{
Vector4 linearDelta = Vector4.zero;
Vector4 angularDelta = Vector4.zero;
foreach (ObiSolver solver in solvers)
{
if (solver != null)
{
linearDelta += solver.rigidbodyLinearDeltas[i] * rcpCount;
angularDelta += solver.rigidbodyAngularDeltas[i] * rcpCount;
}
}
// update rigidbody velocities
rigidbodyHandles[i].owner.UpdateVelocities(linearDelta, angularDelta);
}
}
}
}
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 663bb98786e3a46a3b909fa8d01073f2
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,80 @@
using UnityEngine;
using System;
using System.Collections;
namespace Obi{
/**
* Holds information about the physics properties of a particle or collider, and how it should react to collisions.
*/
[CreateAssetMenu(fileName = "collision material", menuName = "Obi/Collision Material", order = 180)]
public class ObiCollisionMaterial : ScriptableObject
{
protected ObiCollisionMaterialHandle materialHandle;
public float dynamicFriction;
public float staticFriction;
public float stickiness;
public float stickDistance;
public Oni.MaterialCombineMode frictionCombine;
public Oni.MaterialCombineMode stickinessCombine;
[Space]
public bool rollingContacts = false;
[Indent()]
[VisibleIf("rollingContacts")]
public float rollingFriction;
public ObiCollisionMaterialHandle handle
{
get
{
CreateMaterialIfNeeded();
return materialHandle;
}
}
public void OnEnable()
{
UpdateMaterial();
}
public void OnDisable()
{
ObiColliderWorld.GetInstance().DestroyCollisionMaterial(materialHandle);
}
public void OnValidate()
{
// we can't create GameObjects in OnValidate(), so make sure the colliderworld already exists.
UpdateMaterial();
}
public void UpdateMaterial()
{
var world = ObiColliderWorld.GetInstance();
var mat = world.collisionMaterials[handle.index];
mat.FromObiCollisionMaterial(this);
world.collisionMaterials[handle.index] = mat;
}
protected void CreateMaterialIfNeeded()
{
if (materialHandle == null || !materialHandle.isValid)
{
var world = ObiColliderWorld.GetInstance();
// create the material:
materialHandle = world.CreateCollisionMaterial();
materialHandle.owner = this;
// copy material data from this material (use materialHandle instead of handle, to not retrigger CreateMaterialIfNeeded)
var mat = world.collisionMaterials[materialHandle.index];
mat.FromObiCollisionMaterial(this);
world.collisionMaterials[materialHandle.index] = mat;
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5b9c84cdb747944209b1c8753933c5db
timeCreated: 1483360014
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: e101664ae1c454916896aaaa3199221c, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,129 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Obi{
[CreateAssetMenu(fileName = "distance field", menuName = "Obi/Distance Field", order = 181)]
[ExecuteInEditMode]
public class ObiDistanceField : ScriptableObject
{
[SerializeProperty("InputMesh")]
[SerializeField] private Mesh input = null;
[HideInInspector][SerializeField] private float minNodeSize = 0;
[HideInInspector][SerializeField] private Bounds bounds = new Bounds();
[HideInInspector] public List<DFNode> nodes; /**< list of distance field nodes*/
[Range(0.0000001f,0.1f)]
public float maxError = 0.01f;
[Range(1, 8)]
public int maxDepth = 5;
public bool Initialized{
get{return nodes != null;}
}
public Bounds FieldBounds {
get{return bounds;}
}
public float EffectiveSampleSize {
get{return minNodeSize;}
}
public Mesh InputMesh{
set{
if (value != input){
Reset();
input = value;
}
}
get{return input;}
}
public void Reset(){
nodes = null;
if (input != null)
bounds = input.bounds;
}
public IEnumerator Generate(){
Reset();
if (input == null)
yield break;
int[] tris = input.triangles;
Vector3[] verts = input.vertices;
nodes = new List<DFNode>();
var buildingCoroutine = ASDF.Build(maxError, maxDepth, verts, tris, nodes);
while (buildingCoroutine.MoveNext())
yield return new CoroutineJob.ProgressInfo("Processed nodes: " + nodes.Count, 1);
// calculate min node size;
minNodeSize = float.PositiveInfinity;
for (int j = 0; j < nodes.Count; ++j)
minNodeSize = Mathf.Min(minNodeSize, nodes[j].center[3] * 2);
// get bounds:
float max = Mathf.Max(bounds.size[0], Mathf.Max(bounds.size[1], bounds.size[2])) + 0.2f;
bounds.size = new Vector3(max, max, max);
}
/**
* Return a volume texture containing a representation of this distance field.
*/
public Texture3D GetVolumeTexture(int size){
if (!Initialized)
return null;
// upper bound of the distance from any point inside the bounds to the surface.
float maxDist = Mathf.Max(bounds.size.x,bounds.size.y,bounds.size.z);
float spacingX = bounds.size.x / (float)size;
float spacingY = bounds.size.y / (float)size;
float spacingZ = bounds.size.z / (float)size;
Texture3D tex = new Texture3D (size, size, size, TextureFormat.Alpha8, false);
var cols = new Color[size*size*size];
int idx = 0;
Color c = Color.black;
for (int z = 0; z < size; ++z)
{
for (int y = 0; y < size; ++y)
{
for (int x = 0; x < size; ++x, ++idx)
{
Vector3 samplePoint = bounds.min + new Vector3(spacingX * x + spacingX*0.5f,
spacingY * y + spacingY*0.5f,
spacingZ * z + spacingZ*0.5f);
float distance = ASDF.Sample(nodes,samplePoint);
if (distance >= 0)
c.a = distance.Remap(0,maxDist*0.1f,0.5f,1);
else
c.a = distance.Remap(-maxDist*0.1f,0,0,0.5f);
cols[idx] = c;
}
}
}
tex.SetPixels (cols);
tex.Apply ();
return tex;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d31207380edf34b1b84ce50af22f2356
timeCreated: 1516041748
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: a83ed40096e3a40efa85f35b091e6298, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Obi
{
public class ObiDistanceFieldHandle : ObiResourceHandle<ObiDistanceField>
{
public ObiDistanceFieldHandle(ObiDistanceField field, int index = -1) : base(index) { owner = field; }
}
public struct DistanceFieldHeader //we need to use the header in the backend, so it must be a struct.
{
public int firstNode;
public int nodeCount;
public DistanceFieldHeader(int firstNode, int nodeCount)
{
this.firstNode = firstNode;
this.nodeCount = nodeCount;
}
}
public class ObiDistanceFieldContainer
{
public Dictionary<ObiDistanceField, ObiDistanceFieldHandle> handles; /**< dictionary indexed by asset, so that we don't generate data for the same distance field multiple times.*/
public ObiNativeDistanceFieldHeaderList headers; /**< One header per distance field.*/
public ObiNativeDFNodeList dfNodes;
public ObiDistanceFieldContainer()
{
handles = new Dictionary<ObiDistanceField, ObiDistanceFieldHandle>();
headers = new ObiNativeDistanceFieldHeaderList();
dfNodes = new ObiNativeDFNodeList();
}
public ObiDistanceFieldHandle GetOrCreateDistanceField(ObiDistanceField source)
{
ObiDistanceFieldHandle handle;
if (!handles.TryGetValue(source, out handle))
{
handle = new ObiDistanceFieldHandle(source, headers.count);
handles.Add(source, handle);
headers.Add(new DistanceFieldHeader(dfNodes.count, source.nodes.Count));
dfNodes.AddRange(source.nodes);
}
return handle;
}
public void DestroyDistanceField(ObiDistanceFieldHandle handle)
{
if (handle != null && handle.isValid && handle.index < handles.Count)
{
var header = headers[handle.index];
// Update headers:
for (int i = 0; i < headers.count; ++i)
{
var h = headers[i];
if (h.firstNode > header.firstNode)
{
h.firstNode -= header.nodeCount;
headers[i] = h;
}
}
// update handles:
foreach (var pair in handles)
{
if (pair.Value.index > handle.index)
pair.Value.index--;
}
// Remove nodes
dfNodes.RemoveRange(header.firstNode, header.nodeCount);
// remove header:
headers.RemoveAt(handle.index);
// remove the mesh from the dictionary:
handles.Remove(handle.owner);
// Invalidate our handle:
handle.Invalidate();
}
}
public void Dispose()
{
if (headers != null)
headers.Dispose();
if (dfNodes != null)
dfNodes.Dispose();
}
}
}

View File

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

View File

@@ -0,0 +1,163 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Obi
{
public struct Edge : IBounded
{
public int i1;
public int i2;
Aabb b;
public Edge(int i1, int i2, Vector2 v1, Vector2 v2)
{
this.i1 = i1;
this.i2 = i2;
b = new Aabb(v1);
b.Encapsulate(v2);
}
public Aabb GetBounds()
{
return b;
}
}
public class ObiEdgeMeshHandle : ObiResourceHandle<EdgeCollider2D>
{
public ObiEdgeMeshHandle(EdgeCollider2D collider, int index = -1) : base(index) { owner = collider; }
}
public struct EdgeMeshHeader
{
public int firstNode;
public int nodeCount;
public int firstEdge;
public int edgeCount;
public int firstVertex;
public int vertexCount;
public EdgeMeshHeader(int firstNode, int nodeCount, int firstTriangle, int triangleCount, int firstVertex, int vertexCount)
{
this.firstNode = firstNode;
this.nodeCount = nodeCount;
this.firstEdge = firstTriangle;
this.edgeCount = triangleCount;
this.firstVertex = firstVertex;
this.vertexCount = vertexCount;
}
}
public class ObiEdgeMeshContainer
{
public Dictionary<EdgeCollider2D, ObiEdgeMeshHandle> handles; /**< dictionary indexed by mesh, so that we don't generate data for the same mesh multiple times.*/
public ObiNativeEdgeMeshHeaderList headers;
public ObiNativeBIHNodeList bihNodes;
public ObiNativeEdgeList edges;
public ObiNativeVector2List vertices;
public ObiEdgeMeshContainer()
{
handles = new Dictionary<EdgeCollider2D, ObiEdgeMeshHandle>();
headers = new ObiNativeEdgeMeshHeaderList();
bihNodes = new ObiNativeBIHNodeList();
edges = new ObiNativeEdgeList();
vertices = new ObiNativeVector2List();
}
public ObiEdgeMeshHandle GetOrCreateEdgeMesh(EdgeCollider2D source)
{
ObiEdgeMeshHandle handle;
if (!handles.TryGetValue(source, out handle))
{
Vector2[] sourceVertices = source.points;
int[] sourceEdges = new int[source.edgeCount * 2];
for (int i = 0; i < source.edgeCount; ++i)
{
sourceEdges[i * 2] = i;
sourceEdges[i * 2 + 1] = i + 1;
}
// Build a bounding interval hierarchy from the edges:
IBounded[] t = new IBounded[source.edgeCount];
for (int i = 0; i < source.edgeCount; ++i)
{
t[i] = new Edge(i, i+1, sourceVertices[i], sourceVertices[i+1]);
}
var sourceBih = BIH.Build(ref t);
Edge[] edgs = Array.ConvertAll(t, x => (Edge)x);
handle = new ObiEdgeMeshHandle(source, headers.count);
handles.Add(source, handle);
headers.Add(new EdgeMeshHeader(bihNodes.count, sourceBih.Length, edges.count, edgs.Length, vertices.count, sourceVertices.Length));
bihNodes.AddRange(sourceBih);
edges.AddRange(edgs);
vertices.AddRange(sourceVertices);
}
return handle;
}
public void DestroyEdgeMesh(ObiEdgeMeshHandle handle)
{
if (handle != null && handle.isValid && handle.index < handles.Count)
{
var header = headers[handle.index];
// Update headers:
for (int i = 0; i < headers.count; ++i)
{
var h = headers[i];
if (h.firstEdge > header.firstEdge)
{
h.firstNode -= header.nodeCount;
h.firstEdge -= header.edgeCount;
h.firstVertex -= header.vertexCount;
headers[i] = h;
}
}
// update handles:
foreach (var pair in handles)
{
if (pair.Value.index > handle.index)
pair.Value.index--;
}
// Remove nodes, triangles and vertices
bihNodes.RemoveRange(header.firstNode, header.nodeCount);
edges.RemoveRange(header.firstEdge, header.edgeCount);
vertices.RemoveRange(header.firstVertex, header.vertexCount);
// remove header:
headers.RemoveAt(handle.index);
// remove the collider from the dictionary:
handles.Remove(handle.owner);
// Invalidate our handle:
handle.Invalidate();
}
}
public void Dispose()
{
if (headers != null)
headers.Dispose();
if (edges != null)
edges.Dispose();
if (vertices != null)
vertices.Dispose();
if (bihNodes != null)
bihNodes.Dispose();
}
}
}

View File

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

View File

@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Obi
{
public class ObiHeightFieldHandle : ObiResourceHandle<TerrainData>
{
public ObiHeightFieldHandle(TerrainData data, int index = -1) : base(index) { owner = data; }
}
public struct HeightFieldHeader //we need to use the header in the backend, so it must be a struct.
{
public int firstSample;
public int sampleCount;
public HeightFieldHeader(int firstSample, int sampleCount)
{
this.firstSample = firstSample;
this.sampleCount = sampleCount;
}
}
public class ObiHeightFieldContainer
{
public Dictionary<TerrainData, ObiHeightFieldHandle> handles; /**< dictionary indexed by asset, so that we don't generate data for the same distance field multiple times.*/
public ObiNativeHeightFieldHeaderList headers; /**< One header per distance field.*/
public ObiNativeFloatList samples;
public ObiHeightFieldContainer()
{
handles = new Dictionary<TerrainData, ObiHeightFieldHandle>();
headers = new ObiNativeHeightFieldHeaderList();
samples = new ObiNativeFloatList();
}
public ObiHeightFieldHandle GetOrCreateHeightField(TerrainData source)
{
ObiHeightFieldHandle handle;
if (!handles.TryGetValue(source, out handle))
{
// Get the heighfield into a 1d array:
int width = source.heightmapResolution;
int height = source.heightmapResolution;
float[,] heights = source.GetHeights(0, 0, width, height);
bool[,] holes = source.GetHoles(0, 0, width-1, height-1);
float[] buffer = new float[width * height];
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x)
buffer[y * width + x] = heights[y, x] * (holes[Mathf.Min(y,height - 2), Mathf.Min(x, width - 2)] ? 1:-1);
handle = new ObiHeightFieldHandle(source, headers.count);
handles.Add(source, handle);
headers.Add(new HeightFieldHeader(samples.count, buffer.Length));
samples.AddRange(buffer);
}
return handle;
}
public void DestroyHeightField(ObiHeightFieldHandle handle)
{
if (handle != null && handle.isValid && handle.index < handles.Count)
{
var header = headers[handle.index];
// Update headers:
for (int i = 0; i < headers.count; ++i)
{
var h = headers[i];
if (h.firstSample > header.firstSample)
{
h.firstSample -= header.sampleCount;
headers[i] = h;
}
}
// update handles:
foreach (var pair in handles)
{
if (pair.Value.index > handle.index)
pair.Value.index--;
}
// Remove nodes
samples.RemoveRange(header.firstSample, header.sampleCount);
// remove header:
headers.RemoveAt(handle.index);
// remove the heightfield from the dictionary:
handles.Remove(handle.owner);
// Invalidate our handle:
handle.Invalidate();
}
}
public void Dispose()
{
if (headers != null)
headers.Dispose();
if (samples != null)
samples.Dispose();
}
}
}

View File

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

View File

@@ -0,0 +1,89 @@
using UnityEngine;
using System;
using System.Collections;
namespace Obi
{
/**
* Small helper class that lets you specify Obi-only properties for rigidbodies.
*/
[ExecuteInEditMode]
[RequireComponent(typeof(Rigidbody))]
public class ObiRigidbody : ObiRigidbodyBase
{
public Rigidbody unityRigidbody { get; private set; }
public Vector3 position => unityRigidbody.position;
public Quaternion rotation => unityRigidbody.rotation;
public Vector3 linearVelocity { get; protected set; }
public Vector3 angularVelocity { get; protected set; }
private Quaternion prevRotation;
private Vector3 prevPosition;
public override void OnEnable()
{
unityRigidbody = GetComponent<Rigidbody>();
prevPosition = unityRigidbody.position;
prevRotation = unityRigidbody.rotation;
linearVelocity = unityRigidbody.linearVelocity;
angularVelocity = unityRigidbody.angularVelocity;
base.OnEnable();
}
private void UpdateVelocities(float stepTime)
{
// differentiate positions/orientations to get our own velocites for kinematic objects.
// when calling Physics.Simulate, MovePosition/Rotation do not work correctly. Also useful for animations.
if (unityRigidbody.isKinematic)
{
// differentiate positions to obtain linear velocity:
linearVelocity = (unityRigidbody.position - prevPosition) / stepTime;
// differentiate rotations to obtain angular velocity:
Quaternion delta = unityRigidbody.rotation * Quaternion.Inverse(prevRotation);
angularVelocity = new Vector3(delta.x, delta.y, delta.z) * 2.0f / stepTime;
}
else
{
// if the object is non-kinematic, just copy velocities.
linearVelocity = unityRigidbody.linearVelocity;
angularVelocity = unityRigidbody.angularVelocity;
}
prevPosition = unityRigidbody.position;
prevRotation = unityRigidbody.rotation;
}
public override void UpdateIfNeeded(float stepTime)
{
UpdateVelocities(stepTime);
var world = ObiColliderWorld.GetInstance();
var rb = world.rigidbodies[handle.index];
rb.FromRigidbody(this);
world.rigidbodies[handle.index] = rb;
}
/**
* Reads velocities back from the solver.
*/
public override void UpdateVelocities(Vector3 linearDelta, Vector3 angularDelta)
{
// kinematic rigidbodies are passed to Obi with zero velocity, so we must ignore the new velocities calculated by the solver:
if (Application.isPlaying && !(unityRigidbody.isKinematic || kinematicForParticles))
{
if (Vector3.SqrMagnitude(linearDelta) > 0.00001f || Vector3.SqrMagnitude(angularDelta) > 0.00001f)
{
unityRigidbody.linearVelocity += linearDelta;
unityRigidbody.angularVelocity += angularDelta;
}
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 62359d80fdc0e4315ab6331b78ec5b90
timeCreated: 1502137073
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 1788ce7c914374d949486d9789c9f306, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,87 @@
using UnityEngine;
using System;
using System.Collections;
namespace Obi
{
/**
* Small helper class that lets you specify Obi-only properties for rigidbodies.
*/
[ExecuteInEditMode]
[RequireComponent(typeof(Rigidbody2D))]
public class ObiRigidbody2D : ObiRigidbodyBase
{
public Rigidbody2D unityRigidbody { get; private set; }
public Vector2 position => unityRigidbody.position;
public float rotation => unityRigidbody.rotation;
public Vector2 linearVelocity { get; protected set; }
public float angularVelocity { get; protected set; }
private Quaternion prevRotation;
private Vector3 prevPosition;
public override void OnEnable()
{
unityRigidbody = GetComponent<Rigidbody2D>();
prevPosition = transform.position;
prevRotation = transform.rotation;
linearVelocity = unityRigidbody.linearVelocity;
angularVelocity = unityRigidbody.angularVelocity;
base.OnEnable();
}
private void UpdateKinematicVelocities(float stepTime)
{
// differentiate positions/orientations to get our own velocites for kinematic objects.
// when calling Physics.Simulate, MovePosition/Rotation do not work correctly. Also useful for animations.
if (unityRigidbody.isKinematic)
{
// differentiate positions to obtain linear velocity:
linearVelocity = (transform.position - prevPosition) / stepTime;
// differentiate rotations to obtain angular velocity:
Quaternion delta = transform.rotation * Quaternion.Inverse(prevRotation);
angularVelocity = delta.z * Mathf.Rad2Deg * 2.0f / stepTime;
}
else
{
// if the object is non-kinematic, just copy velocities.
linearVelocity = unityRigidbody.linearVelocity;
angularVelocity = unityRigidbody.angularVelocity;
}
prevPosition = transform.position;
prevRotation = transform.rotation;
}
public override void UpdateIfNeeded(float stepTime)
{
UpdateKinematicVelocities(stepTime);
var world = ObiColliderWorld.GetInstance();
var rb = world.rigidbodies[handle.index];
rb.FromRigidbody(this);
world.rigidbodies[handle.index] = rb;
}
/**
* Reads velocities back from the solver.
*/
public override void UpdateVelocities(Vector3 linearDelta, Vector3 angularDelta)
{
// kinematic rigidbodies are passed to Obi with zero velocity, so we must ignore the new velocities calculated by the solver:
if (Application.isPlaying && !(unityRigidbody.isKinematic || kinematicForParticles))
{
unityRigidbody.linearVelocity += new Vector2(linearDelta.x, linearDelta.y);
unityRigidbody.angularVelocity += angularDelta[2] * Mathf.Rad2Deg;
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d8e1dfa1dce354ed499bfb5d2b1cd799
timeCreated: 1517939164
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 21e01eb398ab24a7abb1f3b493af6b20, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,41 @@
using UnityEngine;
using Unity.Profiling;
using System;
using System.Collections;
namespace Obi{
/**
* Small helper class that lets you specify Obi-only properties for rigidbodies.
*/
[ExecuteInEditMode]
public abstract class ObiRigidbodyBase : MonoBehaviour
{
public bool kinematicForParticles = false;
public ObiRigidbodyHandle handle;
public virtual void OnEnable()
{
handle = ObiColliderWorld.GetInstance().CreateRigidbody();
handle.owner = this;
UpdateIfNeeded(1);
}
public void OnDisable()
{
ObiColliderWorld.GetInstance().DestroyRigidbody(handle);
}
public abstract void UpdateIfNeeded(float stepTime);
/**
* Reads velocities back from the solver.
*/
public abstract void UpdateVelocities(Vector3 linearDelta, Vector3 angularDelta);
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 8f0adb129c9cf4d978dc5964711503b8
timeCreated: 1516131526
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,163 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Obi
{
public struct Triangle : IBounded
{
public int i1;
public int i2;
public int i3;
Aabb b;
public Triangle(int i1, int i2, int i3, Vector3 v1, Vector3 v2, Vector3 v3)
{
this.i1 = i1;
this.i2 = i2;
this.i3 = i3;
b = new Aabb(v1);
b.Encapsulate(v2);
b.Encapsulate(v3);
}
public Aabb GetBounds()
{
return b;
}
}
public class ObiTriangleMeshHandle : ObiResourceHandle<Mesh>
{
public ObiTriangleMeshHandle(Mesh mesh, int index = -1) : base(index) { owner = mesh; }
}
public struct TriangleMeshHeader //we need to use the header in the backend, so it must be a struct.
{
public int firstNode;
public int nodeCount;
public int firstTriangle;
public int triangleCount;
public int firstVertex;
public int vertexCount;
public TriangleMeshHeader(int firstNode, int nodeCount, int firstTriangle, int triangleCount, int firstVertex, int vertexCount)
{
this.firstNode = firstNode;
this.nodeCount = nodeCount;
this.firstTriangle = firstTriangle;
this.triangleCount = triangleCount;
this.firstVertex = firstVertex;
this.vertexCount = vertexCount;
}
}
public class ObiTriangleMeshContainer
{
public Dictionary<Mesh,ObiTriangleMeshHandle> handles; /**< dictionary indexed by mesh, so that we don't generate data for the same mesh multiple times.*/
public ObiNativeTriangleMeshHeaderList headers; /**< One header per mesh.*/
public ObiNativeBIHNodeList bihNodes;
public ObiNativeTriangleList triangles;
public ObiNativeVector3List vertices;
public ObiTriangleMeshContainer()
{
handles = new Dictionary<Mesh, ObiTriangleMeshHandle>();
headers = new ObiNativeTriangleMeshHeaderList();
bihNodes = new ObiNativeBIHNodeList();
triangles = new ObiNativeTriangleList();
vertices = new ObiNativeVector3List();
}
public ObiTriangleMeshHandle GetOrCreateTriangleMesh(Mesh source)
{
ObiTriangleMeshHandle handle = new ObiTriangleMeshHandle(null);
if (source != null && !handles.TryGetValue(source, out handle))
{
var sourceTris = source.triangles;
var sourceVertices = source.vertices;
// Build a bounding interval hierarchy from the triangles:
IBounded[] t = new IBounded[sourceTris.Length/3];
for (int i = 0; i < t.Length; ++i)
{
int t1 = sourceTris[i * 3];
int t2 = sourceTris[i * 3 + 1];
int t3 = sourceTris[i * 3 + 2];
t[i] = new Triangle(t1,t2,t3, sourceVertices[t1], sourceVertices[t2], sourceVertices[t3]);
}
var sourceBih = BIH.Build(ref t);
Triangle[] tris = Array.ConvertAll(t, x => (Triangle)x);
handle = new ObiTriangleMeshHandle(source, headers.count);
handles.Add(source, handle);
headers.Add(new TriangleMeshHeader(bihNodes.count, sourceBih.Length, triangles.count, tris.Length, vertices.count, sourceVertices.Length));
bihNodes.AddRange(sourceBih);
triangles.AddRange(tris);
vertices.AddRange(sourceVertices);
}
return handle;
}
public void DestroyTriangleMesh(ObiTriangleMeshHandle handle)
{
if (handle != null && handle.isValid && handle.index < handles.Count)
{
var header = headers[handle.index];
// Update headers:
for (int i = 0; i < headers.count; ++i)
{
var h = headers[i];
if (h.firstTriangle > header.firstTriangle)
{
h.firstNode -= header.nodeCount;
h.firstTriangle -= header.triangleCount;
h.firstVertex -= header.vertexCount;
headers[i] = h;
}
}
// update handles:
foreach (var pair in handles)
{
if (pair.Value.index > handle.index)
pair.Value.index --;
}
// Remove nodes, triangles and vertices
bihNodes.RemoveRange(header.firstNode, header.nodeCount);
triangles.RemoveRange(header.firstTriangle, header.triangleCount);
vertices.RemoveRange(header.firstVertex, header.vertexCount);
// remove header:
headers.RemoveAt(handle.index);
// remove the mesh from the dictionary:
handles.Remove(handle.owner);
// Invalidate our handle:
handle.Invalidate();
}
}
public void Dispose()
{
if (headers != null)
headers.Dispose();
if (triangles != null)
triangles.Dispose();
if (vertices != null)
vertices.Dispose();
if (bihNodes != null)
bihNodes.Dispose();
}
}
}

View File

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