所谓互动草,就是角色跑动或者释放技能,能影响草的摆动方向和幅度.
前面的文章早已经实现了风吹草动的效果,迟迟没有在Unity上面做互动草,是因为以前我在端游项目做过一套太过于牛逼的方案.在CE3的互动草的基础上扩展,效果好,但技术太复杂,效率开销也特别高. 如果在手机上,就得做一套简单高效的.
实现效果:从任意方向碰一下草,草就应该来回晃动,晃动幅度逐渐减小.多次触碰,效果应该叠加.这样的话就比较真实.
实现原理:用正玄波实现草来回摆动的简谐运动,用指数衰减来模拟阻力
实现步骤:
1.每个草挂一个脚本,来处理力的效果叠加
[csharp] view plain copy
- public class Force
- {
- public float m_Time = 0;
- public Vector4 m_Force;
- public Force(Vector4 force)
- {
- m_Force = force;
- }
- }
- public class GrassForce : MonoBehaviour
- {
- public List<Force> m_ForceList = null;
- public float m_WaveFrequency = 6.0f;
- public float m_Resistance = 0.25f;
- public float m_MaxForceMagnitude = 6.0f;
- public float m_AddForceTimeInterval = 0.5f;
- public int m_MaxForceNum = 3;
- private float m_LastAddTime = 0;
- private Material material;
- void Start()
- {
- material = gameObject.renderer.material;
- }
- void Update()
- {
- UpdateForce();
- }
- void OnBecameVisible()
- {
- enabled = true;
- }
- void OnBecameInvisible()
- {
- enabled = false;
- }
- public void AddForce(Vector3 force)
- {
- if (Time.time - m_LastAddTime > m_AddForceTimeInterval)
- {
- m_LastAddTime = Time.time;
- if (m_ForceList == null)
- m_ForceList = new List<Force>();
- if (m_ForceList.Count < m_MaxForceNum)
- {
- Vector4 newForce = new Vector4(force.x, 0, force.z, 0);
- if (newForce.magnitude > m_MaxForceMagnitude)
- newForce = newForce.normalized * m_MaxForceMagnitude;
- m_ForceList.Add(new Force(newForce));
- }
- }
- }
- private void UpdateForce()
- {
- if (m_ForceList == null)
- return;
- Vector4 accForce = Vector4.zero;
- for (int i = m_ForceList.Count - 1; i >= 0; --i)
- {
- if (m_ForceList[i].m_Force.magnitude > 0.1f)
- {
- // [-1, 1] 正玄波模拟简谐运动
- float wave_factor = Mathf.Sin(m_ForceList[i].m_Time * m_WaveFrequency);
- // 力的指数衰减
- float resistance_factor = easeOutExpo(1, 0, m_Resistance * Time.deltaTime);
- m_ForceList[i].m_Force *= resistance_factor;
- m_ForceList[i].m_Time += Time.deltaTime;
- // 累加
- accForce += m_ForceList[i].m_Force * wave_factor;
- }
- else
- {
- m_ForceList.RemoveAt(i);
- }
- }
- if (accForce != Vector4.zero)
- {
- if (material.HasProperty("_Force"))
- {
- accForce = transform.InverseTransformVector(accForce); // 世界空间转换到模型本地空间
- material.SetVector("_Force", accForce);
- }
- }
- }
- public float easeOutExpo(float start, float end, float value)
- {
- end -= start;
- return end * (-Mathf.Pow(2, -10 * value) + 1) + start;
- }
- }
2.如何确定哪些草受到影响,以及受力的方向?
[csharp] view plain copy
- public static void AddForceToGrass(int forceId, Transform transform)
- {
- ForceTable force = ForceTableMgr.Instance.GetDataById(forceId);
- if (force != null)
- {
- Vector3 relativeCenter = new Vector3(force.RelativeCenterX, force.RelativeCenterY, force.RelativeCenterZ);
- Vector3 center = transform.TransformPoint(relativeCenter);
- //Vector3 size = new Vector3(force.Length, force.Height, force.Width);
- Vector3 size = new Vector3(force.Width, force.Height, force.Length);
- // 方向矩阵
- Matrix4x4 m44 = Matrix4x4.TRS(Vector3.zero, Quaternion.Inverse(transform.rotation), Vector3.one);
- PhysicsUtil.AddForceToGrass((RangeType)force.RangeType, (ForceDirType)force.DirType, force.Strength, center, size, transform.forward, m44, force.Degree);
- }
- }
- private static void AddForceToGrass(RangeType type, ForceDirType dirType, float strength, Vector3 center, Vector3 size, Vector3 direction, Matrix4x4 m44, float degree = 360.0f)
- {
- if (type == RangeType.Sphere)
- {
- AddForceInSector(dirType, strength, center, size.x, direction, degree);
- }
- else if (type == RangeType.Cude)
- {
- AddForceInCube(dirType, strength, center, size, direction, m44, degree);
- }
- }
草可以看成一个点,计算和下面范围的相交.
1.圆形和扇形范围
圆形范围计算特别简单,计算距离即可.扇形范围只需要在圆形基础上再计算一次夹角即可,部分核心代码:
[csharp] view plain copy
- Vector3 dir = script.transform.position - center;
- if (dir.sqrMagnitude <= radius * radius)
- {
- dir.y = 0;
- if (Mathf.Abs(Vector3.Angle(dir, direction)) <= degree)
- {
- float factor = 0.25f + Mathf.Clamp01((radius - dir.magnitude) / radius) * 0.75f; // 衰减因子
- Vector3 forceDir;
- if (dirType == ForceDirType.ToTarget)
- forceDir = dir.normalized;
- else
- forceDir = -dir.normalized;
- script.AddForce(forceDir * factor * strength);
- }
- }
2.矩形范围
点和任意方向的矩形的计算,这个比较难.Unity本身也没提供此类相交API.不过熟悉引擎开发的应该知道AABB和OBB吧.其实矩形范围计算,就是计算点和OBB的相交.
点和AABB的相交计算很简单,因为AABB每条边都是和坐标轴平行或者垂直的.而OBB有方向,其实只需要把点矩阵变换到OBB所在的空间,就可以用AABB的方法来计算了.
[csharp] view plain copy
- public struct AABB
- {
- public Vector3 min;
- public Vector3 max;
- public AABB(Vector3 vmin, Vector3 vmax)
- {
- min = vmin;
- max = vmax;
- }
- }
- public struct OBB
- {
- public Matrix4x4 m44;
- public Vector3 h; // half-length-vector
- public Vector3 c; // center of obb
- public OBB(Matrix4x4 mat44, Vector3 hlv, Vector3 center)
- {
- m44 = mat44;
- h = hlv;
- c = center;
- }
- public OBB(Matrix4x4 mat44, AABB aabb)
- {
- m44 = mat44;
- h = (aabb.max - aabb.min) * 0.5f;
- c = (aabb.max + aabb.min) * 0.5f;
- }
- }
- // 点和AABB的相交
- public static bool Overlap_Point_AABB(Vector3 p, AABB aabb)
- {
- return ((p.x >= aabb.min.x && p.x <= aabb.max.x) && (p.y >= aabb.min.y && p.y <= aabb.max.y) && (p.z >= aabb.min.z && p.z <= aabb.max.z));
- }
- // 点和OBB的相交
- public static bool Overlap_Point_OBB(Vector3 p, Vector3 obbWorldPos, OBB obb)
- {
- AABB aabb = new AABB(obb.c - obb.h, obb.c + obb.h);
- Vector3 local_p = p - obbWorldPos;
- Vector3 t = obb.m44.MultiplyVector(local_p);
- return Overlap_Point_AABB(t, aabb);
- }
记住,OBB参数设置,中心一定要在世界原点,这样才方便计算
[csharp] view plain copy
- // 包围盒中心为世界原点.便于计算.
- Vector3 min = - size * 0.5f;
- Vector3 max = size * 0.5f;
- AABB aabb = new AABB(min, max);
- OBB obb = new OBB(m44, aabb);
相交和计算力方向:
[csharp] view plain copy
- if (PhysicsUtil.Overlap_Point_OBB(script.transform.position, center, obb))
- {
- // 暂时只支持Left_Right
- if (dirType == ForceDirType.Left_Right)
- {
- Vector3 dir = script.transform.position - center;
- dir = m44.MultiplyVector(dir);
- dir = (dir.x < 0) ? m44.transpose.MultiplyVector(Vector3.left) : m44.transpose.MultiplyVector(Vector3.right);
- Vector3 force = dir.normalized * strength;
- script.AddForce(force);
- }
- }
效果图:
1.圆形范围,力的方向从圆心到目标,模拟气浪把把草震开.
2.矩形范围,力的方向是玩家面向的左和右.模拟剑气把草劈开的感觉.
效率优化:
1.控制互动草的数量,这种草不能合批,谨记.
2.脚本加上了OnBecameVisible,OnBecameInvisible 只让摄像机内草起作用.
[csharp] view plain copy