添加插件

This commit is contained in:
2025-11-10 00:08:26 +08:00
parent 4059c207c0
commit 76f80db694
2814 changed files with 436400 additions and 178 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

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

View File

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

View File

@@ -0,0 +1,33 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Obi.Samples
{
public class ActorSpawner : MonoBehaviour
{
public ObiActor template;
public int maxInstances = 32;
public float spawnDelay = 0.3f;
private int instances = 0;
private float timeFromLastSpawn = 0;
// Update is called once per frame
void Update()
{
timeFromLastSpawn += Time.deltaTime;
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

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

View File

@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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);
}
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,229 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Obi{
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Animator))]
public class ObiCharacter : MonoBehaviour {
[SerializeField] float m_MovingTurnSpeed = 360;
[SerializeField] float m_StationaryTurnSpeed = 180;
[SerializeField] float m_JumpPower = 12f;
[Range(1f, 4f)][SerializeField] float m_GravityMultiplier = 2f;
[SerializeField] float m_RunCycleLegOffset = 0.2f; //specific to the character in sample assets, will need to be modified to work with others
[SerializeField] float m_MoveSpeedMultiplier = 1f;
[SerializeField] float m_AnimSpeedMultiplier = 1f;
[SerializeField] float m_GroundCheckDistance = 0.1f;
Rigidbody m_Rigidbody;
Animator m_Animator;
bool m_IsGrounded;
float m_OrigGroundCheckDistance;
const float k_Half = 0.5f;
float m_TurnAmount;
float m_ForwardAmount;
Vector3 m_GroundNormal;
float m_CapsuleHeight;
Vector3 m_CapsuleCenter;
CapsuleCollider m_Capsule;
bool m_Crouching;
void Start()
{
Physics.IgnoreLayerCollision(LayerMask.NameToLayer("Default"),LayerMask.NameToLayer("Ignore Raycast"),true);
m_Animator = GetComponent<Animator>();
m_Rigidbody = GetComponent<Rigidbody>();
m_Capsule = GetComponent<CapsuleCollider>();
m_CapsuleHeight = m_Capsule.height;
m_CapsuleCenter = m_Capsule.center;
m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
m_OrigGroundCheckDistance = m_GroundCheckDistance;
}
public void Move(Vector3 move, bool crouch, bool jump)
{
// convert the world relative moveInput vector into a local-relative
// turn amount and forward amount required to head in the desired
// direction.
if (move.magnitude > 1f) move.Normalize();
move = transform.InverseTransformDirection(move);
CheckGroundStatus();
move = Vector3.ProjectOnPlane(move, m_GroundNormal);
m_TurnAmount = Mathf.Atan2(move.x, move.z);
m_ForwardAmount = move.z;
ApplyExtraTurnRotation();
// control and velocity handling is different when grounded and airborne:
if (m_IsGrounded)
{
HandleGroundedMovement(crouch, jump);
}
else
{
HandleAirborneMovement();
}
ScaleCapsuleForCrouching(crouch);
PreventStandingInLowHeadroom();
// send input and other state parameters to the animator
UpdateAnimator(move);
}
void ScaleCapsuleForCrouching(bool crouch)
{
if (m_IsGrounded && crouch)
{
if (m_Crouching) return;
m_Capsule.height = m_Capsule.height / 2f;
m_Capsule.center = m_Capsule.center / 2f;
m_Crouching = true;
}
else
{
Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, ~Physics.IgnoreRaycastLayer, QueryTriggerInteraction.Ignore))
{
m_Crouching = true;
return;
}
m_Capsule.height = m_CapsuleHeight;
m_Capsule.center = m_CapsuleCenter;
m_Crouching = false;
}
}
void PreventStandingInLowHeadroom()
{
// prevent standing up in crouch-only zones
if (!m_Crouching)
{
Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, ~Physics.IgnoreRaycastLayer, QueryTriggerInteraction.Ignore))
{
m_Crouching = true;
}
}
}
void UpdateAnimator(Vector3 move)
{
// update the animator parameters
m_Animator.SetFloat("Forward", m_ForwardAmount, 0.1f, Time.deltaTime);
m_Animator.SetFloat("Turn", m_TurnAmount, 0.1f, Time.deltaTime);
m_Animator.SetBool("Crouch", m_Crouching);
m_Animator.SetBool("OnGround", m_IsGrounded);
if (!m_IsGrounded)
{
m_Animator.SetFloat("Jump", m_Rigidbody.linearVelocity.y);
}
// calculate which leg is behind, so as to leave that leg trailing in the jump animation
// (This code is reliant on the specific run cycle offset in our animations,
// and assumes one leg passes the other at the normalized clip times of 0.0 and 0.5)
float runCycle =
Mathf.Repeat(
m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime + m_RunCycleLegOffset, 1);
float jumpLeg = (runCycle < k_Half ? 1 : -1) * m_ForwardAmount;
if (m_IsGrounded)
{
m_Animator.SetFloat("JumpLeg", jumpLeg);
}
// the anim speed multiplier allows the overall speed of walking/running to be tweaked in the inspector,
// which affects the movement speed because of the root motion.
if (m_IsGrounded && move.magnitude > 0)
{
m_Animator.speed = m_AnimSpeedMultiplier;
}
else
{
// don't use that while airborne
m_Animator.speed = 1;
}
}
void HandleAirborneMovement()
{
// apply extra gravity from multiplier:
Vector3 extraGravityForce = (Physics.gravity * m_GravityMultiplier) - Physics.gravity;
m_Rigidbody.AddForce(extraGravityForce);
m_GroundCheckDistance = m_Rigidbody.linearVelocity.y < 0 ? m_OrigGroundCheckDistance : 0.01f;
}
void HandleGroundedMovement(bool crouch, bool jump)
{
// check whether conditions are right to allow a jump:
if (jump && !crouch && m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Grounded"))
{
// jump!
m_Rigidbody.linearVelocity = new Vector3(m_Rigidbody.linearVelocity.x, m_JumpPower, m_Rigidbody.linearVelocity.z);
m_IsGrounded = false;
m_Animator.applyRootMotion = false;
m_GroundCheckDistance = 0.1f;
}
}
void ApplyExtraTurnRotation()
{
// help the character turn faster (this is in addition to root rotation in the animation)
float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, m_ForwardAmount);
transform.Rotate(0, m_TurnAmount * turnSpeed * Time.deltaTime, 0);
}
public void OnAnimatorMove()
{
// we implement this function to override the default root motion.
// this allows us to modify the positional speed before it's applied.
if (m_IsGrounded)
{
Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;
// we preserve the existing y part of the current velocity.
v.y = m_Rigidbody.linearVelocity.y;
m_Rigidbody.linearVelocity = v;
}
}
void CheckGroundStatus()
{
RaycastHit hitInfo;
#if UNITY_EDITOR
// helper to visualise the ground check ray in the scene view
Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance));
#endif
// 0.1f is a small offset to start the ray from inside the character
// it is also good to note that the transform position in the sample assets is at the base of the character
if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, m_GroundCheckDistance,~Physics.IgnoreRaycastLayer))
{
m_GroundNormal = hitInfo.normal;
m_IsGrounded = true;
m_Animator.applyRootMotion = true;
}
else
{
m_IsGrounded = false;
m_GroundNormal = Vector3.up;
m_Animator.applyRootMotion = false;
}
}
}
}

