去掉obi,使用自写绳索

This commit is contained in:
2026-02-23 20:51:03 +08:00
parent cb636f862d
commit 91e2309eeb
2011 changed files with 2593 additions and 190578 deletions

View File

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

View File

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

View File

@@ -1,48 +0,0 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiBoxShapeTracker2D : ObiShapeTracker
{
public ObiBoxShapeTracker2D(ObiCollider2D source, BoxCollider2D collider){
this.source = source;
this.collider = collider;
}
public override void UpdateIfNeeded (){
BoxCollider2D box = collider as BoxCollider2D;
var world = ObiColliderWorld.GetInstance();
int index = source.Handle.index;
// update collider:
var shape = world.colliderShapes[index];
shape.is2D = true;
shape.type = ColliderShape.ShapeType.Box;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = box.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.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.FromTransform2D(box.transform, source.Rigidbody as ObiRigidbody2D);
world.colliderTransforms[index] = trfm;
}
}
}

View File

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

View File

@@ -1,53 +0,0 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiCapsuleShapeTracker2D : ObiShapeTracker
{
public ObiCapsuleShapeTracker2D(ObiCollider2D source, CapsuleCollider2D collider)
{
this.source = source;
this.collider = collider;
}
public override void 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 = true;
shape.type = ColliderShape.ShapeType.Capsule;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = capsule.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.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.FromTransform2D(capsule.transform, source.Rigidbody as ObiRigidbody2D);
world.colliderTransforms[index] = trfm;
}
}
}

View File

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

View File

@@ -1,52 +0,0 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiCircleShapeTracker2D : ObiShapeTracker
{
public ObiCircleShapeTracker2D(ObiCollider2D source, CircleCollider2D collider)
{
this.source = source;
this.collider = collider;
}
public override void 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 = true;
shape.type = ColliderShape.ShapeType.Sphere;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = sphere.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.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.FromTransform2D(sphere.transform, source.Rigidbody as ObiRigidbody2D);
world.colliderTransforms[index] = trfm;
}
}
}

View File

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

View File

@@ -1,72 +0,0 @@
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 void 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 = true;
shape.type = ColliderShape.ShapeType.EdgeMesh;
shape.filter = source.Filter;
shape.SetSign(source.Inverted);
shape.isTrigger = edgeCollider.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.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.FromTransform2D(edgeCollider.transform, source.Rigidbody as ObiRigidbody2D);
world.colliderTransforms[index] = trfm;
}
public override void Destroy()
{
base.Destroy();
if (handle != null && handle.Dereference())
ObiColliderWorld.GetInstance().DestroyEdgeMesh(handle);
}
}
}

View File

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

View File

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

View File

@@ -1,51 +0,0 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiBoxShapeTracker : ObiShapeTracker
{
public ObiBoxShapeTracker(ObiCollider source, BoxCollider collider)
{
this.source = source;
this.collider = collider;
}
public override void UpdateIfNeeded (){
BoxCollider box = collider as BoxCollider;
// 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.SetSign(source.Inverted);
shape.isTrigger = box.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.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.FromTransform3D(box.transform, source.Rigidbody as ObiRigidbody);
world.colliderTransforms[index] = trfm;
}
}
}

View File

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

View File

@@ -1,49 +0,0 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiCapsuleShapeTracker : ObiShapeTracker
{
public ObiCapsuleShapeTracker(ObiCollider source, CapsuleCollider collider){
this.collider = collider;
this.source = source;
}
public override void 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.SetSign(source.Inverted);
shape.isTrigger = capsule.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.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.FromTransform3D(capsule.transform, source.Rigidbody as ObiRigidbody);
world.colliderTransforms[index] = trfm;
}
}
}

View File

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

View File

@@ -1,52 +0,0 @@
using System;
using UnityEngine;
namespace Obi
{
public class ObiCharacterControllerShapeTracker : ObiShapeTracker
{
public ObiCharacterControllerShapeTracker(ObiCollider source, CharacterController collider)
{
this.collider = collider;
this.source = source;
}
public override void 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.SetSign(source.Inverted);
shape.isTrigger = character.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.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.FromTransform3D(character.transform, source.Rigidbody as ObiRigidbody);
world.colliderTransforms[index] = trfm;
}
}
}

View File

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

View File

