升级obi

This commit is contained in:
2026-01-22 22:08:21 +08:00
parent 120b8cda26
commit 20f14322bc
1067 changed files with 149894 additions and 29583 deletions

View File

@@ -0,0 +1,69 @@
using UnityEngine;
using UnityEngine.Events;
namespace Obi.Samples
{
[RequireComponent(typeof(ObiSolver))]
public class ActorActorCollisionDetector : MonoBehaviour
{
public struct ActorPair
{
public readonly ObiActor actorA;
public readonly ObiActor actorB;
public int particleA;
public int particleB;
public ActorPair(ObiActor actorA, ObiActor actorB, int particleA, int particleB)
{
this.actorA = actorA;
this.actorB = actorB;
this.particleA = particleA;
this.particleB = particleB;
}
}
public UnityEvent<ActorPair> callback;
ObiSolver solver;
void OnEnable()
{
solver = GetComponent<Obi.ObiSolver>();
solver.OnParticleCollision += Solver_OnCollision;
}
void OnDisable()
{
solver.OnParticleCollision -= Solver_OnCollision;
}
void Solver_OnCollision(object sender, ObiNativeContactList e)
{
if (!solver.initialized || callback == null) return;
// just iterate over all contacts in the current frame:
foreach (Oni.Contact contact in e)
{
// if this one is an actual collision:
if (contact.distance < 0.01)
{
// get the index of the first entry in the simplices array for both bodies:
int startA = solver.simplexCounts.GetSimplexStartAndSize(contact.bodyA, out _);
int startB = solver.simplexCounts.GetSimplexStartAndSize(contact.bodyB, out _);
// retrieve the index of both particles from the simplices array:
int particleA = solver.simplices[startA];
int particleB = solver.simplices[startB];
// retrieve info about both actors involved in the collision:
var particleInActorA = solver.particleToActor[particleA];
var particleInActorB = solver.particleToActor[particleB];
// if they're not the same actor, trigger a callback:
if (particleInActorA != null && particleInActorB != null && particleInActorA.actor != particleInActorB.actor)
callback.Invoke(new ActorPair(particleInActorA.actor, particleInActorB.actor, particleA, particleB));
}
}
}
}
}

View File

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

View File

@@ -0,0 +1,32 @@
using UnityEngine;
using Obi;
namespace Obi.Samples
{
[RequireComponent(typeof(ObiActor))]
public class ActorBlinker : MonoBehaviour
{
public Color neutralColor = Color.white;
public Color highlightColor = Color.red;
private ObiActor actor;
void Awake()
{
actor = GetComponent<ObiActor>();
}
public void Blink(int particleIndex)
{
if (actor.solver != null)
actor.solver.colors[particleIndex] = highlightColor;
}
void LateUpdate()
{
if (actor.solver != null)
for (int i = 0; i < actor.activeParticleCount; ++i)
actor.solver.colors[actor.solverIndices[i]] += (neutralColor - actor.solver.colors[actor.solverIndices[i]]) * Time.deltaTime * 5;
}
}
}

View File

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

View File

@@ -1,21 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;
public class ActorCOMTransform : MonoBehaviour
namespace Obi.Samples
{
public Vector3 offset;
public ObiActor actor;
public void Update()
public class ActorCOMTransform : MonoBehaviour
{
if (actor != null && actor.isLoaded)
public Vector3 offset;
public ObiActor actor;
public void Update()
{
Vector3 com;
actor.GetMass(out com);
transform.position = actor.solver.transform.TransformPoint(com) + offset;
if (actor != null && actor.isLoaded)
{
Vector3 com;
actor.GetMass(out com);
transform.position = actor.solver.transform.TransformPoint(com) + offset;
}
}
}
}

View File