View File

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

View File

@@ -0,0 +1,72 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Obi{
[RequireComponent(typeof (ObiCharacter))]
public class SampleCharacterController : MonoBehaviour {
private ObiCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
private Transform m_Cam; // A reference to the main camera in the scenes transform
private Vector3 m_CamForward; // The current forward direction of the camera
private Vector3 m_Move;
private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input.
private void Start()
{
// get the transform of the main camera
if (Camera.main != null)
{
m_Cam = Camera.main.transform;
}
else
{
Debug.LogWarning(
"Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.");
// we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
}
// get the third person character ( this should never be null due to require component )
m_Character = GetComponent<ObiCharacter>();
}
private void Update()
{
if (!m_Jump)
{
m_Jump = Input.GetButtonDown("Jump");
}
// read inputs
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
bool crouch = Input.GetKey(KeyCode.C);
// calculate move direction to pass to character
if (m_Cam != null)
{
// calculate camera relative direction to move:
m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
m_Move = v*m_CamForward + h*m_Cam.right;
}
else
{
// we use world-relative directions in the case of no main camera
m_Move = v*Vector3.forward + h*Vector3.right;
}
#if !MOBILE_INPUT
// walk speed multiplier
if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f;
#endif
// pass all parameters to the character control script
m_Character.Move(m_Move, crouch, m_Jump);
m_Jump = false;
}
}
}

View File

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

View File

