升级obi
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace Obi
|
||||
{
|
||||
public class ObiAmbientForceZone : ObiExternalForce
|
||||
{
|
||||
|
||||
public override void ApplyForcesToActor(ObiActor actor)
|
||||
{
|
||||
|
||||
Matrix4x4 l2sTransform = actor.solver.transform.worldToLocalMatrix * transform.localToWorldMatrix;
|
||||
|
||||
Vector4 force = l2sTransform.MultiplyVector(Vector3.forward * (intensity + GetTurbulence(turbulence)));
|
||||
|
||||
if (actor.usesCustomExternalForces)
|
||||
{
|
||||
for (int i = 0; i < actor.activeParticleCount; ++i)
|
||||
actor.solver.wind[actor.solverIndices[i]] += force;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < actor.activeParticleCount; ++i)
|
||||
actor.solver.externalForces[actor.solverIndices[i]] += force;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDrawGizmosSelected()
|
||||
{
|
||||
|
||||
Gizmos.matrix = transform.localToWorldMatrix;
|
||||
Gizmos.color = new Color(0,0.7f,1,1);
|
||||
|
||||
// arrow body:
|
||||
ObiUtils.DrawArrowGizmo(0.5f + GetTurbulence(1),0.2f,0.3f,0.2f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f33b658b41e945e0bae1d3f0af0830e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 092f06332c018434b8c8ea86164ef4fd, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,50 +0,0 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace Obi
|
||||
{
|
||||
public abstract class ObiExternalForce : MonoBehaviour
|
||||
{
|
||||
|
||||
public float intensity = 0;
|
||||
public float turbulence = 0;
|
||||
public float turbulenceFrequency = 1;
|
||||
public float turbulenceSeed = 0;
|
||||
public ObiSolver[] affectedSolvers;
|
||||
|
||||
public void OnEnable()
|
||||
{
|
||||
foreach (ObiSolver solver in affectedSolvers)
|
||||
{
|
||||
if (solver != null)
|
||||
solver.OnBeginStep += Solver_OnStepBegin;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDisable()
|
||||
{
|
||||
foreach (ObiSolver solver in affectedSolvers)
|
||||
{
|
||||
if (solver != null)
|
||||
solver.OnBeginStep -= Solver_OnStepBegin;
|
||||
}
|
||||
}
|
||||
|
||||
void Solver_OnStepBegin(ObiSolver solver, float stepTime)
|
||||
{
|
||||
foreach (ObiActor actor in solver.actors)
|
||||
{
|
||||
if (actor != null)
|
||||
ApplyForcesToActor(actor);
|
||||
}
|
||||
}
|
||||
|
||||
protected float GetTurbulence(float turbulenceIntensity){
|
||||
return Mathf.PerlinNoise(Time.fixedTime * turbulenceFrequency,turbulenceSeed) * turbulenceIntensity;
|
||||
}
|
||||
|
||||
public abstract void ApplyForcesToActor(ObiActor actor);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4978a525b6164476d96f5d28d8b309f8
|
||||
timeCreated: 1480349422
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
137
Assets/Obi/Scripts/Common/Utils/Forces/ObiForceZone.cs
Normal file
137
Assets/Obi/Scripts/Common/Utils/Forces/ObiForceZone.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Obi
|
||||
{
|
||||
[ExecuteInEditMode]
|
||||
[RequireComponent(typeof(ObiCollider))]
|
||||
public class ObiForceZone : MonoBehaviour
|
||||
{
|
||||
[SerializeProperty("sourceCollider")]
|
||||
[SerializeField] private ObiCollider m_SourceCollider;
|
||||
|
||||
protected ObiForceZoneHandle forcezoneHandle;
|
||||
|
||||
/// <summary>
|
||||
/// The ObiCollider this ObiForceZone should affect.
|
||||
/// </summary>
|
||||
/// This is automatically set when you first create the ObiForceZone component, but you can override it afterwards.
|
||||
public ObiCollider SourceCollider
|
||||
{
|
||||
set
|
||||
{
|
||||
if (value != null && value.gameObject != this.gameObject)
|
||||
{
|
||||
Debug.LogError("The ObiCollider component must reside in the same GameObject as ObiForceZone.");
|
||||
return;
|
||||
}
|
||||
|
||||
RemoveCollider();
|
||||
m_SourceCollider = value;
|
||||
AddCollider();
|
||||
|
||||
}
|
||||
get { return m_SourceCollider; }
|
||||
}
|
||||
|
||||
public ObiForceZoneHandle Handle
|
||||
{
|
||||
get
|
||||
{
|
||||
// don't check forcezoneHandle.isValid:
|
||||
// CreateForceZone 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 CreateForceZone again and create a second handle to the same zone.
|
||||
if (forcezoneHandle == null)
|
||||
{
|
||||
var world = ObiColliderWorld.GetInstance();
|
||||
|
||||
// create the material:
|
||||
forcezoneHandle = world.CreateForceZone();
|
||||
forcezoneHandle.owner = this;
|
||||
}
|
||||
return forcezoneHandle;
|
||||
}
|
||||
}
|
||||
|
||||
public ForceZone.ZoneType type;
|
||||
public ForceZone.ForceMode mode;
|
||||
public float intensity;
|
||||
|
||||
[Header("Damping")]
|
||||
public ForceZone.DampingDirection dampingDir;
|
||||
public float damping = 0;
|
||||
|
||||
[Header("Falloff")]
|
||||
public float minDistance;
|
||||
public float maxDistance;
|
||||
[Min(0)]
|
||||
public float falloffPower = 1;
|
||||
|
||||
[Header("Tint")]
|
||||
public Color color = Color.clear;
|
||||
|
||||
[Header("Pulse")]
|
||||
public float pulseIntensity;
|
||||
public float pulseFrequency;
|
||||
public float pulseSeed;
|
||||
|
||||
protected float intensityVariation;
|
||||
|
||||
public void OnEnable()
|
||||
{
|
||||
forcezoneHandle = ObiColliderWorld.GetInstance().CreateForceZone();
|
||||
forcezoneHandle.owner = this;
|
||||
FindSourceCollider();
|
||||
}
|
||||
|
||||
public void OnDisable()
|
||||
{
|
||||
RemoveCollider();
|
||||
ObiColliderWorld.GetInstance().DestroyForceZone(forcezoneHandle);
|
||||
}
|
||||
|
||||
private void FindSourceCollider()
|
||||
{
|
||||
if (SourceCollider == null)
|
||||
SourceCollider = GetComponent<ObiCollider>();
|
||||
else
|
||||
AddCollider();
|
||||
}
|
||||
|
||||
private void AddCollider()
|
||||
{
|
||||
if (m_SourceCollider != null)
|
||||
m_SourceCollider.ForceZone = this;
|
||||
}
|
||||
|
||||
private void RemoveCollider()
|
||||
{
|
||||
if (m_SourceCollider != null)
|
||||
m_SourceCollider.ForceZone = null;
|
||||
}
|
||||
|
||||
public virtual void UpdateIfNeeded()
|
||||
{
|
||||
if (!Handle.isValid)
|
||||
return;
|
||||
|
||||
var fc = ObiColliderWorld.GetInstance().forceZones[Handle.index];
|
||||
fc.type = type;
|
||||
fc.mode = mode;
|
||||
fc.intensity = intensity + intensityVariation;
|
||||
fc.minDistance = minDistance;
|
||||
fc.maxDistance = maxDistance;
|
||||
fc.falloffPower = falloffPower;
|
||||
fc.damping = damping;
|
||||
fc.dampingDir = dampingDir;
|
||||
fc.color = color;
|
||||
ObiColliderWorld.GetInstance().forceZones[Handle.index] = fc;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (Application.isPlaying)
|
||||
intensityVariation = Mathf.PerlinNoise(Time.time * pulseFrequency, pulseSeed) * pulseIntensity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f05961933f21f46a683fc5d5beec4061
|
||||
guid: 4978a525b6164476d96f5d28d8b309f8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -1,63 +0,0 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace Obi
|
||||
{
|
||||
public class ObiSphericalForceZone : ObiExternalForce
|
||||
{
|
||||
|
||||
public float radius = 5;
|
||||
public bool radial = true;
|
||||
|
||||
public override void ApplyForcesToActor(ObiActor actor)
|
||||
{
|
||||
|
||||
float sqrRadius = radius * radius;
|
||||
float finalIntensity = intensity + GetTurbulence(turbulence);
|
||||
|
||||
Matrix4x4 l2sTransform = actor.solver.transform.worldToLocalMatrix * transform.localToWorldMatrix;
|
||||
|
||||
Vector4 center = l2sTransform.MultiplyPoint3x4(Vector4.zero);
|
||||
Vector4 forward = l2sTransform.MultiplyVector(Vector3.forward);
|
||||
|
||||
// Calculate force intensity for each actor particle:
|
||||
for (int i = 0; i < actor.activeParticleCount; ++i){
|
||||
|
||||
Vector4 distanceVector = actor.solver.positions[actor.solverIndices[i]] - center;
|
||||
|
||||
float sqrMag = distanceVector.sqrMagnitude;
|
||||
float falloff = Mathf.Clamp01((sqrRadius - sqrMag) / sqrRadius);
|
||||
|
||||
Vector4 force;
|
||||
if (radial)
|
||||
force = distanceVector/(Mathf.Sqrt(sqrMag) + float.Epsilon) * falloff * finalIntensity;
|
||||
else
|
||||
force = forward * falloff * finalIntensity;
|
||||
|
||||
if (actor.usesCustomExternalForces)
|
||||
actor.solver.wind[actor.solverIndices[i]] += force;
|
||||
else
|
||||
actor.solver.externalForces[actor.solverIndices[i]] += force;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.matrix = transform.localToWorldMatrix;
|
||||
Gizmos.color = new Color(0,0.7f,1,1);
|
||||
Gizmos.DrawWireSphere(Vector3.zero,radius);
|
||||
|
||||
float turb = GetTurbulence(1);
|
||||
|
||||
if (!radial){
|
||||
ObiUtils.DrawArrowGizmo(radius + turb,radius*0.2f,radius*0.3f,radius*0.2f);
|
||||
}else{
|
||||
Gizmos.DrawLine(new Vector3(0,0,-radius*0.5f)*turb,new Vector3(0,0,radius*0.5f)*turb);
|
||||
Gizmos.DrawLine(new Vector3(0,-radius*0.5f,0)*turb,new Vector3(0,radius*0.5f,0)*turb);
|
||||
Gizmos.DrawLine(new Vector3(-radius*0.5f,0,0)*turb,new Vector3(radius*0.5f,0,0)*turb);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user