@@ -1,35 +1,33 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;
public class ActorSpawner : MonoBehaviour {
namespace Obi.Samples
{
public class ActorSpawner : MonoBehaviour
{
public ObiActor template;
public ObiActor template;
public int basePhase = 2;
public int maxInstances = 32;
public float spawnDelay = 0.3f;
public int maxInstances = 32;
public float spawnDelay = 0.3f;
private int phase = 0;
private int instances = 0;
private float timeFromLastSpawn = 0;
// Update is called once per frame
void Update () {
private int instances = 0;
private float timeFromLastSpawn = 0;
timeFromLastSpawn += Time.deltaTime;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && instances < maxInstances && timeFromLastSpawn > spawnDelay)
{
GameObject go = Instantiate(template.gameObject,transform.position,Quaternion.identity);
go.transform.SetParent(transform.parent);
timeFromLastSpawn += Time.deltaTime;
go.GetComponent<ObiActor>().SetFilterCategory(basePhase + phase);
phase++;
instances++;
timeFromLastSpawn = 0;
}
}
}
if (Input.GetMouseButtonDown(0) && instances < maxInstances && timeFromLastSpawn > spawnDelay)
{
GameObject go = Instantiate(template.gameObject, transform.position, Quaternion.identity);
go.transform.SetParent(transform.parent);
instances++;
timeFromLastSpawn = 0;
}
}
}
}

View File

@@ -1,16 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;
[RequireComponent(typeof(ObiActor))]
public class AddRandomVelocity : MonoBehaviour {
namespace Obi.Samples
{
[RequireComponent(typeof(ObiActor))]
public class AddRandomVelocity : MonoBehaviour
{
public float intensity = 5;
void Update () {
if (Input.GetKeyDown(KeyCode.Space)){
GetComponent<ObiActor>().AddForce(UnityEngine.Random.onUnitSphere*intensity,ForceMode.VelocityChange);
}
}
public float intensity = 5;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GetComponent<ObiActor>().AddForce(UnityEngine.Random.onUnitSphere * intensity, ForceMode.VelocityChange);
}
}
}
}

View File

@@ -1,23 +1,30 @@
using UnityEngine;
public class Blinker : MonoBehaviour {
namespace Obi.Samples
{
public class Blinker : MonoBehaviour
{
public Color highlightColor;
public Color highlightColor;
private Renderer rend;
private Color original;
private Renderer rend;
private Color original;
void Awake(){
rend = GetComponent<Renderer>();
original = rend.material.color;
}
void Awake()
{
rend = GetComponent<Renderer>();
original = rend.material.color;
}
public void Blink(){
rend.material.color = highlightColor;
}
public void Blink()
{
rend.material.color = highlightColor;
}
void LateUpdate(){
rend.material.color += (original - rend.material.color)*Time.deltaTime*5;
}
void LateUpdate()
{
rend.material.color += (original - rend.material.color) * Time.deltaTime * 5;
}
}
}
}

View File