@@ -1,88 +0,0 @@
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 void 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.SetSign(source.Inverted);
shape.isTrigger = trigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.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.FromTransform3D(source.transform, source.Rigidbody as ObiRigidbody);
world.colliderTransforms[index] = trfm;
}
public override void Destroy()
{
base.Destroy();
if (handle != null && handle.Dereference())
ObiColliderWorld.GetInstance().DestroyDistanceField(handle);
}
}
}

View File

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

View File

@@ -1,91 +0,0 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
namespace Obi{
public class ObiMeshShapeTracker : ObiShapeTracker
{
ObiTriangleMeshHandle handle;
public Mesh targetMesh
{
get {
var mc = collider as MeshCollider;
return mc?.sharedMesh;
}
}
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 void 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.SetSign(source.Inverted);
shape.isTrigger = meshCollider.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.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.FromTransform3D(meshCollider.transform, source.Rigidbody as ObiRigidbody);
world.colliderTransforms[index] = trfm;
}
public override void Destroy()
{
base.Destroy();
if (handle != null && handle.Dereference())
ObiColliderWorld.GetInstance().DestroyTriangleMesh(handle);
}
}
}

View File

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

View File

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

View File

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

View File

@@ -1,50 +0,0 @@
using System;
using UnityEngine;
namespace Obi{
public class ObiSphereShapeTracker : ObiShapeTracker
{
public ObiSphereShapeTracker(ObiCollider source, SphereCollider collider)
{
this.source = source;
this.collider = collider;
}
public override void UpdateIfNeeded()
{
SphereCollider sphere = collider as SphereCollider;
// 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.SetSign(source.Inverted);
shape.isTrigger = sphere.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.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.FromTransform3D(sphere.transform, source.Rigidbody as ObiRigidbody);
world.colliderTransforms[index] = trfm;
}
}
}

View File

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

View File

@@ -1,75 +0,0 @@
using System;
using UnityEngine;
using System.Runtime.InteropServices;
namespace Obi{
public class ObiTerrainShapeTracker : ObiShapeTracker
{
ObiHeightFieldHandle handle;
public ObiTerrainShapeTracker(ObiCollider source, TerrainCollider collider){
this.source = source;
this.collider = collider;
}
public void UpdateHeightData()
{
ObiColliderWorld.GetInstance().DestroyHeightField(handle);
}
public override void 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.SetSign(source.Inverted);
shape.isTrigger = terrain.isTrigger;
shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.Handle.index : -1;
shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1;
shape.forceZoneIndex = source.ForceZone != null ? source.ForceZone.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.FromTransform3D(terrain.transform, source.Rigidbody as ObiRigidbody);
world.colliderTransforms[index] = trfm;
}
public override void Destroy()
{
base.Destroy();
if (handle != null && handle.Dereference())
ObiColliderWorld.GetInstance().DestroyHeightField(handle);
}
}
}

View File

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

View File

@@ -1,117 +0,0 @@
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;
}
RemoveCollider();
m_SourceCollider = value;
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

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

View File

@@ -1,82 +0,0 @@
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

@@ -1,12 +0,0 @@
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