@@ -0,0 +1,51 @@
using UnityEngine;
namespace Obi.Samples
{
[RequireComponent(typeof(ObiSolver))]
public class ColliderHighlighter : MonoBehaviour
{
ObiSolver solver;
void Awake()
{
solver = GetComponent<Obi.ObiSolver>();
}
void OnEnable()
{
solver.OnCollision += Solver_OnCollision;
}
void OnDisable()
{
solver.OnCollision -= Solver_OnCollision;
}
void Solver_OnCollision(object sender, ObiNativeContactList e)
{
var colliderWorld = ObiColliderWorld.GetInstance();
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)
{
// 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

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

View File

@@ -0,0 +1,97 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Obi.Samples
{
[RequireComponent(typeof(ObiSolver))]
public class CollisionEventHandler : MonoBehaviour
{
ObiSolver solver;
public int contactCount;
ObiNativeContactList frame;
void Awake()
{
solver = GetComponent<Obi.ObiSolver>();
}
void OnEnable()
{
solver.OnCollision += Solver_OnCollision;
}
void OnDisable()
{
solver.OnCollision -= Solver_OnCollision;
}
void Solver_OnCollision(object sender, ObiNativeContactList e)
{
frame = e;
}
void OnDrawGizmos()
{
if (solver == null || frame == null) return;
Gizmos.matrix = solver.transform.localToWorldMatrix;
contactCount = frame.count;
for (int i = 0; i < frame.count; ++i)
{
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);
}
/*for (int i = 0; i < frame.count; ++i)
{
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 = 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);
}*/
}
}
}

View File

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

View File

@@ -0,0 +1,38 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Obi.Samples
{
/**
* Sample script that colors fluid particles based on their vorticity (2D only)
*/
[RequireComponent(typeof(ObiActor))]
public class ColorFromPhase : MonoBehaviour
{
ObiActor actor;
void Awake(){
actor = GetComponent<ObiActor>();
}
void LateUpdate()
{
if (!isActiveAndEnabled || actor.solver == null)
return;
for (int i = 0; i < actor.solverIndices.count; ++i)
{
int k = actor.solverIndices[i];
int phase = ObiUtils.GetGroupFromPhase(actor.solver.phases[k]);
actor.solver.colors[k] = ObiUtils.colorAlphabet[phase % ObiUtils.colorAlphabet.Length];
}
}
}
}

View File

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

View File

@@ -0,0 +1,43 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Obi.Samples
{
/**
* Sample script that colors fluid particles based on their vorticity (2D only)
*/
[RequireComponent(typeof(ObiActor))]
public class ColorFromVelocity : MonoBehaviour
{
ObiActor actor;
public float sensibility = 0.2f;
void Awake(){
actor = GetComponent<ObiActor>();
}
public void OnEnable(){}
void LateUpdate()
{
if (!isActiveAndEnabled || actor.solver == null)
return;
for (int i = 0; i < actor.solverIndices.count; ++i){
int k = actor.solverIndices[i];
Vector4 vel = actor.solver.velocities[k];
actor.solver.colors[k] = new Color(Mathf.Clamp(vel.x / sensibility,-1,1) * 0.5f + 0.5f,
Mathf.Clamp(vel.y / sensibility,-1,1) * 0.5f + 0.5f,
Mathf.Clamp(vel.z / sensibility,-1,1) * 0.5f + 0.5f,1);
}
}
}
}

View File

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

View File

@@ -0,0 +1,26 @@
using UnityEngine;
namespace Obi.Samples
{
[RequireComponent(typeof(ObiActor))]
public class ColorRandomizer : MonoBehaviour
{
ObiActor actor;
public Gradient gradient = new Gradient();
void Start()
{
actor = GetComponent<ObiActor>();
actor.OnBlueprintLoaded += Actor_OnBlueprintLoaded;
}
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

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

View File

@@ -0,0 +1,42 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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)
{
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

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

View File

@@ -0,0 +1,78 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Obi.Samples
{
public class ExtrapolationCamera : MonoBehaviour
{
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()
{
if (target != null)
lastPosition = target.position;
}
private void FixedUpdate()
{
if (target != null)
{
// 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;
}
}
void LateUpdate()
{
if (target != null)
{
// get vector from the camera to the extrapolated position:
Vector3 toTarget = extrapolatedPos - transform.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

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

View File

@@ -0,0 +1,81 @@
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace Obi.Samples
{
[RequireComponent(typeof(Text))]
public class FPSDisplay : MonoBehaviour
{
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
{
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;
++frames;
// Interval ended - update GUI text and start new interval
if (timeleft <= 0.0)
{
currentFPS = accum / frames;
average += (Mathf.Abs(currentFPS) - average) * 0.1f;
median += Mathf.Sign(currentFPS - median) * Mathf.Min(average * medianLearnrate, Mathf.Abs(currentFPS - median));
// 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);
timeleft = updateInterval;
accum = 0.0F;
frames = 0;
}
//#endif
}
public void ResetMedianAndAverage()
{
median = 0;
average = 0;
}
}
}

View File

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

View File

@@ -0,0 +1,81 @@
using UnityEngine;
namespace Obi.Samples
{
[RequireComponent(typeof(Camera))]
public class LookAroundCamera : MonoBehaviour
{
public struct CameraShot
{
public Vector3 position;
public Quaternion rotation;
public Vector3 up;
public float fieldOfView;
public CameraShot(Vector3 position, Quaternion rotation, Vector3 up, float fieldOfView)
{
this.position = position;
this.rotation = rotation;
this.up = up;
this.fieldOfView = fieldOfView;
}
}
private Camera cam;
private CameraShot currentShot;
public float movementSpeed = 5;
public float rotationSpeed = 8;
public float translationResponse = 10;
public float rotationResponse = 10;
public float fovResponse = 10;
private void Awake()
{
cam = GetComponent<Camera>();
currentShot = new CameraShot(transform.position, transform.rotation, transform.up, cam.fieldOfView);
}
private void LookAt(Vector3 position, Vector3 up)
{
currentShot.up = up;
currentShot.rotation = Quaternion.LookRotation(position - currentShot.position, currentShot.up);
}
private void UpdateShot()
{
transform.position = Vector3.Lerp(transform.position, currentShot.position, translationResponse * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, currentShot.rotation, rotationResponse * Time.deltaTime);
cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, currentShot.fieldOfView, fovResponse * Time.deltaTime);
}
private void LateUpdate()
{
Vector3 delta = Vector3.zero;
if (Input.GetKey(KeyCode.W))
delta += cam.transform.forward;
if (Input.GetKey(KeyCode.A))
delta -= cam.transform.right;
if (Input.GetKey(KeyCode.S))
delta -= cam.transform.forward;
if (Input.GetKey(KeyCode.D))
delta += cam.transform.right;
currentShot.position += delta * Time.deltaTime * movementSpeed;
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;
Quaternion fwd = currentShot.rotation * Quaternion.AngleAxis(deltaX, Vector3.up) * Quaternion.AngleAxis(deltaY, -Vector3.right);
LookAt(currentShot.position + fwd * Vector3.forward, Vector3.up);
}
UpdateShot();
}
}
}

View File

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

View File

@@ -0,0 +1,39 @@
using System;
using UnityEngine;
namespace Obi.Samples
{
public class MoveAndRotate : MonoBehaviour
{
public Vector3andSpace moveUnitsPerSecond;
public Vector3andSpace rotateDegreesPerSecond;
public bool ignoreTimescale;
private float m_LastRealTime;
private void Start()
{
m_LastRealTime = Time.realtimeSinceStartup;
}
// Update is called once per frame
private void FixedUpdate()
{
float deltaTime = Time.fixedDeltaTime;
if (ignoreTimescale)
{
deltaTime = (Time.realtimeSinceStartup - m_LastRealTime);
m_LastRealTime = Time.realtimeSinceStartup;
}
transform.Translate(moveUnitsPerSecond.value*deltaTime, moveUnitsPerSecond.space);
transform.Rotate(rotateDegreesPerSecond.value*deltaTime, rotateDegreesPerSecond.space);
}
[Serializable]
public class Vector3andSpace
{
public Vector3 value;
public Space space = Space.Self;
}
}
}

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,58 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Obi.Samples
{
[RequireComponent(typeof(ObiSolver))]
public class ObiParticleCounter : MonoBehaviour
{
ObiSolver solver;
public int counter = 0;
public Collider2D targetCollider = null;
ObiNativeContactList 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, 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);
}
}
}

View File

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

View File

@@ -0,0 +1,25 @@
using UnityEngine;
using System.Collections;
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

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

View File

@@ -0,0 +1,25 @@
using UnityEngine;
using System.Collections;
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,12 @@
fileFormatVersion: 2
guid: 6da1d294c86704f3394415f542741ebe
timeCreated: 1490723525
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

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

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

View File

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

View File

@@ -0,0 +1,24 @@
using UnityEngine;
using Obi;
namespace Obi.Samples
{
[RequireComponent(typeof(ObiSolver))]
public class WorldSpaceGravity : MonoBehaviour
{
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();
}
}
}

View File

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