@@ -33,7 +33,7 @@ namespace Obi{
}
private void FixedUpdate()
private void Update()
{
if (!m_Jump)
{

View File

@@ -1,48 +1,51 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Obi;
[RequireComponent(typeof(ObiSolver))]
public class ColliderHighlighter : MonoBehaviour {
namespace Obi.Samples
{
[RequireComponent(typeof(ObiSolver))]
public class ColliderHighlighter : MonoBehaviour
{
ObiSolver solver;
ObiSolver solver;
void Awake(){
solver = GetComponent<Obi.ObiSolver>();
}
void Awake()
{
solver = GetComponent<Obi.ObiSolver>();
}
void OnEnable () {
solver.OnCollision += Solver_OnCollision;
}
void OnEnable()
{
solver.OnCollision += Solver_OnCollision;
}
void OnDisable(){
solver.OnCollision -= Solver_OnCollision;
}
void Solver_OnCollision (object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
{
var colliderWorld = ObiColliderWorld.GetInstance();
void OnDisable()
{
solver.OnCollision -= Solver_OnCollision;
}
Oni.Contact[] contacts = e.contacts.Data;
for(int i = 0; i < e.contacts.Count; ++i)
{
Oni.Contact c = contacts[i];
// make sure this is an actual contact:
if (c.distance < 0.01f)
{
// get the collider:
var col = colliderWorld.colliderHandles[c.bodyB].owner;
void Solver_OnCollision(object sender, ObiNativeContactList e)
{
var colliderWorld = ObiColliderWorld.GetInstance();
if (col != null)
for (int i = 0; i < e.count; ++i)
{
Oni.Contact c = e[i];
// make sure this is an actual contact:
if (c.distance < 0.01f)
{
// make it blink:
Blinker blinker = col.GetComponent<Blinker>();
if (blinker)
blinker.Blink();
}
}
}
}
// get the collider:
var col = colliderWorld.colliderHandles[c.bodyB].owner;
if (col != null)
{
// make it blink:
Blinker blinker = col.GetComponent<Blinker>();
if (blinker)
blinker.Blink();
}
}
}
}
}
}

View File

@@ -1,184 +1,97 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Obi;
[RequireComponent(typeof(ObiSolver))]
public class CollisionEventHandler : MonoBehaviour
namespace Obi.Samples
{
ObiSolver solver;
public int contactCount;
Obi.ObiSolver.ObiCollisionEventArgs frame;
void Awake()
[RequireComponent(typeof(ObiSolver))]
public class CollisionEventHandler : MonoBehaviour
{
solver = GetComponent<Obi.ObiSolver>();
}
void OnEnable()
{
solver.OnParticleCollision += Solver_OnCollision;
}
ObiSolver solver;
public int contactCount;
void OnDisable()
{
solver.OnParticleCollision -= Solver_OnCollision;
}
ObiNativeContactList frame;
void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
{
frame = e;
}
void OnDrawGizmos()
{
if (solver == null || frame == null || frame.contacts == null) return;
Gizmos.matrix = solver.transform.localToWorldMatrix;
contactCount = frame.contacts.Count;
/*for (int i = 0; i < frame.contacts.Count; ++i)
void Awake()
{
var contact = frame.contacts.Data[i];
solver = GetComponent<Obi.ObiSolver>();
}
//if (contact.distance > 0.001f) continue;
Gizmos.color = (contact.distance <= 0) ? Color.red : Color.green;
//Gizmos.color = new Color(((i * 100) % 255) / 255.0f, ((i * 50) % 255) / 255.0f, ((i * 20) % 255) / 255.0f);
Vector3 point = frame.contacts.Data[i].pointB;
Gizmos.DrawSphere(point, 0.01f);
Gizmos.DrawRay(point, contact.normal * contact.distance);
Gizmos.color = Color.cyan;
Gizmos.DrawRay(point, contact.tangent * contact.tangentImpulse + contact.bitangent * contact.bitangentImpulse);
}*/
for (int i = 0; i < frame.contacts.Count; ++i)
void OnEnable()
{
var contact = frame.contacts.Data[i];
solver.OnCollision += Solver_OnCollision;
}
//if (contact.distance > 0.001f) continue;
void OnDisable()
{
solver.OnCollision -= Solver_OnCollision;
}
Gizmos.color = (contact.distance <= 0) ? Color.red : Color.green;
void Solver_OnCollision(object sender, ObiNativeContactList e)
{
frame = e;
}
//Gizmos.color = new Color(((i * 100) % 255) / 255.0f, ((i * 50) % 255) / 255.0f, ((i * 20) % 255) / 255.0f);
void OnDrawGizmos()
{
if (solver == null || frame == null) return;
Vector3 point = Vector3.zero;//frame.contacts.Data[i].point;
Gizmos.matrix = solver.transform.localToWorldMatrix;
int simplexStart = solver.simplexCounts.GetSimplexStartAndSize(contact.bodyB, out int simplexSize);
contactCount = frame.count;
float radius = 0;
for (int j = 0; j < simplexSize; ++j)
for (int i = 0; i < frame.count; ++i)
{
point += (Vector3)solver.positions[solver.simplices[simplexStart + j]] * contact.pointB[j];
radius += solver.principalRadii[solver.simplices[simplexStart + j]].x * contact.pointB[j];
var contact = frame[i];
//if (contact.distance > 0.001f) continue;
Gizmos.color = (contact.distance <= 0) ? Color.red : Color.green;
//Gizmos.color = new Color(((i * 100) % 255) / 255.0f, ((i * 50) % 255) / 255.0f, ((i * 20) % 255) / 255.0f);
Vector3 point = frame[i].pointB;
Gizmos.DrawSphere(point, 0.01f);
Gizmos.DrawRay(point, contact.normal * contact.distance);
Gizmos.color = Color.cyan;
//Gizmos.DrawRay(point, contact.tangent * contact.tangentImpulse + contact.bitangent * contact.bitangentImpulse);
}
Vector3 normal = contact.normal;
/*for (int i = 0; i < frame.count; ++i)
{
var contact = frame[i];
//Gizmos.DrawSphere(point + normal.normalized * frame.contacts[i].distance, 0.01f);
//if (contact.distance > 0.001f) continue;
Gizmos.DrawSphere(point + normal * radius, 0.01f);
Gizmos.color = (contact.distance <= 0) ? Color.red : Color.green;
Gizmos.DrawRay(point + normal * radius, normal.normalized * contact.distance);
//Gizmos.color = new Color(((i * 100) % 255) / 255.0f, ((i * 50) % 255) / 255.0f, ((i * 20) % 255) / 255.0f);
Vector3 point = Vector3.zero;//frame.contacts.Data[i].point;
int simplexStart = solver.simplexCounts.GetSimplexStartAndSize(contact.bodyB, out int simplexSize);
float radius = 0;
for (int j = 0; j < simplexSize; ++j)
{
point += (Vector3)solver.positions[solver.simplices[simplexStart + j]] * contact.pointB[j];
radius += solver.principalRadii[solver.simplices[simplexStart + j]].x * contact.pointB[j];
}
Vector3 normal = contact.normal;
//Gizmos.DrawSphere(point + normal.normalized * frame.contacts[i].distance, 0.01f);
Gizmos.DrawSphere(point + normal * radius, 0.01f);
Gizmos.DrawRay(point + normal * radius, normal.normalized * contact.distance);
}*/
}
}
}
/*
[RequireComponent(typeof(ObiSolver))]
public class CollisionEventHandler : MonoBehaviour {
ObiSolver solver;
public int counter = 0;
public Collider targetCollider = null;
HashSet<int> particles = new HashSet<int>();
void Awake(){
solver = GetComponent<Obi.ObiSolver>();
}
void OnEnable () {
solver.OnCollision += Solver_OnCollision;
}
void OnDisable(){
solver.OnCollision -= Solver_OnCollision;
}
void Solver_OnCollision (object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
{
HashSet<int> currentParticles = new HashSet<int>();
for(int i = 0; i < e.contacts.Count; ++i)
{
if (e.contacts.Data[i].distance < 0.001f)
{
Component collider;
if (ObiCollider.idToCollider.TryGetValue(e.contacts.Data[i].other,out collider)){
if (collider == targetCollider)
currentParticles.Add(e.contacts.Data[i].particle);
}
}
}
particles.ExceptWith(currentParticles);
counter += particles.Count;
particles = currentParticles;
}
}
*/
/*[RequireComponent(typeof(ObiSolver))]
public class CollisionEventHandler : MonoBehaviour {
ObiSolver solver;
public Collider targetCollider = null;
void Awake(){
solver = GetComponent<Obi.ObiSolver>();
}
void OnEnable () {
solver.OnCollision += Solver_OnCollision;
}
void OnDisable(){
solver.OnCollision -= Solver_OnCollision;
}
void Solver_OnCollision (object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
{
for(int i = 0; i < e.contacts.Count; ++i)
{
if (e.contacts.Data[i].distance < 0.001f)
{
Component collider;
if (ObiCollider.idToCollider.TryGetValue(e.contacts.Data[i].other,out collider)){
if (collider == targetCollider)
solver.viscosities[e.contacts.Data[i].particle] = Mathf.Max(0,solver.viscosities[e.contacts.Data[i].particle] - 0.1f * Time.fixedDeltaTime);
}
}
}
}
}*/

View File

@@ -3,7 +3,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
namespace Obi
namespace Obi.Samples
{
/**
* Sample script that colors fluid particles based on their vorticity (2D only)
@@ -22,7 +22,7 @@ namespace Obi
if (!isActiveAndEnabled || actor.solver == null)
return;
for (int i = 0; i < actor.solverIndices.Length; ++i)
for (int i = 0; i < actor.solverIndices.count; ++i)
{
int k = actor.solverIndices[i];

View File

@@ -3,7 +3,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
namespace Obi
namespace Obi.Samples
{
/**
* Sample script that colors fluid particles based on their vorticity (2D only)
@@ -25,7 +25,7 @@ namespace Obi
if (!isActiveAndEnabled || actor.solver == null)
return;
for (int i = 0; i < actor.solverIndices.Length; ++i){
for (int i = 0; i < actor.solverIndices.count; ++i){
int k = actor.solverIndices[i];

View File

@@ -1,9 +1,6 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Obi
namespace Obi.Samples
{
[RequireComponent(typeof(ObiActor))]
public class ColorRandomizer : MonoBehaviour
@@ -14,13 +11,16 @@ namespace Obi
void Start()
{
actor = GetComponent<ObiActor>();
actor.OnBlueprintLoaded += Actor_OnBlueprintLoaded;
}
for (int i = 0; i < actor.solverIndices.Length; ++i)
private void Actor_OnBlueprintLoaded(ObiActor a, ObiActorBlueprint blueprint)
{
for (int i = 0; i < actor.solverIndices.count; ++i)
{
actor.solver.colors[actor.solverIndices[i]] = gradient.Evaluate(UnityEngine.Random.value);
}
}
}
}
}
}
}

View File

@@ -1,39 +1,42 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;
[ExecuteInEditMode]
[RequireComponent(typeof(ObiActor))]
public class DebugParticleFrames : MonoBehaviour {
namespace Obi.Samples
{
[ExecuteInEditMode]
[RequireComponent(typeof(ObiActor))]
public class DebugParticleFrames : MonoBehaviour
{
ObiActor actor;
public float size = 1;
public void Awake()
{
actor = GetComponent<ObiActor>();
}
// Update is called once per frame
void OnDrawGizmos ()
{
Vector4 b1 = new Vector4(1, 0, 0, 0);
Vector4 b2 = new Vector4(0, 1, 0, 0);
Vector4 b3 = new Vector4(0, 0, 1, 0);
for (int i = 0; i < actor.activeParticleCount; ++i)
ObiActor actor;
public float size = 1;
public void Awake()
{
actor = GetComponent<ObiActor>();
}
Vector3 position = actor.GetParticlePosition(actor.solverIndices[i]);
Quaternion quat = actor.GetParticleOrientation(actor.solverIndices[i]);
Gizmos.color = Color.red;
Gizmos.DrawRay(position, quat * b1 * size);
Gizmos.color = Color.green;
Gizmos.DrawRay(position, quat * b2 * size);
Gizmos.color = Color.blue;
Gizmos.DrawRay(position, quat * b3 * size);
}
}
// Update is called once per frame
void OnDrawGizmos()
{
Vector4 b1 = new Vector4(1, 0, 0, 0);
Vector4 b2 = new Vector4(0, 1, 0, 0);
Vector4 b3 = new Vector4(0, 0, 1, 0);
for (int i = 0; i < actor.activeParticleCount; ++i)
{
Vector3 position = actor.GetParticlePosition(actor.solverIndices[i]);
Quaternion quat = actor.GetParticleOrientation(actor.solverIndices[i]);
Gizmos.color = Color.red;
Gizmos.DrawRay(position, quat * b1 * size);
Gizmos.color = Color.green;
Gizmos.DrawRay(position, quat * b2 * size);
Gizmos.color = Color.blue;
Gizmos.DrawRay(position, quat * b3 * size);
}
}
}
}

View File

@@ -1,76 +1,78 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;
public class ExtrapolationCamera : MonoBehaviour
namespace Obi.Samples
{
public Transform target = null;
public float extrapolation = 10;
[Range(0, 1)]
public float smoothness = 0.8f;
[Range(0, 1)]
public float linearSpeed = 1;
[Range(0, 1)]
public float rotationalSpeed = 1;
[Min(0)]
public float distanceFromTarget = 4;
Vector3 lastPosition;
Vector3 extrapolatedPos;
void Start()
public class ExtrapolationCamera : MonoBehaviour
{
if (target != null)
lastPosition = target.position;
}
public Transform target = null;
private void FixedUpdate()
{
if (target != null)
public float extrapolation = 10;
[Range(0, 1)]
public float smoothness = 0.8f;
[Range(0, 1)]
public float linearSpeed = 1;
[Range(0, 1)]
public float rotationalSpeed = 1;
[Min(0)]
public float distanceFromTarget = 4;
Vector3 lastPosition;
Vector3 extrapolatedPos;
void Start()
{
// Get position delta since the last physics update:
Vector3 positionDelta = target.position - lastPosition;
positionDelta.y = 0;
// extrapolate position using velocity (the division/multiplication by Time.deltaTime simplify out)
extrapolatedPos = Vector3.Lerp(target.position + positionDelta * extrapolation, extrapolatedPos, smoothness);
// store the target's current com for the next frame:
lastPosition = target.position;
if (target != null)
lastPosition = target.position;
}
}
void LateUpdate()
{
if (target != null)
private void FixedUpdate()
{
// get vector from the camera to the extrapolated position:
Vector3 toTarget = extrapolatedPos - transform.position;
if (target != null)
{
// Get position delta since the last physics update:
Vector3 positionDelta = target.position - lastPosition;
positionDelta.y = 0;
// rotate the camera towards the extrapolated position:
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(toTarget), rotationalSpeed);
// keep our current world space height:
toTarget.y = 0;
// move the camera towards the extrapolated position, keeping some distance to it:
transform.position += toTarget.normalized * (toTarget.magnitude - distanceFromTarget) * linearSpeed;
// extrapolate position using velocity (the division/multiplication by Time.deltaTime simplify out)
extrapolatedPos = Vector3.Lerp(target.position + positionDelta * extrapolation, extrapolatedPos, smoothness);
// store the target's current com for the next frame:
lastPosition = target.position;
}
}
}
public void Teleport(Vector3 position, Quaternion rotation)
{
transform.position = position;
transform.rotation = rotation;
void LateUpdate()
{
if (target != null)
{
// get vector from the camera to the extrapolated position:
Vector3 toTarget = extrapolatedPos - transform.position;
if (target != null)
extrapolatedPos = lastPosition = target.position;
// rotate the camera towards the extrapolated position:
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(toTarget), rotationalSpeed);
// keep our current world space height:
toTarget.y = 0;
// move the camera towards the extrapolated position, keeping some distance to it:
transform.position += toTarget.normalized * (toTarget.magnitude - distanceFromTarget) * linearSpeed;
}
}
public void Teleport(Vector3 position, Quaternion rotation)
{
transform.position = position;
transform.rotation = rotation;
if (target != null)
extrapolatedPos = lastPosition = target.position;
}
}
}

View File

@@ -2,56 +2,59 @@
using UnityEngine.UI;
using System.Collections;
[RequireComponent(typeof(Text))]
public class FPSDisplay : MonoBehaviour
namespace Obi.Samples
{
public float updateInterval = 0.5f;
public bool showMedian = false;
public float medianLearnrate = 0.05f;
private float accum = 0; // FPS accumulated over the interval
private int frames = 0; // Frames drawn over the interval
private float timeleft; // Left time for current interval
private float currentFPS = 0;
private float median = 0;
private float average = 0;
public float CurrentFPS{
get { return currentFPS; }
}
public float FPSMedian
[RequireComponent(typeof(Text))]
public class FPSDisplay : MonoBehaviour
{
get { return median; }
}
public float updateInterval = 0.5f;
public float FPSAverage
{
get { return average; }
}
public bool showMedian = false;
public float medianLearnrate = 0.05f;
Text uguiText;
private float accum = 0; // FPS accumulated over the interval
private int frames = 0; // Frames drawn over the interval
private float timeleft; // Left time for current interval
private float currentFPS = 0;
void Start()
{
uguiText = GetComponent<Text>();
timeleft = updateInterval;
}
private float median = 0;
private float average = 0;
void Update()
{
// Timing inside the editor is not accurate. Only use in actual build.
public float CurrentFPS
{
get { return currentFPS; }
}
//#if !UNITY_EDITOR
public float FPSMedian
{
get { return median; }
}
public float FPSAverage
{
get { return average; }
}
Text uguiText;
void Start()
{
uguiText = GetComponent<Text>();
timeleft = updateInterval;
}
void Update()
{
// Timing inside the editor is not accurate. Only use in actual build.
//#if !UNITY_EDITOR
timeleft -= Time.deltaTime;
accum += Time.timeScale/Time.deltaTime;
accum += Time.timeScale / Time.deltaTime;
++frames;
// Interval ended - update GUI text and start new interval
if( timeleft <= 0.0)
if (timeleft <= 0.0)
{
currentFPS = accum / frames;
@@ -60,18 +63,19 @@ public class FPSDisplay : MonoBehaviour
// display two fractional digits (f2 format)
float fps = showMedian ? median : currentFPS;
uguiText.text = System.String.Format("{0:F2} FPS ({1:F1} ms)", fps, 1000.0f / fps);
uguiText.text = System.String.Format("{0:F2} FPS ({1:F1} ms)", fps, 1000.0f / fps);
timeleft = updateInterval;
accum = 0.0F;
frames = 0;
}
//#endif
}
//#endif
}
public void ResetMedianAndAverage()
{
median = 0;
average = 0;
public void ResetMedianAndAverage()
{
median = 0;
average = 0;
}
}
}

View File

@@ -1,6 +1,6 @@
using UnityEngine;
namespace Obi
namespace Obi.Samples
{
[RequireComponent(typeof(Camera))]
public class LookAroundCamera : MonoBehaviour
@@ -64,7 +64,10 @@ namespace Obi
currentShot.position += delta * Time.deltaTime * movementSpeed;
if (Input.GetKey(KeyCode.Mouse0))
var system = UnityEngine.EventSystems.EventSystem.current;
bool focusUI = system != null && system.IsPointerOverGameObject();
if (Input.GetKey(KeyCode.Mouse0) && !focusUI)
{
float deltaX = Input.GetAxis("Mouse X") * rotationSpeed;
float deltaY = Input.GetAxis("Mouse Y") * rotationSpeed;

View File

@@ -1,7 +1,7 @@
using System;
using UnityEngine;
namespace Obi
namespace Obi.Samples
{
public class MoveAndRotate : MonoBehaviour
{

View File

@@ -1,15 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;
public class ObiActorTeleport : MonoBehaviour
namespace Obi.Samples
{
public ObiActor actor;
public Transform target;
public void Teleport()
public class ObiActorTeleport : MonoBehaviour
{
actor.Teleport(target.position, target.rotation);
public ObiActor actor;
public Transform target;
public void Teleport()
{
actor.Teleport(target.position, target.rotation);
}
}
}

View File

@@ -1,53 +1,58 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;
[RequireComponent(typeof(ObiSolver))]
public class ObiParticleCounter : MonoBehaviour {
ObiSolver solver;
public int counter = 0;
public Collider2D targetCollider = null;
Obi.ObiSolver.ObiCollisionEventArgs frame;
HashSet<int> particles = new HashSet<int>();
void Awake(){
solver = GetComponent<Obi.ObiSolver>();
}
void OnEnable () {
solver.OnCollision += Solver_OnCollision;
}
void OnDisable(){
solver.OnCollision -= Solver_OnCollision;
}
void Solver_OnCollision (object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
namespace Obi.Samples
{
[RequireComponent(typeof(ObiSolver))]
public class ObiParticleCounter : MonoBehaviour
{
HashSet<int> currentParticles = new HashSet<int>();
for(int i = 0; i < e.contacts.Count; ++i)
ObiSolver solver;
public int counter = 0;
public Collider2D targetCollider = null;
ObiNativeContactList frame;
HashSet<int> particles = new HashSet<int>();
void Awake()
{
if (e.contacts.Data[i].distance < 0.001f)
{
/*Component collider;
if (ObiCollider2D.idToCollider.TryGetValue(e.contacts.Data[i].other,out collider)){
if (collider == targetCollider)
currentParticles.Add(e.contacts.Data[i].particle);
}*/
}
solver = GetComponent<Obi.ObiSolver>();
}
void OnEnable()
{
solver.OnCollision += Solver_OnCollision;
}
void OnDisable()
{
solver.OnCollision -= Solver_OnCollision;
}
void Solver_OnCollision(object sender, ObiNativeContactList e)
{
HashSet<int> currentParticles = new HashSet<int>();
for (int i = 0; i < e.count; ++i)
{
if (e[i].distance < 0.001f)
{
/*Component collider;
if (ObiCollider2D.idToCollider.TryGetValue(e.contacts.Data[i].other,out collider)){
if (collider == targetCollider)
currentParticles.Add(e.contacts.Data[i].particle);
}*/
}
}
particles.ExceptWith(currentParticles);
counter += particles.Count;
particles = currentParticles; Debug.Log(counter);
}
particles.ExceptWith(currentParticles);
counter += particles.Count;
particles = currentParticles;Debug.Log(counter);
}
}

