public override bool UpdateIfNeeded() { bool trigger = false; if (collider is Collider) { trigger = ((Collider)collider).isTrigger; } else if (collider is Collider2D) { trigger = ((Collider2D)collider).isTrigger; } // retrieve collision world and index: var world = ObiColliderWorld.GetInstance(); int index = source.Handle.index; // decrease reference count of current handle if the df data it points to is different // than the df used by the collider: if (handle != null && handle.owner != distanceField) { if (handle.Dereference()) { world.DestroyDistanceField(handle); } } if (handle == null || !handle.isValid) { handle = world.GetOrCreateDistanceField(distanceField); handle.Reference(); } // update collider: var shape = world.colliderShapes[index]; shape.type = ColliderShape.ShapeType.SignedDistanceField; shape.phase = source.Phase; shape.flags = trigger ? 1 : 0; shape.rigidbodyIndex = source.Rigidbody != null ? source.Rigidbody.handle.index : -1; shape.materialIndex = source.CollisionMaterial != null ? source.CollisionMaterial.handle.index : -1; shape.contactOffset = source.Thickness; shape.dataIndex = handle.index; world.colliderShapes[index] = shape; // update bounds: var aabb = world.colliderAabbs[index]; aabb.FromBounds(distanceField.FieldBounds.Transform(source.transform.localToWorldMatrix), shape.contactOffset); world.colliderAabbs[index] = aabb; // update transform: var trfm = world.colliderTransforms[index]; trfm.FromTransform(source.transform); world.colliderTransforms[index] = trfm; return(true); }
public 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 DestroyDistanceField(ObiDistanceFieldHandle dfHandle) { distanceFieldContainer.DestroyDistanceField(dfHandle); }