??????以前一直用的runtime transform gizmos插件,今天说操作有问题,要改,效果如下: ??????看出问题了吧?插件移动操作三维和二维不匹配,导致鼠标和轴错位(以前我们操作都不在意这个问题,现在才说要改) ??????RTG插件确实不错,支持TRS操作和回退功能,我实在不想换插件了。一大早百度找RTG不同版本看看,发现都是这样。 ??????还好我这边暂时只需要世界坐标控制移动操作,只能自己写一份了。 ??????首先做一个移动轴,如下: ??????blender做的,学一点简单的建模操作还是必要的,然后unity组装成xyz移动轴,如下: ??????当然RTG插件里面使用的GL.Draw绘制的操作轴,如下:
void DrawLines(List<Vector3> lines, Color color)
{
if(lines.Count == 0) return;
GL.Begin(GL.LINES);
GL.Color(color);
for(int i = 0; i < lines.Count; i += 2)
{
GL.Vertex(lines[i]);
GL.Vertex(lines[i + 1]);
}
GL.End();
}
void DrawTriangles(List<Vector3> lines, Color color)
{
if(lines.Count == 0) return;
GL.Begin(GL.TRIANGLES);
GL.Color(color);
for(int i = 0; i < lines.Count; i += 3)
{
GL.Vertex(lines[i]);
GL.Vertex(lines[i + 1]);
GL.Vertex(lines[i + 2]);
}
GL.End();
}
void DrawQuads(List<Vector3> lines, Color color)
{
if(lines.Count == 0) return;
GL.Begin(GL.QUADS);
GL.Color(color);
for(int i = 0; i < lines.Count; i += 4)
{
GL.Vertex(lines[i]);
GL.Vertex(lines[i + 1]);
GL.Vertex(lines[i + 2]);
GL.Vertex(lines[i + 3]);
}
GL.End();
}
void DrawFilledCircle(List<Vector3> lines, Color color)
{
if(lines.Count == 0) return;
Vector3 center = Vector3.zero;
for(int i = 0; i < lines.Count; i++)
{
center += lines[i];
}
center /= lines.Count;
GL.Begin(GL.TRIANGLES);
GL.Color(color);
for(int i = 0; i + 1 < lines.Count; i++)
{
GL.Vertex(lines[i]);
GL.Vertex(lines[i + 1]);
GL.Vertex(center);
}
GL.End();
}
??????这里我们有时间就用GL绘制,没时间做个模型也行吧。 ??????接下来分析二三维匹配的移动操作,以X轴为例,如下: ??????其实也很简单,我们计算出X轴所在的平面F,然后通过摄像机鼠标射线进行二三维坐标映射,鼠标射线与平面F相交的坐标点P1可以实时与二维鼠标坐标对齐,再计算得到移动量x。 ??????接下来写代码实现,如下: ??????我们一共要写三个类: ??????1.RTMXYZAxis(负责轴的生成、移动、释放等)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityPlugin.RTM
{
public enum EAxisType
{
Null,
X,
Y,
Z
}
public class RTMXYZAxis : MonoBehaviour
{
[Header("X轴")]
public Transform xAxis;
[Header("Y轴")]
public Transform yAxis;
[Header("Z轴")]
public Transform zAxis;
[Header("选中轴类型")]
public EAxisType axisType;
private Material xMat;
private Material yMat;
private Material zMat;
private RTMGo attachRtmGo;
private bool isAttaching = false;
public bool IsAttaching { get { return isAttaching; } }
private Vector3 panelCenter;
private Vector3 panelNormal;
private Vector3 rayInitPoint;
private void Awake()
{
xMat = xAxis.GetComponent<MeshRenderer>().sharedMaterial;
yMat = yAxis.GetComponent<MeshRenderer>().sharedMaterial;
zMat = zAxis.GetComponent<MeshRenderer>().sharedMaterial;
}
void Start()
{
}
public void Create(RTMGo go)
{
attachRtmGo = go;
transform.position = attachRtmGo.GetWorldPos();
xAxis.gameObject.SetActive(true);
yAxis.gameObject.SetActive(true);
zAxis.gameObject.SetActive(true);
isAttaching = true;
#if UNITY_EDITOR
Debug.LogWarningFormat("RTMXYZAxis Create go = {0}", go.GetName());
#endif
}
public bool RaycastAxis(Ray ray)
{
bool israycast = true;
RaycastHit hitinfo;
if (Physics.Raycast(ray, out hitinfo))
{
if (hitinfo.transform == xAxis)
{
axisType = EAxisType.X;
}
else if (hitinfo.transform == yAxis)
{
axisType = EAxisType.Y;
}
else if (hitinfo.transform == zAxis)
{
axisType = EAxisType.Z;
}
else
{
axisType = EAxisType.Null;
israycast = false;
}
#if UNITY_EDITOR
Debug.LogWarningFormat("RTMXYZAxis RaycastAxis");
#endif
}
else
{
axisType = EAxisType.Null;
israycast = false;
}
ChangeMat(axisType);
GetPanelParams(axisType, ray);
return israycast;
}
private void ChangeMat(EAxisType atype)
{
switch (atype)
{
case EAxisType.X:
{
xMat.SetColor("_Color", Color.yellow);
yMat.SetColor("_Color", Color.green);
zMat.SetColor("_Color", Color.blue);
}
break;
case EAxisType.Y:
{
xMat.SetColor("_Color", Color.red);
yMat.SetColor("_Color", Color.yellow);
zMat.SetColor("_Color", Color.blue);
}
break;
case EAxisType.Z:
{
xMat.SetColor("_Color", Color.red);
yMat.SetColor("_Color", Color.green);
zMat.SetColor("_Color", Color.yellow);
}
break;
case EAxisType.Null:
{
xMat.SetColor("_Color", Color.red);
yMat.SetColor("_Color", Color.green);
zMat.SetColor("_Color", Color.blue);
}
break;
}
}
private void GetPanelParams(EAxisType atype, Ray ray)
{
switch (atype)
{
case EAxisType.X:
case EAxisType.Y:
{
panelCenter = transform.position;
panelNormal = Vector3.back;
rayInitPoint = RayCrossPanel(panelCenter, panelNormal, ray);
}
break;
case EAxisType.Z:
{
panelCenter = transform.position;
panelNormal = Vector3.right;
rayInitPoint = RayCrossPanel(panelCenter, panelNormal, ray);
}
break;
case EAxisType.Null:
{
panelCenter = Vector3.zero;
panelNormal = Vector3.zero;
rayInitPoint = Vector3.zero;
}
break;
}
}
public void MovementAxis(Ray ray)
{
if (axisType != EAxisType.Null)
{
Vector3 mpos = RayCrossPanel(panelCenter, panelNormal, ray);
Vector3 bias = mpos - rayInitPoint;
switch (axisType)
{
case EAxisType.X:
transform.position = panelCenter + new Vector3(bias.x, 0, 0);
break;
case EAxisType.Y:
transform.position = panelCenter + new Vector3(0, bias.y, 0);
break;
case EAxisType.Z:
transform.position = panelCenter + new Vector3(0, 0, bias.z);
break;
}
attachRtmGo.SetWorldPos(transform.position);
}
}
private Vector3 RayCrossPanel(Vector3 center, Vector3 norm, Ray ray)
{
float a = center.x, b = center.y, c = center.z;
float u = norm.x, v = norm.y, w = norm.z;
Vector3 start = ray.origin;
Vector3 dir = ray.direction;
float sx = start.x, sy = start.y, sz = start.z;
float dx = dir.x, dy = dir.y, dz = dir.z;
float n = ((u * a + v * b + w * c) - (u * sx + v * sy + w * sz)) / (u * dx + v * dy + w * dz);
Vector3 ret = start + n * dir;
return ret;
}
public void DisposeAxis()
{
axisType = EAxisType.Null;
panelCenter = Vector3.zero;
panelNormal = Vector3.zero;
rayInitPoint = Vector3.zero;
ChangeMat(axisType);
#if UNITY_EDITOR
Debug.LogWarningFormat("RTMXYZAxis DisposeAxis");
#endif
}
public void Release()
{
isAttaching = false;
attachRtmGo = null;
DisposeAxis();
xAxis.gameObject.SetActive(false);
yAxis.gameObject.SetActive(false);
zAxis.gameObject.SetActive(false);
#if UNITY_EDITOR
Debug.LogWarningFormat("RTMXYZAxis Release");
#endif
}
}
}
??????2.RTMGo(负责过滤可操作物体)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityPlugin.RTM
{
public class RTMGo : MonoBehaviour
{
void Start()
{
RTMController.GetInstance().AddRtmGo(this);
}
public Vector3 GetWorldPos()
{
return transform.position;
}
public void SetWorldPos(Vector3 wpos)
{
transform.position = wpos;
}
public string GetName()
{
return transform.name;
}
}
}
??????3.RTMController(RTM控制器,管理可操作物体,处理操作逻辑等)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityPlugin.RTM
{
public enum EMouseHandleType
{
Left,
Right,
}
public class RTMController : MonoSingleton<RTMController>
{
[Header("操作轴")]
public RTMXYZAxis rtmAxis;
[Header("鼠标操作类型")]
public EMouseHandleType mouseHandleType;
private List<RTMGo> rtmGoList = new List<RTMGo>();
private RTMGo selectRtmGo;
private Camera mainCamera;
void Start()
{
mainCamera = Camera.main;
}
public void AddRtmGo(RTMGo rtmgo)
{
if (!rtmGoList.Contains(rtmgo))
{
rtmGoList.Add(rtmgo);
}
}
public void RemoveRtmGo(RTMGo rtmgo)
{
if (rtmGoList.Contains(rtmgo))
{
rtmGoList.Remove(rtmgo);
}
}
void Update()
{
int mid = (mouseHandleType == EMouseHandleType.Left ? 0 : 1);
if (Input.GetMouseButtonDown(mid))
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
RTMGo rtmgo = hit.transform.GetComponent<RTMGo>();
if (rtmgo != null)
{
if (selectRtmGo == rtmgo)
{
return;
}
else
{
selectRtmGo = rtmgo;
rtmAxis.Create(selectRtmGo);
return;
}
}
if (rtmAxis.RaycastAxis(ray))
{
return;
}
rtmAxis.Release();
selectRtmGo = null;
}
else
{
rtmAxis.Release();
selectRtmGo = null;
}
}
if (Input.GetMouseButton(mid))
{
if (rtmAxis.IsAttaching)
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
rtmAxis.MovementAxis(ray);
}
}
if (Input.GetMouseButtonUp(mid))
{
if (rtmAxis.IsAttaching)
{
rtmAxis.DisposeAxis();
}
}
}
}
}
??????效果如下: ??????效果不错,二三维映射匹配,移动操作才更精准。 ??????当然RTM的核心功能就是射线与平面相交,再加上一些操作逻辑控制处理,理解原理即可。 ??????当然还有个小问题可能需要改进,那就是因为我们XYZ轴是模型,所以移动到很远的时候,XYZ轴就变得很小了,有点难选中轴,我们需要加一个根据XYZ轴到摄像机距离进行相应缩放的功能,如下:
private void MoveDistanceScale()
{
float distance = Vector3.Distance(mainCamera.position, transform.position);
transform.localScale = Vector3.one * distance * distanceScale;
}
??????参数自行调整: ??????效果如下: ??????当然还有一个问题就是如果RTMGo碰撞器体积边界超过RTM轴的三个轴碰撞器,那么选中这个RTMGo就无法操作轴了,如下:
??????这种修改有两种方法: ??????1.选中RTMGo就禁用其Collider,这样可能对RTMGo的其他碰撞业务逻辑产生影响 ??????2.根据raycastlayer进行分层射线判断,比较适合 ??????代码修改如下:
if (Input.GetMouseButtonDown(mid))
{
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
int rtmlayer = LayerMask.NameToLayer("RTM");
if (Physics.Raycast(ray, out hit, mainCamera.farClipPlane, 1 << rtmlayer))
{
if (hit.transform != null)
{
if (rtmAxis.RaycastAxis(ray))
{
return;
}
}
}
if (Physics.Raycast(ray, out hit))
{
RTMGo rtmgo = hit.transform.GetComponent<RTMGo>();
if (rtmgo != null)
{
if (selectRtmGo == rtmgo)
{
return;
}
else
{
selectRtmGo = rtmgo;
rtmAxis.Create(selectRtmGo);
return;
}
}
if (rtmAxis.RaycastAxis(ray))
{
return;
}
rtmAxis.Release();
selectRtmGo = null;
}
else
{
rtmAxis.Release();
selectRtmGo = null;
}
}
??????在Controller按下优先判断是否选中"RTM"Layer的轴,同时RaycastAxis中判断加上“RTM”Layer过滤即可,如下: ??????对了,至于XYZ轴最前景渲染,只需要在shader中关闭ZTest ZWrite即可,如下:
ZTest off
ZWrite off
??????功能完成,放假。
|