View File

@@ -1,23 +1,25 @@
using UnityEngine;
using System.Collections;
public class ObjectDragger : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
transform.position = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
}
namespace Obi.Samples
{
public class ObjectDragger : MonoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
transform.position = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
}
}
}

View File

@@ -1,22 +1,25 @@
using UnityEngine;
using System.Collections;
public class ObjectLimit : MonoBehaviour
{
public float minX = 0;
public float maxX = 1;
public float minY = 0;
public float maxY = 1;
public float minZ = 0;
public float maxZ = 1;
void Update()
{
transform.localPosition = new Vector3(Mathf.Clamp(gameObject.transform.localPosition.x,minX,maxX),
Mathf.Clamp(gameObject.transform.localPosition.y,minY,maxY),
Mathf.Clamp(gameObject.transform.localPosition.z,minZ,maxZ));
}
}
namespace Obi.Samples
{
public class ObjectLimit : MonoBehaviour
{
public float minX = 0;
public float maxX = 1;
public float minY = 0;
public float maxY = 1;
public float minZ = 0;
public float maxZ = 1;
void Update()
{
transform.localPosition = new Vector3(Mathf.Clamp(gameObject.transform.localPosition.x, minX, maxX),
Mathf.Clamp(gameObject.transform.localPosition.y, minY, maxY),
Mathf.Clamp(gameObject.transform.localPosition.z, minZ, maxZ));
}
}
}