@@ -1,228 +0,0 @@
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("Inverted")]
[SerializeField] private bool inverted = false;
[SerializeProperty("CollisionMaterial")]
[SerializeField] private ObiCollisionMaterial material;
[SerializeField] private int filter = ObiUtils.MakeFilter(ObiUtils.CollideWithEverything, 0);
public ObiCollisionMaterial CollisionMaterial
{
set
{
if (material != value)
{
material = value;
ForceUpdate();
}
}
get { return material; }
}
public int Filter
{
set
{
if (filter != value)
{
filter = value;
ForceUpdate();
}
}
get { return filter; }
}
public float Thickness
{
set
{
if (!Mathf.Approximately(thickness, value))
{
thickness = value;
ForceUpdate();
}
}
get { return thickness; }
}
public bool Inverted
{
set
{
if (inverted != value)
{
inverted = value;
ForceUpdate();
}
}
get { return inverted; }
}
public ObiShapeTracker Tracker
{
get { return tracker; }
}
public ObiColliderHandle Handle
{
get
{
if (shapeHandle == null)
FindSourceCollider();
return shapeHandle;
}
}
public ObiForceZone ForceZone
{
get; set;
}
public ObiRigidbodyBase Rigidbody
{
get { return obiRigidbody; }
}
protected ObiColliderHandle shapeHandle;
protected ObiRigidbodyBase obiRigidbody;
protected bool wasUnityColliderEnabled = true;
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;
}
}
/**
* Flags the collider as needing to be updated from now on. If the object it's on has any editor static flag enabled,
* the collider will be again flagged as *not* needing to be updated after its next update.
*/
public void ForceUpdate()
{
ObiColliderWorld.GetInstance().MarkColliderAsNeedingUpdate(shapeHandle);
}
/**
* 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);
if (unityCollider != null)
{
// Only if this object is not static:
if (tracker != null) //&& needsUpdate)
{
tracker.UpdateIfNeeded();
}
// check isStatic, *after* updating the tracker at least once.
if (unityCollider.gameObject.isStatic)
ObiColliderWorld.GetInstance().MarkColliderAsNotNeedingUpdate(shapeHandle);
}
// 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

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

View File

@@ -1,704 +0,0 @@
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 ObiForceZoneHandle : ObiResourceHandle<ObiForceZone>
{
public ObiForceZoneHandle(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) { }
}
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<ObiForceZoneHandle> forceZoneHandles; // list of collider handles, used by ObiForceZone components to retrieve them.
[NonSerialized] public ObiNativeForceZoneList forceZones; // list of collider force zones.
[NonSerialized] public List<ObiCollisionMaterialHandle> materialHandles; // list of material handles, used by ObiCollisionMaterial components to retrieve them.
[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 List<ObiColliderHandle> collidersToCreate;
private List<ObiColliderHandle> collidersToDestroy;
private List<ObiForceZoneHandle> forceZonesToCreate;
private List<ObiForceZoneHandle> forceZonesToDestroy;
private List<ObiRigidbodyHandle> rigidbodiesToCreate;
private List<ObiRigidbodyHandle> rigidbodiesToDestroy;
public int collidersToUpdateCount { private set; get; } = 0; // amount of colliders that need to be updated. These are always grouped at the start of the collider arrays (handles, shapes, etc).
private bool dirty = false;
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 (forceZoneHandles == null)
forceZoneHandles = new List<ObiForceZoneHandle>();
if (forceZones == null)
forceZones = new ObiNativeForceZoneList();
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();
if (collidersToCreate == null)
collidersToCreate = new List<ObiColliderHandle>();
if (collidersToDestroy == null)
collidersToDestroy = new List<ObiColliderHandle>();
if (forceZonesToCreate == null)
forceZonesToCreate = new List<ObiForceZoneHandle>();
if (forceZonesToDestroy == null)
forceZonesToDestroy = new List<ObiForceZoneHandle>();
if (rigidbodiesToCreate == null)
rigidbodiesToCreate = new List<ObiRigidbodyHandle>();
if (rigidbodiesToDestroy == null)
rigidbodiesToDestroy = new List<ObiRigidbodyHandle>();
}
private void Destroy()
{
dirty = false;
for (int i = 0; i < implementations.Count; ++i)
{
implementations[i].SetColliders(colliderShapes, colliderAabbs, colliderTransforms);
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();
if (forceZoneHandles != null)
foreach (var handle in forceZoneHandles)
handle.Invalidate();
// Dispose of all lists:
implementations = null;
colliderHandles = null;
rigidbodyHandles = null;
materialHandles = null;
forceZoneHandles = null;
collidersToCreate = null;
collidersToDestroy = null;
forceZonesToCreate = null;
forceZonesToDestroy = null;
rigidbodiesToCreate = null;
rigidbodiesToDestroy = null;
colliderShapes?.Dispose();
colliderAabbs?.Dispose();
colliderTransforms?.Dispose();
forceZones?.Dispose();
collisionMaterials?.Dispose();
rigidbodies?.Dispose();
triangleMeshContainer?.Dispose();
edgeMeshContainer?.Dispose();
distanceFieldContainer?.Dispose();
heightFieldContainer?.Dispose();
instance = null;
}
private void DestroyIfUnused()
{
// when there is no data and no implementations, the world gets destroyed.
// don't check materialHandles.Count == 0, as these are scriptable objects and may outlive the world.
if (colliderHandles.Count == 0 &&
rigidbodyHandles.Count == 0 &&
forceZoneHandles.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();
// in-editor, we create data right away since the simulation is not running.
if (!Application.isPlaying)
CreateColliderData(handle);
else
collidersToCreate.Add(handle);
return handle;
}
public ObiForceZoneHandle CreateForceZone()
{
var handle = new ObiForceZoneHandle();
// in-editor, we create data right away since the simulation is not running.
if (!Application.isPlaying)
CreateForceZoneData(handle);
else
forceZonesToCreate.Add(handle);
return handle;
}
public ObiRigidbodyHandle CreateRigidbody()
{
var handle = new ObiRigidbodyHandle();
// in-editor, we create data right away since the simulation is not running.
if (!Application.isPlaying)
CreateRigidbodyData(handle);
else
rigidbodiesToCreate.Add(handle);
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)
{
// Destroy data right away if no simulation is running.
if (!Application.isPlaying || implementations.Count == 0)
DestroyColliderData(handle);
else
{
// In case the handle is in the creation queue, just remove it.
if (!collidersToCreate.Remove(handle))
collidersToDestroy.Add(handle);
}
}
public void DestroyForceZone(ObiForceZoneHandle handle)
{
// Destroy data right away if no simulation is running.
if (!Application.isPlaying || implementations.Count == 0)
DestroyForceZoneData(handle);
else
{
// In case the handle is in the creation queue, just remove it.
if (!forceZonesToCreate.Remove(handle))
forceZonesToDestroy.Add(handle);
}
}
public void DestroyRigidbody(ObiRigidbodyHandle handle)
{
// Destroy data right away if no simulation is running.
if (!Application.isPlaying || implementations.Count == 0)
DestroyRigidbodyData(handle);
else
{
// In case the handle is in the creation queue, just remove it.
if (!rigidbodiesToCreate.Remove(handle))
rigidbodiesToDestroy.Add(handle);
}
}
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();
}
}
private void DestroyColliderData (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--;
// force all colliders to update next frame, as the index of the data they reference
// (eg the mesh in a MeshCollider) may have changed as a result of deleting this collider's data.
collidersToUpdateCount = colliderHandles.Count;
DestroyIfUnused();
}
}
private void DestroyForceZoneData(ObiForceZoneHandle handle)
{
if (forceZones != null && handle != null && handle.isValid && handle.index < forceZoneHandles.Count)
{
int index = handle.index;
int lastIndex = forceZoneHandles.Count - 1;
// swap all force zone info:
forceZoneHandles.Swap(index, lastIndex);
forceZones.Swap(index, lastIndex);
// update the index of the handle we swapped with:
forceZoneHandles[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:
forceZoneHandles.RemoveAt(lastIndex);
forceZones.count--;
DestroyIfUnused();
}
}
private void DestroyRigidbodyData(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();
}
}
private void CreateColliderData(ObiColliderHandle handle)
{
handle.index = colliderHandles.Count;
colliderHandles.Add(handle);
colliderShapes.Add(new ColliderShape { materialIndex = -1, rigidbodyIndex = -1, dataIndex = -1 });
colliderAabbs.Add(new Aabb());
colliderTransforms.Add(new AffineTransform());
MarkColliderAsNeedingUpdate(handle);
}
private void CreateForceZoneData(ObiForceZoneHandle handle)
{
handle.index = forceZoneHandles.Count;
forceZoneHandles.Add(handle);
forceZones.Add(new ForceZone());
}
private void CreateRigidbodyData(ObiRigidbodyHandle handle)
{
handle.index = rigidbodyHandles.Count;
rigidbodyHandles.Add(handle);
rigidbodies.Add(new ColliderRigidbody());
}
public bool DoesColliderRequireToBeUpdated(ObiColliderHandle handle)
{
if (handle != null && handle.isValid &&
handle.index < colliderHandles.Count && handle.index < collidersToUpdateCount)
return true;
return false;
}
public void MarkColliderAsNeedingUpdate(ObiColliderHandle handle)
{
if (colliderShapes != null && handle != null && handle.isValid &&
handle.index < colliderHandles.Count && handle.index >= collidersToUpdateCount &&
collidersToUpdateCount < colliderHandles.Count)
{
int index = handle.index;
int lastIndex = collidersToUpdateCount;
// swap all collider info:
colliderHandles.Swap(index, lastIndex);
colliderShapes.Swap(index, lastIndex);
colliderAabbs.Swap(index, lastIndex);
colliderTransforms.Swap(index, lastIndex);
// update handles:
colliderHandles[lastIndex].index = lastIndex;
colliderHandles[index].index = index;
collidersToUpdateCount++;
}
}
public void MarkColliderAsNotNeedingUpdate(ObiColliderHandle handle)
{
if (colliderShapes != null && handle != null && handle.isValid &&
handle.index < colliderHandles.Count && handle.index < collidersToUpdateCount)
{
int index = handle.index;
int lastIndex = collidersToUpdateCount-1;
// swap all collider info:
colliderHandles.Swap(index, lastIndex);
colliderShapes.Swap(index, lastIndex);
colliderAabbs.Swap(index, lastIndex);
colliderTransforms.Swap(index, lastIndex);
// update handles:
colliderHandles[lastIndex].index = lastIndex;
colliderHandles[index].index = index;
collidersToUpdateCount--;
}
}
public void FlushHandleBuffers()
{
// First process destruction, then process creation.
// In case we create a handle and then destroy it,
// we should enqueue it for destruction only if it's not in the creation queue.
// If it is, just remove if from the creation queue.
if (collidersToDestroy != null)
{
foreach (var handle in collidersToDestroy)
DestroyColliderData(handle);
collidersToDestroy?.Clear();
}
if (forceZonesToDestroy != null)
{
foreach (var handle in forceZonesToDestroy)
DestroyForceZoneData(handle);
forceZonesToDestroy?.Clear();
}
if (rigidbodiesToDestroy != null)
{
foreach (var handle in rigidbodiesToDestroy)
DestroyRigidbodyData(handle);
rigidbodiesToDestroy?.Clear();
}
if (collidersToCreate != null)
{
foreach (var handle in collidersToCreate)
CreateColliderData(handle);
collidersToCreate?.Clear();
}
if (forceZonesToCreate != null)
{
foreach (var handle in forceZonesToCreate)
CreateForceZoneData(handle);
forceZonesToCreate?.Clear();
}
if (rigidbodiesToCreate != null)
{
foreach (var handle in rigidbodiesToCreate)
CreateRigidbodyData(handle);
rigidbodiesToCreate?.Clear();
}
}
public void UpdateWorld(float deltaTime, bool updateDynamics = true)
{
if (!dirty)
return;
dirty = false;
// ensure all objects have valid handles.
// May destroy the world if it's empty,
// so we next check that handle/implementations are not null.
FlushHandleBuffers();
// update all colliders:
if (colliderHandles != null)
for (int i = 0; i < collidersToUpdateCount; ++i)
colliderHandles[i].owner.UpdateIfNeeded();
// update all force zones:
if (forceZoneHandles != null)
for (int i = 0; i < forceZoneHandles.Count; ++i)
forceZoneHandles[i].owner.UpdateIfNeeded();
// update rigidbodies:
if (rigidbodyHandles != null && updateDynamics)
for (int i = 0; i < rigidbodyHandles.Count; ++i)
rigidbodyHandles[i].owner.UpdateIfNeeded(deltaTime);
// update implementations:
if (implementations != null)
for (int i = 0; i < implementations.Count; ++i)
{
if (implementations[i].referenceCount > 0)
{
// set arrays:
implementations[i].SetColliders(colliderShapes, colliderAabbs, colliderTransforms);
implementations[i].SetForceZones(forceZones);
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:
if (updateDynamics)
implementations[i].UpdateWorld(deltaTime);
}
}
}
public void SetDirty()
{
dirty = true;
}
public void UpdateCollisionMaterials()
{
if (implementations != null)
for (int i = 0; i < implementations.Count; ++i)
{
if (implementations[i].referenceCount > 0)
{
implementations[i].SetCollisionMaterials(collisionMaterials);
}
}
}
public void UpdateRigidbodyVelocities(ObiSolver solver)
{
if (solver != null && solver.initialized)
{
int count = Mathf.Min(rigidbodyHandles.Count, solver.rigidbodyLinearDeltas.count);
for (int i = 0; i < count; ++i)
rigidbodyHandles[i].owner.UpdateVelocities(solver.rigidbodyLinearDeltas[i], solver.rigidbodyAngularDeltas[i]);
}
solver.rigidbodyLinearDeltas.WipeToZero();
solver.rigidbodyAngularDeltas.WipeToZero();
solver.rigidbodyLinearDeltas.Upload();
solver.rigidbodyAngularDeltas.Upload();
}
}
}

View File

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

View File

@@ -1,452 +0,0 @@
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

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

View File

@@ -1,80 +0,0 @@
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;
}
}
private void OnEnable()
{
UpdateMaterial();
}
private void OnDisable()
{
ObiColliderWorld.GetInstance().DestroyCollisionMaterial(materialHandle);
}
private 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

@@ -1,12 +0,0 @@
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

@@ -1,129 +0,0 @@
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

@@ -1,12 +0,0 @@
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

@@ -1,103 +0,0 @@
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

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

View File

@@ -1,163 +0,0 @@
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

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

View File

@@ -1,114 +0,0 @@
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

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

View File

@@ -1,102 +0,0 @@
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;
protected override void OnEnable()
{
unityRigidbody = GetComponent<Rigidbody>();
ResetPosition();
base.OnEnable();
}
public void ResetPosition()
{
prevPosition = unityRigidbody.position;
prevRotation = unityRigidbody.rotation;
linearVelocity = unityRigidbody.linearVelocity;
angularVelocity = unityRigidbody.angularVelocity;
}
private void CacheVelocities(float stepTime)
{
// differentiate positions/orientations to get our own velocites for kinematic objects.
// also useful for animations.
if (unityRigidbody.isKinematic && stepTime > 0)
{
// 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)
{
// Rigidbody might not exist, as rigidbody deletion is buffered.
// This means the unity rigidbody might be deleted before the rigidbody handle is invalidated.
// Conversely, rigidbodyHandle might be non-null but invalid.
if (unityRigidbody == null || !Handle.isValid) return;
CacheVelocities(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)
{
// Rigidbody might not exist, as rigidbody deletion is buffered.
// This means the unity rigidbody might be deleted before the rigidbody handle is invalidated.
if (unityRigidbody == null) return;
// 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

@@ -1,12 +0,0 @@
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

@@ -1,98 +0,0 @@
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;
protected override void OnEnable()
{
unityRigidbody = GetComponent<Rigidbody2D>();
ResetPosition();
base.OnEnable();
}
public void ResetPosition()
{
prevPosition = unityRigidbody.position;
prevRotation = Quaternion.AngleAxis(unityRigidbody.rotation, Vector3.forward);
linearVelocity = unityRigidbody.linearVelocity;
angularVelocity = unityRigidbody.angularVelocity;
}
private void CacheVelocities(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 && stepTime > 0)
{
// 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)
{
// Rigidbody might not exist, as rigidbody deletion is buffered.
// This means the unity rigidbody might be deleted before the rigidbody handle is invalidated.
if (unityRigidbody == null) return;
CacheVelocities(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)
{
// Rigidbody might not exist, as rigidbody deletion is buffered.
// This means the unity rigidbody might be deleted before the rigidbody handle is invalidated.
if (unityRigidbody == null) return;
// 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

@@ -1,12 +0,0 @@
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

@@ -1,58 +0,0 @@
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;
protected ObiRigidbodyHandle rigidbodyHandle;
public ObiRigidbodyHandle Handle
{
get
{
// don't check rigidbodyHandle.isValid:
// CreateRigidbody may defer creation, so we get a non-null, but invalid handle.
// If calling handle again right away before it becomes valid, it will call CreateRigidbody() again and create a second handle to the same body.
if (rigidbodyHandle == null)
{
var world = ObiColliderWorld.GetInstance();
// create the material:
rigidbodyHandle = world.CreateRigidbody();
rigidbodyHandle.owner = this;
}
return rigidbodyHandle;
}
}
protected virtual void OnEnable()
{
rigidbodyHandle = ObiColliderWorld.GetInstance().CreateRigidbody();
rigidbodyHandle.owner = this;
}
public void OnDisable()
{
ObiColliderWorld.GetInstance().DestroyRigidbody(rigidbodyHandle);
}
public abstract void UpdateIfNeeded(float stepTime);
/**
* Reads velocities back from the solver.
*/
public abstract void UpdateVelocities(Vector3 linearDelta, Vector3 angularDelta);
}
}

View File

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

View File

@@ -1,171 +0,0 @@
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))
{
if (source.isReadable)
{
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);
}
else
{
handle = new ObiTriangleMeshHandle(source);
handles.Add(source, handle);
}
}
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

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