XChart 图表框架落地(P0–P17):格线单一作者、面板一行状态、矢量场
此前整个 XChart 特性一直没有提交(分期 P0–P12 的代码与素材都还在工作区里)。这一批一并入库, 并含本轮 P13–P17 的改动(规范 Documentation/XChart-RenderFramework.md 新增 §22–§25): - P13:格线只剩一个作者——线框工具不再有"画格线 / 发布格线插槽"的代码路径;BG Cube Grid(原 MeshBg) 是唯一作者,且"画上去的线=发布给轴线的那份比例数组";插槽板新增 Unpublish(说得出"这里不再有线", 否则掩码筛掉的面会把上一轮的值留着被消费者吸走);渲染参数搬进元素表 (DeclareDefaultElements / AddDefaultElement + XChartElementTable 按类型去重,只在第一次读到表时补); 图上「立方体外观」那组字段删除、公共属性转发到元素表;面板逐行点名脚本注册的工具。 - P14 / P15:数据窗口文案收敛(行末 `-`、标题 `X [0]`、长说明退回 tooltip、范围页三行不再被压扁); 元素块收成一行状态 `▼ 名称 √ R… D… [↑][↓][-]`;**注册序由类型定死、只显示不给输入控件, 绘制序与元素在表里的位置绑定**(§24.5,他 2026-10-25 澄清);默认元素让删也让关; EnsureSpace 改成每次重建后都跑(删掉"空间"不再等于整张图空掉)。 - P17:矢量场——维数不够时不再静默不画(元素配置报需求,面板与报表同时印"需求/现有"); 相对/绝对两种读法收进纯算术 XChartVectorFieldMath;连续绘制(互斥显示把"相对/绝对"藏起来); 颜色维(色相走 0..256 色环,饱和/明度/不透明 0..1,不透明度不算硬需求);新增螺旋矢量场测试数据。 数据口径按他澄清修正为:前三维=坐标、一支箭头=相邻两条记录、第 4~7 维是颜色(§25、CHANGELOG 修正行)。 判据:编译门三臂 + 3 支对照绿;离线探针 436 项 0 失败;p4_control 55 支臂各红在自己点名的断言上; p2_control / pool_family / generator_gate / hierarchy_gate 在同一棵交付树上复跑。 (证据脚本与日志在包外的 .ximage-gate/,未入本仓库。)
This commit is contained in:
@@ -0,0 +1,722 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using XericLibrary.Runtime.UIGraph;
|
||||
using XericUI.Core;
|
||||
using XericUI.XChart.Space;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 圆柱元素:两端圆盘(各自圆心/法线)+ 半径 → <b>视角下的外轮廓包边</b> + 轮廓填充
|
||||
/// + 可选端面十字 + 可选内部母线。
|
||||
///
|
||||
/// <para><b>为什么不再"把两端顶点都连起来"</b>:扫掠体在人眼里的样子是它的<b>外轮廓</b>——
|
||||
/// 圆在正交投影下压成椭圆,"过圆心的长轴方向"那两条桥接线才是侧面的可见边界;
|
||||
/// 把相邻端面点一片片连成四边形,画出来的是"所有母线都可见"的线框,不是包边。
|
||||
/// 现在轮廓=两片椭圆点集的凸包(对凸截面是精确解,平行/斜置/两端不同法线同一套代码),
|
||||
/// 填充就是同一个多边形。几何在 <see cref="XChartCylinderGeometry"/>(纯数学、离线可验)。</para>
|
||||
///
|
||||
/// <para><b>投影只做三次</b>:每片端面只投影"圆心 + 两根基",椭圆在 2D 上按参数式展开——
|
||||
/// 不再对盘上每点做 3D 投影(这是他要的"从 2D 上算,最大限度复用结果")。</para>
|
||||
///
|
||||
/// <para><b>弧走曲线渲染器、直线走折线渲染器</b>:库里 <c>Generation/Curve</c> 那套内部就是贝塞尔细分
|
||||
/// (所以放大也不会露出折点),轮廓的弧段与两个端面圆环都交给它,控制点由
|
||||
/// <see cref="XChartCylinderGeometry.BuildArcPiece"/> 算(我不自己切段);十字与母线是直线,
|
||||
/// 留在折线渲染器上——他一 2026-10-23 的原话是"折线渲染器适合用来显示直线"。</para>
|
||||
/// </summary>
|
||||
[AddComponentMenu(XericRegistrationPaths.Chart + "/Mesh/Cylinder Tool")]
|
||||
public sealed class ChartToolCylinder : XChartToolBase
|
||||
{
|
||||
// ── 几何(立方体单位,±1 就是盒子边界) ───────────────────
|
||||
|
||||
public Vector3 Start { get; set; } = new Vector3(-0.6f, -0.6f, -0.6f);
|
||||
public Vector3 End { get; set; } = new Vector3(0.6f, 0.6f, 0.6f);
|
||||
public float Radius { get; set; } = 0.22f;
|
||||
public int SegmentCount { get; set; } = 48;
|
||||
|
||||
/// <summary>起点端面的法线。<b>零向量=沿两端连线</b>(与旧行为一致)</summary>
|
||||
public Vector3 StartNormal { get; set; }
|
||||
/// <summary>终点端面的法线。零向量=沿两端连线</summary>
|
||||
public Vector3 EndNormal { get; set; }
|
||||
|
||||
/// <summary>圆柱自己的变换(叠在图的空间之上)</summary>
|
||||
public XChartToolTransform Transform { get; set; } = XChartToolTransform.Identity;
|
||||
|
||||
// ── 几何来源:常量 / 数据(一组记录一根圆柱) ──
|
||||
|
||||
/// <summary>关=用 <see cref="Start"/>/<see cref="End"/> 的常量坐标;开=从数据源取</summary>
|
||||
public bool DriveByData { get; set; }
|
||||
public int DimensionX { get; set; }
|
||||
public int DimensionY { get; set; } = 1;
|
||||
public int DimensionZ { get; set; } = 2;
|
||||
/// <summary>几条记录凑一根圆柱;2=两两一组(记录 0-1、2-3…,不重叠也不并入上一组)</summary>
|
||||
public int GroupSize { get; set; } = 2;
|
||||
/// <summary>最多画几根</summary>
|
||||
public int MaxCylinders { get; set; } = 64;
|
||||
|
||||
// ── 呈现(各有开关) ──────────────────────────────────────
|
||||
|
||||
/// <summary>外轮廓包边</summary>
|
||||
public bool ShowOutline { get; set; } = true;
|
||||
public Color OutlineColor { get; set; } = new Color(0.75f, 0.85f, 1f, 0.9f);
|
||||
public float OutlineThickness { get; set; } = 2f;
|
||||
|
||||
/// <summary>轮廓填充(同一个凸包多边形)</summary>
|
||||
public bool ShowFill { get; set; } = true;
|
||||
public Color FillColor { get; set; } = new Color(0.34f, 0.55f, 0.85f, 0.35f);
|
||||
|
||||
/// <summary>端面十字:用端面自己的采样顶点连,所以它**永远与实际端面对得上**,能看出滚转</summary>
|
||||
public bool ShowCapCross { get; set; }
|
||||
public Color CrossColor { get; set; } = new Color(0.9f, 0.95f, 1f, 0.85f);
|
||||
public float CrossThickness { get; set; } = 1.5f;
|
||||
|
||||
/// <summary>起点端面的闭合圆环(画满一整圈端面椭圆,=端面自己的边界)</summary>
|
||||
public bool ShowStartRing { get; set; }
|
||||
/// <summary>终点端面的闭合圆环</summary>
|
||||
public bool ShowEndRing { get; set; }
|
||||
|
||||
/// <summary>内部母线数量(把两端对应点连起来;0=不画)。<b>默认 0</b>:轮廓之外的东西要显式要</summary>
|
||||
public int GeneratrixCount { get; set; }
|
||||
public Color GeneratrixColor { get; set; } = new Color(0.75f, 0.85f, 1f, 0.35f);
|
||||
|
||||
// ── 内部 ──────────────────────────────────────────────────
|
||||
|
||||
public XChartMeshFillRenderer FillRenderer { get; private set; }
|
||||
/// <summary>曲线渲染器(贝塞尔细分):弧段走它。折线渲染器只留给直线(十字/母线)</summary>
|
||||
public UICurveRenderer CurveRenderer { get; private set; }
|
||||
public UILineRendererV2 WireRenderer { get; private set; }
|
||||
|
||||
private Matrix4x4 m_Composed = Matrix4x4.identity;
|
||||
private int m_LastSpaceVersion = -1;
|
||||
private bool m_Dirty = true;
|
||||
|
||||
private Vector2[] m_Scratch = new Vector2[0];
|
||||
private Vector2[] m_Outline = new Vector2[0];
|
||||
private byte[] m_Tags = new byte[0];
|
||||
private int[] m_RunStarts = new int[0];
|
||||
private int[] m_RunEnds = new int[0];
|
||||
private byte[] m_RunTags = new byte[0];
|
||||
private int m_RunCount;
|
||||
private readonly Vector2[] m_Bezier = new Vector2[4];
|
||||
private readonly float[] m_BezierWidths = new float[4];
|
||||
private readonly Vector2[] m_Cross = new Vector2[3];
|
||||
private int m_CurveEntries;
|
||||
private int m_RingArcs;
|
||||
private int m_RingFullCaps;
|
||||
private XChartCylinderDisc2D m_DiscStart;
|
||||
private XChartCylinderDisc2D m_DiscEnd;
|
||||
private int m_OutlineCount;
|
||||
|
||||
// ── 多根圆柱:一组记录一根,组与组不重叠(尾巴凑不满一组就不画) ──
|
||||
private Vector3[] m_CylinderStart = new Vector3[0];
|
||||
private Vector3[] m_CylinderEnd = new Vector3[0];
|
||||
private float[] m_CylinderDepth = new float[0];
|
||||
private int[] m_CylinderOrder = new int[0];
|
||||
private int m_CylinderCount;
|
||||
private int m_GroupSkipped;
|
||||
private bool m_GroupCapped;
|
||||
|
||||
/// <summary>数据版本:数据模式下几何随数据变,脏判定要把它算进来</summary>
|
||||
private long m_LastDataVersion = long.MinValue;
|
||||
/// <summary>本帧几何是怎么来的(报表用):literal / data(groups …) / data-skipped(原因)</summary>
|
||||
private string m_GeometryNote = "literal";
|
||||
|
||||
public void MarkDirty() => m_Dirty = true;
|
||||
|
||||
protected override void OnInitialize()
|
||||
{
|
||||
FillRenderer = CreateChildComponent<XChartMeshFillRenderer>("CylinderFill");
|
||||
if (FillRenderer != null) Stretch(FillRenderer.rectTransform);
|
||||
|
||||
// 弧段走曲线渲染器:它内部就是贝塞尔细分,所以椭圆弧不会像折线那样在接缝上露断口
|
||||
CurveRenderer = CreateChildComponent<UICurveRenderer>("CylinderCurve");
|
||||
if (CurveRenderer != null)
|
||||
{
|
||||
CurveRenderer.autoRebuild = false; // 控制点由本工具每帧喂,别让它自己重建默认那条
|
||||
Stretch(CurveRenderer.rectTransform);
|
||||
}
|
||||
|
||||
WireRenderer = CreateChildComponent<UILineRendererV2>("CylinderWire");
|
||||
if (WireRenderer != null)
|
||||
{
|
||||
WireRenderer.defaultColor = OutlineColor;
|
||||
WireRenderer.defaultThickness = OutlineThickness;
|
||||
Stretch(WireRenderer.rectTransform);
|
||||
}
|
||||
SetVisible(true);
|
||||
}
|
||||
|
||||
private static void Stretch(RectTransform rect)
|
||||
{
|
||||
rect.anchorMin = Vector2.zero;
|
||||
rect.anchorMax = Vector2.one;
|
||||
rect.offsetMin = Vector2.zero;
|
||||
rect.offsetMax = Vector2.zero;
|
||||
}
|
||||
|
||||
public override void OnRender()
|
||||
{
|
||||
base.OnRender();
|
||||
if (FillRenderer == null) return;
|
||||
|
||||
var space = Context?.Space;
|
||||
if (space == null) return;
|
||||
|
||||
var data = Context?.Data;
|
||||
long dataVersion = data != null ? data.Version : 0L;
|
||||
if (!m_Dirty && space.ProjectionVersion == m_LastSpaceVersion
|
||||
&& (!DriveByData || dataVersion == m_LastDataVersion)) return;
|
||||
m_Dirty = false;
|
||||
m_LastSpaceVersion = space.ProjectionVersion;
|
||||
m_LastDataVersion = dataVersion;
|
||||
m_Composed = space.Compose(Transform);
|
||||
|
||||
int segments = Mathf.Clamp(SegmentCount, 8, 256);
|
||||
EnsureBuffers(segments);
|
||||
|
||||
string note;
|
||||
if (!TryCollectCylinders(data, out note))
|
||||
{
|
||||
// 这一帧给不出几何:把上一帧收干净。留着旧几何是最坏的结果——那几根圆柱
|
||||
// 会被读成"当前值"。(不能走绘制路径:端面圆环吃的是上一帧的圆盘。)
|
||||
m_GeometryNote = note;
|
||||
ClearGeometry();
|
||||
return;
|
||||
}
|
||||
m_GeometryNote = note;
|
||||
|
||||
SortCylindersByDepth(space); // DepthOf 越大越近 ⇒ 先画远的
|
||||
BeginGeometry();
|
||||
for (int i = 0; i < m_CylinderCount; i++)
|
||||
DrawCylinder(space, segments, m_CylinderOrder[i]);
|
||||
EndGeometry();
|
||||
}
|
||||
|
||||
private void BeginGeometry()
|
||||
{
|
||||
FillRenderer.ClearFaces();
|
||||
if (CurveRenderer != null) CurveRenderer.ClearAll();
|
||||
if (WireRenderer != null) WireRenderer.ClearAll();
|
||||
m_CurveEntries = 0;
|
||||
m_RingArcs = 0;
|
||||
m_RingFullCaps = 0;
|
||||
}
|
||||
|
||||
private void EndGeometry()
|
||||
{
|
||||
FillRenderer.Apply();
|
||||
if (CurveRenderer != null) CurveRenderer.RebuildAll();
|
||||
}
|
||||
|
||||
/// <summary>一根圆柱的全部落笔:两片端面 → 轮廓 → 填充 → 弧(轮廓/圆环)→ 直线(十字/母线)。
|
||||
/// 三个渲染器都是"先清一次、循环里追加、最后统一提交",所以这里只追加、不提交。</summary>
|
||||
private void DrawCylinder(ChartToolSpace space, int segments, int index)
|
||||
{
|
||||
Vector3 start = m_CylinderStart[index];
|
||||
Vector3 end = m_CylinderEnd[index];
|
||||
float radius = Mathf.Max(1e-4f, Radius);
|
||||
|
||||
XChartCylinderGeometry.ResolveNormals(StartNormal, EndNormal, start, end,
|
||||
out Vector3 startNormal, out Vector3 endNormal);
|
||||
bool built = BuildDisc(space, start, startNormal, radius, segments, out m_DiscStart)
|
||||
&& BuildDisc(space, end, endNormal, radius, segments, out m_DiscEnd);
|
||||
|
||||
m_OutlineCount = built
|
||||
? XChartCylinderGeometry.BuildOutline(m_DiscStart, m_DiscEnd, segments, m_Scratch, m_Outline)
|
||||
: 0;
|
||||
|
||||
AppendFill();
|
||||
AppendCurves(segments);
|
||||
AppendStraightWires();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把三个渲染器清空到"什么都没画",并且不动圆盘缓冲——给"这一帧拿不到几何"用。
|
||||
/// </summary>
|
||||
private void ClearGeometry()
|
||||
{
|
||||
m_OutlineCount = 0;
|
||||
m_CurveEntries = 0;
|
||||
m_RingArcs = 0;
|
||||
m_RingFullCaps = 0;
|
||||
|
||||
FillRenderer.ClearFaces();
|
||||
FillRenderer.Apply();
|
||||
if (CurveRenderer != null)
|
||||
{
|
||||
CurveRenderer.ClearAll();
|
||||
CurveRenderer.RebuildAll();
|
||||
}
|
||||
if (WireRenderer != null) WireRenderer.ClearAll();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 这一帧要画哪几根圆柱。常量模式=一根(配置里那两个坐标);数据模式=<b>按组</b>:
|
||||
/// 组 <c>g</c> 覆盖记录 <c>[g*GroupSize, g*GroupSize + GroupSize - 1]</c>,两端取<b>组首与组尾</b>,
|
||||
/// 组与组严格不重叠、也<b>不把上一组并进来</b>;凑不满一组的尾巴不画(记进 <see cref="m_GroupSkipped"/>)。
|
||||
///
|
||||
/// <para><b>下标只有一个权威</b>(他 2026-10-24 的要求):记录下标 0 = 最早、n-1 = 最新,
|
||||
/// 与数据窗口的行号同一套下标,所以这里只按<b>绝对下标</b>取记录——"往回数第几条"那种
|
||||
/// 相对量会让同一份数据在窗口里是第 1 条、在工具里却成了第 n-2 条(现场就是它)。</para>
|
||||
///
|
||||
/// <para>数据值必须先用该维值域归一化到立方体单位才进几何(不归一化的话数据值可能上千,
|
||||
/// 直接飞出盒子)。每一步失败都给出可点名的原因,报表里印出来。</para>
|
||||
/// </summary>
|
||||
private bool TryCollectCylinders(IXChartDataSource data, out string note)
|
||||
{
|
||||
m_CylinderCount = 0;
|
||||
m_GroupSkipped = 0;
|
||||
m_GroupCapped = false;
|
||||
|
||||
if (!DriveByData)
|
||||
{
|
||||
EnsureCylinderCapacity(1);
|
||||
Store(0, Start, End);
|
||||
note = "literal";
|
||||
return true;
|
||||
}
|
||||
|
||||
if (data == null) { note = "data-skipped(no-source)"; return false; }
|
||||
|
||||
int groupSize = Mathf.Max(2, GroupSize);
|
||||
int available = XChartBindingMath.CompleteGroupCount(data.RecordCount, groupSize);
|
||||
if (available == 0)
|
||||
{
|
||||
note = "data-skipped(need " + groupSize + " records per group, have " + data.RecordCount + ")";
|
||||
return false;
|
||||
}
|
||||
|
||||
int groups = Mathf.Min(available, Mathf.Max(1, MaxCylinders));
|
||||
m_GroupCapped = groups < available;
|
||||
|
||||
double minX, maxX, minY, maxY, minZ, maxZ;
|
||||
if (!TryResolveDomains(out minX, out maxX, out minY, out maxY, out minZ, out maxZ))
|
||||
{
|
||||
note = "data-skipped(no-domain)";
|
||||
return false;
|
||||
}
|
||||
|
||||
EnsureCylinderCapacity(groups);
|
||||
for (int g = 0; g < groups; g++)
|
||||
{
|
||||
int first, last;
|
||||
if (!XChartBindingMath.TryResolveGroup(data.RecordCount, g, groupSize, out first, out last))
|
||||
{
|
||||
m_GroupSkipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
Vector3 head, tail;
|
||||
if (!TryResolveNormalized(data, first, minX, maxX, minY, maxY, minZ, maxZ, out head)
|
||||
|| !TryResolveNormalized(data, last, minX, maxX, minY, maxY, minZ, maxZ, out tail))
|
||||
{
|
||||
m_GroupSkipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
Store(m_CylinderCount, head, tail);
|
||||
}
|
||||
|
||||
if (m_CylinderCount == 0) { note = "data-skipped(all-groups-rejected)"; return false; }
|
||||
|
||||
int drawnRecords = m_CylinderCount * groupSize;
|
||||
note = "data(groups " + m_CylinderCount + "/" + available + ", size " + groupSize
|
||||
+ ", rec 0->" + (drawnRecords - 1)
|
||||
+ (m_GroupCapped ? ", capped" : string.Empty)
|
||||
+ (m_GroupSkipped > 0 ? ", skip " + m_GroupSkipped : string.Empty) + ")";
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Store(int index, Vector3 start, Vector3 end)
|
||||
{
|
||||
m_CylinderStart[index] = start;
|
||||
m_CylinderEnd[index] = end;
|
||||
m_CylinderCount = index + 1;
|
||||
}
|
||||
|
||||
private void EnsureCylinderCapacity(int count)
|
||||
{
|
||||
if (m_CylinderStart.Length >= count) return;
|
||||
m_CylinderStart = new Vector3[count];
|
||||
m_CylinderEnd = new Vector3[count];
|
||||
m_CylinderDepth = new float[count];
|
||||
m_CylinderOrder = new int[count];
|
||||
}
|
||||
|
||||
private bool TryResolveNormalized(IXChartDataSource data, int record,
|
||||
double minX, double maxX, double minY, double maxY, double minZ, double maxZ, out Vector3 unit)
|
||||
{
|
||||
unit = Vector3.zero;
|
||||
Vector3 raw;
|
||||
if (!XChartBindingMath.TryResolvePosition(data, record, DimensionX, DimensionY, DimensionZ, out raw))
|
||||
return false;
|
||||
|
||||
float x, y, z;
|
||||
if (!XChartBindingMath.TryNormalize(raw.x, minX, maxX, out x)
|
||||
|| !XChartBindingMath.TryNormalize(raw.y, minY, maxY, out y)
|
||||
|| !XChartBindingMath.TryNormalize(raw.z, minZ, maxZ, out z)) return false;
|
||||
|
||||
unit = new Vector3(x, y, z);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按深度从远到近排序(<see cref="ChartToolSpace.DepthOf"/> 越大越靠近观察者):
|
||||
/// 同一个工具内的多根圆柱靠<b>提交顺序</b>决定画家序(三个渲染器都不按深度自己排),
|
||||
/// 不排就会让近的被远的盖住。
|
||||
/// </summary>
|
||||
private void SortCylindersByDepth(ChartToolSpace space)
|
||||
{
|
||||
for (int i = 0; i < m_CylinderCount; i++)
|
||||
{
|
||||
m_CylinderDepth[i] = (space.DepthOf(m_CylinderStart[i], m_Composed)
|
||||
+ space.DepthOf(m_CylinderEnd[i], m_Composed)) * 0.5f;
|
||||
m_CylinderOrder[i] = i;
|
||||
}
|
||||
|
||||
// 插入排序:根数有上限(MaxCylinders),且渲染路径里不分配
|
||||
for (int i = 1; i < m_CylinderCount; i++)
|
||||
{
|
||||
int key = m_CylinderOrder[i];
|
||||
float depth = m_CylinderDepth[key];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && m_CylinderDepth[m_CylinderOrder[j]] > depth)
|
||||
{
|
||||
m_CylinderOrder[j + 1] = m_CylinderOrder[j];
|
||||
j--;
|
||||
}
|
||||
m_CylinderOrder[j + 1] = key;
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryResolveDomains(out double minX, out double maxX,
|
||||
out double minY, out double maxY, out double minZ, out double maxZ)
|
||||
{
|
||||
minX = maxX = minY = maxY = minZ = maxZ = 0d;
|
||||
var context = Context;
|
||||
if (context == null) return false;
|
||||
return context.TryResolveDomain(DimensionX, out minX, out maxX)
|
||||
&& context.TryResolveDomain(DimensionY, out minY, out maxY)
|
||||
&& context.TryResolveDomain(DimensionZ, out minZ, out maxZ);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 一片端面 → 2D 椭圆:<b>只投影圆心与两根基</b>。基向量的像素像就是椭圆的半轴,
|
||||
/// 半径并进半轴里,之后所有采样都在 2D 上做。
|
||||
/// </summary>
|
||||
private bool BuildDisc(ChartToolSpace space, Vector3 center, Vector3 normal, float radius,
|
||||
int segments, out XChartCylinderDisc2D disc)
|
||||
{
|
||||
disc = default(XChartCylinderDisc2D);
|
||||
Vector3 u, v;
|
||||
if (!XChartCylinderGeometry.TryOrthogonalBasis(normal, out u, out v)) return false;
|
||||
|
||||
Vector2 centerPixels = space.Project(center, m_Composed);
|
||||
Vector2 uPixels = space.Project(center + u, m_Composed) - centerPixels;
|
||||
Vector2 vPixels = space.Project(center + v, m_Composed) - centerPixels;
|
||||
|
||||
return XChartCylinderGeometry.TryProjectDisc(centerPixels, uPixels, vPixels, radius, out disc);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 填充=轮廓多边形本身,用"扇形"喂进去(库里只有四边形图元):每片是
|
||||
/// (中心, 第 i 点, 第 i+1 点, 第 i+1 点) 的退化三角形。整片轮廓在一处连续提交,
|
||||
/// 所以它相对其它元素是<b>整块</b>参与画家序,内部不再分片排序。
|
||||
/// </summary>
|
||||
private void AppendFill()
|
||||
{
|
||||
if (!ShowFill || m_OutlineCount < 3) return;
|
||||
|
||||
Vector2 centroid = Vector2.zero;
|
||||
for (int i = 0; i < m_OutlineCount; i++) centroid += m_Outline[i];
|
||||
centroid /= m_OutlineCount;
|
||||
|
||||
for (int i = 0; i < m_OutlineCount; i++)
|
||||
{
|
||||
Vector2 a = m_Outline[i];
|
||||
Vector2 b = m_Outline[(i + 1) % m_OutlineCount];
|
||||
FillRenderer.AddFace(centroid, a, b, b, FillColor, FillColor);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>弧线交给曲线渲染器(贝塞尔细分),直线留在折线渲染器上——他 2026-10-23 的原话:
|
||||
/// "你现在用的折线渲染器就是分段的,适合用来显示直线"。</summary>
|
||||
private void AppendCurves(int segments)
|
||||
{
|
||||
if (CurveRenderer == null) return;
|
||||
|
||||
ComputeRuns();
|
||||
if (ShowOutline && m_OutlineCount >= 3) AddOutlineCurves();
|
||||
if (ShowStartRing) AddRingCurves(1, segments);
|
||||
if (ShowEndRing) AddRingCurves(2, segments);
|
||||
}
|
||||
|
||||
/// <summary>提交一节三次贝塞尔(4 个控制点)。控制点会被库拷进它自己的扁平缓存,所以复用 scratch 安全。</summary>
|
||||
private void AddCurveEntry(Color color)
|
||||
{
|
||||
for (int i = 0; i < m_BezierWidths.Length; i++) m_BezierWidths[i] = OutlineThickness;
|
||||
var entry = new CurveCacheEntry
|
||||
{
|
||||
startColor = color,
|
||||
endColor = color,
|
||||
tessellationSegments = Mathf.Clamp(SegmentCount / 6, 4, 32),
|
||||
};
|
||||
CurveRenderer.AddCurve(entry, m_Bezier, m_BezierWidths);
|
||||
m_CurveEntries++;
|
||||
}
|
||||
|
||||
/// <summary>端面圆环=把端面圆<b>补成整圈</b>:包边已经画了那一片的<b>外侧弧</b>,
|
||||
/// 环就补剩下的<b>内侧弧</b>(同两个端点、反方向走)——他 2026-10-23 的要求:
|
||||
/// "关闭 start/end ring 时消失的应该是内部圆环那部分",而且"画整圈会和包边重复出两条线框"。
|
||||
/// 包边没在画(或这一片在轮廓上只占一个点)时,环自己补整圈。</summary>
|
||||
private void AddRingCurves(byte tag, int segments)
|
||||
{
|
||||
XChartCylinderDisc2D disc = tag == 1 ? m_DiscStart : m_DiscEnd;
|
||||
int index = FindRun(tag);
|
||||
if (!ShowOutline || index < 0)
|
||||
{
|
||||
// 包边没在画,或这一片整个躲在外轮廓里(凸包上连一段可见弧都没有)⇒ 只能画整圈
|
||||
m_RingFullCaps++;
|
||||
AddFullRing(disc, segments);
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_RunStarts[index] == m_RunEnds[index])
|
||||
{
|
||||
m_RingFullCaps++;
|
||||
AddFullRing(disc, segments);
|
||||
return;
|
||||
}
|
||||
|
||||
// 补弧:起点接在可见弧的终点、扫过 2π-|可见|(显式走长路,不能用"反向短路"表达)
|
||||
if (!XChartCylinderGeometry.TryParameterOf(disc, m_Outline[m_RunStarts[index]], out float fromAngle))
|
||||
return;
|
||||
if (!XChartCylinderGeometry.TryParameterOf(disc, m_Outline[m_RunEnds[index]], out float toAngle))
|
||||
return;
|
||||
float visible = XChartCylinderGeometry.SweepToDirection(toAngle - fromAngle, DirectionOfRun(index));
|
||||
XChartCylinderGeometry.RingArc(fromAngle, visible, out float ringStart, out float ringSweep);
|
||||
AddArcPieces(disc, ringStart, ringSweep);
|
||||
m_RingArcs++;
|
||||
}
|
||||
|
||||
/// <summary>整圈(每节 ≤90°;与采样段数解耦,所以放大也不会露出折点)</summary>
|
||||
private void AddFullRing(in XChartCylinderDisc2D disc, int segments)
|
||||
{
|
||||
int pieces = Mathf.Max(1, segments / 4);
|
||||
for (int p = 0; p < pieces; p++)
|
||||
{
|
||||
XChartCylinderGeometry.BuildArcPiece(disc, 0f, 6.2831855f, pieces, p, m_Bezier);
|
||||
AddCurveEntry(OutlineColor);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 轮廓=弧段 + 桥接段。凸包点先按"落在哪片端面的椭圆上"打标记,同标记的连续段是椭圆弧
|
||||
/// (走贝塞尔),跨标记那条就是"过圆心的长轴方向连外框"的桥接线(走直线)。
|
||||
/// </summary>
|
||||
private void AddOutlineCurves()
|
||||
{
|
||||
for (int r = 0; r < m_RunCount; r++)
|
||||
{
|
||||
int start = m_RunStarts[r];
|
||||
int end = m_RunEnds[r];
|
||||
if (m_RunTags[r] != 0 && start != end)
|
||||
{
|
||||
AddArcBetween(DiscOfRun(r), m_Outline[start], m_Outline[end],
|
||||
DirectionOfRun(r), m_Outline[start]);
|
||||
}
|
||||
|
||||
int nextStart = m_RunStarts[(r + 1) % m_RunCount];
|
||||
AddStraightCurve(m_Outline[end], m_Outline[nextStart]); // 段与段之间(含首尾相接那条桥接)
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把凸包切成"同端面的连续段"。切分本身在纯几何里(`SplitOutlineRuns`),因为
|
||||
/// "哪半边在轮廓里"这种判定必须能离线验。
|
||||
/// </summary>
|
||||
private void ComputeRuns()
|
||||
{
|
||||
m_RunCount = 0;
|
||||
if (m_OutlineCount < 3) return;
|
||||
|
||||
EnsureRunBuffers(m_OutlineCount);
|
||||
m_RunCount = XChartCylinderGeometry.SplitOutlineRuns(m_Outline, m_OutlineCount, m_DiscStart, m_DiscEnd,
|
||||
m_Tags, m_RunStarts, m_RunEnds, m_RunTags);
|
||||
}
|
||||
|
||||
private void EnsureRunBuffers(int count)
|
||||
{
|
||||
if (m_Tags.Length < count)
|
||||
{
|
||||
m_Tags = new byte[count];
|
||||
m_RunStarts = new int[count];
|
||||
m_RunEnds = new int[count];
|
||||
m_RunTags = new byte[count];
|
||||
}
|
||||
}
|
||||
|
||||
private int FindRun(byte tag)
|
||||
{
|
||||
for (int r = 0; r < m_RunCount; r++)
|
||||
{
|
||||
if (m_RunTags[r] == tag) return r;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private XChartCylinderDisc2D DiscOfRun(int index) => m_RunTags[index] == 1 ? m_DiscStart : m_DiscEnd;
|
||||
|
||||
/// <summary>这一段弧的转向:看段内第二个点(凸包是逆时针,弧必须顺着它走)</summary>
|
||||
private int DirectionOfRun(int index)
|
||||
{
|
||||
if (m_RunStarts[index] == m_RunEnds[index]) return 1;
|
||||
XChartCylinderDisc2D disc = DiscOfRun(index);
|
||||
int second = (m_RunStarts[index] + 1) % m_OutlineCount;
|
||||
if (!XChartCylinderGeometry.TryParameterOf(disc, m_Outline[m_RunStarts[index]], out float fromAngle))
|
||||
return 1;
|
||||
if (!XChartCylinderGeometry.TryParameterOf(disc, m_Outline[second], out float secondAngle))
|
||||
return 1;
|
||||
return XChartCylinderGeometry.ShortestDirection(secondAngle - fromAngle);
|
||||
}
|
||||
|
||||
/// <summary>提交一段椭圆弧;<paramref name="direction"/> 非 0 时强制转向,为 0 时按 <paramref name="hintPoint"/> 定</summary>
|
||||
private void AddArcBetween(in XChartCylinderDisc2D disc, Vector2 fromPoint, Vector2 toPoint,
|
||||
int direction, Vector2 hintPoint)
|
||||
{
|
||||
if (!XChartCylinderGeometry.TryParameterOf(disc, fromPoint, out float fromAngle)) return;
|
||||
if (!XChartCylinderGeometry.TryParameterOf(disc, toPoint, out float toAngle)) return;
|
||||
|
||||
if (direction == 0 && XChartCylinderGeometry.TryParameterOf(disc, hintPoint, out float hintAngle))
|
||||
direction = XChartCylinderGeometry.ShortestDirection(hintAngle - fromAngle);
|
||||
|
||||
AddArcPieces(disc, fromAngle, XChartCylinderGeometry.SweepToDirection(toAngle - fromAngle, direction));
|
||||
}
|
||||
|
||||
/// <summary>把"起点角 + 扫过角"切成 ≤90° 的贝塞尔节提交</summary>
|
||||
private void AddArcPieces(in XChartCylinderDisc2D disc, float fromAngle, float sweep)
|
||||
{
|
||||
int pieces = XChartCylinderGeometry.ArcPieceCount(sweep);
|
||||
for (int p = 0; p < pieces; p++)
|
||||
{
|
||||
XChartCylinderGeometry.BuildArcPiece(disc, fromAngle, sweep, pieces, p, m_Bezier);
|
||||
AddCurveEntry(OutlineColor);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddStraightCurve(Vector2 from, Vector2 to)
|
||||
{
|
||||
if ((from - to).sqrMagnitude < 1e-8f) return;
|
||||
XChartCylinderGeometry.BuildLineBezier(from, to, m_Bezier);
|
||||
AddCurveEntry(OutlineColor);
|
||||
}
|
||||
|
||||
private void AppendStraightWires()
|
||||
{
|
||||
if (WireRenderer == null) return;
|
||||
|
||||
if (ShowCapCross)
|
||||
{
|
||||
DrawCross(m_DiscStart);
|
||||
DrawCross(m_DiscEnd);
|
||||
}
|
||||
|
||||
if (GeneratrixCount > 0 && m_OutlineCount > 0) DrawGeneratrices();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 一整条折线一次提交(<b>不要</b>一段一段 DrawLine):库里网格生成器的"转角连接三角形"只在
|
||||
/// 同一根线内的相邻子段之间补,所以逐段画会在**每个转角留下断口**——他 2026-10-23 放大看到的
|
||||
/// "拉近了有断口"就是这么来的。闭合的多边形靠把首点补到末尾来实现。
|
||||
/// </summary>
|
||||
private void DrawPolyline(Vector2[] points, int count, bool close, Color color, float thickness)
|
||||
{
|
||||
int length = count + (close ? 1 : 0);
|
||||
if (length < 2) return;
|
||||
var buffer = new Vector2[length];
|
||||
for (int i = 0; i < count; i++) buffer[i] = points[i];
|
||||
if (close) buffer[count] = points[0];
|
||||
WireRenderer.DrawPolyline(buffer, color, thickness);
|
||||
}
|
||||
|
||||
private void DrawCross(in XChartCylinderDisc2D disc)
|
||||
{
|
||||
Vector2 left, top, right, bottom;
|
||||
XChartCylinderGeometry.CrossPoints(disc, SegmentCount, out left, out top, out right, out bottom);
|
||||
// 过圆心的三段折线(左-心-右 / 上-心-下):转角连接由同一条线内的接缝补齐
|
||||
m_Cross[0] = left;
|
||||
m_Cross[1] = disc.Center;
|
||||
m_Cross[2] = right;
|
||||
DrawPolyline(m_Cross, 3, false, CrossColor, CrossThickness);
|
||||
m_Cross[0] = top;
|
||||
m_Cross[1] = disc.Center;
|
||||
m_Cross[2] = bottom;
|
||||
DrawPolyline(m_Cross, 3, false, CrossColor, CrossThickness);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内部母线:两端<b>对应</b>采样点连线(不是随便连)——对应关系由同一个采样角保证,
|
||||
/// 所以它和端面十字是同一套顶点,转起来不会互相错位。
|
||||
/// </summary>
|
||||
private void DrawGeneratrices()
|
||||
{
|
||||
int segments = Mathf.Clamp(SegmentCount, 8, 256);
|
||||
int stride = Mathf.Max(1, segments / Mathf.Max(1, GeneratrixCount));
|
||||
for (int i = 0; i < segments; i += stride)
|
||||
{
|
||||
Vector2 from = m_DiscStart.PointAtStep(i, segments);
|
||||
Vector2 to = m_DiscEnd.PointAtStep(i, segments);
|
||||
WireRenderer.DrawLine(from, to, GeneratrixColor, OutlineThickness);
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsureBuffers(int segments)
|
||||
{
|
||||
// scratch 装两片椭圆的采样点;包点缓冲按凸包容量口径要 2×采样数
|
||||
int samples = segments * 2;
|
||||
if (m_Scratch.Length < samples)
|
||||
{
|
||||
m_Scratch = new Vector2[samples];
|
||||
m_Outline = new Vector2[samples * 2];
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDispose()
|
||||
{
|
||||
FillRenderer = null;
|
||||
CurveRenderer = null;
|
||||
WireRenderer = null;
|
||||
m_Scratch = null;
|
||||
m_Outline = null;
|
||||
m_Tags = null;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
/// <summary>报表里点名轮廓点数与各开关:0 要能分清"没算"还是"算出来是空"(轮廓点数=0 时 m_Dirty 也停了)</summary>
|
||||
internal override void AppendDebugLine(System.Text.StringBuilder sb)
|
||||
{
|
||||
base.AppendDebugLine(sb);
|
||||
sb.Append(" outlinePoints=").Append(m_OutlineCount)
|
||||
.Append(" curveEntries=").Append(m_CurveEntries)
|
||||
.Append(" fill=").Append(ShowFill)
|
||||
.Append(" fillChunks=").Append(FillRenderer != null ? FillRenderer.ChunkCount : 0)
|
||||
.Append(" fillMaxVerts=").Append(FillRenderer != null ? FillRenderer.MaxChunkVertices : 0)
|
||||
.Append(" curveVerts=").Append(CurveRenderer != null ? CurveRenderer.LiveVertexCount : 0)
|
||||
.Append(" wireVerts=").Append(WireRenderer != null ? WireRenderer.LiveVertexCount : 0)
|
||||
.Append(" ringStart=").Append(ShowStartRing)
|
||||
.Append(" ringEnd=").Append(ShowEndRing)
|
||||
.Append(" ringArcs=").Append(m_RingArcs)
|
||||
.Append(" ringFullCaps=").Append(m_RingFullCaps)
|
||||
.Append(" cross=").Append(ShowCapCross)
|
||||
.Append(" generatrices=").Append(GeneratrixCount)
|
||||
.Append(" normals=").Append(StartNormal.sqrMagnitude > 1e-8f ? "explicit" : "alongAxis")
|
||||
.Append("/").Append(EndNormal.sqrMagnitude > 1e-8f ? "explicit" : "alongAxis")
|
||||
// 几何来源:数据模式下必须能看出"这一帧画了几根、取的是哪几组记录",
|
||||
// 否则 skipped 与 literal、以及"截断了多少"都分不清
|
||||
.Append(" geometry=").Append(m_GeometryNote)
|
||||
.Append(" cylinders=").Append(m_CylinderCount);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 75b05406ca016db469518a7bff37d1a4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,510 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using XericLibrary.Runtime.UIGraph;
|
||||
using XericUI.Core;
|
||||
using XericUI.XChart.Axis;
|
||||
using XericUI.XChart.Space;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 伪立方体背景元素:<b>填充面 + 面内网格</b>(线框棱由 <c>ChartToolCubeBg</c> 负责,两者可同用)。
|
||||
///
|
||||
/// <para><b>画哪些面由掩码决定,不看可见性</b>:图表里的背景网格常画在背朝观察者的那几面
|
||||
/// (后墙 / 底面),所以 <see cref="FillFaces"/> 与 <see cref="GridFaces"/> 是两份独立的
|
||||
/// <see cref="XChartCubeFaceMask"/>,字母契约见该类型(a=前 b=后 c=上 d=下 e=左 f=右)。</para>
|
||||
///
|
||||
/// <para><b>深度顺序</b>:填充按面心深度从远到近提交(画家算法),半透明叠色才正确;
|
||||
/// 网格线跟在填充之后(子节点顺序 = 绘制顺序)。</para>
|
||||
///
|
||||
/// <para><b>空间来源</b>:本元素<b>不再自己算投影</b>,而是问图里的
|
||||
/// <see cref="ChartToolSpace"/> 要矩阵和面帧(<c>投影 = 基变换 ∘ 工具变换</c>)。
|
||||
/// 散点/轴线问的是同一个空间,所以点必然落在背景里——这是结构保证,不是两边凑数值。</para>
|
||||
/// </summary>
|
||||
[AddComponentMenu(XericRegistrationPaths.Chart + "/Mesh/Cube Mesh Background Tool")]
|
||||
public class ChartToolMeshBg : XChartToolBase
|
||||
{
|
||||
// ── 工具变换(在图的空间之上再叠一层) ────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// 背景自己的变换(立方体局部单位)。默认恒等——背景通常就占满图的空间。
|
||||
/// 想画一个"小一号/偏一角"的背景格子时才用它。
|
||||
/// </summary>
|
||||
public XChartToolTransform Transform { get; set; } = XChartToolTransform.Identity;
|
||||
|
||||
// ── 填充 ──────────────────────────────────────────────────
|
||||
|
||||
/// <summary>要填充哪些面</summary>
|
||||
public XChartCubeFaceMask FillFaces { get; set; } = XChartCubeFaceMask.None;
|
||||
|
||||
/// <summary>填充色(面边缘)</summary>
|
||||
public Color FillColor { get; set; } = new Color(0.16f, 0.19f, 0.24f, 0.85f);
|
||||
|
||||
/// <summary>填充面中心色;与 <see cref="FillColor"/> 不同时走径向渐变</summary>
|
||||
public Color FillCenterColor { get; set; } = new Color(0.16f, 0.19f, 0.24f, 0.85f);
|
||||
|
||||
// ── 面内网格 ──────────────────────────────────────────────
|
||||
|
||||
/// <summary>要画网格的面</summary>
|
||||
public XChartCubeFaceMask GridFaces { get; set; } = XChartCubeFaceMask.None;
|
||||
|
||||
/// <summary>网格线颜色</summary>
|
||||
public Color GridColor { get; set; } = new Color(0.55f, 0.6f, 0.7f, 0.35f);
|
||||
|
||||
/// <summary>网格线宽</summary>
|
||||
public float GridThickness { get; set; } = 1f;
|
||||
|
||||
/// <summary>每个方向的网格分割数(统一值;S/T 各自给了正数时按各自的来)</summary>
|
||||
public int GridDivisions { get; set; } = 5;
|
||||
|
||||
/// <summary>S 方向分格数。0=跟随 <see cref="GridDivisions"/>。
|
||||
/// 等分时"沿 S 的线由 T 的分格数决定"(算式在 <see cref="XChartGridDivisions"/>,探针钉着);
|
||||
/// 绑了维度时这个数是<b>那一族线的目标刻度条数</b>(不是分格数,规范 §22)。</summary>
|
||||
public int GridDivisionsS { get; set; }
|
||||
|
||||
/// <summary>T 方向分格数。0=跟随 <see cref="GridDivisions"/></summary>
|
||||
public int GridDivisionsT { get; set; }
|
||||
|
||||
/// <summary>是否同时描出面轮廓(没有线框元素时也能看清面的边界)</summary>
|
||||
public bool ShowFaceOutline { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 只画朝向观察者的面。默认 false——背景网格本就常画在背朝观察者的那几面(后墙/底面);
|
||||
/// 想要"实体盒子"的观感时打开它。
|
||||
/// </summary>
|
||||
public bool OnlyVisibleFaces { get; set; }
|
||||
|
||||
/// <summary>网格与轮廓的颜色与线宽(轮廓更实一点)</summary>
|
||||
public Color OutlineColor { get; set; } = new Color(0.7f, 0.75f, 0.85f, 0.6f);
|
||||
|
||||
// ── 内部组件 ──────────────────────────────────────────────
|
||||
|
||||
/// <summary>面填充渲染器</summary>
|
||||
public XChartMeshFillRenderer FillRenderer { get; private set; }
|
||||
|
||||
/// <summary>面内网格渲染器</summary>
|
||||
public UILineRendererV2 GridRenderer { get; private set; }
|
||||
|
||||
// ── 投影状态(权威在 ChartToolSpace,这里只缓存复合结果) ──
|
||||
|
||||
private readonly XChartFaceFrame[] m_FaceFrames = new XChartFaceFrame[XChartCubeGeometry.FaceCount];
|
||||
private readonly List<FaceSortEntry> m_SortedFaces = new List<FaceSortEntry>(XChartCubeGeometry.FaceCount);
|
||||
|
||||
private struct FaceSortEntry
|
||||
{
|
||||
public int Face;
|
||||
public float Depth;
|
||||
}
|
||||
|
||||
private static readonly Comparison<FaceSortEntry> s_FarToNear =
|
||||
(left, right) => left.Depth.CompareTo(right.Depth);
|
||||
|
||||
private ChartToolSpace m_Space;
|
||||
private Matrix4x4 m_Composed = Matrix4x4.identity;
|
||||
private int m_LastSpaceVersion = -1;
|
||||
/// <summary>上一次发布格线时数据模型的版本号:绑了维度的格线要跟着数据走,
|
||||
/// 而数据变化不会动空间的投影版本,所以这个号要单独判(否则"数据变了格线还停在老位置")</summary>
|
||||
private long m_LastDataVersion = -1;
|
||||
private bool m_Dirty = true;
|
||||
/// <summary>发布插槽用的复用缓冲(一个面的四角 = 12 个值)</summary>
|
||||
private readonly float[] m_SlotValues = new float[12];
|
||||
|
||||
// 本帧的自述读数:画了几条线 / 发了几条线 / 提交了几块几面 / 面帧有效几个
|
||||
private int m_LinesS;
|
||||
private int m_LinesT;
|
||||
private int m_GridLinesPublished;
|
||||
private int m_GridFacesPublished;
|
||||
private int m_GridSlotsRetracted;
|
||||
private int m_GridLinesDrawn;
|
||||
private int m_GridFacesDrawn;
|
||||
private int m_FacesOutlined;
|
||||
private int m_FacesFilled;
|
||||
|
||||
/// <summary>格线插槽的值暂存:<b>两个方向各一个</b>,长度按各自的条数精确相等(板子按数组长度算组数)</summary>
|
||||
private float[] m_GridValuesS;
|
||||
private float[] m_GridValuesT;
|
||||
|
||||
/// <summary>这一方向各条线的面参数比例(等分或数据刻度),复用不分配</summary>
|
||||
private float[] m_RatioScratchS;
|
||||
private float[] m_RatioScratchT;
|
||||
private readonly List<double> m_TickScratch = new List<double>();
|
||||
|
||||
/// <summary>沿 S 的线绑哪一维(<0 = 不绑,等分)</summary>
|
||||
public int GridDimensionS { get; set; } = -1;
|
||||
|
||||
/// <summary>沿 T 的线绑哪一维(<0 = 不绑,等分)</summary>
|
||||
public int GridDimensionT { get; set; } = -1;
|
||||
|
||||
/// <summary>本元素的变换变了(或想强制重取空间)时调用</summary>
|
||||
public void MarkDirty() => m_Dirty = true;
|
||||
|
||||
/// <summary>本工具定义网格(发布面/格线插槽、算范围)⇒ 归定义组、最先跑(规范 §11.4);
|
||||
/// 它"画在底下"这件事由绘制序(默认最小)表达,与分组无关</summary>
|
||||
public override XChartToolCategory Category => XChartToolCategory.Definition;
|
||||
|
||||
/// <summary>取某个面的面帧(图表局部像素)</summary>
|
||||
public XChartFaceFrame GetFaceFrame(XChartCubeGeometry.CubeFace face)
|
||||
{
|
||||
UpdateProjection();
|
||||
return m_FaceFrames[(int)face];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 空间版本没变、自己的变换也没变,就一次矩阵乘法都不做。
|
||||
/// 空间是渲染时现找的(不是初始化时塞的),所以注册先后顺序不影响正确性。
|
||||
/// </summary>
|
||||
private void UpdateProjection()
|
||||
{
|
||||
m_Space = Context?.Space;
|
||||
if (m_Space == null) return;
|
||||
|
||||
// 数据版本也要判:格线绑了维度时"线在哪"是数据的函数,而写数据不会动空间的投影版本
|
||||
var data = Context != null ? Context.Data : null;
|
||||
long dataVersion = data != null ? data.Version : -1;
|
||||
if (!m_Dirty && m_Space.ProjectionVersion == m_LastSpaceVersion && dataVersion == m_LastDataVersion) return;
|
||||
m_Dirty = false;
|
||||
m_LastSpaceVersion = m_Space.ProjectionVersion;
|
||||
m_LastDataVersion = dataVersion;
|
||||
|
||||
m_Composed = m_Space.Compose(Transform);
|
||||
m_Space.BuildFaceFrames(m_FaceFrames, m_Composed);
|
||||
SortFacesByDepth();
|
||||
PublishFaceSlots();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把六个面发布成 `Quad3D` 插槽(规范 §11.2):<b>本工具是定义网格的那一个,其余工具按 id 取用</b>。
|
||||
/// 发布的是**单位立方体的四角**(不是像素):消费者用自己的矩阵投影,于是
|
||||
/// "谁投影"仍只有一条路,而"哪个面在哪儿"由定义者说了算——换成异形网格只需改这里。
|
||||
/// </summary>
|
||||
private void PublishFaceSlots()
|
||||
{
|
||||
IXChartSlotBoard board = Context?.Slots;
|
||||
if (board == null) return;
|
||||
|
||||
for (int i = 0; i < XChartCubeGeometry.FaceCount; i++)
|
||||
{
|
||||
var face = (XChartCubeGeometry.CubeFace)i;
|
||||
Vector3 c00, c01, c11, c10;
|
||||
XChartCubeGeometry.GetFaceCorners(face, out c00, out c01, out c11, out c10);
|
||||
|
||||
m_SlotValues[0] = c00.x; m_SlotValues[1] = c00.y; m_SlotValues[2] = c00.z;
|
||||
m_SlotValues[3] = c01.x; m_SlotValues[4] = c01.y; m_SlotValues[5] = c01.z;
|
||||
m_SlotValues[6] = c11.x; m_SlotValues[7] = c11.y; m_SlotValues[8] = c11.z;
|
||||
m_SlotValues[9] = c10.x; m_SlotValues[10] = c10.y; m_SlotValues[11] = c10.z;
|
||||
|
||||
board.Publish(XChartCubeGeometry.FaceSlotId(face), XChartSlotMode.Quad3D,
|
||||
XChartSlotAccess.ReadOnly, m_SlotValues, out string _);
|
||||
}
|
||||
|
||||
PublishGridSlots(board);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把每个面的**格线**发布成 <c>Line3D</c> 插槽(规范 §11.2 / P9):沿 S 的一组与
|
||||
/// 沿 T 的一组。发布的是<b>单位立方体端点</b>,与 <c>mesh.face.<n></c> 的四角同一口径
|
||||
/// ⇒ 谁投影仍只有一条路。
|
||||
///
|
||||
/// <para><b>线位置由谁定</b>:绑了维度(<see cref="GridDimensionS"/>/<see cref="GridDimensionT"/> ≥ 0)
|
||||
/// 就用<b>该维的数据范围</b>算刻度位置(<see cref="XChartGridLines"/>,与轴线刻度同一套 nice step);
|
||||
/// 没绑就等分(比例 <c>i/(count-1)</c>,与旧行为逐位相同)——"网格线的定义权在网格、
|
||||
/// 轴线按发布的线跟随"这句话就落在这里。</para>
|
||||
///
|
||||
/// <para>缓冲必须<b>长度精确</b>:插槽板按数组长度算组数,开大了会把残留当成额外的线发出去。</para>
|
||||
/// </summary>
|
||||
private void PublishGridSlots(IXChartSlotBoard board)
|
||||
{
|
||||
// 条数只算一次(两个方向各一份),缓冲按两者最大值开;比例数组算完就留着给绘制侧用,
|
||||
// 于是"画上哪几条线"与"发布给轴线哪几条线"不可能各说各话(规范 §22)
|
||||
m_LinesS = XChartGridDivisions.LinesAlongS(GridDivisionsS, GridDivisionsT, GridDivisions);
|
||||
m_LinesT = XChartGridDivisions.LinesAlongT(GridDivisionsS, GridDivisionsT, GridDivisions);
|
||||
EnsureRatioBuffers(Mathf.Max(m_LinesS, m_LinesT));
|
||||
m_LinesS = BuildGridRatios(true, m_RatioScratchS);
|
||||
m_LinesT = BuildGridRatios(false, m_RatioScratchT);
|
||||
|
||||
m_GridLinesPublished = 0;
|
||||
m_GridFacesPublished = 0;
|
||||
m_GridSlotsRetracted = 0;
|
||||
for (int i = 0; i < XChartCubeGeometry.FaceCount; i++)
|
||||
{
|
||||
var face = (XChartCubeGeometry.CubeFace)i;
|
||||
if (DrawsGridOnFace(face))
|
||||
{
|
||||
m_GridFacesPublished++;
|
||||
m_GridLinesPublished += PublishGridSlot(board, face, true, m_RatioScratchS, m_LinesS);
|
||||
m_GridLinesPublished += PublishGridSlot(board, face, false, m_RatioScratchT, m_LinesT);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 不画的那个面得把上一轮发出去的那批线**撤下来**:插槽说的是"这里有线可吸",
|
||||
// 只发不收就等于留一批画面上并不存在的线给轴线去吸(规范 §22)
|
||||
m_GridSlotsRetracted += RetractGridSlot(board, face, true);
|
||||
m_GridSlotsRetracted += RetractGridSlot(board, face, false);
|
||||
}
|
||||
}
|
||||
|
||||
private static int RetractGridSlot(IXChartSlotBoard board, XChartCubeGeometry.CubeFace face, bool alongS)
|
||||
=> board.Unpublish(XChartCubeGeometry.GridSlotId(face, alongS)) ? 1 : 0;
|
||||
|
||||
/// <summary>
|
||||
/// 这个面的格线<b>这一帧到底会不会被画出来</b>——发布侧与绘制侧共用这同一个判据。
|
||||
///
|
||||
/// <para>插槽的语义是"这里有格线,可以吸附"。把掩码筛掉的面、背朝观察者的面(开了"只画可见面"时)、
|
||||
/// 面帧退化的面也发出去,轴线就会去吸一条画面上一条都不存在的线——那正是本轮要消灭的
|
||||
/// "关掉一边、另一边还跟着"的形状,只是换到了插槽这一层(规范 §22)。
|
||||
/// 两侧各写一遍过滤迟早会飘,所以只写一次。</para>
|
||||
/// </summary>
|
||||
private bool DrawsGridOnFace(XChartCubeGeometry.CubeFace face)
|
||||
{
|
||||
if (!XChartCubeFaceMaskUtility.HasFace(GridFaces, face)) return false;
|
||||
if (!m_FaceFrames[(int)face].IsValid) return false;
|
||||
if (OnlyVisibleFaces && m_Space != null && !m_Space.IsFaceVisible(face, m_Composed)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
private int PublishGridSlot(IXChartSlotBoard board, XChartCubeGeometry.CubeFace face,
|
||||
bool alongS, float[] ratios, int lines)
|
||||
{
|
||||
int needed = XChartCubeGeometry.GridSlotValueCount(lines);
|
||||
if (needed <= 0) return 0;
|
||||
|
||||
float[] buffer = alongS ? m_GridValuesS : m_GridValuesT;
|
||||
if (buffer == null || buffer.Length != needed)
|
||||
{
|
||||
buffer = new float[needed];
|
||||
if (alongS) m_GridValuesS = buffer;
|
||||
else m_GridValuesT = buffer;
|
||||
}
|
||||
|
||||
if (XChartCubeGeometry.FillGridLineValues(face, alongS, ratios, lines, buffer) <= 0) return 0;
|
||||
board.Publish(XChartCubeGeometry.GridSlotId(face, alongS), XChartSlotMode.Line3D,
|
||||
XChartSlotAccess.ReadOnly, buffer, out string _);
|
||||
return lines;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 这一方向的线位置:绑了维度就用数据范围算刻度(与轴线同源),否则等分。
|
||||
/// <b>那一维没有数据时退回等分而不是不画</b>——网格整片消失比"线回到等分"难查得多。
|
||||
/// </summary>
|
||||
private int BuildGridRatios(bool alongS, float[] ratios)
|
||||
{
|
||||
// 等分那一档的条数:算式只在 XChartGridDivisions 里("沿 S 的线由 T 的分格数决定",探针钉着)
|
||||
int lines = alongS
|
||||
? XChartGridDivisions.LinesAlongS(GridDivisionsS, GridDivisionsT, GridDivisions)
|
||||
: XChartGridDivisions.LinesAlongT(GridDivisionsS, GridDivisionsT, GridDivisions);
|
||||
int dimension = alongS ? GridDimensionS : GridDimensionT;
|
||||
var context = Context;
|
||||
|
||||
if (dimension < 0 || context == null)
|
||||
return XChartGridLines.BuildUniformRatios(lines, ratios);
|
||||
|
||||
double min, max;
|
||||
if (!context.TryResolveDomain(dimension, out min, out max))
|
||||
return XChartGridLines.BuildUniformRatios(lines, ratios);
|
||||
|
||||
// 绑了维度:条数目标=该方向自己那个分格数(0=统一值),刻度由数据范围定
|
||||
int target = Mathf.Max(2, alongS
|
||||
? XChartGridDivisions.Resolve(GridDivisionsS, GridDivisions)
|
||||
: XChartGridDivisions.Resolve(GridDivisionsT, GridDivisions));
|
||||
return XChartGridLines.BuildRatios(min, max, XChartAxisScale.Linear, 0.0,
|
||||
target, 0.0, true, ratios, m_TickScratch);
|
||||
}
|
||||
|
||||
private void EnsureRatioBuffers(int capacity)
|
||||
{
|
||||
if (m_RatioScratchS == null || m_RatioScratchS.Length < capacity) m_RatioScratchS = new float[capacity];
|
||||
if (m_RatioScratchT == null || m_RatioScratchT.Length < capacity) m_RatioScratchT = new float[capacity];
|
||||
}
|
||||
|
||||
// ── 初始化 ────────────────────────────────────────────────
|
||||
|
||||
protected override void OnInitialize()
|
||||
{
|
||||
// 先建填充、后建网格:兄弟顺序就是绘制顺序,网格要在填充之上
|
||||
FillRenderer = CreateChildComponent<XChartMeshFillRenderer>("CubeFaceFill");
|
||||
if (FillRenderer != null) Stretch(FillRenderer.rectTransform);
|
||||
|
||||
GridRenderer = CreateChildComponent<UILineRendererV2>("CubeFaceGrid");
|
||||
if (GridRenderer != null)
|
||||
{
|
||||
GridRenderer.defaultColor = GridColor;
|
||||
GridRenderer.defaultThickness = GridThickness;
|
||||
Stretch(GridRenderer.rectTransform);
|
||||
}
|
||||
|
||||
SetVisible(true);
|
||||
}
|
||||
|
||||
private static void Stretch(RectTransform rect)
|
||||
{
|
||||
rect.anchorMin = Vector2.zero;
|
||||
rect.anchorMax = Vector2.one;
|
||||
rect.offsetMin = Vector2.zero;
|
||||
rect.offsetMax = Vector2.zero;
|
||||
}
|
||||
|
||||
// ── 画家序 ────────────────────────────────────────────────
|
||||
|
||||
/// <summary>按面心深度从远到近排序(画家算法:远的先提交)</summary>
|
||||
private void SortFacesByDepth()
|
||||
{
|
||||
m_SortedFaces.Clear();
|
||||
for (int i = 0; i < XChartCubeGeometry.FaceCount; i++)
|
||||
{
|
||||
m_SortedFaces.Add(new FaceSortEntry
|
||||
{
|
||||
Face = i,
|
||||
Depth = XChartCubeGeometry.GetFaceDepth(m_Composed, (XChartCubeGeometry.CubeFace)i),
|
||||
});
|
||||
}
|
||||
m_SortedFaces.Sort(s_FarToNear);
|
||||
}
|
||||
|
||||
// ── 渲染 ──────────────────────────────────────────────────
|
||||
|
||||
public override void OnRender()
|
||||
{
|
||||
base.OnRender();
|
||||
|
||||
UpdateProjection();
|
||||
|
||||
DrawFill();
|
||||
DrawGrid();
|
||||
}
|
||||
|
||||
private void DrawFill()
|
||||
{
|
||||
m_FacesFilled = 0;
|
||||
if (FillRenderer == null) return;
|
||||
|
||||
FillRenderer.ClearFaces();
|
||||
if (FillFaces != XChartCubeFaceMask.None)
|
||||
{
|
||||
for (int i = 0; i < m_SortedFaces.Count; i++)
|
||||
{
|
||||
var face = (XChartCubeGeometry.CubeFace)m_SortedFaces[i].Face;
|
||||
if (!XChartCubeFaceMaskUtility.HasFace(FillFaces, face)) continue;
|
||||
if (OnlyVisibleFaces && !m_Space.IsFaceVisible(face, m_Composed)) continue;
|
||||
|
||||
XChartFaceFrame frame = m_FaceFrames[(int)face];
|
||||
if (!frame.IsValid) continue;
|
||||
|
||||
FillRenderer.AddFace(frame.CornerST00, frame.CornerST01, frame.CornerST11, frame.CornerST10,
|
||||
FillColor, FillCenterColor);
|
||||
m_FacesFilled++;
|
||||
}
|
||||
}
|
||||
FillRenderer.Apply();
|
||||
}
|
||||
|
||||
private void DrawGrid()
|
||||
{
|
||||
m_GridLinesDrawn = 0;
|
||||
m_GridFacesDrawn = 0;
|
||||
m_FacesOutlined = 0;
|
||||
if (GridRenderer == null) return;
|
||||
|
||||
GridRenderer.ClearAll();
|
||||
if (GridFaces == XChartCubeFaceMask.None) return;
|
||||
|
||||
// 格线的位置**不在这里再算一遍**:用发布那一侧留下的同一份比例数组(PublishGridSlots 里填的),
|
||||
// 于是"画上的线"与"轴线能吸附到的线"永远是同一批。旧版这里自己按 divisions 等分,
|
||||
// 发布侧按刻度算,两条路各画一套(规范 §22)
|
||||
for (int i = 0; i < m_SortedFaces.Count; i++)
|
||||
{
|
||||
var face = (XChartCubeGeometry.CubeFace)m_SortedFaces[i].Face;
|
||||
// 与发布侧同一个判据:哪些面有格线,只许有一处说了算(DrawsGridOnFace)
|
||||
if (!DrawsGridOnFace(face)) continue;
|
||||
|
||||
XChartFaceFrame frame = m_FaceFrames[(int)face];
|
||||
m_GridFacesDrawn++;
|
||||
|
||||
if (ShowFaceOutline)
|
||||
{
|
||||
frame.GetLineAlongS(0f, out Vector2 bottomFrom, out Vector2 bottomTo);
|
||||
frame.GetLineAlongS(1f, out Vector2 topFrom, out Vector2 topTo);
|
||||
frame.GetLineAlongT(0f, out Vector2 leftFrom, out Vector2 leftTo);
|
||||
frame.GetLineAlongT(1f, out Vector2 rightFrom, out Vector2 rightTo);
|
||||
|
||||
GridRenderer.DrawLine(bottomFrom, bottomTo, OutlineColor, GridThickness);
|
||||
GridRenderer.DrawLine(topFrom, topTo, OutlineColor, GridThickness);
|
||||
GridRenderer.DrawLine(leftFrom, leftTo, OutlineColor, GridThickness);
|
||||
GridRenderer.DrawLine(rightFrom, rightTo, OutlineColor, GridThickness);
|
||||
m_GridLinesDrawn += 4;
|
||||
m_FacesOutlined++;
|
||||
}
|
||||
|
||||
for (int d = 0; d < m_LinesS; d++)
|
||||
{
|
||||
frame.GetLineAlongS(m_RatioScratchS[d], out Vector2 from, out Vector2 to);
|
||||
GridRenderer.DrawLine(from, to, GridColor, GridThickness);
|
||||
m_GridLinesDrawn++;
|
||||
}
|
||||
|
||||
for (int d = 0; d < m_LinesT; d++)
|
||||
{
|
||||
frame.GetLineAlongT(m_RatioScratchT[d], out Vector2 from, out Vector2 to);
|
||||
GridRenderer.DrawLine(from, to, GridColor, GridThickness);
|
||||
m_GridLinesDrawn++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
/// <summary>
|
||||
/// 这一帧的自述:<c>fillFaces/gridFaces</c> 是掩码字母(面板上改的数有没有真落到工具上),
|
||||
/// <c>filled=</c> 提交了几面填充,<c>faces=画上/发出</c> 与 <c>linesCheck=</c> 是本元素的**自证**:
|
||||
/// "画上哪几条线"和"发布给轴线哪几条线"必须同一批(差值只许来自描轮廓的 4 条/面)。
|
||||
/// <b>判决由被测进程自己印</b>——"应当相等"写在文档里没人查就等于没写(规范 §22)。
|
||||
/// 他报的"填充面改了没反应"这类症状,也只有这几个数能分清是"没提交"还是"提交了看不见"。
|
||||
/// </summary>
|
||||
internal override void AppendDebugLine(System.Text.StringBuilder sb)
|
||||
{
|
||||
base.AppendDebugLine(sb);
|
||||
int valid = 0;
|
||||
for (int i = 0; i < m_FaceFrames.Length; i++)
|
||||
if (m_FaceFrames[i].IsValid) valid++;
|
||||
// 轮廓那 4 条只画不发(插槽里发的是格线,不含边框),所以期望值要把它加回去
|
||||
int want = m_GridLinesPublished + m_FacesOutlined * 4;
|
||||
sb.Append(" fillFaces=").Append(XChartCubeFaceMaskUtility.Describe(FillFaces))
|
||||
.Append(" gridFaces=").Append(XChartCubeFaceMaskUtility.Describe(GridFaces))
|
||||
.Append(" filled=").Append(m_FacesFilled)
|
||||
.Append(" gridDrawn=").Append(m_GridLinesDrawn)
|
||||
.Append(" gridPublished=").Append(m_GridLinesPublished)
|
||||
.Append(" faces=").Append(m_GridFacesDrawn).Append('/').Append(m_GridFacesPublished)
|
||||
.Append(" retracted=").Append(m_GridSlotsRetracted)
|
||||
.Append(" linesCheck=").Append(m_GridLinesDrawn == want ? "ok"
|
||||
: "DIFF drawn=" + m_GridLinesDrawn + " want=" + want)
|
||||
.Append(" lines=").Append(m_LinesS).Append('/').Append(m_LinesT)
|
||||
.Append(" frames=").Append(valid).Append('/').Append(m_FaceFrames.Length)
|
||||
.Append(" chunks=").Append(FillRenderer != null ? FillRenderer.ChunkCount : -1)
|
||||
.Append(" renderers=").Append(FillRenderer == null ? "fill-NULL" : "ok")
|
||||
.Append('/').Append(GridRenderer == null ? "grid-NULL" : "ok");
|
||||
}
|
||||
#endif
|
||||
|
||||
// ── 清理 ──────────────────────────────────────────────────
|
||||
|
||||
protected override void OnDispose()
|
||||
{
|
||||
// 元素是格线的唯一作者 ⇒ 元素没了,它发过的格线插槽必须跟着没(否则轴线还在吸一批
|
||||
// 已经没有任何东西在画的线,规范 §22)。这里**只撤自己发的**:`mesh.face.<n>` 那批四角
|
||||
// 是立方体本身的几何,线框元素也在发,动它就是拆别人定义的东西。
|
||||
var board = Context?.Slots;
|
||||
if (board != null)
|
||||
{
|
||||
for (int i = 0; i < XChartCubeGeometry.FaceCount; i++)
|
||||
{
|
||||
var face = (XChartCubeGeometry.CubeFace)i;
|
||||
board.Unpublish(XChartCubeGeometry.GridSlotId(face, true));
|
||||
board.Unpublish(XChartCubeGeometry.GridSlotId(face, false));
|
||||
}
|
||||
}
|
||||
|
||||
FillRenderer = null;
|
||||
GridRenderer = null;
|
||||
m_SortedFaces.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4bc06795ba034015814db2d0981a74a7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,296 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using XericLibrary.Runtime.UIGraph;
|
||||
using XericUI.Core;
|
||||
using XericUI.XChart.Space;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 向量场 / 流形箭头元素:一条记录占 <b>6 个数值维</b>——两组三维,
|
||||
/// 按 <see cref="VectorMode"/> 解释成一支箭头(绝对=方向矢量 / 相对=第二个点)。
|
||||
/// 后面可以再接 3~4 维颜色(色相/饱和度/明度 [+不透明度]),见 <see cref="UseColorDimensions"/>。
|
||||
///
|
||||
/// <para><b>两种读法</b>(他 2026-10-25 口述):
|
||||
/// 绝对 ⇒ 后三维是方向本身,起点加方向乘倍率就是末端(螺旋点画出来是一片"刺猬");
|
||||
/// 相对 ⇒ 后三维是第二个点,箭头是第一点指向第二点(一串首尾相接的点读起来就是"连着头转")。</para>
|
||||
///
|
||||
/// <para><b>连续绘制</b>:把所有记录的<b>第一组三维</b>按顺序连成折线(只有最后一段带箭头)。
|
||||
/// 这时"相对/绝对"不适用 ⇒ 配置里那一项隐藏(互斥显示)。</para>
|
||||
///
|
||||
/// <para><b>箭头是自己拼的,没有用库里的 ArrowShape</b>:那套箭头是给曲线的屏幕空间
|
||||
/// 图元用的,而这里的箭头必须跟着立方体姿态走(投影后长短和方向都在变)。做法是
|
||||
/// 只让空间负责两端的投影,杆和头都在<b>像素空间</b>里量出来:杆是一段等宽四边形,
|
||||
/// 头是一个退化四边形(三角形),宽度恒定所以任何姿态下都不会塌成一条线。</para>
|
||||
///
|
||||
/// <para><b>数据只经过中转接口</b>:按维度下标 <see cref="IXChartDataSource.GetValueAt"/> 逐条取,
|
||||
/// 不碰记录数组;条数由 <see cref="MaxArrowCount"/> 兜住,免得十万条记录一口气投影。</para>
|
||||
/// </summary>
|
||||
[AddComponentMenu(XericRegistrationPaths.Chart + "/Mesh/Vector Arrow Tool")]
|
||||
public sealed class ChartToolVectorArrows : XChartToolBase
|
||||
{
|
||||
/// <summary>起点的维度下标(矢量再占三维,颜色再往后)</summary>
|
||||
public int OriginDimension { get; set; }
|
||||
/// <summary>两组三维怎么读成一支箭头</summary>
|
||||
public XChartArrowVectorMode VectorMode { get; set; } = XChartArrowVectorMode.Absolute;
|
||||
/// <summary>把每条记录的第一个点按顺序连成折线(此时相对/绝对不适用)</summary>
|
||||
public bool Continuous { get; set; }
|
||||
/// <summary>方向向量的长度倍率(立方体单位);相对模式下作用在差向量上</summary>
|
||||
public float DirectionScale { get; set; } = 0.25f;
|
||||
/// <summary>最多画多少条(数据再多也只取前 N 条,避免一次性铺满)</summary>
|
||||
public int MaxArrowCount { get; set; } = 512;
|
||||
|
||||
/// <summary>箭头自己的变换(叠在图的空间之上)</summary>
|
||||
public XChartToolTransform Transform { get; set; } = XChartToolTransform.Identity;
|
||||
|
||||
// ── 颜色:可选的三到四个数值维 ────────────────────────────
|
||||
|
||||
/// <summary>是否用额外的数值维给每支箭头上色</summary>
|
||||
public bool UseColorDimensions { get; set; }
|
||||
/// <summary>颜色维的起点(色相、饱和度、明度 [+不透明度])</summary>
|
||||
public int ColorDimension { get; set; } = 6;
|
||||
/// <summary>是否多读一维不透明度</summary>
|
||||
public bool HasAlphaDimension { get; set; }
|
||||
|
||||
public Color ArrowColor { get; set; } = new Color(0.95f, 0.6f, 0.25f, 0.9f);
|
||||
public float ShaftWidth { get; set; } = 2f;
|
||||
public float HeadLength { get; set; } = 10f;
|
||||
public float HeadWidth { get; set; } = 7f;
|
||||
/// <summary>方向为零向量时什么都不画;给个数就画成一个点</summary>
|
||||
public bool ShowZeroVectorsAsDots { get; set; }
|
||||
|
||||
public XChartMeshFillRenderer FillRenderer { get; private set; }
|
||||
|
||||
private Matrix4x4 m_Composed = Matrix4x4.identity;
|
||||
private int m_LastSpaceVersion = -1;
|
||||
private long m_LastDataVersion = -1;
|
||||
private bool m_Dirty = true;
|
||||
|
||||
private readonly List<ArrowFace> m_Faces = new List<ArrowFace>(256);
|
||||
|
||||
// 本帧的自述读数:算了几支 / 表里几个数值维 / 够不够
|
||||
private int m_ArrowsBuilt;
|
||||
private int m_DimensionsHave;
|
||||
private int m_DimensionsNeed;
|
||||
|
||||
private struct ArrowFace
|
||||
{
|
||||
public Vector2 A;
|
||||
public Vector2 B;
|
||||
public Vector2 C;
|
||||
public Vector2 D;
|
||||
public float Depth;
|
||||
public Color Color;
|
||||
}
|
||||
|
||||
private static readonly Comparison<ArrowFace> s_FarToNear =
|
||||
(left, right) => left.Depth.CompareTo(right.Depth);
|
||||
|
||||
public void MarkDirty() => m_Dirty = true;
|
||||
|
||||
/// <summary>
|
||||
/// 这个元素要几个数值维:矢量 6 +(可选)颜色 3。
|
||||
/// <b>不透明度那一维不算硬需求</b>:它多给就多读、没给就退回单色的 alpha——
|
||||
/// 把它算进门槛的话,勾一下"有不透明度"会让整个元素突然一条都不画(维数不够),
|
||||
/// 那是把这一个开关变成了总闸。
|
||||
/// </summary>
|
||||
public int RequiredDimensionCount
|
||||
=> XChartArrowConfig.VectorDimensionCount + (UseColorDimensions ? 3 : 0);
|
||||
|
||||
protected override void OnInitialize()
|
||||
{
|
||||
FillRenderer = CreateChildComponent<XChartMeshFillRenderer>("ArrowFill");
|
||||
if (FillRenderer != null)
|
||||
{
|
||||
var rect = FillRenderer.rectTransform;
|
||||
rect.anchorMin = Vector2.zero;
|
||||
rect.anchorMax = Vector2.one;
|
||||
rect.offsetMin = Vector2.zero;
|
||||
rect.offsetMax = Vector2.zero;
|
||||
}
|
||||
SetVisible(true);
|
||||
}
|
||||
|
||||
public override void OnRender()
|
||||
{
|
||||
base.OnRender();
|
||||
if (FillRenderer == null) return;
|
||||
|
||||
var space = Context?.Space;
|
||||
if (space == null) return;
|
||||
|
||||
long dataVersion = Data != null ? Data.Version : -1;
|
||||
if (!m_Dirty && space.ProjectionVersion == m_LastSpaceVersion && dataVersion == m_LastDataVersion)
|
||||
return;
|
||||
|
||||
m_Dirty = false;
|
||||
m_LastSpaceVersion = space.ProjectionVersion;
|
||||
m_LastDataVersion = dataVersion;
|
||||
m_Composed = space.Compose(Transform);
|
||||
|
||||
BuildFaces(space);
|
||||
|
||||
FillRenderer.ClearFaces();
|
||||
for (int i = 0; i < m_Faces.Count; i++)
|
||||
{
|
||||
ArrowFace face = m_Faces[i];
|
||||
FillRenderer.AddFace(face.A, face.B, face.C, face.D, face.Color, face.Color);
|
||||
}
|
||||
FillRenderer.Apply();
|
||||
}
|
||||
|
||||
private void BuildFaces(ChartToolSpace space)
|
||||
{
|
||||
m_Faces.Clear();
|
||||
m_ArrowsBuilt = 0;
|
||||
m_DimensionsHave = 0;
|
||||
m_DimensionsNeed = RequiredDimensionCount;
|
||||
|
||||
var source = Data;
|
||||
if (source == null || !source.HasData) return;
|
||||
|
||||
m_DimensionsHave = source.NumericDimensionCount;
|
||||
// 维数不够就一条都不画(现象是"加了工具但什么都没显示")——需求与实际两个数都留在报表里,
|
||||
// 面板也会印同一对(规范 §25)
|
||||
if (m_DimensionsHave < OriginDimension + RequiredDimensionCount) return;
|
||||
|
||||
int records = source.RecordCount;
|
||||
int limit = records;
|
||||
if (MaxArrowCount > 0 && MaxArrowCount < limit) limit = MaxArrowCount;
|
||||
|
||||
if (Continuous)
|
||||
{
|
||||
BuildContinuous(source, space, limit);
|
||||
m_Faces.Sort(s_FarToNear);
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < limit; i++)
|
||||
{
|
||||
Vector3 first = ReadPoint(source, i, OriginDimension);
|
||||
Vector3 second = ReadPoint(source, i, OriginDimension + 3);
|
||||
// 两种读法的算式在纯算术里(离线钉着):绝对=后三维就是方向;相对=后三维是第二个点
|
||||
Vector3 tip = XChartVectorFieldMath.Tip(VectorMode, first, second, DirectionScale);
|
||||
|
||||
if (AddArrow(space, first, tip, ColorAt(source, i))) m_ArrowsBuilt++;
|
||||
}
|
||||
|
||||
m_Faces.Sort(s_FarToNear);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连续绘制:把所有记录的第一个点按顺序连成一条折线,<b>只有最后一段带箭头</b>。
|
||||
/// 读的是每组的第一组三维(坐标),后面那几维(方向/第二点/颜色维)在这种模式下不参与——
|
||||
/// 连起来的本来就是空间里的一条路径。
|
||||
/// </summary>
|
||||
private void BuildContinuous(IXChartDataSource source, ChartToolSpace space, int limit)
|
||||
{
|
||||
if (limit < 2) return;
|
||||
|
||||
Vector3 previous = ReadPoint(source, 0, OriginDimension);
|
||||
for (int i = 1; i < limit; i++)
|
||||
{
|
||||
Vector3 current = ReadPoint(source, i, OriginDimension);
|
||||
bool last = i == limit - 1;
|
||||
// 段用"到达点"的颜色(最后一段的箭头也用它):一串颜色维读下来就是沿路径渐变
|
||||
AddArrow(space, previous, current, ColorAt(source, i), last);
|
||||
m_ArrowsBuilt++;
|
||||
previous = current;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>一支箭头 = 杆 +(可选)头。退化(零长度)时按开关画成一个小方块</summary>
|
||||
private bool AddArrow(ChartToolSpace space, Vector3 origin, Vector3 tip, Color color, bool withHead = true)
|
||||
{
|
||||
Vector2 from = space.Project(origin, m_Composed);
|
||||
Vector2 to = space.Project(tip, m_Composed);
|
||||
float depth = (space.DepthOf(origin, m_Composed) + space.DepthOf(tip, m_Composed)) * 0.5f;
|
||||
|
||||
Vector2 shaftA, shaftB, shaftC, shaftD, headA, headB, headC, headD;
|
||||
if (!XChartArrowGeometry.TryBuild(from, to, ShaftWidth, HeadLength, HeadWidth,
|
||||
out shaftA, out shaftB, out shaftC, out shaftD, out headA, out headB, out headC, out headD))
|
||||
{
|
||||
if (!ShowZeroVectorsAsDots) return false;
|
||||
// 退化箭头:画一个极小的四边形,宽度至少一个像素,否则整条向量看不见
|
||||
m_Faces.Add(new ArrowFace
|
||||
{
|
||||
A = from + new Vector2(-1f, -1f),
|
||||
B = from + new Vector2(-1f, 1f),
|
||||
C = from + new Vector2(1f, 1f),
|
||||
D = from + new Vector2(1f, -1f),
|
||||
Depth = depth,
|
||||
Color = color,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
m_Faces.Add(new ArrowFace
|
||||
{
|
||||
A = shaftA, B = shaftB, C = shaftC, D = shaftD, Depth = depth, Color = color,
|
||||
});
|
||||
if (!withHead) return true;
|
||||
|
||||
m_Faces.Add(new ArrowFace
|
||||
{
|
||||
A = headA, B = headB, C = headC, D = headD, Depth = depth, Color = color,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Vector3 ReadPoint(IXChartDataSource source, int record, int dimension)
|
||||
{
|
||||
return new Vector3(
|
||||
(float)source.GetValueAt(record, dimension),
|
||||
(float)source.GetValueAt(record, dimension + 1),
|
||||
(float)source.GetValueAt(record, dimension + 2));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 这一条记录的颜色:没开颜色维就是单色;开了就按
|
||||
/// <c>[色相(0..256 色环), 饱和度(0..1), 明度(0..1), (不透明度 0..1)]</c> 读。
|
||||
/// <b>读不成(维不够或值不是有限数)就退回单色</b>——不是画成黑:黑在深色背景上等于没画,
|
||||
/// 而"退回了单色"这件事在报表的 <c>colors=</c> 读数里看得见。
|
||||
/// </summary>
|
||||
private Color ColorAt(IXChartDataSource source, int record)
|
||||
{
|
||||
if (!UseColorDimensions) return ArrowColor;
|
||||
if (source.NumericDimensionCount < ColorDimension + 3) return ArrowColor;
|
||||
|
||||
float hue = (float)source.GetValueAt(record, ColorDimension);
|
||||
float saturation = (float)source.GetValueAt(record, ColorDimension + 1);
|
||||
float value = (float)source.GetValueAt(record, ColorDimension + 2);
|
||||
float alpha = HasAlphaDimension
|
||||
? (float)source.GetValueAt(record, ColorDimension + 3)
|
||||
: ArrowColor.a;
|
||||
|
||||
Color color;
|
||||
return XChartVectorFieldMath.TryColor(hue, saturation, value, alpha, out color) ? color : ArrowColor;
|
||||
}
|
||||
|
||||
protected override void OnDispose()
|
||||
{
|
||||
FillRenderer = null;
|
||||
m_Faces.Clear();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
/// <summary>
|
||||
/// 报表里点名这一帧:<b>维数(现有/需要)</b>+算了几支+读法+面数/块数。
|
||||
/// "加了工具什么都没显示"这件事只有这一行分得清是"维数不够"还是"算了但没提交"
|
||||
/// (他 2026-10-25 现场;规范 §25)。
|
||||
/// </summary>
|
||||
internal override void AppendDebugLine(System.Text.StringBuilder sb)
|
||||
{
|
||||
base.AppendDebugLine(sb);
|
||||
sb.Append(" dims=").Append(m_DimensionsHave).Append('/').Append(m_DimensionsNeed)
|
||||
.Append(" arrows=").Append(m_ArrowsBuilt)
|
||||
.Append(" mode=").Append(Continuous ? "continuous" : (VectorMode == XChartArrowVectorMode.Relative ? "relative" : "absolute"))
|
||||
.Append(" colors=").Append(UseColorDimensions ? (HasAlphaDimension ? "hsv+a" : "hsv") : "single")
|
||||
.Append(" arrowFaces=").Append(FillRenderer != null ? FillRenderer.FaceCount : 0)
|
||||
.Append(" fillChunks=").Append(FillRenderer != null ? FillRenderer.ChunkCount : 0)
|
||||
.Append(" fillMaxVerts=").Append(FillRenderer != null ? FillRenderer.MaxChunkVertices : 0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bde26796110e5ae4b9a4f3d79fe688c2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 箭头的像素空间几何:<b>杆 + 头</b>两组四边形。
|
||||
///
|
||||
/// <para>放在像素空间算是有意的:箭头的<b>宽度</b>要恒定(不管视角怎么转、向量投得多短,
|
||||
/// 头都得是个看得见的头),而长度和方向已经由 <see cref="Space.ChartToolSpace"/> 投影给出来了。
|
||||
/// 所以这里只做"沿单位方向量长度、按垂直方向量宽度"这一件事,纯数学、离线可验。</para>
|
||||
///
|
||||
/// <para><see cref="HeadLength"/> 会被钳到向量屏幕长度的 60%,因此短箭头的杆不会被头穿过去;
|
||||
/// 屏幕长度接近 0 时返回 false,由调用方决定是画个点还是什么都不画。</para>
|
||||
/// </summary>
|
||||
public static class XChartArrowGeometry
|
||||
{
|
||||
/// <summary>杆最长能占屏幕长度的比例上限之外,头也不得超过这个比例</summary>
|
||||
public const float HeadLengthRatioCap = 0.6f;
|
||||
|
||||
/// <summary>屏幕长度小于这个值就当"没有方向",交给调用方的退化分支。</summary>
|
||||
public const float MinScreenLength = 1e-3f;
|
||||
|
||||
/// <summary>杆的四角(沿箭头方向绕一圈)</summary>
|
||||
public static void BuildShaft(Vector2 from, Vector2 unit, Vector2 side, float shaftEndDistance,
|
||||
float width, out Vector2 a, out Vector2 b, out Vector2 c, out Vector2 d)
|
||||
{
|
||||
float half = width * 0.5f;
|
||||
Vector2 end = from + unit * shaftEndDistance;
|
||||
a = from + side * half;
|
||||
b = end + side * half;
|
||||
c = end - side * half;
|
||||
d = from - side * half;
|
||||
}
|
||||
|
||||
/// <summary>头的四角:两个底角 + 尖点(尖点重复一次,四边形即三角形)</summary>
|
||||
public static void BuildHead(Vector2 baseCenter, Vector2 tip, Vector2 side, float headWidth,
|
||||
out Vector2 a, out Vector2 b, out Vector2 c, out Vector2 d)
|
||||
{
|
||||
float half = headWidth * 0.5f;
|
||||
a = baseCenter + side * half;
|
||||
b = tip;
|
||||
c = tip;
|
||||
d = baseCenter - side * half;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 一条箭头的完整拆分。<paramref name="to"/> 是屏幕上的尖点。
|
||||
/// 返回 false 表示屏幕长度退化。
|
||||
/// </summary>
|
||||
public static bool TryBuild(Vector2 from, Vector2 to, float shaftWidth, float headLength,
|
||||
float headWidth, out Vector2 shaftA, out Vector2 shaftB, out Vector2 shaftC, out Vector2 shaftD,
|
||||
out Vector2 headA, out Vector2 headB, out Vector2 headC, out Vector2 headD)
|
||||
{
|
||||
shaftA = shaftB = shaftC = shaftD = headA = headB = headC = headD = Vector2.zero;
|
||||
|
||||
Vector2 axis = to - from;
|
||||
float length = axis.magnitude;
|
||||
if (length < MinScreenLength) return false;
|
||||
|
||||
Vector2 unit = axis / length;
|
||||
Vector2 side = new Vector2(-unit.y, unit.x);
|
||||
float head = Mathf.Min(Mathf.Max(0f, headLength), length * HeadLengthRatioCap);
|
||||
|
||||
BuildShaft(from, unit, side, length - head, shaftWidth, out shaftA, out shaftB, out shaftC, out shaftD);
|
||||
BuildHead(from + unit * (length - head), to, side, headWidth, out headA, out headB, out headC, out headD);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e7522065536fdf438d0e378d47ca462
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 立方体六面的选择掩码。用它可以精确控制"渲染哪些面"(填充与网格各自独立一份掩码)。
|
||||
///
|
||||
/// <para><b>面编号契约(固定不变)</b></para>
|
||||
/// <code>
|
||||
/// a = 前 Front (法线 +Z) d = 下 Down (法线 -Y)
|
||||
/// b = 后 Back (法线 -Z) e = 左 Left (法线 -X)
|
||||
/// c = 上 Up (法线 +Y) f = 右 Right (法线 +X)
|
||||
/// </code>
|
||||
/// 字母按<b>语义</b>绑定,与内部遍历顺序无关:<see cref="XChartCubeGeometry.CubeFace"/> 的枚举值
|
||||
/// 顺序将来即使调整,字母也不会跟着漂。
|
||||
///
|
||||
/// <para><b>注意</b>:掩码是"画哪些面"的唯一依据,<b>不看可见性</b>——图表里的背景网格
|
||||
/// 恰恰常画在背朝观察者的那几面(后墙 / 底面)。要只画朝向观察者的面请自行用
|
||||
/// <see cref="XChartCubeGeometry.IsFaceVisible"/> 过滤。</para>
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum XChartCubeFaceMask
|
||||
{
|
||||
None = 0,
|
||||
/// <summary>a 前</summary>
|
||||
Front = 1 << 0,
|
||||
/// <summary>b 后</summary>
|
||||
Back = 1 << 1,
|
||||
/// <summary>c 上</summary>
|
||||
Up = 1 << 2,
|
||||
/// <summary>d 下</summary>
|
||||
Down = 1 << 3,
|
||||
/// <summary>e 左</summary>
|
||||
Left = 1 << 4,
|
||||
/// <summary>f 右</summary>
|
||||
Right = 1 << 5,
|
||||
|
||||
All = Front | Back | Up | Down | Left | Right,
|
||||
|
||||
/// <summary>三面(后 + 下 + 左):常见的"背景墙"一组</summary>
|
||||
BackWalls = Back | Down | Left,
|
||||
}
|
||||
|
||||
/// <summary>面掩码与面之间的换算(与 <see cref="XChartCubeFaceMask"/> 的字母契约配套)</summary>
|
||||
public static class XChartCubeFaceMaskUtility
|
||||
{
|
||||
/// <summary>面的稳定字母(a=前 b=后 c=上 d=下 e=左 f=右)</summary>
|
||||
public static char GetLetter(XChartCubeGeometry.CubeFace face)
|
||||
{
|
||||
switch (face)
|
||||
{
|
||||
case XChartCubeGeometry.CubeFace.Front: return 'a';
|
||||
case XChartCubeGeometry.CubeFace.Back: return 'b';
|
||||
case XChartCubeGeometry.CubeFace.Top: return 'c';
|
||||
case XChartCubeGeometry.CubeFace.Bottom: return 'd';
|
||||
case XChartCubeGeometry.CubeFace.Left: return 'e';
|
||||
default: return 'f';
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>字母反查面</summary>
|
||||
public static bool TryParseLetter(char letter, out XChartCubeGeometry.CubeFace face)
|
||||
{
|
||||
switch (char.ToLowerInvariant(letter))
|
||||
{
|
||||
case 'a': face = XChartCubeGeometry.CubeFace.Front; return true;
|
||||
case 'b': face = XChartCubeGeometry.CubeFace.Back; return true;
|
||||
case 'c': face = XChartCubeGeometry.CubeFace.Top; return true;
|
||||
case 'd': face = XChartCubeGeometry.CubeFace.Bottom; return true;
|
||||
case 'e': face = XChartCubeGeometry.CubeFace.Left; return true;
|
||||
case 'f': face = XChartCubeGeometry.CubeFace.Right; return true;
|
||||
default: face = XChartCubeGeometry.CubeFace.Front; return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>单个面对应的掩码位</summary>
|
||||
public static XChartCubeFaceMask GetMask(XChartCubeGeometry.CubeFace face)
|
||||
{
|
||||
switch (face)
|
||||
{
|
||||
case XChartCubeGeometry.CubeFace.Front: return XChartCubeFaceMask.Front;
|
||||
case XChartCubeGeometry.CubeFace.Back: return XChartCubeFaceMask.Back;
|
||||
case XChartCubeGeometry.CubeFace.Top: return XChartCubeFaceMask.Up;
|
||||
case XChartCubeGeometry.CubeFace.Bottom: return XChartCubeFaceMask.Down;
|
||||
case XChartCubeGeometry.CubeFace.Left: return XChartCubeFaceMask.Left;
|
||||
default: return XChartCubeFaceMask.Right;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>掩码里是否包含某个面</summary>
|
||||
public static bool HasFace(XChartCubeFaceMask mask, XChartCubeGeometry.CubeFace face)
|
||||
=> (mask & GetMask(face)) != 0;
|
||||
|
||||
/// <summary>
|
||||
/// 掩码的<b>字母写法</b>(<c>a=前 b=后 c=上 d=下 e=左 f=右</c>,按面号升序),空掩码写作 <c>-</c>。
|
||||
///
|
||||
/// <para>报表与调试行要说"这一帧工具手上到底是哪几面",直接印枚举的<b>数字</b>没用——
|
||||
/// 面板上的掩码下拉显示的是字母组合,数字对不上眼睛;而印枚举名(<c>Back | Down | Left</c>)
|
||||
/// 又会随枚举定义漂移。字母是这份契约里唯一钉住的语义(见类型注释),所以读数用它。</para>
|
||||
/// </summary>
|
||||
public static string Describe(XChartCubeFaceMask mask)
|
||||
{
|
||||
var builder = new System.Text.StringBuilder();
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
var face = (XChartCubeGeometry.CubeFace)i;
|
||||
if (!HasFace(mask, face)) continue;
|
||||
builder.Append(GetLetter(face));
|
||||
}
|
||||
return builder.Length > 0 ? builder.ToString() : "-";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 95a4a48449654bb4b8d72924f5e50632
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,395 @@
|
||||
using UnityEngine;
|
||||
using XericUI.XChart.Space;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 伪立方体的几何来源:8 顶点 / 12 棱 / 6 面,以及"把面算成屏幕四边形"的推导方法。
|
||||
///
|
||||
/// <para><b>为什么它决定协议</b></para>
|
||||
/// 我们要的不是"立方体",而是"任意一个面在屏幕上是哪四个点"。把它写出来就能看清协议该长什么样:
|
||||
/// <code>
|
||||
/// 面局部 3D : p3(s,t) = O3 + s * S3 + t * T3 (s,t ∈ [0,1])
|
||||
/// 投影 : screen = (R * p3).xy * scale + offset ← 正交投影,丢 Z
|
||||
/// 合成 : screen = O2 + s * S2 + t * T2 ← 关于 (s,t) 是<b>仿射</b>的
|
||||
/// </code>
|
||||
/// 也就是说:正交投影下,每个面的屏幕映射必定是仿射(平行四边形),只需要"原点 + 两个基向量";
|
||||
/// 一旦换成透视投影(除以 w),它升级成一般四边形/单应。协议因此只需四个角点,
|
||||
/// 不需要知道立方体、pitch/yaw、透视——详见 <see cref="XChartFaceFrame"/>。</para>
|
||||
///
|
||||
/// <para><b>坐标与单位</b>:立方体中心在原点、半边长 1(顶点在 <c>[-1,1]³</c>);S3/T3 是<b>整条边</b>
|
||||
/// 向量(长度 2),所以 s,t 直接就是 [0,1] 的面参数。因为顶点就在 ±1,投影缩放
|
||||
/// <c>scale</c> 在"半边长口径"下<b>就等于半边长(像素)</b>——见
|
||||
/// <see cref="XChartCubeView"/>,旋转与尺寸都收在那里,本类只负责顶点和可见性判定。</para>
|
||||
///
|
||||
/// <para><b>与 ChartToolCubeBg / ChartToolMeshBg 的关系</b>:面顺序与本类的枚举一致
|
||||
/// (Front/Back/Right/Left/Top/Bottom),两个工具都消费本类的顶点/面/棱表,
|
||||
/// 不再各自带一份私有副本。</para>
|
||||
/// </summary>
|
||||
public static class XChartCubeGeometry
|
||||
{
|
||||
/// <summary>面的枚举,顺序与 <see cref="FaceCount"/> 个面帧的输出顺序一致</summary>
|
||||
public enum CubeFace
|
||||
{
|
||||
/// <summary>法线 (0,0,1),面参数 (s,t) = (X,Y)</summary>
|
||||
Front = 0,
|
||||
/// <summary>法线 (0,0,-1),面参数 (s,t) = (-X,Y)</summary>
|
||||
Back = 1,
|
||||
/// <summary>法线 (1,0,0),面参数 (s,t) = (-Z,Y)</summary>
|
||||
Right = 2,
|
||||
/// <summary>法线 (-1,0,0),面参数 (s,t) = (Z,Y)</summary>
|
||||
Left = 3,
|
||||
/// <summary>法线 (0,1,0),面参数 (s,t) = (X,Z)(当作地板用)</summary>
|
||||
Top = 4,
|
||||
/// <summary>法线 (0,-1,0),面参数 (s,t) = (X,Z)</summary>
|
||||
Bottom = 5,
|
||||
}
|
||||
|
||||
public const int FaceCount = 6;
|
||||
public const int EdgeCount = 12;
|
||||
|
||||
// ── 顶点与棱 ──────────────────────────────────────────────
|
||||
|
||||
/// <summary>8 个顶点(中心原点、半边长 1)</summary>
|
||||
public static readonly Vector3[] Vertices =
|
||||
{
|
||||
new Vector3(-1, -1, -1), // 0 后下左
|
||||
new Vector3( 1, -1, -1), // 1 后下右
|
||||
new Vector3( 1, 1, -1), // 2 后上右
|
||||
new Vector3(-1, 1, -1), // 3 后上左
|
||||
new Vector3(-1, -1, 1), // 4 前下左
|
||||
new Vector3( 1, -1, 1), // 5 前下右
|
||||
new Vector3( 1, 1, 1), // 6 前上右
|
||||
new Vector3(-1, 1, 1), // 7 前上左
|
||||
};
|
||||
|
||||
/// <summary>12 条棱(顶点索引对):0-3 后面,4-7 前面,8-11 连接棱</summary>
|
||||
public static readonly int[,] Edges =
|
||||
{
|
||||
{0,1}, {1,2}, {2,3}, {3,0},
|
||||
{4,5}, {5,6}, {6,7}, {7,4},
|
||||
{0,4}, {1,5}, {2,6}, {3,7},
|
||||
};
|
||||
|
||||
// ── 面帧(3D) ────────────────────────────────────────────
|
||||
|
||||
private static readonly Vector3[] s_FaceNormal = new Vector3[FaceCount];
|
||||
private static readonly Vector3[] s_FaceOrigin = new Vector3[FaceCount]; // (s,t)=(0,0)
|
||||
private static readonly Vector3[] s_FaceAxisS = new Vector3[FaceCount]; // 整条边,长度 2
|
||||
private static readonly Vector3[] s_FaceAxisT = new Vector3[FaceCount]; // 整条边,长度 2
|
||||
|
||||
// 每个面的四个角点对应的顶点索引,顺序与 (s,t) 角点一致: (0,0) (0,1) (1,1) (1,0)
|
||||
// 不手写这张表:由 3D 角点与 Vertices 比对得出(探针里有"每面四角恰落在顶点上"的断言兜底)
|
||||
private static readonly int[,] s_FaceVertexIndices = new int[FaceCount, 4];
|
||||
|
||||
/// <summary>棱 -> 相邻面(每面两条). 由面顶点索引反查得出,供"棱是否朝向观察者"判定</summary>
|
||||
private static readonly int[,] s_EdgeAdjacentFaces = new int[EdgeCount, 2];
|
||||
|
||||
static XChartCubeGeometry()
|
||||
{
|
||||
for (int i = 0; i < FaceCount; i++)
|
||||
BuildFace3D((CubeFace)i, out s_FaceNormal[i], out s_FaceOrigin[i], out s_FaceAxisS[i], out s_FaceAxisT[i]);
|
||||
|
||||
for (int f = 0; f < FaceCount; f++)
|
||||
{
|
||||
for (int k = 0; k < 4; k++)
|
||||
{
|
||||
float s = (k == 2 || k == 3) ? 1f : 0f;
|
||||
float t = (k == 1 || k == 2) ? 1f : 0f;
|
||||
s_FaceVertexIndices[f, k] = FindVertexIndex(GetFacePoint3D((CubeFace)f, s, t));
|
||||
}
|
||||
}
|
||||
|
||||
for (int e = 0; e < EdgeCount; e++)
|
||||
{
|
||||
s_EdgeAdjacentFaces[e, 0] = -1;
|
||||
s_EdgeAdjacentFaces[e, 1] = -1;
|
||||
int slot = 0;
|
||||
for (int f = 0; f < FaceCount && slot < 2; f++)
|
||||
{
|
||||
if (!FaceContainsEdge(f, Edges[e, 0], Edges[e, 1])) continue;
|
||||
s_EdgeAdjacentFaces[e, slot++] = f;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int FindVertexIndex(Vector3 point)
|
||||
{
|
||||
int best = -1;
|
||||
float bestDistance = 1e-6f;
|
||||
for (int i = 0; i < Vertices.Length; i++)
|
||||
{
|
||||
float d = (point - Vertices[i]).sqrMagnitude;
|
||||
if (d <= bestDistance) { bestDistance = d; best = i; }
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
private static bool FaceContainsEdge(int face, int v0, int v1)
|
||||
{
|
||||
return FaceContainsVertex(face, v0) && FaceContainsVertex(face, v1);
|
||||
}
|
||||
|
||||
private static bool FaceContainsVertex(int face, int vertex)
|
||||
{
|
||||
for (int k = 0; k < 4; k++)
|
||||
if (s_FaceVertexIndices[face, k] == vertex) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 由"外法线 + 参考上方向"导出面的 3D 帧。
|
||||
/// 一致的右手规则:right = up x n,t = n x right,原点取该面的 (0,0) 角点。
|
||||
/// 侧面用世界 Y 当参考上方向,顶/底面用 Z(这样顶面天然是 (X,Z) 地板参数化)。
|
||||
/// </summary>
|
||||
private static void BuildFace3D(CubeFace face, out Vector3 normal, out Vector3 origin,
|
||||
out Vector3 axisS, out Vector3 axisT)
|
||||
{
|
||||
normal = GetFaceNormalVector(face);
|
||||
Vector3 upReference = (face == CubeFace.Top || face == CubeFace.Bottom)
|
||||
? new Vector3(0f, 0f, 1f)
|
||||
: new Vector3(0f, 1f, 0f);
|
||||
|
||||
Vector3 right = Vector3.Cross(upReference, normal).normalized;
|
||||
Vector3 up = Vector3.Cross(normal, right).normalized;
|
||||
|
||||
// 面在 ±1 的立方体上,所以 (0,0) 角点 = 法线方向的面 + 反向的两个轴
|
||||
origin = normal - right - up;
|
||||
axisS = right * 2f;
|
||||
axisT = up * 2f;
|
||||
}
|
||||
|
||||
private static Vector3 GetFaceNormalVector(CubeFace face)
|
||||
{
|
||||
switch (face)
|
||||
{
|
||||
case CubeFace.Front: return new Vector3(0f, 0f, 1f);
|
||||
case CubeFace.Back: return new Vector3(0f, 0f, -1f);
|
||||
case CubeFace.Right: return new Vector3(1f, 0f, 0f);
|
||||
case CubeFace.Left: return new Vector3(-1f, 0f, 0f);
|
||||
case CubeFace.Top: return new Vector3(0f, 1f, 0f);
|
||||
default: return new Vector3(0f, -1f, 0f);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>面的外法线(立方体局部空间)</summary>
|
||||
public static Vector3 GetFaceNormal(CubeFace face) => s_FaceNormal[(int)face];
|
||||
|
||||
/// <summary>面参数 (0,0) 对应的 3D 点</summary>
|
||||
public static Vector3 GetFaceOrigin(CubeFace face) => s_FaceOrigin[(int)face];
|
||||
|
||||
/// <summary>面参数 s 方向的 3D 边向量(长度 2)</summary>
|
||||
public static Vector3 GetFaceAxisS(CubeFace face) => s_FaceAxisS[(int)face];
|
||||
|
||||
/// <summary>面参数 t 方向的 3D 边向量(长度 2)</summary>
|
||||
public static Vector3 GetFaceAxisT(CubeFace face) => s_FaceAxisT[(int)face];
|
||||
|
||||
/// <summary>
|
||||
/// 面片四个角(<b>单位立方体</b>,±1 就是边界),角序与面帧完全一致
|
||||
/// (ST00→(s0,t0)、ST01→(s0,t1)、ST11→(s1,t1)、ST10→(s1,t0))。
|
||||
/// 定义网格的工具把这四个点发布成 `Quad3D` 插槽,消费者用自己的矩阵投影它们即可——
|
||||
/// 这条与 <see cref="BuildFaceFrames"/> 的角序必须一直一致(探针把两者对拍)。
|
||||
/// </summary>
|
||||
public static void GetFaceCorners(CubeFace face, out Vector3 cornerST00, out Vector3 cornerST01,
|
||||
out Vector3 cornerST11, out Vector3 cornerST10)
|
||||
{
|
||||
Vector3 origin = s_FaceOrigin[(int)face];
|
||||
Vector3 axisS = s_FaceAxisS[(int)face];
|
||||
Vector3 axisT = s_FaceAxisT[(int)face];
|
||||
cornerST00 = origin;
|
||||
cornerST01 = origin + axisT;
|
||||
cornerST11 = origin + axisT + axisS;
|
||||
cornerST10 = origin + axisS;
|
||||
}
|
||||
|
||||
/// <summary>插槽 id 约定:定义网格的工具把每个面发布成 <c>mesh.face.<面号></c></summary>
|
||||
public const string FaceSlotPrefix = "mesh.face.";
|
||||
|
||||
/// <summary>某个面的插槽 id</summary>
|
||||
public static string FaceSlotId(CubeFace face) => FaceSlotPrefix + (int)face;
|
||||
|
||||
/// <summary>
|
||||
/// 格线插槽 id:<c>mesh.face.<面号>.grid.S</c>(沿 S 的等 t 线)/
|
||||
/// <c>.grid.T</c>(沿 T 的等 s 线)。格线由<b>定义网格的那个工具</b>发布,轴线按 id 取用——
|
||||
/// 于是"刻度落在格线上"这件事有一个共同的来源,而不是两边各自算一遍。
|
||||
/// </summary>
|
||||
public static string GridSlotId(CubeFace face, bool alongS)
|
||||
=> FaceSlotPrefix + (int)face + (alongS ? ".grid.S" : ".grid.T");
|
||||
|
||||
/// <summary>
|
||||
/// 一组格线要占多少个 float(<paramref name="lineCount"/> 组 × 每组两个端点 × 3 个分量):
|
||||
/// 两个背景都按它开缓冲,"值布局"只有这一个来源。
|
||||
/// </summary>
|
||||
public static int GridSlotValueCount(int lineCount)
|
||||
{
|
||||
if (lineCount <= 0) return 0;
|
||||
return lineCount * XChartSlotModes.PointsPerGroup(XChartSlotMode.Line3D) * 3;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按给定的一组面参数比例把格线写进 <paramref name="values"/>:
|
||||
/// <b>每组 6 个 float(两个单位立方体端点)</b>,共 <paramref name="lineCount"/> 组。
|
||||
///
|
||||
/// <para>比例来自 <see cref="XChartGridLines"/>(等分,或按数据范围算出来的刻度位置)——
|
||||
/// 这里只管"比例 → 端点",于是"格线在哪儿"只有一个实现、两个背景共用,离线探针钉得住
|
||||
/// (轴线对齐格线的判据也靠它同源)。</para>
|
||||
///
|
||||
/// <para><b>沿 S 的线 = 等 t 线</b>(从 (0,t) 到 (1,t)),沿 T 的线 = 等 s 线——
|
||||
/// 与面帧 <c>GetLineAlongS/GetLineAlongT</c> 同一条约定(写反了就是整片网格转 90°)。</para>
|
||||
/// </summary>
|
||||
public static int FillGridLineValues(CubeFace face, bool alongS, float[] ratios, int lineCount, float[] values)
|
||||
{
|
||||
if (values == null || ratios == null || lineCount <= 0) return 0;
|
||||
if (values.Length < GridSlotValueCount(lineCount)) return 0;
|
||||
|
||||
int written = 0;
|
||||
for (int i = 0; i < lineCount; i++)
|
||||
{
|
||||
float ratio = ratios[i];
|
||||
Vector3 from = alongS ? GetFacePoint3D(face, 0f, ratio) : GetFacePoint3D(face, ratio, 0f);
|
||||
Vector3 to = alongS ? GetFacePoint3D(face, 1f, ratio) : GetFacePoint3D(face, ratio, 1f);
|
||||
|
||||
int at = i * 6;
|
||||
values[at + 0] = from.x; values[at + 1] = from.y; values[at + 2] = from.z;
|
||||
values[at + 3] = to.x; values[at + 4] = to.y; values[at + 5] = to.z;
|
||||
written++;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 面参数 (s,t) -> 立方体局部 3D 点
|
||||
/// </summary>
|
||||
public static Vector3 GetFacePoint3D(CubeFace face, float s, float t)
|
||||
=> s_FaceOrigin[(int)face] + s_FaceAxisS[(int)face] * s + s_FaceAxisT[(int)face] * t;
|
||||
|
||||
/// <summary>
|
||||
/// 立方体局部 3D 点 → 面参数 (s,t):<b>与 <see cref="GetFacePoint3D"/> 互为逆</b>。
|
||||
/// 面基是单位正交的(角的定义就是 origin / +axisT / +axisT+axisS / +axisS),所以直接点积即可。
|
||||
///
|
||||
/// <para>谁需要它:轴线要按网格<b>发布的线</b>(单位立方体端点)反解出"这条线在面参数的哪儿",
|
||||
/// 才能把刻度吸附上去——于是"网格定义、轴线跟随"落地,而两边不各算一遍。</para>
|
||||
/// </summary>
|
||||
public static void GetFaceParameter(CubeFace face, Vector3 point, out float s, out float t)
|
||||
{
|
||||
Vector3 axisS = s_FaceAxisS[(int)face];
|
||||
Vector3 axisT = s_FaceAxisT[(int)face];
|
||||
Vector3 relative = point - s_FaceOrigin[(int)face];
|
||||
|
||||
// 面轴是"整条边",长度 2(±1 的立方体)⇒ 点积后要**除以 |轴|²** 才是参数。
|
||||
// 少了这一步,参数会整整齐齐地放大 4 倍(看着像对的,其实全偏了)。
|
||||
float lengthS = axisS.sqrMagnitude;
|
||||
float lengthT = axisT.sqrMagnitude;
|
||||
s = lengthS > 1e-8f ? Vector3.Dot(relative, axisS) / lengthS : 0f;
|
||||
t = lengthT > 1e-8f ? Vector3.Dot(relative, axisT) / lengthT : 0f;
|
||||
}
|
||||
|
||||
/// <summary>面的第 corner 个角点(0=(0,0) 1=(0,1) 2=(1,1) 3=(1,0))对应的顶点索引</summary>
|
||||
public static int GetFaceVertexIndex(CubeFace face, int corner)
|
||||
=> s_FaceVertexIndices[(int)face, Mathf.Clamp(corner, 0, 3)];
|
||||
|
||||
// ── 投影 ──────────────────────────────────────────────────
|
||||
|
||||
/// <summary>立方体局部 3D 点 -> 图表局部像素(旋转 -> 正交投影丢 Z -> 缩放平移)</summary>
|
||||
public static Vector2 Project(Matrix4x4 rotation, float scale, Vector2 offset, Vector3 point)
|
||||
{
|
||||
Vector3 rotated = rotation.MultiplyPoint3x4(point);
|
||||
return new Vector2(rotated.x * scale + offset.x, rotated.y * scale + offset.y);
|
||||
}
|
||||
|
||||
/// <summary>投影后的包围盒(旋转 + 正交投影丢 Z 后的 XY 范围)。半边长为 1 时范围在 ±√2 内</summary>
|
||||
public static void ComputeProjectedBounds(Matrix4x4 rotation, out Vector2 min, out Vector2 max)
|
||||
{
|
||||
min = new Vector2(float.MaxValue, float.MaxValue);
|
||||
max = new Vector2(float.MinValue, float.MinValue);
|
||||
for (int i = 0; i < Vertices.Length; i++)
|
||||
{
|
||||
Vector3 rotated = rotation.MultiplyPoint3x4(Vertices[i]);
|
||||
if (rotated.x < min.x) min.x = rotated.x;
|
||||
if (rotated.x > max.x) max.x = rotated.x;
|
||||
if (rotated.y < min.y) min.y = rotated.y;
|
||||
if (rotated.y > max.y) max.y = rotated.y;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用四元数投影:先换成旋转矩阵,再走 <see cref="Project"/>。
|
||||
/// 缩放/平移的算法(半边长口径 vs 视口适配口径)不在这里,见
|
||||
/// <see cref="XChartCubeView.ComputeProjection(Quaternion,float,Vector2,XChartCubeSizeMode,out float,out Vector2)"/>。
|
||||
/// </summary>
|
||||
public static Vector2 Project(Quaternion rotation, float scale, Vector2 offset, Vector3 point)
|
||||
=> Project(XChartCubeView.ToMatrix(rotation), scale, offset, point);
|
||||
|
||||
// ── 面帧(屏幕) ──────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// 算出六个面在图表局部像素空间的四边形——这就是"协议"的来源。
|
||||
/// 输出顺序与 <see cref="CubeFace"/> 一致。
|
||||
/// </summary>
|
||||
public static void BuildFaceFrames(Matrix4x4 rotation, float scale, Vector2 offset,
|
||||
XChartFaceFrame[] output)
|
||||
{
|
||||
if (output == null || output.Length < FaceCount)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < FaceCount; i++)
|
||||
{
|
||||
var face = (CubeFace)i;
|
||||
Vector3 o3 = s_FaceOrigin[i];
|
||||
Vector3 s3 = s_FaceAxisS[i];
|
||||
Vector3 t3 = s_FaceAxisT[i];
|
||||
|
||||
Vector2 c00 = Project(rotation, scale, offset, o3);
|
||||
Vector2 c01 = Project(rotation, scale, offset, o3 + t3);
|
||||
Vector2 c11 = Project(rotation, scale, offset, o3 + t3 + s3);
|
||||
Vector2 c10 = Project(rotation, scale, offset, o3 + s3);
|
||||
|
||||
XChartFaceFrame frame = XChartFaceFrame.FromCorners(c00, c01, c11, c10);
|
||||
frame.FaceId = i;
|
||||
output[i] = frame;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 一次把"四元数 + 半边长 + 口径"算成完整的投影状态与六个面帧。
|
||||
/// scale/offset 一并吐出来,数据类元素(散点等)要和背景共用同一份,不能各算一次。
|
||||
/// </summary>
|
||||
public static XChartFaceFrame[] BuildFaceFrames(Quaternion rotation, float halfEdge,
|
||||
Vector2 centerOffset, XChartCubeSizeMode mode, out float scale, out Vector2 offset)
|
||||
{
|
||||
Matrix4x4 matrix = XChartCubeView.ToMatrix(rotation);
|
||||
XChartCubeView.ComputeProjection(matrix, halfEdge, centerOffset, mode, out scale, out offset);
|
||||
var frames = new XChartFaceFrame[FaceCount];
|
||||
BuildFaceFrames(matrix, scale, offset, frames);
|
||||
return frames;
|
||||
}
|
||||
|
||||
/// <summary>面的朝向是否对着观察者(旋转后法线 Z > 0)——填充面必须用它判可见性</summary>
|
||||
public static bool IsFaceVisible(Matrix4x4 rotation, CubeFace face)
|
||||
=> rotation.MultiplyVector(s_FaceNormal[(int)face]).z > 0f;
|
||||
|
||||
/// <summary>
|
||||
/// 棱是否朝向观察者:它相邻的两个面里只要有一个可见就算。
|
||||
/// 这样棱和面用同一套口径(旧实现里棱按平均 Z 判、网格线按法线判,两套口径)。
|
||||
/// </summary>
|
||||
public static bool IsEdgeVisible(Matrix4x4 rotation, int edgeIndex)
|
||||
{
|
||||
for (int slot = 0; slot < 2; slot++)
|
||||
{
|
||||
int face = s_EdgeAdjacentFaces[edgeIndex, slot];
|
||||
if (face < 0) continue;
|
||||
if (IsFaceVisible(rotation, (CubeFace)face)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>面的旋转后 Z(面心深度),供"先画远的再画近的"排序</summary>
|
||||
public static float GetFaceDepth(Matrix4x4 rotation, CubeFace face)
|
||||
{
|
||||
Vector3 center = s_FaceOrigin[(int)face]
|
||||
+ s_FaceAxisS[(int)face] * 0.5f
|
||||
+ s_FaceAxisT[(int)face] * 0.5f;
|
||||
return rotation.MultiplyPoint3x4(center).z;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 894f93de698f45ae8c22edc26599b938
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,304 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 立方体尺寸的两种口径。两者的差别正是"旋转时立方体会不会变小"。
|
||||
/// </summary>
|
||||
public enum XChartCubeSizeMode
|
||||
{
|
||||
/// <summary>
|
||||
/// <b>半边长口径(默认)</b>:给定值是立方体<b>真实边长的一半</b>(也等于面心到中心的距离,
|
||||
/// 即内切球半径)。投影缩放与旋转<b>无关</b>:怎么转,边长都是 <c>2 * halfEdge</c> 像素。
|
||||
/// </summary>
|
||||
HalfEdgeLength,
|
||||
|
||||
/// <summary>
|
||||
/// <b>视口适配口径</b>:把<b>投影包围盒</b>的最长边缩放到 <c>2 * halfEdge</c> 像素,
|
||||
/// 于是立方体永远刚好铺满那块矩形。代价是尺寸随旋转变化:包围盒在某屏幕轴上的半长是
|
||||
/// <c>h * (|ux| + |uy| + |uz|)</c>(<c>u</c> 是该屏幕轴在立方体局部空间的像,
|
||||
/// 即 <c>R^T</c> 的那一行),正对时为 <c>h</c>,某条体对角线对着该轴时最大、为 <c>√3 h</c>。
|
||||
/// 同一份配置下的实际尺度因此只有设定值的 <c>1/√3 ≈ 0.577</c>(等距预设下垂直方向半长
|
||||
/// <c>√(8/3) h</c>,即 <c>≈ 0.612</c> 倍)——这就是"越转越小"。
|
||||
/// 需要"填满指定的矩形"时才用它。
|
||||
/// </summary>
|
||||
FitToViewport,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 视角预设:每个值都对应<b>一个四元数</b>(见 <see cref="XChartCubeView.RotationOf"/>),不是角度分量。
|
||||
/// <see cref="Manual"/> 表示不做预设覆盖,直接用配置里序列化好的那个四元数。
|
||||
/// </summary>
|
||||
public enum XChartCubeViewPreset
|
||||
{
|
||||
Manual = 0,
|
||||
/// <summary>45° 正交(体对角线对着观察者),轮廓是<b>正六边形</b>,Front/Right/Top 三面可见</summary>
|
||||
Isometric = 1,
|
||||
/// <summary>等距视角的镜像(Front/Left/Top 可见)</summary>
|
||||
IsometricMirrored = 2,
|
||||
Front = 3,
|
||||
Back = 4,
|
||||
Right = 5,
|
||||
Left = 6,
|
||||
Top = 7,
|
||||
Bottom = 8,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 立方体视图:旋转<b>只有一个权威量——四元数</b>,尺寸<b>只有一个权威量——半边长</b>。
|
||||
///
|
||||
/// <para><b>为什么不再拆 pitch/yaw</b>:两轴拆分本身不算错,但它把"视角"表达成了
|
||||
/// "绕世界 X 转一下、再绕世界 Y 转一下",于是任何第三个轴的姿态都拿不到(roll 被钉死为 0),
|
||||
/// 而且默认值 (25, -30) 只是随手取的,既不是正交等距也不是正视,所以画面里的"矩形"总是歪的。
|
||||
/// 本类把姿态收成一个四元数:预设、拖拽、代码 API 都直接给四元数,需要由角度合成时用
|
||||
/// <see cref="Euler"/>(它按 <c>Rx * Ry * Rz</c> 合成,与 <c>Quaternion.Euler</c> 同一约定)。</para>
|
||||
///
|
||||
/// <para><b>为什么尺寸要收成半边长</b>:见 <see cref="XChartCubeSizeMode"/>。
|
||||
/// 旧实现按"投影包围盒最长边"来 fit,而包围盒半长随旋转从 <c>h</c> 一路长大到 <c>√3 h</c>,
|
||||
/// 立方体就必须缩小,调一次俯仰小一档、再调一次偏航又小一档,最后远小于设定的尺寸。
|
||||
/// 默认口径改成与旋转无关的半边长后,尺寸这件事和视角这件事彻底解耦。</para>
|
||||
///
|
||||
/// <para><b>几种半径的关系</b>(半边长记作 <c>h</c>,立方体顶点在 <c>±h</c>):
|
||||
/// <code>
|
||||
/// 面心到中心(内切球半径) = h
|
||||
/// 顶点到中心(外接球半径) = √3 h ≈ 1.7321 h
|
||||
/// 等距轮廓正六边形的外接圆半径 = √(8/3) h ≈ 1.6330 h
|
||||
/// 等距轮廓正六边形的内切圆半径 = √2 h ≈ 1.4142 h
|
||||
/// 沿某屏幕轴的包围盒半长 = h * (|ux| + |uy| + |uz|),取值 h … √3 h
|
||||
/// </code>
|
||||
/// 所以"要画进一个 R 半径的圆里"时,半边长取 <c>R / √(8/3)</c>;
|
||||
/// 换算函数在下面这几个 <c>*FromHalfEdge</c> / <c>HalfEdgeFrom*</c> 里。</para>
|
||||
///
|
||||
/// <para><b>离线可跑</b>:这里不做任何引擎内部调用(<c>Quaternion.Euler</c>、
|
||||
/// <c>Quaternion.FromToRotation</c>、<c>Matrix4x4.TRS</c> 出不了 Unity),
|
||||
/// 四元数由 axis-angle + Hamilton 积纯托管合成,矩阵由字段逐个拼出来。
|
||||
/// 编译门里的 mono 探针会把 <see cref="ToMatrix"/> 与 <c>Matrix4x4.Rotate</c> 对拍,
|
||||
/// 把 <see cref="Euler"/> 与三次 <see cref="AxisAngle"/> 的连乘对拍,
|
||||
/// 并把"等距视角轮廓是正六边形""半边长口径下尺度不随旋转变化"当断言钉住。</para>
|
||||
/// </summary>
|
||||
public static class XChartCubeView
|
||||
{
|
||||
/// <summary>atan(1/√2) 的度数:体对角线对着观察者时,对角线与水平面的夹角</summary>
|
||||
public const float IsometricPitchDegrees = 35.2643897f;
|
||||
|
||||
/// <summary>等距视角的偏航量(绝对值),符号由 <see cref="XChartCubeViewPreset"/> 决定</summary>
|
||||
public const float IsometricYawDegrees = 45f;
|
||||
|
||||
private const float k_Sqrt3 = 1.7320508f;
|
||||
private const float k_Sqrt2 = 1.41421356f;
|
||||
private const float k_Sqrt8Over3 = 1.6329932f;
|
||||
private const float k_MinLength = 1e-8f;
|
||||
|
||||
// ── 四元数合成(纯托管) ──────────────────────────────────
|
||||
|
||||
/// <summary>归一化后的四元数。长度过短返回单位四元数(不做除法放大噪声)</summary>
|
||||
public static Quaternion Normalized(float x, float y, float z, float w)
|
||||
{
|
||||
float length = Mathf.Sqrt(x * x + y * y + z * z + w * w);
|
||||
if (length < k_MinLength) return new Quaternion(0f, 0f, 0f, 1f);
|
||||
float inv = 1f / length;
|
||||
return new Quaternion(x * inv, y * inv, z * inv, w * inv);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 绕任意轴的旋转(右手定则,角度制)。轴不必预先归一化。
|
||||
/// <c>q = (axis * sin(θ/2), cos(θ/2))</c>
|
||||
/// </summary>
|
||||
public static Quaternion AxisAngle(Vector3 axis, float angleDegrees)
|
||||
{
|
||||
float length = Mathf.Sqrt(axis.x * axis.x + axis.y * axis.y + axis.z * axis.z);
|
||||
if (length < k_MinLength) return new Quaternion(0f, 0f, 0f, 1f);
|
||||
|
||||
float inv = 1f / length;
|
||||
float half = angleDegrees * 0.5f * Mathf.Deg2Rad;
|
||||
float s = Mathf.Sin(half);
|
||||
return new Quaternion(
|
||||
axis.x * inv * s,
|
||||
axis.y * inv * s,
|
||||
axis.z * inv * s,
|
||||
Mathf.Cos(half));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Hamilton 积 <c>a ⊗ b</c>,按 Unity 的约定即"先施加 <paramref name="b"/>、再施加 <paramref name="a"/>"
|
||||
/// (矩阵视角就是 <c>Ra * Rb</c>)。
|
||||
/// 手写展开而不用引擎的 <c>operator*</c>,好让整条合成链在离线探针里也走得通。
|
||||
/// </summary>
|
||||
public static Quaternion Multiply(Quaternion a, Quaternion b)
|
||||
{
|
||||
return new Quaternion(
|
||||
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
|
||||
a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
|
||||
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
|
||||
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按 <c>Rx(xDeg) * Ry(yDeg) * Rz(zDeg)</c> 合成——与 <c>Quaternion.Euler</c> 的约定一致
|
||||
/// (先绕 Z、再绕 Y、最后绕 X,都是绕<b>世界</b>轴)。
|
||||
/// </summary>
|
||||
public static Quaternion Euler(float xDegrees, float yDegrees, float zDegrees)
|
||||
{
|
||||
Quaternion rx = AxisAngle(Vector3.right, xDegrees);
|
||||
Quaternion ry = AxisAngle(Vector3.up, yDegrees);
|
||||
Quaternion rz = AxisAngle(Vector3.forward, zDegrees);
|
||||
return Multiply(Multiply(rx, ry), rz);
|
||||
}
|
||||
|
||||
/// <summary>把配置里的四元数收正:归一化 + 长度失效时退回单位旋转</summary>
|
||||
public static Quaternion Sanitize(Quaternion q) => Normalized(q.x, q.y, q.z, q.w);
|
||||
|
||||
// ── 预设 ──────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// 预设对应的旋转。"某面正对观察者"= 把该面的外法线转到 +Z(投影保留 X/Y、丢弃 Z,
|
||||
/// 所以 <c>(R n).z == 1</c> 时该面正对观察者)。
|
||||
/// </summary>
|
||||
public static Quaternion RotationOf(XChartCubeViewPreset preset)
|
||||
{
|
||||
switch (preset)
|
||||
{
|
||||
case XChartCubeViewPreset.Isometric:
|
||||
return Euler(IsometricPitchDegrees, -IsometricYawDegrees, 0f);
|
||||
case XChartCubeViewPreset.IsometricMirrored:
|
||||
return Euler(IsometricPitchDegrees, IsometricYawDegrees, 0f);
|
||||
case XChartCubeViewPreset.Front:
|
||||
return new Quaternion(0f, 0f, 0f, 1f);
|
||||
case XChartCubeViewPreset.Back:
|
||||
return AxisAngle(Vector3.up, 180f);
|
||||
case XChartCubeViewPreset.Right:
|
||||
return AxisAngle(Vector3.up, -90f);
|
||||
case XChartCubeViewPreset.Left:
|
||||
return AxisAngle(Vector3.up, 90f);
|
||||
case XChartCubeViewPreset.Top:
|
||||
return AxisAngle(Vector3.right, 90f);
|
||||
case XChartCubeViewPreset.Bottom:
|
||||
return AxisAngle(Vector3.right, -90f);
|
||||
default:
|
||||
return new Quaternion(0f, 0f, 0f, 1f);
|
||||
}
|
||||
}
|
||||
|
||||
// ── 旋转 -> 矩阵 ──────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// 四元数 -> 3D 旋转矩阵(齐次形式,<c>m33 = 1</c>)。
|
||||
/// 这里逐字段拼装,和 <c>Matrix4x4.Rotate</c> 是两条独立实现,探针要求它们一致。
|
||||
/// 输入必须是单位四元数,所以先过一遍 <see cref="Sanitize"/>。
|
||||
/// </summary>
|
||||
public static Matrix4x4 ToMatrix(Quaternion q)
|
||||
{
|
||||
Quaternion u = Sanitize(q);
|
||||
|
||||
float xx = u.x * u.x, yy = u.y * u.y, zz = u.z * u.z;
|
||||
float xy = u.x * u.y, xz = u.x * u.z, yz = u.y * u.z;
|
||||
float wx = u.w * u.x, wy = u.w * u.y, wz = u.w * u.z;
|
||||
|
||||
var m = new Matrix4x4
|
||||
{
|
||||
m00 = 1f - 2f * (yy + zz),
|
||||
m01 = 2f * (xy - wz),
|
||||
m02 = 2f * (xz + wy),
|
||||
m03 = 0f,
|
||||
|
||||
m10 = 2f * (xy + wz),
|
||||
m11 = 1f - 2f * (xx + zz),
|
||||
m12 = 2f * (yz - wx),
|
||||
m13 = 0f,
|
||||
|
||||
m20 = 2f * (xz - wy),
|
||||
m21 = 2f * (yz + wx),
|
||||
m22 = 1f - 2f * (xx + yy),
|
||||
m23 = 0f,
|
||||
|
||||
m30 = 0f,
|
||||
m31 = 0f,
|
||||
m32 = 0f,
|
||||
m33 = 1f,
|
||||
};
|
||||
return m;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 仿射复合 <c>a ∘ b</c>(矩阵意义下的 <c>a * b</c>:先施加 <paramref name="b"/>,再施加
|
||||
/// <paramref name="a"/>)。只支持"线性 + 平移"这一族(最后一行恒为 0,0,0,1),
|
||||
/// 所以 12 个元素就够,不用写全 4x4 乘。
|
||||
/// <para>为什么不用 <c>Matrix4x4.operator*</c>:它底层是引擎内部调用,出不了 Unity,
|
||||
/// 离线探针就跑不了这条链;空间层的"基变换 × 工具变换"必须能离线验。</para>
|
||||
/// </summary>
|
||||
public static Matrix4x4 Compose(Matrix4x4 a, Matrix4x4 b)
|
||||
{
|
||||
var m = new Matrix4x4
|
||||
{
|
||||
m00 = a.m00 * b.m00 + a.m01 * b.m10 + a.m02 * b.m20,
|
||||
m01 = a.m00 * b.m01 + a.m01 * b.m11 + a.m02 * b.m21,
|
||||
m02 = a.m00 * b.m02 + a.m01 * b.m12 + a.m02 * b.m22,
|
||||
m03 = a.m00 * b.m03 + a.m01 * b.m13 + a.m02 * b.m23 + a.m03,
|
||||
|
||||
m10 = a.m10 * b.m00 + a.m11 * b.m10 + a.m12 * b.m20,
|
||||
m11 = a.m10 * b.m01 + a.m11 * b.m11 + a.m12 * b.m21,
|
||||
m12 = a.m10 * b.m02 + a.m11 * b.m12 + a.m12 * b.m22,
|
||||
m13 = a.m10 * b.m03 + a.m11 * b.m13 + a.m12 * b.m23 + a.m13,
|
||||
|
||||
m20 = a.m20 * b.m00 + a.m21 * b.m10 + a.m22 * b.m20,
|
||||
m21 = a.m20 * b.m01 + a.m21 * b.m11 + a.m22 * b.m21,
|
||||
m22 = a.m20 * b.m02 + a.m21 * b.m12 + a.m22 * b.m22,
|
||||
m23 = a.m20 * b.m03 + a.m21 * b.m13 + a.m22 * b.m23 + a.m23,
|
||||
|
||||
m30 = 0f, m31 = 0f, m32 = 0f, m33 = 1f,
|
||||
};
|
||||
return m;
|
||||
}
|
||||
|
||||
// ── 尺寸口径 ──────────────────────────────────────────────
|
||||
|
||||
/// <summary>半边长 -> 外接球半径(顶点到中心)= √3 h</summary>
|
||||
public static float CircumradiusFromHalfEdge(float halfEdge) => halfEdge * k_Sqrt3;
|
||||
|
||||
/// <summary>外接球半径 -> 半边长</summary>
|
||||
public static float HalfEdgeFromCircumradius(float circumradius) => circumradius / k_Sqrt3;
|
||||
|
||||
/// <summary>半边长 -> 等距轮廓正六边形的外接圆半径 = √(8/3) h</summary>
|
||||
public static float IsometricHexagonCircumradius(float halfEdge) => halfEdge * k_Sqrt8Over3;
|
||||
|
||||
/// <summary>想让轮廓正六边形内接于半径 r 的圆,半边长该给多少</summary>
|
||||
public static float HalfEdgeFromHexagonCircumradius(float radius) => radius / k_Sqrt8Over3;
|
||||
|
||||
/// <summary>半边长 -> 等距轮廓正六边形的内切圆半径 = √2 h</summary>
|
||||
public static float IsometricHexagonInradius(float halfEdge) => halfEdge * k_Sqrt2;
|
||||
|
||||
/// <summary>
|
||||
/// 按口径算投影缩放与平移。
|
||||
/// <para><see cref="XChartCubeSizeMode.HalfEdgeLength"/>:<c>scale = halfEdge</c>——
|
||||
/// 立方体顶点在 ±1,所以 scale 就是半边长,与旋转无关;包围盒中心仍然对齐到
|
||||
/// <paramref name="centerOffset"/>,于是转起来只是转、不会漂。</para>
|
||||
/// <para><see cref="XChartCubeSizeMode.FitToViewport"/>:<c>scale</c> 让包围盒最长边等于
|
||||
/// <c>2 * halfEdge</c>,铺满固定矩形但尺寸随旋转变化。</para>
|
||||
/// </summary>
|
||||
public static void ComputeProjection(Matrix4x4 rotation, float halfEdge, Vector2 centerOffset,
|
||||
XChartCubeSizeMode mode, out float scale, out Vector2 offset)
|
||||
{
|
||||
XChartCubeGeometry.ComputeProjectedBounds(rotation, out Vector2 min, out Vector2 max);
|
||||
Vector2 center = new Vector2((min.x + max.x) * 0.5f, (min.y + max.y) * 0.5f);
|
||||
|
||||
if (mode == XChartCubeSizeMode.HalfEdgeLength)
|
||||
{
|
||||
scale = Mathf.Max(0.001f, halfEdge);
|
||||
}
|
||||
else
|
||||
{
|
||||
float maxRange = Mathf.Max(max.x - min.x, max.y - min.y);
|
||||
scale = maxRange > 0.001f ? halfEdge * 2f / maxRange : 1f;
|
||||
}
|
||||
|
||||
offset = new Vector2(centerOffset.x - center.x * scale, centerOffset.y - center.y * scale);
|
||||
}
|
||||
|
||||
/// <summary>四元数版入口:内部先转矩阵,调用方不必自己拿 <see cref="ToMatrix"/></summary>
|
||||
public static void ComputeProjection(Quaternion rotation, float halfEdge, Vector2 centerOffset,
|
||||
XChartCubeSizeMode mode, out float scale, out Vector2 offset)
|
||||
{
|
||||
ComputeProjection(ToMatrix(rotation), halfEdge, centerOffset, mode, out scale, out offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1cd7950bf06cfc34b9beecec773cce9b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,388 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 一片端面在 <b>2D</b> 上的像:中心 + 盘内一对基向量(已投影、<b>已乘半径</b>)。
|
||||
///
|
||||
/// <para>正交投影是<b>仿射</b>的,所以 3D 圆盘的像正好是"把盘内基向量的像当半轴"的椭圆:
|
||||
/// <c>p(θ) = 中心 + U·cosθ + V·sinθ</c>。<b>只需投影中心与两个基各一次</b>,
|
||||
/// 不必对盘上每点做 3D 投影——这就是"投影从 2D 算起"(2026-10-23 他的要求)。</para>
|
||||
/// </summary>
|
||||
public struct XChartCylinderDisc2D
|
||||
{
|
||||
public Vector2 Center;
|
||||
/// <summary>盘内第一根半轴(已投影、已乘半径)</summary>
|
||||
public Vector2 U;
|
||||
/// <summary>盘内第二根半轴(已投影、已乘半径)</summary>
|
||||
public Vector2 V;
|
||||
|
||||
public Vector2 PointAt(float angle)
|
||||
{
|
||||
return Center + U * Mathf.Cos(angle) + V * Mathf.Sin(angle);
|
||||
}
|
||||
|
||||
/// <summary>第 <paramref name="index"/> 个采样点(角度均匀一圈)</summary>
|
||||
public Vector2 PointAtStep(int index, int count)
|
||||
{
|
||||
if (count <= 0) return Center;
|
||||
return PointAt(6.2831855f * index / count);
|
||||
}
|
||||
|
||||
/// <summary>椭圆参数式的导数 <c>p'(θ) = -U·sinθ + V·cosθ</c>(贝塞尔控制点要用)</summary>
|
||||
public Vector2 TangentAt(float angle)
|
||||
{
|
||||
float c = Mathf.Cos(angle);
|
||||
float s = Mathf.Sin(angle);
|
||||
return new Vector2(-U.x * s + V.x * c, -U.y * s + V.y * c);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 圆柱的纯几何:<b>不碰 Unity 对象</b>,所以离线 mono 探针能直接验"外轮廓是不是两片椭圆的凸包"
|
||||
/// "桥接点是否落在垂直于桥接方向的极值点上"这类断言。
|
||||
///
|
||||
/// <para><b>为什么外轮廓=凸包</b>:扫掠体的可见轮廓就是两个端面点集的<b>凸包</b>——
|
||||
/// 对凸截面这是精确解,而且平行、斜置、两端不同法线、两端不同心都是同一套代码,
|
||||
/// 不需要为"斜圆柱"再写一条切线公式。他要的那两条"过圆心的长轴方向连着外边框"的桥接线
|
||||
/// 就是凸包自动给出的那两段。</para>
|
||||
///
|
||||
/// <para><b>母线只在显式要求时才画</b>:轮廓只画包边;"把两端顶点全连起来"那种透视线框
|
||||
/// 是可选元素(<c>GeneratrixCount</c>),不再是侧面填充的实现方式。</para>
|
||||
/// </summary>
|
||||
public static class XChartCylinderGeometry
|
||||
{
|
||||
/// <summary>
|
||||
/// 取与 <paramref name="normal"/> 垂直的一对单位向量。辅助轴按"离轴最远的那根世界轴"选,
|
||||
/// 这样叉积不会退化(法线与某世界轴平行时那个叉积接近 0)。
|
||||
/// </summary>
|
||||
public static bool TryOrthogonalBasis(Vector3 normal, out Vector3 u, out Vector3 v)
|
||||
{
|
||||
u = Vector3.zero;
|
||||
v = Vector3.zero;
|
||||
float length = normal.magnitude;
|
||||
if (length < 1e-6f) return false;
|
||||
|
||||
Vector3 n = normal / length;
|
||||
Vector3 auxiliary = Mathf.Abs(n.y) < 0.9f ? Vector3.up : Vector3.right;
|
||||
|
||||
u = Vector3.Cross(n, auxiliary);
|
||||
float uLength = u.magnitude;
|
||||
if (uLength < 1e-6f) return false;
|
||||
u = u / uLength;
|
||||
v = Vector3.Cross(n, u);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 端面盘 → 2D。只投影中心与两根基(基向量按 <paramref name="pixelsPerUnit"/> 缩放,
|
||||
/// 半径并进基向量里),所以调用方拿到的是"椭圆的半轴"而不是 3D 点集。
|
||||
/// </summary>
|
||||
public static bool TryProjectDisc(Vector2 centerPixels, Vector2 uPixels, Vector2 vPixels,
|
||||
float radius, out XChartCylinderDisc2D disc)
|
||||
{
|
||||
disc = default(XChartCylinderDisc2D);
|
||||
if (radius <= 0f) return false;
|
||||
|
||||
disc.Center = centerPixels;
|
||||
disc.U = uPixels * radius;
|
||||
disc.V = vPixels * radius;
|
||||
return disc.U.sqrMagnitude > 1e-12f || disc.V.sqrMagnitude > 1e-12f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 凸包(Andrew monotone chain),返回包上点数并写进 <paramref name="output"/>。
|
||||
/// <b><paramref name="points"/> 会被就地排序</b>(它同时充当 scratch,避免每帧分配)。
|
||||
/// <para><b>容量</b>:两条链各自最多 push 一次每个输入点 ⇒ 写入下标最大到 <c>2×count−3</c>,
|
||||
/// 所以 <paramref name="output"/> 要能装下 <b>2×count</b> 个点。这不是留白:写成"≥count"
|
||||
/// 在"输入点几乎全在包上"时会越界(我写的一支变异臂就是这么把它照出来的)。</para>
|
||||
/// </summary>
|
||||
public static int ConvexHull(Vector2[] points, int count, Vector2[] output)
|
||||
{
|
||||
if (points == null || output == null) throw new ArgumentNullException(nameof(points));
|
||||
if (output.Length < count * 2) throw new ArgumentException("output 至少要能装下 2×count 个点");
|
||||
if (count <= 0) return 0;
|
||||
if (count < 3)
|
||||
{
|
||||
int copy = Math.Min(count, output.Length);
|
||||
for (int i = 0; i < copy; i++) output[i] = points[i];
|
||||
return copy;
|
||||
}
|
||||
|
||||
SortByXThenY(points, count);
|
||||
|
||||
int total = 0;
|
||||
// 下凸链:从左到右;上凸链:从右到左。两链拼起来就是凸包(逆时针)
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
while (total >= 2 && Cross(output[total - 2], output[total - 1], points[i]) <= 0f) total--;
|
||||
output[total++] = points[i];
|
||||
}
|
||||
|
||||
int lower = total + 1;
|
||||
for (int i = count - 2; i >= 0; i--)
|
||||
{
|
||||
while (total >= lower && Cross(output[total - 2], output[total - 1], points[i]) <= 0f) total--;
|
||||
output[total++] = points[i];
|
||||
}
|
||||
|
||||
return total - 1; // 首点重复了一次
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 外轮廓=两片端面椭圆点集的凸包。<c>scratch</c> 装采样点(长度 ≥ 2×<paramref name="segmentCount"/>),
|
||||
/// <c>output</c> 按 <see cref="ConvexHull"/> 的容量口径要能装下 <b>2×采样数</b>(长度 ≥ 4×<paramref name="segmentCount"/>)。
|
||||
/// </summary>
|
||||
public static int BuildOutline(in XChartCylinderDisc2D first, in XChartCylinderDisc2D second,
|
||||
int segmentCount, Vector2[] scratch, Vector2[] output)
|
||||
{
|
||||
if (scratch == null || output == null) throw new ArgumentNullException(nameof(scratch));
|
||||
int samples = segmentCount * 2;
|
||||
if (scratch.Length < samples || output.Length < samples * 2)
|
||||
throw new ArgumentException("scratch 要装下两片椭圆的采样点,output 要装下 2×采样数");
|
||||
|
||||
float step = 6.2831855f / segmentCount;
|
||||
for (int i = 0; i < segmentCount; i++)
|
||||
{
|
||||
float angle = i * step;
|
||||
scratch[i] = first.PointAt(angle);
|
||||
scratch[segmentCount + i] = second.PointAt(angle);
|
||||
}
|
||||
|
||||
return ConvexHull(scratch, segmentCount * 2, output);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 两片端面的法线:显式给了就用显式;没给则<b>两端都用同一条轴、同向</b>。
|
||||
///
|
||||
/// <para><b>为什么不是"各自朝另一端"</b>:`TryOrthogonalBasis(-n)` 会给出 <c>u' = -u</c>
|
||||
/// (叉积对第一个参数反号),于是两片的采样角是<b>镜像</b>的——同一个采样下标落在两片上的
|
||||
/// 方向不一致,可选元素里"两端对应点相连"的母线就会拧成 X 形,端面十字两端也是镜像的
|
||||
/// (他 2026-10-23 现场看出来的"两个端面是旋转了吗,它的连接顺序错了")。
|
||||
/// 同向法线让两片参数化完全一致,采样下标 i 在两片上就是同一个 3D 方向。</para>
|
||||
/// </summary>
|
||||
public static void ResolveNormals(Vector3 explicitStart, Vector3 explicitEnd,
|
||||
Vector3 start, Vector3 end, out Vector3 startNormal, out Vector3 endNormal)
|
||||
{
|
||||
Vector3 axis = end - start;
|
||||
if (axis.sqrMagnitude < 1e-8f) axis = Vector3.forward;
|
||||
startNormal = explicitStart.sqrMagnitude > 1e-8f ? explicitStart : axis;
|
||||
endNormal = explicitEnd.sqrMagnitude > 1e-8f ? explicitEnd : axis;
|
||||
}
|
||||
|
||||
/// <summary>多边形有向面积(逆时针为正)。探针用它比"凸包面积 ≥ 单个椭圆面积"。</summary>
|
||||
public static float SignedArea(Vector2[] polygon, int count)
|
||||
{
|
||||
if (polygon == null || count < 3) return 0f;
|
||||
float twice = 0f;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Vector2 a = polygon[i];
|
||||
Vector2 b = polygon[(i + 1) % count];
|
||||
twice += a.x * b.y - b.x * a.y;
|
||||
}
|
||||
return twice * 0.5f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 椭圆上"带滚转信息"的四个点:+U 方向是 right、+V 是 top、-U 是 left、-V 是 bottom
|
||||
/// (<c>right - left = 2U</c>、<c>top - bottom = 2V</c>,这样十字的两臂方向就是盘自己的两根基)。
|
||||
/// 端面十字用它而不是用椭圆的长短轴:长短轴会随观察方向自己转、把滚转吃掉,
|
||||
/// 而这四个点就是<b>实际渲染用的那几个顶点</b>,所以十字永远与实际端面对得上。
|
||||
/// </summary>
|
||||
public static void CrossPoints(in XChartCylinderDisc2D disc, int segmentCount,
|
||||
out Vector2 left, out Vector2 top, out Vector2 right, out Vector2 bottom)
|
||||
{
|
||||
int quarter = Math.Max(1, segmentCount / 4);
|
||||
right = disc.PointAtStep(0, segmentCount);
|
||||
top = disc.PointAtStep(quarter, segmentCount);
|
||||
left = disc.PointAtStep(quarter * 2, segmentCount);
|
||||
bottom = disc.PointAtStep(quarter * 3, segmentCount);
|
||||
}
|
||||
|
||||
private static float Cross(Vector2 origin, Vector2 a, Vector2 b)
|
||||
{
|
||||
return (a.x - origin.x) * (b.y - origin.y) - (a.y - origin.y) * (b.x - origin.x);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反解椭圆参数:<c>p = C + U·cosθ + V·sinθ</c> ⇒ 解 2×2 线性方程得 (cosθ, sinθ),
|
||||
/// 再按 <c>cos²+sin² ≈ 1</c> 判这点是否真的落在这片椭圆上。凸包分组(弧段 vs 桥接段)靠它。
|
||||
/// </summary>
|
||||
public static bool TryParameterOf(in XChartCylinderDisc2D disc, Vector2 point, out float angle)
|
||||
{
|
||||
angle = 0f;
|
||||
float det = disc.U.x * disc.V.y - disc.U.y * disc.V.x;
|
||||
if (Mathf.Abs(det) < 1e-9f) return false; // 半轴退化(端面看到侧刃)⇒ 参数不唯一
|
||||
|
||||
Vector2 d = point - disc.Center;
|
||||
float cos = (d.x * disc.V.y - d.y * disc.V.x) / det;
|
||||
float sin = (disc.U.x * d.y - disc.U.y * d.x) / det;
|
||||
if (Mathf.Abs(cos * cos + sin * sin - 1f) > 1e-3f) return false;
|
||||
angle = Mathf.Atan2(sin, cos);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>把一段扫过的角整理成"朝指定方向"的弧长:先折到 (-π, π],再按方向补一圈。</summary>
|
||||
public static float SweepToDirection(float sweep, int direction)
|
||||
{
|
||||
const float twoPi = 6.2831855f;
|
||||
sweep = Mathf.Repeat(sweep + 3.1415927f, twoPi) - 3.1415927f;
|
||||
if (direction > 0 && sweep < 0f) sweep += twoPi;
|
||||
if (direction < 0 && sweep > 0f) sweep -= twoPi;
|
||||
return sweep;
|
||||
}
|
||||
|
||||
/// <summary>按"最短路"定转向:折到 (-π, π] 后为正就 +1,否则 -1(弧段的转向靠它推断)。</summary>
|
||||
public static int ShortestDirection(float sweep)
|
||||
{
|
||||
float wrapped = Mathf.Repeat(sweep + 3.1415927f, 6.2831855f) - 3.1415927f;
|
||||
return wrapped > 0f ? 1 : -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 一段弧的<b>补弧</b>:起点接在它的终点、<b>同方向</b>走完剩下的那一圈(扫过 <c>2π-|sweep|</c>)。
|
||||
/// <para><b>不是"反过来走"</b>——这是它最容易写错的地方:反向走会原路覆盖同一段弧(半圆时尤其明显,
|
||||
/// 反着走一遍正好还是那半个圆),于是端面环又画回包边那条线上(他 2026-10-23 报的"补成外半环")。
|
||||
/// 补弧与可见弧合起来才是整圈,所以两者<b>同号</b>、绝对值和 = 2π。</para>
|
||||
/// </summary>
|
||||
public static float ComplementSweep(float sweep)
|
||||
{
|
||||
const float twoPi = 6.2831855f;
|
||||
if (sweep > 0f) return twoPi - sweep;
|
||||
if (sweep < 0f) return -twoPi - sweep;
|
||||
return twoPi;
|
||||
}
|
||||
|
||||
/// <summary>一段椭圆弧要拆成几节三次贝塞尔(每节 ≤ 90°,与圆的贝塞尔逼近同口径)</summary>
|
||||
public static int ArcPieceCount(float sweep)
|
||||
{
|
||||
float magnitude = Mathf.Abs(sweep);
|
||||
if (magnitude < 1e-6f) return 0;
|
||||
return Mathf.Max(1, Mathf.CeilToInt(magnitude / 1.5707964f));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写出一节椭圆弧的三次贝塞尔控制点(4 个,顺序 P0/P1/P2/P3)。
|
||||
/// 系数 <c>k = 4/3·tan(Δ/4)</c> 是"圆弧 → 三次贝塞尔"的经典值。<b>椭圆是圆的仿射像</b>,
|
||||
/// 而贝塞尔控制点随仿射变换同样变换,所以同一个 k 对椭圆弧一样成立——
|
||||
/// 我只算控制点,<b>细分交给曲线渲染器</b>(他 2026-10-23 点名的
|
||||
/// <c>Generation/Curve</c> 那套,它的结构里就是贝塞尔细分)。
|
||||
/// </summary>
|
||||
public static void BuildArcPiece(in XChartCylinderDisc2D disc, float fromAngle, float sweep,
|
||||
int pieceCount, int index, Vector2[] output)
|
||||
{
|
||||
if (output == null || output.Length < 4) throw new ArgumentException("控制点数组至少要 4 个点");
|
||||
if (pieceCount <= 0) pieceCount = 1;
|
||||
float delta = sweep / pieceCount;
|
||||
float a = fromAngle + delta * index;
|
||||
float b = a + delta;
|
||||
float k = 1.3333333f * Mathf.Tan(delta * 0.25f);
|
||||
|
||||
Vector2 pa = disc.PointAt(a);
|
||||
Vector2 pb = disc.PointAt(b);
|
||||
output[0] = pa;
|
||||
output[1] = pa + disc.TangentAt(a) * k;
|
||||
output[2] = pb - disc.TangentAt(b) * k;
|
||||
output[3] = pb;
|
||||
}
|
||||
|
||||
/// <summary>直线段 → 一次三次贝塞尔(控制点在 1/3、2/3,形状就是这条直线)</summary>
|
||||
public static void BuildLineBezier(Vector2 a, Vector2 b, Vector2[] output)
|
||||
{
|
||||
if (output == null || output.Length < 4) throw new ArgumentException("控制点数组至少要 4 个点");
|
||||
Vector2 third = (b - a) / 3f;
|
||||
output[0] = a;
|
||||
output[1] = a + third;
|
||||
output[2] = a + third * 2f;
|
||||
output[3] = b;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给轮廓每个点打"落在哪片端面上"的标记(1=第一片 / 2=第二片 / 0=都认不出)。
|
||||
/// 凸包按端面分组,才能把弧段当曲线、把跨端面那两段当直线("过圆心的长轴方向连外框"那两条桥接线)。
|
||||
/// </summary>
|
||||
public static void ClassifyOutline(Vector2[] outline, int count, in XChartCylinderDisc2D first,
|
||||
in XChartCylinderDisc2D second, byte[] tags)
|
||||
{
|
||||
if (outline == null || tags == null) throw new ArgumentNullException(nameof(outline));
|
||||
if (tags.Length < count) throw new ArgumentException("tags 长度不足");
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
tags[i] = TryParameterOf(first, outline[i], out float _) ? (byte)1
|
||||
: TryParameterOf(second, outline[i], out float _) ? (byte)2
|
||||
: (byte)0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把凸包切成"同一片端面"的连续段(每段是那片的可见弧)。从"标记变化处"起步走一圈,
|
||||
/// 跨末尾的那一段才不会被劈成两半。各段写进 <paramref name="runStarts"/> / <paramref name="runEnds"/> /
|
||||
/// <paramref name="runTags"/>,返回段数(数组要能装下 count 段)。
|
||||
/// <para>凸包上每片凸集只贡献一段弧,所以正常就是"2 弧 + 2 桥接";<b>远侧端面可能一段都没有</b>
|
||||
/// (它整个躲在外轮廓里),这时它没有"可见弧"可补——环只能自己画整圈。</para>
|
||||
/// </summary>
|
||||
public static int SplitOutlineRuns(Vector2[] outline, int count, in XChartCylinderDisc2D first,
|
||||
in XChartCylinderDisc2D second, byte[] tags, int[] runStarts, int[] runEnds, byte[] runTags)
|
||||
{
|
||||
if (outline == null || tags == null || runStarts == null || runEnds == null || runTags == null)
|
||||
throw new ArgumentNullException(nameof(outline));
|
||||
if (tags.Length < count || runStarts.Length < count || runEnds.Length < count || runTags.Length < count)
|
||||
throw new ArgumentException("缓冲长度不足");
|
||||
if (count < 3) return 0;
|
||||
|
||||
ClassifyOutline(outline, count, first, second, tags);
|
||||
|
||||
int start = 0;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (tags[i] != tags[(i + count - 1) % count]) { start = i; break; }
|
||||
}
|
||||
|
||||
int runs = 0;
|
||||
int runStart = start;
|
||||
int runTag = tags[start];
|
||||
for (int step = 1; step <= count; step++)
|
||||
{
|
||||
int index = (start + step) % count;
|
||||
if (step < count && tags[index] == runTag) continue;
|
||||
|
||||
runTags[runs] = (byte)runTag;
|
||||
runStarts[runs] = runStart;
|
||||
runEnds[runs] = (start + step - 1) % count;
|
||||
runs++;
|
||||
|
||||
runStart = index;
|
||||
if (step < count) runTag = tags[index];
|
||||
}
|
||||
return runs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 端面环=包边那段可见弧的<b>补弧</b>:起点接在可见弧的终点、扫过 <c>2π-|可见|</c>(方向相反),
|
||||
/// 也就是端面圆上<b>躲在外轮廓里面</b>的那一半。外侧由包边画、内侧由环画,两边都不重复。
|
||||
/// </summary>
|
||||
public static void RingArc(float fromAngle, float visibleSweep, out float ringStartAngle, out float ringSweep)
|
||||
{
|
||||
ringStartAngle = fromAngle + visibleSweep;
|
||||
ringSweep = ComplementSweep(visibleSweep);
|
||||
}
|
||||
|
||||
/// <summary>按 x 再按 y 排序(就地)。凸包要它,且顺序稳定才能让"包点顺序"可复现。</summary>
|
||||
private static void SortByXThenY(Vector2[] points, int count)
|
||||
{
|
||||
for (int i = 1; i < count; i++)
|
||||
{
|
||||
Vector2 key = points[i];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && (points[j].x > key.x || (points[j].x == key.x && points[j].y > key.y)))
|
||||
{
|
||||
points[j + 1] = points[j];
|
||||
j--;
|
||||
}
|
||||
points[j + 1] = key;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1141350d0fe65dc41847751ed92b2aa1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,174 @@
|
||||
using UnityEngine;
|
||||
using XericUI.OverrideUI;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 面帧:把一个平面面片自己的参数域 (s,t) 映射到图表局部像素空间的<b>变换协议</b>。
|
||||
///
|
||||
/// <para><b>它从哪来的(推导链)</b></para>
|
||||
/// 以伪立方体为例,要画它任意一个面,我们必须回答"这个面在屏幕上是哪四个点":
|
||||
/// <code>
|
||||
/// 1) 面在立方体局部空间里是一个四边形
|
||||
/// 面原点 O3 + s * S3 + t * T3 (s,t ∈ [0,1],S3/T3 是面的两个边向量)
|
||||
/// 2) 把 3D 点投到屏幕局部像素
|
||||
/// 正交投影(旋转后丢 Z): P(p) = (R*p).xy * scale + offset → 线性
|
||||
/// 透视投影(除以 w) : 一般四边形(梯形)
|
||||
/// 3) 于是 "面参数 -> 屏幕" 只剩两种可能:
|
||||
/// 仿射(S/T 的投影像仍是平面上的两个固定向量) —— 正交投影下恒成立
|
||||
/// 一般四边形 / 单应(投影后不再保持平行) —— 透视投影
|
||||
/// </code>
|
||||
/// 所以协议不需要知道立方体、pitch/yaw、透视——它只需要四个角点,剩下的都由此推出。</para>
|
||||
///
|
||||
/// <para><b>两档表示</b></para>
|
||||
/// 同一个类型承担两档,不做两个类型:
|
||||
/// <list type="bullet">
|
||||
/// <item>仿射档:四角构成平行四边形,(1,0)-(0,0) 与 (0,1)-(0,0) 就是两个基向量,映射可解析求逆,零开销。</item>
|
||||
/// <item>一般四边形档:任意四角,用单应(透视)映射,退化时会失败。</item>
|
||||
/// </list>
|
||||
/// 映射实现直接复用 <see cref="XericQuadProjection"/>(同一个"单位正方形 -> 四边形"的投影,已被
|
||||
/// 数值验证),本类型只负责角点约定与语义,<b>不再另写一份映射</b>。</para>
|
||||
///
|
||||
/// <para><b>角点约定</b>(与 Image 顶点顺序同源):</para>
|
||||
/// <code>
|
||||
/// CornerST00 = Map(0,0) 左下 CornerST01 = Map(0,1) 左上
|
||||
/// CornerST11 = Map(1,1) 右上 CornerST10 = Map(1,0) 右下
|
||||
/// </code>
|
||||
///
|
||||
/// <para><b>谁用它</b>:网格绘制器(填充面)、面内网格线、轴线组件(在面上取
|
||||
/// 值 -> 比例 -> 面参数 -> 像素)、数据图元(把点投到面上)。</para>
|
||||
/// </summary>
|
||||
public struct XChartFaceFrame
|
||||
{
|
||||
/// <summary>(s,t)=(0,0) 角点(左下),局部像素</summary>
|
||||
public Vector2 CornerST00;
|
||||
/// <summary>(s,t)=(0,1) 角点(左上)</summary>
|
||||
public Vector2 CornerST01;
|
||||
/// <summary>(s,t)=(1,1) 角点(右上)</summary>
|
||||
public Vector2 CornerST11;
|
||||
/// <summary>(s,t)=(1,0) 角点(右下)</summary>
|
||||
public Vector2 CornerST10;
|
||||
|
||||
/// <summary>面语义(哪个面 / 哪条轴用它做宿主),仅作标识,不参与映射</summary>
|
||||
public int FaceId;
|
||||
|
||||
private XericQuadProjection m_Projection;
|
||||
private bool m_Valid;
|
||||
|
||||
/// <summary>面参数到像素的映射是否可用(四角退化时不可用)</summary>
|
||||
public bool IsValid => m_Valid;
|
||||
|
||||
/// <summary>
|
||||
/// 是否平行四边形。成立时映射退化为仿射,可解析求逆;正交投影下恒成立,
|
||||
/// 透视投影下一般不成立。
|
||||
/// </summary>
|
||||
public bool IsAffine
|
||||
{
|
||||
get
|
||||
{
|
||||
Vector2 d = CornerST00 + CornerST11 - CornerST01 - CornerST10;
|
||||
return d.sqrMagnitude <= 1e-6f;
|
||||
}
|
||||
}
|
||||
|
||||
// ── 构造 ──────────────────────────────────────────────────
|
||||
|
||||
/// <summary>由四个角点构造(左下 / 左上 / 右上 / 右下)</summary>
|
||||
public static XChartFaceFrame FromCorners(Vector2 cornerST00, Vector2 cornerST01,
|
||||
Vector2 cornerST11, Vector2 cornerST10)
|
||||
{
|
||||
var frame = new XChartFaceFrame
|
||||
{
|
||||
CornerST00 = cornerST00,
|
||||
CornerST01 = cornerST01,
|
||||
CornerST11 = cornerST11,
|
||||
CornerST10 = cornerST10,
|
||||
};
|
||||
// 单位正方形顺序:p00 -> p10 -> p11 -> p01
|
||||
frame.m_Valid = XericQuadProjection.TryCreate(
|
||||
cornerST00, cornerST10, cornerST11, cornerST01, out frame.m_Projection);
|
||||
return frame;
|
||||
}
|
||||
|
||||
/// <summary>由仿射基构造:原点 + s 方向 + t 方向(正交投影下最省的一条路)</summary>
|
||||
public static XChartFaceFrame FromAffine(Vector2 origin, Vector2 axisS, Vector2 axisT)
|
||||
{
|
||||
return FromCorners(
|
||||
origin,
|
||||
origin + axisT,
|
||||
origin + axisT + axisS,
|
||||
origin + axisS);
|
||||
}
|
||||
|
||||
/// <summary>把面参数域里的一个矩形子区间映射成新的面帧(留白 / 内缩用)</summary>
|
||||
public XChartFaceFrame Slice(float s0, float t0, float s1, float t1)
|
||||
{
|
||||
return FromCorners(
|
||||
Map(s0, t0),
|
||||
Map(s0, t1),
|
||||
Map(s1, t1),
|
||||
Map(s1, t0));
|
||||
}
|
||||
|
||||
// ── 映射 ──────────────────────────────────────────────────
|
||||
|
||||
/// <summary>面参数 (s,t) -> 图表局部像素</summary>
|
||||
public Vector2 Map(float s, float t)
|
||||
{
|
||||
if (!m_Valid)
|
||||
return CornerST00;
|
||||
return m_Projection.Transform(s, t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 子区间内插:把 (u,v) ∈ [0,1]^2 映射到面参数域里 [s0,s1] x [t0,t1] 的位置。
|
||||
/// 轴线的留白、图例区、数据区都靠它表达,避免各处自己乘偏移。
|
||||
/// </summary>
|
||||
public Vector2 MapRange(float s0, float t0, float s1, float t1, float u, float v)
|
||||
{
|
||||
return Map(s0 + (s1 - s0) * u, t0 + (t1 - t0) * v);
|
||||
}
|
||||
|
||||
/// <summary>局部像素 -> 面参数(反查,命中测试/拖拽用)</summary>
|
||||
public bool TryInverse(Vector2 local, out Vector2 st)
|
||||
{
|
||||
st = Vector2.zero;
|
||||
if (!m_Valid)
|
||||
return false;
|
||||
if (!m_Projection.TryInvert(out var inverse))
|
||||
return false;
|
||||
st = inverse.Transform(local.x, local.y);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>取一条等 t 线(沿 s 方向)的两端</summary>
|
||||
public void GetLineAlongS(float t, out Vector2 from, out Vector2 to)
|
||||
{
|
||||
from = Map(0f, t);
|
||||
to = Map(1f, t);
|
||||
}
|
||||
|
||||
/// <summary>取一条等 s 线(沿 t 方向)的两端</summary>
|
||||
public void GetLineAlongT(float s, out Vector2 from, out Vector2 to)
|
||||
{
|
||||
from = Map(s, 0f);
|
||||
to = Map(s, 1f);
|
||||
}
|
||||
|
||||
/// <summary>四个角点(顺序:左下 / 左上 / 右上 / 右下)</summary>
|
||||
public Vector2[] GetCorners() => new[]
|
||||
{
|
||||
CornerST00, CornerST01, CornerST11, CornerST10,
|
||||
};
|
||||
|
||||
/// <summary>包围盒(局部像素)</summary>
|
||||
public Rect GetBounds()
|
||||
{
|
||||
float minX = Mathf.Min(Mathf.Min(CornerST00.x, CornerST01.x), Mathf.Min(CornerST11.x, CornerST10.x));
|
||||
float maxX = Mathf.Max(Mathf.Max(CornerST00.x, CornerST01.x), Mathf.Max(CornerST11.x, CornerST10.x));
|
||||
float minY = Mathf.Min(Mathf.Min(CornerST00.y, CornerST01.y), Mathf.Min(CornerST11.y, CornerST10.y));
|
||||
float maxY = Mathf.Max(Mathf.Max(CornerST00.y, CornerST01.y), Mathf.Max(CornerST11.y, CornerST10.y));
|
||||
return new Rect(minX, minY, maxX - minX, maxY - minY);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 451d6866f0204c85b7ecee44c7d6ab92
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 网格分格数的唯一算法(规范 §8.2)。
|
||||
///
|
||||
/// <para>立方体的线框要能和轴刻度逐线对上,分格数就不能只有一份——两条轴的范围不同,
|
||||
/// 需要的格数也不同(验收现场:<c>-100..100</c> 要 4 格、<c>-40..40</c> 要 8 格,
|
||||
/// 而网格当时只有一份"5")。所以按方向给:S 一份、T 一份,<b>不给(0 或负)表示跟随统一值</b>。</para>
|
||||
///
|
||||
/// <para><b>沿 S 的格线数由 T 方向的分格数决定</b>("沿 S 走"的线就是"每个 T 分格边界一条"),
|
||||
/// 反过来也一样——这一句最容易写反,所以从绘制代码里抽出来,让离线探针钉住它。</para>
|
||||
/// </summary>
|
||||
public static class XChartGridDivisions
|
||||
{
|
||||
/// <summary>某个方向的分格数:给了正数就用它,否则回落到统一值(统一值也不正就取 1)</summary>
|
||||
public static int Resolve(int perDirection, int uniform)
|
||||
{
|
||||
if (perDirection >= 1) return perDirection;
|
||||
return uniform >= 1 ? uniform : 1;
|
||||
}
|
||||
|
||||
/// <summary>沿 S 方向的格线数 = T 方向分格数 + 1(含两条边)</summary>
|
||||
public static int LinesAlongS(int divisionsS, int divisionsT, int uniform)
|
||||
{
|
||||
return Resolve(divisionsT, uniform) + 1;
|
||||
}
|
||||
|
||||
/// <summary>沿 T 方向的格线数 = S 方向分格数 + 1(含两条边)</summary>
|
||||
public static int LinesAlongT(int divisionsS, int divisionsT, int uniform)
|
||||
{
|
||||
return Resolve(divisionsS, uniform) + 1;
|
||||
}
|
||||
|
||||
/// <summary>统一值(不拆方向)时两个方向的格线数——留给"只有一份分格数"的旧配置用</summary>
|
||||
public static int UniformLineCount(int uniform)
|
||||
{
|
||||
return Resolve(0, uniform) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4aff13f79207a164c889411dea6b9839
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,79 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using XericUI.XChart.Axis;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 网格线的位置从哪儿来(P9):<b>网格是定义方</b>——按绑定的那一维的数据范围与目标条数
|
||||
/// 算出一组"好看"的刻度值,再折成面参数比例发布出去;轴线以后按这些发布的线吸附,
|
||||
/// 不再两边各算一遍(他 2026-10-23 的原话:"立方体网格上的分割线也都统一通过 chart 上的
|
||||
/// 数据范围控制")。
|
||||
///
|
||||
/// <para><b>不绑数据(维度 < 0)时退回等分</b>:比例 = <c>i/(count-1)</c>,与旧行为逐位相同——
|
||||
/// 所以"没配维度"这件事不会改变任何画面。</para>
|
||||
///
|
||||
/// <para>刻度值那一套(nice step + 升序铺 + 指数/对数换算)直接复用
|
||||
/// <see cref="XChartAxisMath"/>:**网格与刻度同源**,"逐条坐标相等"才有基础。</para>
|
||||
/// </summary>
|
||||
public static class XChartGridLines
|
||||
{
|
||||
/// <summary>等分(不绑数据):<paramref name="count"/> 条线,含两条边,比例 i/(count-1)。</summary>
|
||||
public static int BuildUniformRatios(int count, float[] ratios)
|
||||
{
|
||||
if (ratios == null || count <= 0) return 0;
|
||||
count = Mathf.Min(count, ratios.Length);
|
||||
for (int i = 0; i < count; i++)
|
||||
ratios[i] = count > 1 ? (float)i / (count - 1) : 0f;
|
||||
return count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 按数据范围算线位置:用 <see cref="XChartAxisMath.BuildTicks"/> 求出刻度值,
|
||||
/// 每个值经 <see cref="XChartAxisMath.ToParameter"/> 折成面参数比例并<b>夹到 [0,1]</b>
|
||||
/// (范围外的刻度值不该画到面外去)。
|
||||
/// </summary>
|
||||
public static int BuildRatios(double min, double max, XChartAxisScale scale, double strength,
|
||||
int targetCount, double manualStep, bool autoStep, float[] ratios, List<double> scratch)
|
||||
{
|
||||
if (ratios == null || scratch == null) return 0;
|
||||
|
||||
XChartAxisMath.BuildTicks(min, max, scale, manualStep, targetCount, autoStep, scratch);
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < scratch.Count && count < ratios.Length; i++)
|
||||
{
|
||||
double ratio = XChartAxisMath.ToParameter(scratch[i], min, max, scale, strength);
|
||||
if (double.IsNaN(ratio) || double.IsInfinity(ratio)) continue;
|
||||
ratios[count++] = Mathf.Clamp01((float)ratio);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
/// <summary>
|
||||
/// 把 <paramref name="ratio"/> 吸附到<b>最近的一条格线比例</b>上(P9 的"轴线跟随"那一步)。
|
||||
/// 最近那条的距离超过 <paramref name="maxDistance"/> 就<b>不吸</b>——宁可不对齐,也不要把刻度
|
||||
/// 扭到老远的地方(那会让人以为数据变了)。吸不上时 <paramref name="snapped"/> 原样回填,返回 false。
|
||||
/// </summary>
|
||||
public static bool TrySnapToGrid(float[] ratios, int count, float ratio, float maxDistance, out float snapped)
|
||||
{
|
||||
snapped = ratio;
|
||||
if (ratios == null || count <= 0) return false;
|
||||
|
||||
int best = -1;
|
||||
float bestDistance = float.MaxValue;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
float distance = Mathf.Abs(ratios[i] - ratio);
|
||||
if (distance < bestDistance)
|
||||
{
|
||||
bestDistance = distance;
|
||||
best = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (best < 0 || bestDistance > maxDistance) return false;
|
||||
snapped = ratios[best];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eef8dae19b1d78648ac23a0bd8e50170
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,153 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using XericUI.XChart.Space;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 立方体<b>网格背景</b>元素(面板上叫 <c>BG Cube Grid</c>)的配置——<b>Inspector 上看到的就是这一份</b>。
|
||||
///
|
||||
/// <para>它是<b>格线的唯一作者</b>:画面内格线、也按同一套条数发布
|
||||
/// <c>mesh.face.<面号>.grid.S/.T</c> 插槽给轴线跟随(规范 §22)。线框元素
|
||||
/// (<see cref="XChartCubeWireConfig"/>)只画棱、只发四角,两边不再抢同一批 id。</para>
|
||||
///
|
||||
/// <para>它通过 <see cref="XChartElementConfig"/> 协议接进图表:图表用 [SerializeReference]
|
||||
/// 持有一张配置表,Unity 会按实际类型展开这里的字段,所以新增元素不需要写自定义 Editor。</para>
|
||||
///
|
||||
/// <para>两份面掩码各自独立:<see cref="FillFaces"/> 管填充,<see cref="GridFaces"/> 管面内网格。
|
||||
/// 掩码不看可见性——背景网格本来就常画在背朝观察者的那几面(后墙 / 底面)。
|
||||
/// 字母契约:<b>a=前 b=后 c=上 d=下 e=左 f=右</b>。</para>
|
||||
///
|
||||
/// <para><b>它不再持有坐标系</b>:姿态(四元数)/半边长/尺寸口径/中心偏移都归
|
||||
/// <see cref="XChartSpaceConfig"/>(空间元素)管,本配置只报出自己的<b>工具变换</b>。
|
||||
/// 投影是 <c>基变换 ∘ 工具变换</c>,所以背景、线框、散点、轴线必然落在同一个空间里。</para>
|
||||
///
|
||||
/// <para><b>名字为什么不叫 MeshBg 了</b>:后面还要有平面坐标系网格、雷达图网格、神经图谱网格,
|
||||
/// "Mesh/背景"分不开它们;名字按<b>画的是什么几何</b>取(<c>BG Cube Grid</c>),
|
||||
/// 与 <c>BG Cube Wire</c>(线框)、将来的 <c>BG Plane Grid</c> 一类并列(他 2026-10-25 点名要求)。</para>
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[XChartElement("cube-grid", "BG Cube Grid", 10)]
|
||||
public sealed class XChartMeshBgConfig : XChartElementConfig
|
||||
{
|
||||
[Header("工具变换")]
|
||||
[SerializeField, Tooltip("背景自己的缩放/旋转/平移。默认恒等——背景就占满图的空间")]
|
||||
private XChartToolTransform m_Transform = XChartToolTransform.Identity;
|
||||
|
||||
[Header("填充面")]
|
||||
[SerializeField, Tooltip("要填充哪些面;None = 不填充")]
|
||||
private XChartCubeFaceMask m_FillFaces = XChartCubeFaceMask.BackWalls;
|
||||
|
||||
[SerializeField, Tooltip("填充色(面边缘)")]
|
||||
private Color m_FillColor = new Color(0.16f, 0.19f, 0.24f, 0.85f);
|
||||
|
||||
[SerializeField, Tooltip("填充中心色;与边缘色不同则走径向渐变")]
|
||||
private Color m_FillCenterColor = new Color(0.16f, 0.19f, 0.24f, 0.85f);
|
||||
|
||||
[Header("网格面")]
|
||||
[SerializeField, Tooltip("要在哪些面上画网格;None = 不画")]
|
||||
private XChartCubeFaceMask m_GridFaces = XChartCubeFaceMask.BackWalls;
|
||||
|
||||
[SerializeField, Tooltip("网格线颜色")]
|
||||
private Color m_GridColor = new Color(0.55f, 0.6f, 0.7f, 0.35f);
|
||||
|
||||
[SerializeField, Min(0.1f), Tooltip("网格线宽")]
|
||||
private float m_GridThickness = 1f;
|
||||
|
||||
[SerializeField, Range(1, 50), Tooltip("每方向的网格分格数(统一值;下面 S/T 给了正数就按各自的来)。" +
|
||||
"绑了维度时它当\"目标刻度条数\"")]
|
||||
private int m_GridDivisions = 5;
|
||||
|
||||
[SerializeField, Min(0), Tooltip("S 方向分格数(0=跟随上面的统一值)。等分时\"沿 S 的线由 T 的分格数决定\"" +
|
||||
"(两条边都算,所以 N 段出 N+1 条);绑了维度时这个数是那一族线的目标刻度条数")]
|
||||
private int m_GridDivisionsS;
|
||||
|
||||
[SerializeField, Min(0), Tooltip("T 方向分格数(0=跟随统一值)")]
|
||||
private int m_GridDivisionsT;
|
||||
|
||||
[SerializeField, Tooltip("沿 S 的格线绑哪一维(-1=不绑、等分):绑了就用该维数据范围算线位置,轴线可按发布的线跟随")]
|
||||
private int m_GridDimensionS = -1;
|
||||
|
||||
[SerializeField, Tooltip("沿 T 的格线绑哪一维(-1=不绑、等分)")]
|
||||
private int m_GridDimensionT = -1;
|
||||
|
||||
[SerializeField, Tooltip("是否同时描出面轮廓")]
|
||||
private bool m_ShowFaceOutline = true;
|
||||
|
||||
[SerializeField, Tooltip("只画朝向观察者的面(关掉才会画后墙/底面这类背朝观察者的面)")]
|
||||
private bool m_OnlyVisibleFaces;
|
||||
|
||||
/// <summary>元素在 ToolsRoot 下的名字</summary>
|
||||
public override string ElementName => "BG Cube Grid";
|
||||
|
||||
// ── 给脚本用的入口(默认长什么样由预设说,不在图上再存一份字段)──────────
|
||||
// 这些就是上面序列化字段的读写口,不是第二份状态:配置本身是权威,工具在 ApplyTo 时读它。
|
||||
|
||||
/// <summary>填哪些面(None=不填)</summary>
|
||||
public XChartCubeFaceMask FillFaces { get => m_FillFaces; set => m_FillFaces = value; }
|
||||
|
||||
/// <summary>在哪些面上画格线(None=不画)</summary>
|
||||
public XChartCubeFaceMask GridFaces { get => m_GridFaces; set => m_GridFaces = value; }
|
||||
|
||||
/// <summary>只画朝向观察者的面</summary>
|
||||
public bool OnlyVisibleFaces { get => m_OnlyVisibleFaces; set => m_OnlyVisibleFaces = value; }
|
||||
|
||||
/// <summary>描面轮廓:图里已经有 <c>BG Cube Wire</c> 时应当关掉,否则同一条边被描两遍</summary>
|
||||
public bool ShowFaceOutline { get => m_ShowFaceOutline; set => m_ShowFaceOutline = value; }
|
||||
|
||||
/// <summary>每方向分格数(统一值)</summary>
|
||||
public int GridDivisions { get => m_GridDivisions; set => m_GridDivisions = Mathf.Max(1, value); }
|
||||
|
||||
/// <summary>S 方向分格数(0=跟随统一值)</summary>
|
||||
public int GridDivisionsS { get => m_GridDivisionsS; set => m_GridDivisionsS = Mathf.Max(0, value); }
|
||||
|
||||
/// <summary>T 方向分格数(0=跟随统一值)</summary>
|
||||
public int GridDivisionsT { get => m_GridDivisionsT; set => m_GridDivisionsT = Mathf.Max(0, value); }
|
||||
|
||||
/// <summary>格线颜色</summary>
|
||||
public Color GridColor { get => m_GridColor; set => m_GridColor = value; }
|
||||
|
||||
/// <summary>格线线宽</summary>
|
||||
public float GridThickness { get => m_GridThickness; set => m_GridThickness = Mathf.Max(0.1f, value); }
|
||||
|
||||
/// <summary>沿 S 的格线绑哪一维(<0=不绑、等分)</summary>
|
||||
public int GridDimensionS { get => m_GridDimensionS; set => m_GridDimensionS = value; }
|
||||
|
||||
/// <summary>沿 T 的格线绑哪一维(<0=不绑、等分)</summary>
|
||||
public int GridDimensionT { get => m_GridDimensionT; set => m_GridDimensionT = value; }
|
||||
|
||||
public override XChartToolBase CreateTool() => new ChartToolMeshBg();
|
||||
|
||||
public override void ApplyTo(XChartToolBase tool)
|
||||
{
|
||||
if (!(tool is ChartToolMeshBg meshBg)) return;
|
||||
|
||||
// 投影不在这里:背景只报出自己的工具变换,基变换在空间元素那份配置里
|
||||
meshBg.Transform = m_Transform;
|
||||
|
||||
meshBg.FillFaces = m_FillFaces;
|
||||
meshBg.FillColor = m_FillColor;
|
||||
meshBg.FillCenterColor = m_FillCenterColor;
|
||||
|
||||
meshBg.GridFaces = m_GridFaces;
|
||||
meshBg.GridColor = m_GridColor;
|
||||
meshBg.GridThickness = m_GridThickness;
|
||||
meshBg.GridDivisions = m_GridDivisions;
|
||||
meshBg.GridDivisionsS = m_GridDivisionsS;
|
||||
meshBg.GridDivisionsT = m_GridDivisionsT;
|
||||
meshBg.GridDimensionS = m_GridDimensionS;
|
||||
meshBg.GridDimensionT = m_GridDimensionT;
|
||||
meshBg.ShowFaceOutline = m_ShowFaceOutline;
|
||||
meshBg.OnlyVisibleFaces = m_OnlyVisibleFaces;
|
||||
|
||||
meshBg.MarkDirty();
|
||||
}
|
||||
|
||||
public override void CaptureFrom(XChartToolBase tool)
|
||||
{
|
||||
if (!(tool is ChartToolMeshBg meshBg)) return;
|
||||
|
||||
m_Transform = meshBg.Transform;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e071b542959f4fce9abe272bea055082
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,254 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using XericUI.Core;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 顶点预算分块(纯算,离线可验)。
|
||||
///
|
||||
/// <para><b>为什么要有它</b>:UGUI 的 65535 顶点上限是<b>每个 Graphic 各自</b>的,而有些元素是
|
||||
/// 按记录数长网格的(箭头一条记录 2 个四边形、散点一个点 1 个四边形)⇒ 记录一多必然撞上
|
||||
/// (他 2026-10-23 灌 10000 条时现场抛 `Mesh can not have more than 65000 vertices`)。
|
||||
/// 所以提交前先按预算把面切成若干块,每块一个 Graphic。</para>
|
||||
/// </summary>
|
||||
public static class XChartMeshBudget
|
||||
{
|
||||
/// <summary>每块顶点上限。65535 是硬上限,这里留足余量(还要给别的顶点与实现细节留空间)。</summary>
|
||||
public const int ChunkVertexBudget = 16000;
|
||||
|
||||
/// <summary>
|
||||
/// 切块:把 <paramref name="faceCount"/> 个面按"每面顶点数"切成若干块,
|
||||
/// 各块起始面下标写进 <paramref name="starts"/>,返回块数。
|
||||
/// 块 k 覆盖面 <c>[starts[k], starts[k+1])</c>,最后一块到 <paramref name="faceCount"/>。
|
||||
/// 顺序不变、不重不漏;单面就超过预算时它自成一块(无解,只能交给渲染器)。
|
||||
/// </summary>
|
||||
public static int Split(int[] faceVertexCounts, int faceCount, int budget, int[] starts)
|
||||
{
|
||||
if (faceVertexCounts == null) throw new ArgumentNullException(nameof(faceVertexCounts));
|
||||
if (starts == null) throw new ArgumentNullException(nameof(starts));
|
||||
if (starts.Length < 1) throw new ArgumentException("starts 至少要有 1 个位置");
|
||||
if (faceCount <= 0)
|
||||
{
|
||||
starts[0] = 0;
|
||||
return 0;
|
||||
}
|
||||
if (budget < 1) budget = 1;
|
||||
|
||||
int chunks = 0;
|
||||
int start = 0;
|
||||
int used = 0;
|
||||
for (int i = 0; i < faceCount; i++)
|
||||
{
|
||||
int vertices = Mathf.Max(1, faceVertexCounts[i]);
|
||||
if (used > 0 && used + vertices > budget)
|
||||
{
|
||||
starts[chunks++] = start;
|
||||
start = i;
|
||||
used = 0;
|
||||
}
|
||||
used += vertices;
|
||||
}
|
||||
starts[chunks++] = start;
|
||||
return chunks;
|
||||
}
|
||||
|
||||
/// <summary>各块顶点数的最大值(报表用:报告"最大的那个 Graphic 有多少顶点")</summary>
|
||||
public static int MaxChunkVertices(int[] faceVertexCounts, int faceCount, int[] starts, int chunkCount)
|
||||
{
|
||||
int max = 0;
|
||||
for (int c = 0; c < chunkCount; c++)
|
||||
{
|
||||
int end = (c + 1 < chunkCount) ? starts[c + 1] : faceCount;
|
||||
int sum = 0;
|
||||
for (int i = starts[c]; i < end; i++) sum += Mathf.Max(1, faceVertexCounts[i]);
|
||||
if (sum > max) max = sum;
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 面填充渲染器:提交若干四边形,<b>按顶点预算自动分块</b>,每块一个子 Graphic。
|
||||
///
|
||||
/// <para>每面可以纯色填充,也可以"四角色 + 中心色"径向渐变(背景做浅淡的立体感很需要这个)。
|
||||
/// 面的<b>提交顺序就是绘制顺序</b>,所以调用方要按深度从远到近喂(画家算法)——
|
||||
/// 半透明面叠在一起时,顺序错了颜色就错了。分块不改这个顺序:块按提交顺序排,块内也按顺序。</para>
|
||||
///
|
||||
/// <para>不参与射线检测(背景不该挡住交互)。</para>
|
||||
/// </summary>
|
||||
[AddComponentMenu(XericRegistrationPaths.Chart + "/Mesh/Cube Face Fill Renderer")]
|
||||
public sealed class XChartMeshFillRenderer : MaskableGraphic
|
||||
{
|
||||
private struct FaceQuad
|
||||
{
|
||||
public Vector2 Corner00;
|
||||
public Vector2 Corner01;
|
||||
public Vector2 Corner11;
|
||||
public Vector2 Corner10;
|
||||
public Color32 EdgeColor;
|
||||
public Color32 CenterColor;
|
||||
}
|
||||
|
||||
/// <summary>一块:只画 [Start, End) 之间的面。顶点数受预算限制,所以不会撞 65535 上限。</summary>
|
||||
private sealed class FaceChunk : MaskableGraphic
|
||||
{
|
||||
public XChartMeshFillRenderer Owner;
|
||||
public int FirstFace;
|
||||
public int LastFace;
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
raycastTarget = false;
|
||||
}
|
||||
|
||||
protected override void OnPopulateMesh(VertexHelper vh)
|
||||
{
|
||||
vh.Clear();
|
||||
if (Owner != null) Owner.PopulateSlice(vh, FirstFace, LastFace);
|
||||
}
|
||||
}
|
||||
|
||||
private readonly List<FaceQuad> m_Faces = new List<FaceQuad>(8);
|
||||
private readonly List<FaceChunk> m_Chunks = new List<FaceChunk>();
|
||||
private int[] m_FaceVertexCounts = new int[0];
|
||||
private int[] m_ChunkStarts = new int[8];
|
||||
|
||||
/// <summary>报表用:这一帧提交了几个面 / 切成几块 / 最大的那块有多少顶点</summary>
|
||||
public int FaceCount => m_Faces.Count;
|
||||
public int ChunkCount => m_Chunks.Count;
|
||||
public int MaxChunkVertices { get; private set; }
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
raycastTarget = false;
|
||||
}
|
||||
|
||||
/// <summary>清空已提交的面(每次重建网格前调用)</summary>
|
||||
public void ClearFaces() => m_Faces.Clear();
|
||||
|
||||
/// <summary>
|
||||
/// 追加一个面。<paramref name="centerColor"/> 与 <paramref name="edgeColor"/> 不同时
|
||||
/// 走径向渐变(多一个中心顶点 + 4 个三角),相同则平坦填充(4 顶点 2 三角)。
|
||||
/// </summary>
|
||||
public void AddFace(Vector2 corner00, Vector2 corner01, Vector2 corner11, Vector2 corner10,
|
||||
Color edgeColor, Color centerColor)
|
||||
{
|
||||
m_Faces.Add(new FaceQuad
|
||||
{
|
||||
Corner00 = corner00,
|
||||
Corner01 = corner01,
|
||||
Corner11 = corner11,
|
||||
Corner10 = corner10,
|
||||
EdgeColor = edgeColor,
|
||||
CenterColor = centerColor,
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>提交:按预算分块,然后各块重画</summary>
|
||||
public void Apply()
|
||||
{
|
||||
RebuildChunks();
|
||||
SetVerticesDirty(); // 本体不画东西,但仍刷一次,保证子块的顺序紧跟其后
|
||||
for (int i = 0; i < m_Chunks.Count; i++) m_Chunks[i].SetVerticesDirty();
|
||||
}
|
||||
|
||||
private void RebuildChunks()
|
||||
{
|
||||
if (m_FaceVertexCounts.Length < m_Faces.Count) m_FaceVertexCounts = new int[m_Faces.Count];
|
||||
for (int i = 0; i < m_Faces.Count; i++)
|
||||
m_FaceVertexCounts[i] = IsGradient(m_Faces[i]) ? 5 : 4;
|
||||
|
||||
if (m_ChunkStarts.Length < m_Faces.Count + 1) m_ChunkStarts = new int[m_Faces.Count + 1];
|
||||
int chunks = XChartMeshBudget.Split(m_FaceVertexCounts, m_Faces.Count,
|
||||
XChartMeshBudget.ChunkVertexBudget, m_ChunkStarts);
|
||||
MaxChunkVertices = XChartMeshBudget.MaxChunkVertices(m_FaceVertexCounts, m_Faces.Count,
|
||||
m_ChunkStarts, chunks);
|
||||
|
||||
EnsureChunkCount(chunks);
|
||||
for (int c = 0; c < chunks; c++)
|
||||
{
|
||||
m_Chunks[c].FirstFace = m_ChunkStarts[c];
|
||||
m_Chunks[c].LastFace = (c + 1 < chunks) ? m_ChunkStarts[c + 1] : m_Faces.Count;
|
||||
m_Chunks[c].enabled = true;
|
||||
}
|
||||
for (int c = chunks; c < m_Chunks.Count; c++) m_Chunks[c].enabled = false; // 多余的块停掉,不画空网格
|
||||
}
|
||||
|
||||
private void EnsureChunkCount(int count)
|
||||
{
|
||||
while (m_Chunks.Count < count)
|
||||
{
|
||||
var chunk = new GameObject("FillChunk" + m_Chunks.Count, typeof(RectTransform)).AddComponent<FaceChunk>();
|
||||
chunk.Owner = this;
|
||||
var rect = (RectTransform)chunk.transform;
|
||||
rect.SetParent(transform, false);
|
||||
rect.anchorMin = Vector2.zero;
|
||||
rect.anchorMax = Vector2.one;
|
||||
rect.offsetMin = Vector2.zero;
|
||||
rect.offsetMax = Vector2.zero;
|
||||
chunk.raycastTarget = false;
|
||||
m_Chunks.Add(chunk);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>本体不画东西:所有面都交给受预算约束的子块</summary>
|
||||
protected override void OnPopulateMesh(VertexHelper vh) => vh.Clear();
|
||||
|
||||
private static bool IsGradient(FaceQuad face)
|
||||
{
|
||||
// Color32 是逐字节比较:"两色不同"就说明这次要做渐变
|
||||
return face.CenterColor.r != face.EdgeColor.r
|
||||
|| face.CenterColor.g != face.EdgeColor.g
|
||||
|| face.CenterColor.b != face.EdgeColor.b
|
||||
|| face.CenterColor.a != face.EdgeColor.a;
|
||||
}
|
||||
|
||||
internal void PopulateSlice(VertexHelper vh, int start, int end)
|
||||
{
|
||||
if (start < 0) start = 0;
|
||||
if (end > m_Faces.Count) end = m_Faces.Count;
|
||||
|
||||
for (int i = start; i < end; i++)
|
||||
{
|
||||
FaceQuad face = m_Faces[i];
|
||||
int first = vh.currentVertCount;
|
||||
|
||||
vh.AddVert(face.Corner00, face.EdgeColor, new Vector2(0f, 0f));
|
||||
vh.AddVert(face.Corner01, face.EdgeColor, new Vector2(0f, 1f));
|
||||
vh.AddVert(face.Corner11, face.EdgeColor, new Vector2(1f, 1f));
|
||||
vh.AddVert(face.Corner10, face.EdgeColor, new Vector2(1f, 0f));
|
||||
|
||||
if (IsGradient(face))
|
||||
{
|
||||
Vector2 center = (face.Corner00 + face.Corner01 + face.Corner11 + face.Corner10) * 0.25f;
|
||||
vh.AddVert(center, face.CenterColor, new Vector2(0.5f, 0.5f));
|
||||
|
||||
vh.AddTriangle(first + 0, first + 1, first + 4);
|
||||
vh.AddTriangle(first + 1, first + 2, first + 4);
|
||||
vh.AddTriangle(first + 2, first + 3, first + 4);
|
||||
vh.AddTriangle(first + 3, first + 0, first + 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
vh.AddTriangle(first + 0, first + 1, first + 2);
|
||||
vh.AddTriangle(first + 2, first + 3, first + 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDisable()
|
||||
{
|
||||
base.OnDisable();
|
||||
// 图形停用时子块也得停,否则会留下"看不见但还在"的空网格
|
||||
for (int i = 0; i < m_Chunks.Count; i++)
|
||||
{
|
||||
if (m_Chunks[i] != null) m_Chunks[i].enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 75d18a4a019d4d50842820a2bdf026ab
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,75 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 矢量场(箭头)的<b>纯算术</b>:一条记录的两组三维怎么变成一支箭头,以及一组颜色维怎么变成颜色。
|
||||
/// 单独拎出来是因为这两条是"看图看不出来"的判据——
|
||||
/// 他 2026-10-25 现场:"我把它添加进来了,之后它并没有显示出来",以及"相对和绝对就是这两种区别"。
|
||||
/// 读数与算式放在一起,离线探针才能逐条钉住(规范 §25)。
|
||||
/// </summary>
|
||||
public static class XChartVectorFieldMath
|
||||
{
|
||||
/// <summary>
|
||||
/// 末端位置。<paramref name="mode"/>=<see cref="XChartArrowVectorMode.Absolute"/> 时
|
||||
/// <paramref name="second"/> 是<b>方向矢量</b>本身(起点 + 方向×倍率);
|
||||
/// =<see cref="XChartArrowVectorMode.Relative"/> 时它是<b>第二个点</b>(起点 + 差向量×倍率)。
|
||||
/// 两种读法在同一个数据上画出来是两幅不同的图:前者"刺猬",后者"首尾相接"。
|
||||
/// </summary>
|
||||
public static Vector3 Tip(XChartArrowVectorMode mode, Vector3 first, Vector3 second, float scale)
|
||||
{
|
||||
Vector3 direction = mode == XChartArrowVectorMode.Relative ? second - first : second;
|
||||
return first + direction * scale;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读一组颜色维:<paramref name="hue"/> 按 <b>0..256 的色环</b>(256 与 0 是同一个颜色,
|
||||
/// 取模,所以 300 也读得动),饱和度/明度/不透明度按 0..1 夹住。
|
||||
///
|
||||
/// <para><b>任何一个不是有限数就拒绝</b>(返回 false,调用方退回单色):
|
||||
/// NaN 经取模与 HSV 换算会得到一个看着像"黑色块"的东西——那不是数据的意思,
|
||||
/// 是我们在替它编一个颜色(同 §14.1 的"宁可这一帧不画")。</para>
|
||||
/// </summary>
|
||||
public static bool TryColor(float hue, float saturation, float value, float alpha, out Color color)
|
||||
{
|
||||
color = Color.white;
|
||||
if (!IsFinite(hue) || !IsFinite(saturation) || !IsFinite(value) || !IsFinite(alpha)) return false;
|
||||
|
||||
color = HsvToRgb(Mathf.Repeat(hue / 256f, 1f), Mathf.Clamp01(saturation), Mathf.Clamp01(value));
|
||||
color.a = Mathf.Clamp01(alpha);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsFinite(float value)
|
||||
=> !float.IsNaN(value) && !float.IsInfinity(value);
|
||||
|
||||
/// <summary>
|
||||
/// HSV → RGB(h∈[0,1),s/v∈[0,1])。
|
||||
/// <b>自己写而不是调 <c>Color.HSVToRGB</c></b>:后者是引擎侧的静态调用,
|
||||
/// 离线探针(mono + CoreModule)里跑不起来,而这条换算恰恰是要离线钉住的(规范 §25)。
|
||||
/// </summary>
|
||||
private static Color HsvToRgb(float h, float s, float v)
|
||||
{
|
||||
if (s <= 0f) return new Color(v, v, v, 1f);
|
||||
|
||||
float sector = h * 6f;
|
||||
int index = Mathf.FloorToInt(sector) % 6;
|
||||
if (index < 0) index += 6;
|
||||
float f = sector - Mathf.Floor(sector);
|
||||
|
||||
float p = v * (1f - s);
|
||||
float q = v * (1f - s * f);
|
||||
float t = v * (1f - s * (1f - f));
|
||||
|
||||
switch (index)
|
||||
{
|
||||
case 0: return new Color(v, t, p, 1f);
|
||||
case 1: return new Color(q, v, p, 1f);
|
||||
case 2: return new Color(p, v, t, 1f);
|
||||
case 3: return new Color(p, q, v, 1f);
|
||||
case 4: return new Color(t, p, v, 1f);
|
||||
default: return new Color(v, p, q, 1f);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2bd6270bc110b8f4e992616c92d55453
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,244 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using XericUI.XChart.Space;
|
||||
|
||||
namespace XericUI.XChart.Mesh
|
||||
{
|
||||
/// <summary>
|
||||
/// 一条记录里的两个点怎么变成一支箭头。
|
||||
///
|
||||
/// <para><b>绝对</b>:后三点是方向矢量本身(起点 + 方向×倍率=末端)。螺旋点这样画出来是"刺猬"。
|
||||
/// <b>相对</b>:后三点是第二个点,箭头=第一点指向第二点(差向量×倍率)。
|
||||
/// 连续绘制模式下两者都不适用(连起来的是一条空间路径)。</para>
|
||||
/// </summary>
|
||||
public enum XChartArrowVectorMode
|
||||
{
|
||||
/// <summary>后三维=方向矢量</summary>
|
||||
Absolute = 0,
|
||||
/// <summary>后三维=第二个点(箭头由第一点指向它)</summary>
|
||||
Relative = 1,
|
||||
}
|
||||
|
||||
/// <summary>圆柱元素的配置(几何单位都是立方体局部:±1 就是盒子边界)。</summary>
|
||||
[Serializable]
|
||||
[XChartElement("cylinder", "圆柱", 40)]
|
||||
public sealed class XChartCylinderConfig : XChartElementConfig
|
||||
{
|
||||
[Header("几何")]
|
||||
[SerializeField, Tooltip("起点圆心(立方体单位)")]
|
||||
private Vector3 m_Start = new Vector3(-0.6f, -0.6f, -0.6f);
|
||||
[SerializeField, Tooltip("终点圆心(立方体单位);两端不重合就是斜圆柱")]
|
||||
private Vector3 m_End = new Vector3(0.6f, 0.6f, 0.6f);
|
||||
|
||||
[SerializeField, Tooltip("几何来源:关=用上面的常量坐标;开=从数据取(每组的首尾两条记录=这条圆柱的两端)")]
|
||||
private bool m_DriveByData;
|
||||
[SerializeField, Tooltip("数据模式下 X 取哪一维")]
|
||||
private int m_DimensionX;
|
||||
[SerializeField, Tooltip("数据模式下 Y 取哪一维")]
|
||||
private int m_DimensionY = 1;
|
||||
[SerializeField, Tooltip("数据模式下 Z 取哪一维")]
|
||||
private int m_DimensionZ = 2;
|
||||
[SerializeField, Min(2), Tooltip("几条记录凑一根圆柱(2=两两一组:记录 0-1、2-3…,不重叠也不并入上一组)")]
|
||||
private int m_GroupSize = 2;
|
||||
[SerializeField, Min(1), Tooltip("最多画几根(记录多时按组数上限截断,报表会点名截了多少)")]
|
||||
private int m_MaxCylinders = 64;
|
||||
|
||||
[SerializeField, Min(0.001f), Tooltip("半径(立方体单位)")]
|
||||
private float m_Radius = 0.22f;
|
||||
[SerializeField, Range(8, 256), Tooltip("一圈分多少段(轮廓与十字的采样精度)")]
|
||||
private int m_SegmentCount = 48;
|
||||
|
||||
[SerializeField, Tooltip("起点端面的法线。零=沿两端连线(与旧行为一致)")]
|
||||
private Vector3 m_StartNormal = Vector3.zero;
|
||||
[SerializeField, Tooltip("终点端面的法线。零=沿两端连线。两端各给各的,就能做出端面不平行的那种")]
|
||||
private Vector3 m_EndNormal = Vector3.zero;
|
||||
|
||||
[SerializeField, Tooltip("圆柱自己的变换(叠在图的空间之上)")]
|
||||
private XChartToolTransform m_Transform = XChartToolTransform.Identity;
|
||||
|
||||
[Header("呈现(各有开关)")]
|
||||
[SerializeField, Tooltip("外轮廓包边(视角下的轮廓,不是把所有母线都连起来)")]
|
||||
private bool m_ShowOutline = true;
|
||||
[SerializeField] private Color m_OutlineColor = new Color(0.75f, 0.85f, 1f, 0.9f);
|
||||
[SerializeField, Min(0.1f)] private float m_OutlineThickness = 2f;
|
||||
|
||||
[SerializeField, Tooltip("轮廓填充(与包边同一个凸包多边形)")]
|
||||
private bool m_ShowFill = true;
|
||||
[SerializeField] private Color m_FillColor = new Color(0.34f, 0.55f, 0.85f, 0.35f);
|
||||
|
||||
[SerializeField, Tooltip("端面十字:用端面自己的采样顶点连成,所以永远与实际端面对得上,能看出滚转")]
|
||||
private bool m_ShowCapCross;
|
||||
[SerializeField] private Color m_CrossColor = new Color(0.9f, 0.95f, 1f, 0.85f);
|
||||
[SerializeField, Min(0.1f)] private float m_CrossThickness = 1.5f;
|
||||
|
||||
[SerializeField, Tooltip("起点端面的闭合圆环(端面自己的边界,整圈闭合)")]
|
||||
private bool m_ShowStartRing;
|
||||
[SerializeField, Tooltip("终点端面的闭合圆环")]
|
||||
private bool m_ShowEndRing;
|
||||
|
||||
[SerializeField, Range(0, 64), Tooltip("内部母线数量(两端对应点相连;0=不画)")]
|
||||
private int m_GeneratrixCount;
|
||||
[SerializeField] private Color m_GeneratrixColor = new Color(0.75f, 0.85f, 1f, 0.35f);
|
||||
|
||||
public override string ElementName => "Cylinder";
|
||||
|
||||
public override XChartToolBase CreateTool() => new ChartToolCylinder();
|
||||
|
||||
public override void ApplyTo(XChartToolBase tool)
|
||||
{
|
||||
if (!(tool is ChartToolCylinder cylinder)) return;
|
||||
|
||||
cylinder.Start = m_Start;
|
||||
cylinder.End = m_End;
|
||||
cylinder.DriveByData = m_DriveByData;
|
||||
cylinder.DimensionX = m_DimensionX;
|
||||
cylinder.DimensionY = m_DimensionY;
|
||||
cylinder.DimensionZ = m_DimensionZ;
|
||||
cylinder.GroupSize = m_GroupSize;
|
||||
cylinder.MaxCylinders = m_MaxCylinders;
|
||||
cylinder.Radius = m_Radius;
|
||||
cylinder.SegmentCount = m_SegmentCount;
|
||||
cylinder.StartNormal = m_StartNormal;
|
||||
cylinder.EndNormal = m_EndNormal;
|
||||
cylinder.Transform = m_Transform;
|
||||
|
||||
cylinder.ShowOutline = m_ShowOutline;
|
||||
cylinder.OutlineColor = m_OutlineColor;
|
||||
cylinder.OutlineThickness = m_OutlineThickness;
|
||||
|
||||
cylinder.ShowFill = m_ShowFill;
|
||||
cylinder.FillColor = m_FillColor;
|
||||
|
||||
cylinder.ShowCapCross = m_ShowCapCross;
|
||||
cylinder.CrossColor = m_CrossColor;
|
||||
cylinder.CrossThickness = m_CrossThickness;
|
||||
|
||||
cylinder.ShowStartRing = m_ShowStartRing;
|
||||
cylinder.ShowEndRing = m_ShowEndRing;
|
||||
|
||||
cylinder.GeneratrixCount = m_GeneratrixCount;
|
||||
cylinder.GeneratrixColor = m_GeneratrixColor;
|
||||
|
||||
cylinder.MarkDirty();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向量箭头(矢量场)元素的配置。
|
||||
///
|
||||
/// <para><b>一条记录怎么读</b>:前 3 维是"一个点",后 3 维是"另一个点",
|
||||
/// 按 <see cref="VectorMode"/> 解释成一支箭头——</para>
|
||||
/// <list type="bullet">
|
||||
/// <item><see cref="XChartArrowVectorMode.Absolute"/>(绝对):后 3 维是<b>方向矢量</b>本身,
|
||||
/// 起点 + 方向×倍率 = 箭头末端。螺旋点在这种读法下画出来是一片"刺猬"(每根朝外)。</item>
|
||||
/// <item><see cref="XChartArrowVectorMode.Relative"/>(相对):后 3 维是<b>第二个点</b>,
|
||||
/// 箭头 = 第一点指向第二点(差向量×倍率)。一串首尾相接的点在这种读法下就是"连着头往下转"。</item>
|
||||
/// </list>
|
||||
///
|
||||
/// <para><b>连续绘制</b>(<see cref="Continuous"/>):把所有记录的点<b>按顺序连成一条折线</b>——
|
||||
/// 这时"相对/绝对"没有意义(连起来的本来就是空间里的一条路径)⇒ 面板上那两个选项**隐藏**
|
||||
/// (互斥显示,见 <see cref="IsFieldHidden"/>)。</para>
|
||||
///
|
||||
/// <para><b>颜色是可选的额外维度</b>:<c>[h, s, v]</c>(可选 <c>+a</c>)接在矢量后面读,
|
||||
/// 于是"给每个点标颜色"不需要往表里塞非数值列——色相走 0..256 的色环,其余三个按 0..1。</para>
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[XChartElement("arrows", "向量箭头", 50)]
|
||||
public sealed class XChartArrowConfig : XChartElementConfig
|
||||
{
|
||||
/// <summary>3 维起点 + 3 维方向/第二点</summary>
|
||||
public const int VectorDimensionCount = 6;
|
||||
|
||||
[Header("数据")]
|
||||
[SerializeField, Tooltip("起点从第几个数值维开始(一条记录要占 6 维:起点 3 + 方向或第二点 3)")]
|
||||
private int m_OriginDimension;
|
||||
|
||||
[SerializeField, Tooltip("绝对:后三维是方向矢量(起点→末端)。相对:后三维是第二个点," +
|
||||
"箭头=第一点指向第二点。**连续绘制打开时这一项不适用**")]
|
||||
private XChartArrowVectorMode m_VectorMode = XChartArrowVectorMode.Absolute;
|
||||
|
||||
[SerializeField, Tooltip("把每条记录的点按顺序连成一条折线(只有最后一段带箭头)。" +
|
||||
"打开之后「相对/绝对」被隐藏——连起来的是一条空间路径,谈不到这两者")]
|
||||
private bool m_Continuous;
|
||||
|
||||
[SerializeField, Tooltip("方向向量长度倍率(立方体单位);相对模式下作用在差向量上")]
|
||||
private float m_DirectionScale = 0.25f;
|
||||
|
||||
[SerializeField, Range(0, 100000), Tooltip("最多画多少条(0=不限);防止十万条记录一口气铺满")]
|
||||
private int m_MaxArrowCount = 512;
|
||||
|
||||
[SerializeField, Tooltip("箭头自己的变换(叠在图的空间之上)")]
|
||||
private XChartToolTransform m_Transform = XChartToolTransform.Identity;
|
||||
|
||||
[Header("颜色(可选维度)")]
|
||||
[SerializeField, Tooltip("用额外的数值维给每支箭头上色。三个维依次是 色相(0..256 色环) / 饱和度(0..1) / 明度(0..1)")]
|
||||
private bool m_UseColorDimensions;
|
||||
|
||||
[SerializeField, Tooltip("颜色三维从第几维开始(默认 6=紧跟在矢量后面)。" +
|
||||
"这些维**不进**前面的数据语义,只是被读走做颜色")]
|
||||
private int m_ColorDimension = VectorDimensionCount;
|
||||
|
||||
[SerializeField, Tooltip("再加一维不透明度(0..1),读颜色起点维 +3")]
|
||||
private bool m_HasAlphaDimension;
|
||||
|
||||
[Header("外观")]
|
||||
[SerializeField, Tooltip("没有给颜色维度时用的单色")]
|
||||
private Color m_ArrowColor = new Color(0.95f, 0.6f, 0.25f, 0.9f);
|
||||
[SerializeField, Min(0.1f), Tooltip("杆宽(像素)")]
|
||||
private float m_ShaftWidth = 2f;
|
||||
[SerializeField, Min(0.1f), Tooltip("箭头长(像素)")]
|
||||
private float m_HeadLength = 10f;
|
||||
[SerializeField, Min(0.1f), Tooltip("箭头宽(像素)")]
|
||||
private float m_HeadWidth = 7f;
|
||||
[SerializeField, Tooltip("零向量也画成一个极小四边形(不画的话它会整条消失)")]
|
||||
private bool m_ShowZeroVectorsAsDots;
|
||||
|
||||
public override string ElementName => "Arrows";
|
||||
|
||||
/// <summary>
|
||||
/// 这个元素要几个数值维才画得出来(面板据此报"表里只有几个",不再"什么都不显示")。
|
||||
/// 不透明度那一维**不算硬需求**(多给就多读、没给退回单色 alpha)——算进去就成了
|
||||
/// "勾一个开关,整个元素消失"。
|
||||
/// </summary>
|
||||
public override int RequiredNumericDimensions
|
||||
=> VectorDimensionCount + (m_UseColorDimensions ? 3 : 0);
|
||||
|
||||
/// <summary>
|
||||
/// 互斥显示:连续绘制打开时「相对/绝对」没有意义(他 2026-10-25:勾一个就把排斥的那几个藏起来)。
|
||||
/// 判据只有一处,面板照着问,不在面板里写特例。
|
||||
/// </summary>
|
||||
public override bool IsFieldHidden(string fieldName)
|
||||
{
|
||||
if (fieldName == nameof(m_VectorMode)) return m_Continuous;
|
||||
if (fieldName == nameof(m_ColorDimension) || fieldName == nameof(m_HasAlphaDimension))
|
||||
return !m_UseColorDimensions;
|
||||
return false;
|
||||
}
|
||||
|
||||
public override XChartToolBase CreateTool() => new ChartToolVectorArrows();
|
||||
|
||||
public override void ApplyTo(XChartToolBase tool)
|
||||
{
|
||||
if (!(tool is ChartToolVectorArrows arrows)) return;
|
||||
|
||||
arrows.OriginDimension = m_OriginDimension;
|
||||
arrows.VectorMode = m_VectorMode;
|
||||
arrows.Continuous = m_Continuous;
|
||||
arrows.DirectionScale = m_DirectionScale;
|
||||
arrows.MaxArrowCount = m_MaxArrowCount;
|
||||
arrows.Transform = m_Transform;
|
||||
|
||||
arrows.UseColorDimensions = m_UseColorDimensions;
|
||||
arrows.ColorDimension = m_ColorDimension;
|
||||
arrows.HasAlphaDimension = m_HasAlphaDimension;
|
||||
|
||||
arrows.ArrowColor = m_ArrowColor;
|
||||
arrows.ShaftWidth = m_ShaftWidth;
|
||||
arrows.HeadLength = m_HeadLength;
|
||||
arrows.HeadWidth = m_HeadWidth;
|
||||
arrows.ShowZeroVectorsAsDots = m_ShowZeroVectorsAsDots;
|
||||
|
||||
arrows.MarkDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 453779c1de4515048aa67a2ac98cac43
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user