View File

@@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Obi.Samples
{
[RequireComponent(typeof(Rigidbody))]
public class RigidbodyMaxAngularVel : MonoBehaviour
{
public float maxAngularVelocity = 20;
// Start is called before the first frame update
void Start()
{
GetComponent<Rigidbody>().maxAngularVelocity = maxAngularVelocity;
}
// Update is called once per frame
void Update()
{
}
}
}

View File

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

View File

@@ -2,9 +2,14 @@
using System.Collections.Generic;
using UnityEngine;
public class SlowmoToggler : MonoBehaviour {
namespace Obi.Samples
{
public class SlowmoToggler : MonoBehaviour
{
public void Slowmo (bool slowmo) {
Time.timeScale = slowmo?0.25f:1;
}
public void Slowmo(bool slowmo)
{
Time.timeScale = slowmo ? 0.25f : 1;
}
}
}

View File

@@ -1,21 +1,24 @@
using UnityEngine;
using Obi;
[RequireComponent(typeof(ObiSolver))]
public class WorldSpaceGravity : MonoBehaviour
namespace Obi.Samples
{
ObiSolver solver;
public Vector3 worldGravity = new Vector3(0,-9.81f,0);
void Awake()
[RequireComponent(typeof(ObiSolver))]
public class WorldSpaceGravity : MonoBehaviour
{
solver = GetComponent<ObiSolver>();
}
void Update()
{
solver.parameters.gravity = transform.InverseTransformDirection(worldGravity);
solver.PushSolverParameters();
ObiSolver solver;
public Vector3 worldGravity = new Vector3(0, -9.81f, 0);
void Awake()
{
solver = GetComponent<ObiSolver>();
}
void Update()
{
solver.parameters.gravity = transform.InverseTransformDirection(worldGravity);
solver.PushSolverParameters();
}
}
}