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,156 @@
|
||||
using UnityEngine;
|
||||
using XericUI.Core;
|
||||
using XericUI.XChart.Mesh;
|
||||
|
||||
namespace XericUI.XChart.Space
|
||||
{
|
||||
/// <summary>
|
||||
/// 空间工具(<b>数据源工具</b>):一张图里<b>唯一</b>的"立方体局部空间 → 图表局部像素"权威。
|
||||
///
|
||||
/// <para><b>为什么要有它</b>:背景网格、线框、散点、轴线以前各算各的投影——同一份 pitch/yaw
|
||||
/// 在两个类里各存一份、各推一遍,点就落不进盒子里。本工具把基变换收在一处,
|
||||
/// 所有元素只问它要矩阵和面帧:<c>投影 = 基变换 ∘ 工具变换</c>,
|
||||
/// 于是"点落在背景里"是结构保证,不是靠两边数值凑巧一致。</para>
|
||||
///
|
||||
/// <para><b>两档出口</b>:</para>
|
||||
/// <list type="bullet">
|
||||
/// <item><see cref="Compose"/>:基矩阵 × 工具矩阵,得到该工具用的仿射矩阵,
|
||||
/// 交给 <see cref="XChartCubeGeometry.Project(Matrix4x4,float,Vector2,Vector3)"/> /
|
||||
/// <see cref="XChartCubeGeometry.BuildFaceFrames"/> 复用同一套投影与面帧推导。</item>
|
||||
/// <item><see cref="CubeToPixel"/> / <see cref="Offset"/>:需要自己换算长度或偏移的元素(轴线的
|
||||
/// 刻度长度、标签的让位距离)用它们,不要各自再算一遍 scale。</item>
|
||||
/// </list>
|
||||
///
|
||||
/// <para><b>版本号</b>:<see cref="ProjectionVersion"/> 在标记脏的那一刻递增,
|
||||
/// 消费者拿它比对"要不要重新投影",不需要谁去清脏标记。</para>
|
||||
/// </summary>
|
||||
[AddComponentMenu(XericRegistrationPaths.Chart + "/Space/Space Tool")]
|
||||
public sealed class ChartToolSpace : XChartToolBase
|
||||
{
|
||||
/// <summary>图表自带的空间工具在管理器里的名字</summary>
|
||||
public const string DefaultToolName = "Space";
|
||||
|
||||
/// <summary>空间是"投影权威":投影矩阵没算出来,谁也算不了 ⇒ 归定义组,最先跑(规范 §11.4)</summary>
|
||||
public override XChartToolCategory Category => XChartToolCategory.Definition;
|
||||
|
||||
// ── 基变换 ────────────────────────────────────────────────
|
||||
|
||||
/// <summary>图表级姿态(四元数,唯一的旋转权威量)</summary>
|
||||
public Quaternion Rotation { get; set; } =
|
||||
XChartCubeView.RotationOf(XChartCubeViewPreset.Isometric);
|
||||
|
||||
/// <summary>半边长(UI 像素)</summary>
|
||||
public float HalfEdge { get; set; } = 120f;
|
||||
|
||||
/// <summary>尺寸口径(默认半边长:尺度不随旋转变化)</summary>
|
||||
public XChartCubeSizeMode SizeMode { get; set; } = XChartCubeSizeMode.HalfEdgeLength;
|
||||
|
||||
/// <summary>立方体中心在图表局部像素里的偏移</summary>
|
||||
public Vector2 CenterOffset { get; set; }
|
||||
|
||||
// ── 派生状态 ──────────────────────────────────────────────
|
||||
|
||||
private Matrix4x4 m_Base;
|
||||
private float m_Scale = 1f;
|
||||
private Vector2 m_Offset;
|
||||
private bool m_ProjectionDirty = true;
|
||||
|
||||
/// <summary>投影版本号(标记脏时递增)</summary>
|
||||
public int ProjectionVersion { get; private set; }
|
||||
|
||||
/// <summary>基变换矩阵(纯旋转)</summary>
|
||||
public Matrix4x4 BaseMatrix { get { EnsureProjection(); return m_Base; } }
|
||||
|
||||
/// <summary>立方体单位 → 像素的缩放(半边长口径下就等于 <see cref="HalfEdge"/>)</summary>
|
||||
public float CubeToPixel { get { EnsureProjection(); return m_Scale; } }
|
||||
|
||||
/// <summary>投影后的平移(图表局部像素)</summary>
|
||||
public Vector2 Offset { get { EnsureProjection(); return m_Offset; } }
|
||||
|
||||
public void MarkProjectionDirty()
|
||||
{
|
||||
m_ProjectionDirty = true;
|
||||
ProjectionVersion++;
|
||||
}
|
||||
|
||||
private void EnsureProjection()
|
||||
{
|
||||
if (!m_ProjectionDirty) return;
|
||||
m_ProjectionDirty = false;
|
||||
m_Base = XChartCubeView.ToMatrix(Rotation);
|
||||
XChartCubeView.ComputeProjection(m_Base, HalfEdge, CenterOffset, SizeMode, out m_Scale, out m_Offset);
|
||||
}
|
||||
|
||||
// ── 出口 ──────────────────────────────────────────────────
|
||||
|
||||
/// <summary>基变换 ∘ 工具变换(<paramref name="tool"/> 为恒等时就只剩基矩阵)</summary>
|
||||
public Matrix4x4 Compose(XChartToolTransform tool)
|
||||
{
|
||||
EnsureProjection();
|
||||
return tool.IsIdentity ? m_Base : XChartCubeView.Compose(m_Base, tool.Build());
|
||||
}
|
||||
|
||||
/// <summary>立方体局部点 → 图表局部像素(带工具变换)</summary>
|
||||
public Vector2 Project(Vector3 cubePoint, Matrix4x4 composed)
|
||||
{
|
||||
EnsureProjection();
|
||||
return XChartCubeGeometry.Project(composed, m_Scale, m_Offset, cubePoint);
|
||||
}
|
||||
|
||||
/// <summary>立方体局部点 → 图表局部像素(只用基变换)</summary>
|
||||
public Vector2 Project(Vector3 cubePoint)
|
||||
{
|
||||
EnsureProjection();
|
||||
return XChartCubeGeometry.Project(m_Base, m_Scale, m_Offset, cubePoint);
|
||||
}
|
||||
|
||||
/// <summary>深度(越大越靠近观察者),用来做画家序</summary>
|
||||
public float DepthOf(Vector3 cubePoint, Matrix4x4 composed)
|
||||
{
|
||||
EnsureProjection();
|
||||
return composed.MultiplyPoint3x4(cubePoint).z;
|
||||
}
|
||||
|
||||
/// <summary>六个面帧(<paramref name="output"/> 长度至少 6;顺序同 <see cref="XChartCubeGeometry.CubeFace"/>)</summary>
|
||||
public void BuildFaceFrames(XChartFaceFrame[] output, Matrix4x4 composed)
|
||||
{
|
||||
EnsureProjection();
|
||||
XChartCubeGeometry.BuildFaceFrames(composed, m_Scale, m_Offset, output);
|
||||
}
|
||||
|
||||
/// <summary>六个面帧(只用基变换)</summary>
|
||||
public void BuildFaceFrames(XChartFaceFrame[] output)
|
||||
{
|
||||
EnsureProjection();
|
||||
XChartCubeGeometry.BuildFaceFrames(m_Base, m_Scale, m_Offset, output);
|
||||
}
|
||||
|
||||
/// <summary>某个面是否朝向观察者。注意:法线是按矩阵的线性部分转的,
|
||||
/// 工具带<b>非等比缩放</b>时这只是近似(立方体场景里够用,别拿它判曲面)。</summary>
|
||||
public bool IsFaceVisible(XChartCubeGeometry.CubeFace face, Matrix4x4 composed)
|
||||
=> XChartCubeGeometry.IsFaceVisible(composed, face);
|
||||
|
||||
public bool IsFaceVisible(XChartCubeGeometry.CubeFace face)
|
||||
{
|
||||
EnsureProjection();
|
||||
return XChartCubeGeometry.IsFaceVisible(m_Base, face);
|
||||
}
|
||||
|
||||
// ── 数据域 → 立方体域 ─────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// 归一化数据(每维 [0,1])→ 立方体局部点([-1,1])。
|
||||
/// 数据元素统一走这一条,保证"0.5 就是立方体中间"这件事只有一处定义。
|
||||
/// </summary>
|
||||
public static Vector3 NormalizedToCube(Vector3 zeroOne)
|
||||
=> new Vector3(zeroOne.x * 2f - 1f, zeroOne.y * 2f - 1f, zeroOne.z * 2f - 1f);
|
||||
|
||||
protected override void OnInitialize()
|
||||
{
|
||||
// 空间本身不画东西:它只是别的元素的坐标系。节点仍然要建,
|
||||
// 因为元素的兄弟序(= 绘制序)里得有它占的那一格
|
||||
SetVisible(true);
|
||||
MarkProjectionDirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d9c1534a81b1ed4883cbc4eebcc9581
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,116 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using XericLibrary.Runtime.UIGraph;
|
||||
using PrimitiveType = XericLibrary.Runtime.UIGraph.PrimitiveType;
|
||||
using XericUI.XChart.Pseudo3D;
|
||||
using XericUI.XChart.Space;
|
||||
|
||||
namespace XericUI.XChart.Space
|
||||
{
|
||||
/// <summary>
|
||||
/// 散点元素的配置。数据默认<b>直接读图的数据源</b>(按维度下标 + 聚合范围归一化),
|
||||
/// 所以配好三个维度下标就能出图,不需要在图表上为散点再写一遍数据。
|
||||
///
|
||||
/// <para>关掉 <see cref="m_AutoFromData"/> 就变成"脚本喂点"模式:由调用方
|
||||
/// <c>SetCubePoints</c> 给立方体单位的点([-1,1] 就是盒子内部)。</para>
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[XChartElement("scatter", "散点", 30)]
|
||||
public sealed class XChartScatterConfig : XChartElementConfig
|
||||
{
|
||||
[Header("数据来源")]
|
||||
[SerializeField, Tooltip("真=按下面的维度下标读图的数据源;假=脚本用 SetCubePoints 喂")]
|
||||
private bool m_AutoFromData = true;
|
||||
|
||||
[SerializeField, Tooltip("X / Y / Z 轴取哪几个维度下标(绑定;显示范围在 chart 的数据板块逐维给)")]
|
||||
private int m_DimensionX;
|
||||
[SerializeField] private int m_DimensionY = 1;
|
||||
[SerializeField] private int m_DimensionZ = 2;
|
||||
|
||||
[SerializeField, Tooltip("点云自己的变换(叠在图的空间之上)")]
|
||||
private XChartToolTransform m_Transform = XChartToolTransform.Identity;
|
||||
|
||||
[SerializeField, Min(0), Tooltip("桶采样目标点数:0 或 ≥ 记录数=不聚合;填了数就把记录按桶归约成这么多个点")]
|
||||
private int m_BucketTargetPoints;
|
||||
|
||||
[SerializeField, Tooltip("桶内取哪个统计量当这个点:抽稀用 First/Last(不造新值),看趋势用 Mean/Median,看包络用 Minimum/Maximum")]
|
||||
private XChartAggregateKind m_BucketReduce = XChartAggregateKind.Last;
|
||||
|
||||
[Header("外观")]
|
||||
[SerializeField, Min(0.5f), Tooltip("点的大小(像素)")]
|
||||
private float m_PointSize = 8f;
|
||||
|
||||
[SerializeField, Tooltip("图元类型")]
|
||||
private PrimitiveType m_PointType = PrimitiveType.Polygon;
|
||||
|
||||
[SerializeField, Tooltip("图元边数(多边形时有效)")]
|
||||
private int m_SideCount = 8;
|
||||
|
||||
[SerializeField, Tooltip("填充色")]
|
||||
private Color m_PointColor = new Color(0.33f, 0.67f, 1f, 1f);
|
||||
|
||||
[SerializeField, Tooltip("中心色;与填充色不同就走径向渐变")]
|
||||
private Color m_CenterColor = Color.white;
|
||||
|
||||
[SerializeField, Tooltip("边框色")]
|
||||
private Color m_BorderColor = new Color(0.2f, 0.2f, 0.2f, 1f);
|
||||
|
||||
[SerializeField, Min(0f), Tooltip("边框宽度")]
|
||||
private float m_BorderThickness = 1f;
|
||||
|
||||
[Header("按数值上色")]
|
||||
[SerializeField, Tooltip("用某一维的数值做颜色渐变")]
|
||||
private bool m_EnableColorMapping;
|
||||
|
||||
[SerializeField, Tooltip("拿哪一维的数值")]
|
||||
private int m_ColorMapDimension = 2;
|
||||
|
||||
[SerializeField, Tooltip("范围下限对应颜色")]
|
||||
private Color m_ColorMapMin = Color.blue;
|
||||
|
||||
[SerializeField, Tooltip("范围上限对应颜色")]
|
||||
private Color m_ColorMapMax = Color.red;
|
||||
|
||||
public override string ElementName => "Scatter";
|
||||
|
||||
public override XChartToolBase CreateTool() => new ChartToolScatterPoints();
|
||||
|
||||
public override void ApplyTo(XChartToolBase tool)
|
||||
{
|
||||
if (!(tool is ChartToolScatterPoints scatter)) return;
|
||||
|
||||
scatter.AutoFromData = m_AutoFromData;
|
||||
scatter.DimensionX = m_DimensionX;
|
||||
scatter.DimensionY = m_DimensionY;
|
||||
scatter.DimensionZ = m_DimensionZ;
|
||||
scatter.Transform = m_Transform;
|
||||
scatter.BucketTargetPoints = m_BucketTargetPoints;
|
||||
scatter.BucketReduce = m_BucketReduce;
|
||||
|
||||
scatter.PointSize = m_PointSize;
|
||||
scatter.PointType = m_PointType;
|
||||
scatter.SideCount = m_SideCount;
|
||||
scatter.PointColor = m_PointColor;
|
||||
scatter.CenterColor = m_CenterColor;
|
||||
scatter.BorderColor = m_BorderColor;
|
||||
scatter.BorderThickness = m_BorderThickness;
|
||||
|
||||
scatter.EnableColorMapping = m_EnableColorMapping;
|
||||
scatter.ColorMapDimension = m_ColorMapDimension;
|
||||
scatter.ColorMapMin = m_ColorMapMin;
|
||||
scatter.ColorMapMax = m_ColorMapMax;
|
||||
|
||||
scatter.MarkDataDirty();
|
||||
}
|
||||
|
||||
public override void CaptureFrom(XChartToolBase tool)
|
||||
{
|
||||
if (!(tool is ChartToolScatterPoints scatter)) return;
|
||||
|
||||
m_AutoFromData = scatter.AutoFromData;
|
||||
m_PointSize = scatter.PointSize;
|
||||
m_PointColor = scatter.PointColor;
|
||||
m_Transform = scatter.Transform;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f4e9ce2094df4084ca17a83c04839e07
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,350 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace XericUI.XChart.Space
|
||||
{
|
||||
/// <summary>
|
||||
/// 插槽的数据模式:决定"怎么切值 + 每个参数怎么解释"。**纯数据描述** —— 切值与解释都能离线断言。
|
||||
/// </summary>
|
||||
public enum XChartSlotMode
|
||||
{
|
||||
/// <summary>2 值/点,2 点一组:起点 → 终点(面内坐标)</summary>
|
||||
Line2D = 0,
|
||||
/// <summary>3 值/点,2 点一组:起点 → 终点(立方体单位)</summary>
|
||||
Line3D = 1,
|
||||
/// <summary>2 值=圆心,1 值=半径(面内圆)</summary>
|
||||
Circle2D = 2,
|
||||
/// <summary>3 值=圆心,3 值=法向,1 值=半径(空间圆,法向定斜切面)</summary>
|
||||
Circle3D = 3,
|
||||
/// <summary>3 值/点,4 点一组(ST00→ST01→ST11→ST10):空间四边形面片</summary>
|
||||
Quad3D = 4,
|
||||
}
|
||||
|
||||
/// <summary>读写权限:只读槽被写要拒(可写用于"拖手柄写回去让网格跟随")。</summary>
|
||||
public enum XChartSlotAccess
|
||||
{
|
||||
ReadOnly = 0,
|
||||
ReadWrite = 1,
|
||||
}
|
||||
|
||||
/// <summary>模式的切值规则(各一条判据:一组几个值、个数不对就拒、越界就拒)。</summary>
|
||||
public static class XChartSlotModes
|
||||
{
|
||||
/// <summary>空间面片固定四个角(ST00→ST01→ST11→ST10)</summary>
|
||||
public const int QuadCorners = 4;
|
||||
|
||||
/// <summary>这种模式一组要几个值;未知模式返回 0</summary>
|
||||
public static int ValuesPerGroup(XChartSlotMode mode)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case XChartSlotMode.Line2D: return 4; // 2 值 × 2 点
|
||||
case XChartSlotMode.Line3D: return 6; // 3 值 × 2 点
|
||||
case XChartSlotMode.Circle2D: return 3; // 圆心 2 + 半径 1
|
||||
case XChartSlotMode.Circle3D: return 7; // 圆心 3 + 法向 3 + 半径 1
|
||||
case XChartSlotMode.Quad3D: return 12; // 3 值 × 4 点
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>一组里的点数(圆算 0,它的"点"是圆心 + 半径、另有法向)</summary>
|
||||
public static int PointsPerGroup(XChartSlotMode mode)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case XChartSlotMode.Line2D:
|
||||
case XChartSlotMode.Line3D: return 2;
|
||||
case XChartSlotMode.Quad3D: return QuadCorners;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>按模式切组:值个数必须正好装下整数个组,否则拒收(越界/个数不对是一类错)</summary>
|
||||
public static bool TryGroupCount(XChartSlotMode mode, int valueCount, out int groups)
|
||||
{
|
||||
groups = 0;
|
||||
int perGroup = ValuesPerGroup(mode);
|
||||
if (perGroup <= 0 || valueCount <= 0 || valueCount % perGroup != 0) return false;
|
||||
groups = valueCount / perGroup;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>值必须是有限数:NaN/Inf 会让几何静默画歪,所以发布时就拒(零要说清零在哪一层)</summary>
|
||||
public static bool AllFinite(float[] values, int count)
|
||||
{
|
||||
if (values == null) return false;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (float.IsNaN(values[i]) || float.IsInfinity(values[i])) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 一个插槽的视图:值留在板子里(不复制),视图只带模式/权限/版本 + 该模式的取点方法。
|
||||
/// <b>过期视图不许写</b>:板子版本前进过就说明它已经不是最新的那份。
|
||||
/// </summary>
|
||||
public struct XChartSlotView
|
||||
{
|
||||
private float[] m_Values;
|
||||
private XChartSlotBoard m_Board;
|
||||
private int m_BoardVersion;
|
||||
|
||||
public string Id { get; internal set; }
|
||||
public XChartSlotMode Mode { get; internal set; }
|
||||
public XChartSlotAccess Access { get; internal set; }
|
||||
public int Version { get; internal set; }
|
||||
public int GroupCount { get; internal set; }
|
||||
|
||||
internal void Bind(float[] values, XChartSlotBoard board, int boardVersion)
|
||||
{
|
||||
m_Values = values;
|
||||
m_Board = board;
|
||||
m_BoardVersion = boardVersion;
|
||||
}
|
||||
|
||||
private bool InRange(int group, int point, int pointCount)
|
||||
{
|
||||
return m_Values != null && group >= 0 && group < GroupCount
|
||||
&& point >= 0 && point < pointCount;
|
||||
}
|
||||
|
||||
/// <summary>取第 group 组第 point 个点(2D 模式 z 补 0)</summary>
|
||||
public bool TryGetPoint(int group, int point, out Vector3 result)
|
||||
{
|
||||
result = Vector3.zero;
|
||||
int points = XChartSlotModes.PointsPerGroup(Mode);
|
||||
if (!InRange(group, point, points)) return false;
|
||||
int stride = 3;
|
||||
if (Mode == XChartSlotMode.Line2D) stride = 2;
|
||||
int baseIndex = group * XChartSlotModes.ValuesPerGroup(Mode) + point * stride;
|
||||
result = new Vector3(m_Values[baseIndex], m_Values[baseIndex + 1],
|
||||
stride == 3 ? m_Values[baseIndex + 2] : 0f);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>取一条线(Line2D / Line3D)</summary>
|
||||
public bool TryGetLine(int group, out Vector3 from, out Vector3 to)
|
||||
{
|
||||
from = Vector3.zero;
|
||||
to = Vector3.zero;
|
||||
if (Mode != XChartSlotMode.Line2D && Mode != XChartSlotMode.Line3D) return false;
|
||||
return TryGetPoint(group, 0, out from) && TryGetPoint(group, 1, out to);
|
||||
}
|
||||
|
||||
/// <summary>取一个圆(Circle2D 的法向固定 +Z;Circle3D 用法向定斜切面)</summary>
|
||||
public bool TryGetCircle(int group, out Vector3 center, out Vector3 normal, out float radius)
|
||||
{
|
||||
center = Vector3.zero;
|
||||
normal = Vector3.forward;
|
||||
radius = 0f;
|
||||
if (group < 0 || group >= GroupCount || m_Values == null) return false;
|
||||
|
||||
if (Mode == XChartSlotMode.Circle2D)
|
||||
{
|
||||
int i = group * 3;
|
||||
center = new Vector3(m_Values[i], m_Values[i + 1], 0f);
|
||||
radius = m_Values[i + 2];
|
||||
return true;
|
||||
}
|
||||
if (Mode == XChartSlotMode.Circle3D)
|
||||
{
|
||||
int i = group * 7;
|
||||
center = new Vector3(m_Values[i], m_Values[i + 1], m_Values[i + 2]);
|
||||
normal = new Vector3(m_Values[i + 3], m_Values[i + 4], m_Values[i + 5]);
|
||||
radius = m_Values[i + 6];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>取一个面片(Quad3D),角序固定 ST00→ST01→ST11→ST10</summary>
|
||||
public bool TryGetQuad(int group, out Vector3 cornerST00, out Vector3 cornerST01,
|
||||
out Vector3 cornerST11, out Vector3 cornerST10)
|
||||
{
|
||||
cornerST00 = Vector3.zero;
|
||||
cornerST01 = Vector3.zero;
|
||||
cornerST11 = Vector3.zero;
|
||||
cornerST10 = Vector3.zero;
|
||||
if (Mode != XChartSlotMode.Quad3D) return false;
|
||||
return TryGetPoint(group, 0, out cornerST00)
|
||||
&& TryGetPoint(group, 1, out cornerST01)
|
||||
&& TryGetPoint(group, 2, out cornerST11)
|
||||
&& TryGetPoint(group, 3, out cornerST10);
|
||||
}
|
||||
|
||||
/// <summary>能不能写:只读槽、或视图已经过期(板子版本前进过)都不行</summary>
|
||||
public bool CanWrite => m_Board != null && Access == XChartSlotAccess.ReadWrite
|
||||
&& m_BoardVersion == m_Board.Version && m_Values != null;
|
||||
|
||||
/// <summary>把一条线写回去(可拖动的手柄就是靠它反写网格);写成功会推板子版本,别的消费者据此重建</summary>
|
||||
public bool TryWriteLine(int group, Vector3 from, Vector3 to)
|
||||
{
|
||||
if (!CanWrite) return false;
|
||||
if (Mode != XChartSlotMode.Line2D && Mode != XChartSlotMode.Line3D) return false;
|
||||
if (group < 0 || group >= GroupCount) return false;
|
||||
|
||||
int stride = Mode == XChartSlotMode.Line2D ? 2 : 3;
|
||||
int baseIndex = group * XChartSlotModes.ValuesPerGroup(Mode);
|
||||
WritePoint(baseIndex, from, stride);
|
||||
WritePoint(baseIndex + stride, to, stride);
|
||||
m_Board.BumpVersionForWrite();
|
||||
m_BoardVersion = m_Board.Version; // 写成功之后自己也跟着前进
|
||||
return true;
|
||||
}
|
||||
|
||||
private void WritePoint(int index, Vector3 point, int stride)
|
||||
{
|
||||
m_Values[index] = point.x;
|
||||
m_Values[index + 1] = point.y;
|
||||
if (stride == 3) m_Values[index + 2] = point.z;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插槽板:定义网格的工具把自己的一组组标定顶点放上来,其余工具按 id 取用。
|
||||
/// <b>几何的通路,不碰数据</b>(数据仍走 IXChartDataSource)。
|
||||
/// </summary>
|
||||
public interface IXChartSlotBoard
|
||||
{
|
||||
/// <summary>板子版本:任何一次成功发布/写回都会前进;消费者据此判断要不要重建</summary>
|
||||
int Version { get; }
|
||||
|
||||
/// <summary>发布的槽数(报表用)</summary>
|
||||
int SlotCount { get; }
|
||||
|
||||
bool Publish(string id, XChartSlotMode mode, XChartSlotAccess access, float[] values, out string reason);
|
||||
bool TryRead(string id, out XChartSlotView view);
|
||||
|
||||
/// <summary>
|
||||
/// 第 <paramref name="index"/> 个已发布槽的 id 与模式。<b>按索引取、不给集合</b>:
|
||||
/// 这份清单只给面板做"已发布插槽"下拉用(每帧取一次不能分配)。
|
||||
/// </summary>
|
||||
bool TryGetSlotAt(int index, out string id, out XChartSlotMode mode);
|
||||
|
||||
/// <summary>
|
||||
/// 撤下一个插槽:从这一刻起<b>这里没有几何可吸附</b>。真撤掉了返回 true,不在板上返回 false。
|
||||
/// </summary>
|
||||
bool Unpublish(string id);
|
||||
|
||||
void Clear();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public sealed class XChartSlotBoard : IXChartSlotBoard
|
||||
{
|
||||
private sealed class Entry
|
||||
{
|
||||
public string Id;
|
||||
public XChartSlotMode Mode;
|
||||
public XChartSlotAccess Access;
|
||||
public float[] Values;
|
||||
public int Version;
|
||||
}
|
||||
|
||||
private readonly List<Entry> m_Entries = new List<Entry>(8);
|
||||
|
||||
public int Version { get; private set; }
|
||||
public int SlotCount => m_Entries.Count;
|
||||
|
||||
/// <summary>发布(同 id 覆盖)。拒收:空值、个数不是整数组、含 NaN/Inf、未知模式。</summary>
|
||||
public bool Publish(string id, XChartSlotMode mode, XChartSlotAccess access, float[] values, out string reason)
|
||||
{
|
||||
reason = null;
|
||||
if (string.IsNullOrEmpty(id)) { reason = "插槽 id 为空"; return false; }
|
||||
|
||||
int groups;
|
||||
if (!XChartSlotModes.TryGroupCount(mode, values == null ? 0 : values.Length, out groups))
|
||||
{
|
||||
reason = "值个数不是这种模式的整数组:" + (values == null ? 0 : values.Length);
|
||||
return false;
|
||||
}
|
||||
if (!XChartSlotModes.AllFinite(values, values.Length)) { reason = "值里有 NaN/Inf"; return false; }
|
||||
|
||||
var entry = Find(id);
|
||||
if (entry == null)
|
||||
{
|
||||
entry = new Entry { Id = id };
|
||||
m_Entries.Add(entry);
|
||||
}
|
||||
entry.Mode = mode;
|
||||
entry.Access = access;
|
||||
if (entry.Values == null || entry.Values.Length != values.Length) entry.Values = new float[values.Length];
|
||||
Array.Copy(values, entry.Values, values.Length);
|
||||
Version++;
|
||||
entry.Version = Version;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryRead(string id, out XChartSlotView view)
|
||||
{
|
||||
view = default(XChartSlotView);
|
||||
var entry = Find(id);
|
||||
if (entry == null) return false;
|
||||
|
||||
view.Id = entry.Id;
|
||||
view.Mode = entry.Mode;
|
||||
view.Access = entry.Access;
|
||||
view.Version = entry.Version;
|
||||
XChartSlotModes.TryGroupCount(entry.Mode, entry.Values.Length, out int groups);
|
||||
view.GroupCount = groups;
|
||||
view.Bind(entry.Values, this, Version);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
if (m_Entries.Count == 0) return;
|
||||
m_Entries.Clear();
|
||||
Version++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 撤下一个插槽(不在板上就没什么可撤 ⇒ false,并且不推版本)。
|
||||
///
|
||||
/// <para><b>为什么板子必须有这一面</b>:只有 <see cref="Publish"/>(同 id 覆盖)的话,
|
||||
/// 定义者说得出"这里的线变成这一批",却说不出"<b>这里不再有线</b>"——上一轮那批值会一直留在板上,
|
||||
/// 消费者照吸不误。本轮把格线收成一个作者之后,这件事必须有出口:网格掩码筛掉某个面、
|
||||
/// 或者整个网格元素被删掉,那些面的格线插槽就得撤下来,否则"关掉一边、另一边还跟着"
|
||||
/// 只是换到插槽那一层复发(规范 §22)。空数组发布是<em>拒</em>的(组数必须是正数,
|
||||
/// 见 <see cref="XChartSlotModes.TryGroupCount"/>),所以"撤下"只能是一个显式动作。</para>
|
||||
/// </summary>
|
||||
public bool Unpublish(string id)
|
||||
{
|
||||
if (string.IsNullOrEmpty(id)) return false;
|
||||
for (int i = 0; i < m_Entries.Count; i++)
|
||||
{
|
||||
if (m_Entries[i].Id != id) continue;
|
||||
m_Entries.RemoveAt(i);
|
||||
Version++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGetSlotAt(int index, out string id, out XChartSlotMode mode)
|
||||
{
|
||||
id = null;
|
||||
mode = default(XChartSlotMode);
|
||||
if (index < 0 || index >= m_Entries.Count) return false;
|
||||
|
||||
id = m_Entries[index].Id;
|
||||
mode = m_Entries[index].Mode;
|
||||
return true;
|
||||
}
|
||||
|
||||
private Entry Find(string id)
|
||||
{
|
||||
for (int i = 0; i < m_Entries.Count; i++)
|
||||
{
|
||||
if (string.Equals(m_Entries[i].Id, id, StringComparison.Ordinal)) return m_Entries[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>写回也算"内容变了":推版本号,别的消费者下一帧会重建</summary>
|
||||
internal void BumpVersionForWrite() => Version++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b091fa6c38d0c364ab68723d2e25c761
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using XericUI.XChart.Mesh;
|
||||
|
||||
namespace XericUI.XChart.Space
|
||||
{
|
||||
/// <summary>
|
||||
/// 空间(基变换)元素的配置——<b>整张图的坐标系就是这一份</b>。
|
||||
///
|
||||
/// <para>图表在初始化时会保证图里一定有一个 <see cref="ChartToolSpace"/>:两条注册路径
|
||||
/// 都没登记时自动补一个默认的。想调它就在层级面板里加这个元素(<b>加出来的是同一个名字
|
||||
/// "Space",所以不会和默认的那个并存</b>),或者在脚本里直接改 <c>chart.Space</c>。</para>
|
||||
///
|
||||
/// <para><b>渲染只看一个量:四元数 <see cref="Rotation"/></b>。没有相机、没有焦距、没有第二处
|
||||
/// 输入;尺寸也只有一个量(半边长)。理由与换算见 <see cref="XChartCubeView"/>。</para>
|
||||
///
|
||||
/// <para><b>预设是按钮不是模式</b>:选一次预设 = 把那个四元数写进 <see cref="Rotation"/>,
|
||||
/// 选择器随即回到"手动"。所以不存在"选了预设,Rotation 字段就失效"这种事——
|
||||
/// 想在等距基础上再拧一点,直接改 Rotation 就行。</para>
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
[XChartElement("space", "空间(基变换)", -1000)]
|
||||
public sealed class XChartSpaceConfig : XChartElementConfig
|
||||
{
|
||||
[Header("基变换")]
|
||||
[SerializeField, Tooltip("立方体半边长(UI 像素):真实边长的一半,也等于面心到中心的距离")]
|
||||
private float m_HalfEdge = 120f;
|
||||
|
||||
[SerializeField, Tooltip("视角预设:<b>一次性动作,不是模式</b>。选它在写入的那一刻把预设四元数灌进下面的 Rotation," +
|
||||
"然后自己回到\"手动\"。渲染永远只读 Rotation。")]
|
||||
private XChartCubeViewPreset m_ViewPreset = XChartCubeViewPreset.Manual;
|
||||
|
||||
[SerializeField, Tooltip("姿态四元数(x,y,z,w)——<b>唯一的旋转权威量</b>:没有相机、没有第二处输入")]
|
||||
private Quaternion m_Rotation = XChartCubeView.RotationOf(XChartCubeViewPreset.Isometric);
|
||||
|
||||
[SerializeField, Tooltip("尺寸口径:半边长(尺度与旋转无关)/ 视口适配(铺满固定矩形,转起来会变小)")]
|
||||
private XChartCubeSizeMode m_SizeMode = XChartCubeSizeMode.HalfEdgeLength;
|
||||
|
||||
[SerializeField, Tooltip("立方体中心在图表局部像素里的偏移")]
|
||||
private Vector2 m_CenterOffset = Vector2.zero;
|
||||
|
||||
/// <summary>元素在 ToolsRoot 下的名字(和图表自动补的那个是同一个,所以不会并存)</summary>
|
||||
public override string ElementName => ChartToolSpace.DefaultToolName;
|
||||
|
||||
/// <summary>当前姿态(渲染唯一权威)。写它=放弃预设、手动定角度(与面板上直接改四元数同一条路)</summary>
|
||||
public Quaternion Rotation
|
||||
{
|
||||
get => m_Rotation;
|
||||
set { m_Rotation = value; m_ViewPreset = XChartCubeViewPreset.Manual; }
|
||||
}
|
||||
|
||||
/// <summary>半边长(UI 像素)</summary>
|
||||
public float HalfEdge { get => m_HalfEdge; set => m_HalfEdge = Mathf.Max(0.01f, value); }
|
||||
|
||||
/// <summary>尺寸口径(半边长 / 视口适配)</summary>
|
||||
public XChartCubeSizeMode SizeMode { get => m_SizeMode; set => m_SizeMode = value; }
|
||||
|
||||
/// <summary>中心偏移(图表局部像素)</summary>
|
||||
public Vector2 CenterOffset { get => m_CenterOffset; set => m_CenterOffset = value; }
|
||||
|
||||
/// <summary>一步跳到某个预设视角:等价于把那个四元数写进 <see cref="Rotation"/></summary>
|
||||
public void ApplyViewPreset(XChartCubeViewPreset preset)
|
||||
{
|
||||
if (preset == XChartCubeViewPreset.Manual) return;
|
||||
m_Rotation = XChartCubeView.RotationOf(preset);
|
||||
m_ViewPreset = XChartCubeViewPreset.Manual;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把预设当按钮用:非"手动"时把预设四元数写进 <see cref="m_Rotation"/>,随即把选择器复位。
|
||||
/// 这样"改一下当前视角"和"从某个预设起步"用的是同一个字段,不存在"预设挡着 Rotation 让它失效"。
|
||||
/// </summary>
|
||||
private void ConsumeViewPreset()
|
||||
{
|
||||
if (m_ViewPreset == XChartCubeViewPreset.Manual) return;
|
||||
m_Rotation = XChartCubeView.RotationOf(m_ViewPreset);
|
||||
m_ViewPreset = XChartCubeViewPreset.Manual;
|
||||
}
|
||||
|
||||
public override XChartToolBase CreateTool() => new ChartToolSpace();
|
||||
|
||||
public override void ApplyTo(XChartToolBase tool)
|
||||
{
|
||||
ConsumeViewPreset();
|
||||
if (!(tool is ChartToolSpace space)) return;
|
||||
|
||||
space.Rotation = m_Rotation;
|
||||
space.HalfEdge = m_HalfEdge;
|
||||
space.SizeMode = m_SizeMode;
|
||||
space.CenterOffset = m_CenterOffset;
|
||||
|
||||
// 基变换变了要立刻让版本号走,下游元素下次渲染就会重投影
|
||||
space.MarkProjectionDirty();
|
||||
}
|
||||
|
||||
public override void CaptureFrom(XChartToolBase tool)
|
||||
{
|
||||
if (!(tool is ChartToolSpace space)) return;
|
||||
|
||||
m_HalfEdge = space.HalfEdge;
|
||||
m_SizeMode = space.SizeMode;
|
||||
m_CenterOffset = space.CenterOffset;
|
||||
m_Rotation = space.Rotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e912716caa7c82c4ea9407b9b9aa6620
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,68 @@
|
||||
using UnityEngine;
|
||||
using XericUI.XChart.Mesh;
|
||||
|
||||
namespace XericUI.XChart.Space
|
||||
{
|
||||
/// <summary>
|
||||
/// 工具级变换:<b>作用在立方体局部空间里</b>的一个仿射(缩放 → 旋转 → 平移)。
|
||||
///
|
||||
/// <para><b>单位</b>:立方体局部空间是"半边长 1"的归一化域(顶点在 ±1),所以
|
||||
/// <see cref="Position"/> 的 1 就是立方体边界、<see cref="Scale"/> 的 1 就是原尺寸。
|
||||
/// 这样工具自己不用关心像素,也不用关心图表当前多大。</para>
|
||||
///
|
||||
/// <para><b>和基变换的关系</b>:投影时算的是 <c>Base ∘ Tool</c>(先工具、后基),
|
||||
/// 由 <see cref="XericUI.XChart.Mesh.XChartCubeView.Compose"/> 复合,
|
||||
/// 结果仍然是一个仿射矩阵,所以面帧依旧落在平行四边形那一档(协议见
|
||||
/// <see cref="XChartFaceFrame"/>)。</para>
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public struct XChartToolTransform
|
||||
{
|
||||
[SerializeField] private Vector3 m_Position;
|
||||
[SerializeField] private Quaternion m_Rotation;
|
||||
[SerializeField] private Vector3 m_Scale;
|
||||
|
||||
/// <summary>
|
||||
/// 结构体不能有字段初始化器,所以<b>单位变换只能由 <see cref="Identity"/> 或这个构造给</b>。
|
||||
/// 直接 <c>new XChartToolTransform()</c> 会得到 scale=0(整个塌成一个点),
|
||||
/// 现象很扎眼但不是语义——所有配置字段都写成 <c>= XChartToolTransform.Identity</c>。
|
||||
/// </summary>
|
||||
public XChartToolTransform(Vector3 position, Quaternion rotation, Vector3 scale)
|
||||
{
|
||||
m_Position = position;
|
||||
m_Rotation = rotation;
|
||||
m_Scale = scale;
|
||||
}
|
||||
|
||||
/// <summary>立方体局部空间里的平移(1 = 到立方体边界)</summary>
|
||||
public Vector3 Position { get => m_Position; set => m_Position = value; }
|
||||
|
||||
/// <summary>工具自身的旋转(四元数,和基变换同样不拆角度)</summary>
|
||||
public Quaternion Rotation { get => m_Rotation; set => m_Rotation = value; }
|
||||
|
||||
/// <summary>各轴缩放(1 = 原尺寸)</summary>
|
||||
public Vector3 Scale { get => m_Scale; set => m_Scale = value; }
|
||||
|
||||
public static XChartToolTransform Identity
|
||||
=> new XChartToolTransform { Scale = Vector3.one };
|
||||
|
||||
/// <summary>是否等于不做任何事(用来走"省掉复合"的快速路径)</summary>
|
||||
public bool IsIdentity => m_Position == Vector3.zero
|
||||
&& m_Rotation == new Quaternion(0f, 0f, 0f, 1f)
|
||||
&& m_Scale == Vector3.one;
|
||||
|
||||
/// <summary><c>T * R * S</c>(线性 + 平移,最后一行恒 0,0,0,1)</summary>
|
||||
public Matrix4x4 Build()
|
||||
{
|
||||
Matrix4x4 r = XChartCubeView.ToMatrix(m_Rotation);
|
||||
var m = new Matrix4x4
|
||||
{
|
||||
m00 = r.m00 * m_Scale.x, m01 = r.m01 * m_Scale.y, m02 = r.m02 * m_Scale.z, m03 = m_Position.x,
|
||||
m10 = r.m10 * m_Scale.x, m11 = r.m11 * m_Scale.y, m12 = r.m12 * m_Scale.z, m13 = m_Position.y,
|
||||
m20 = r.m20 * m_Scale.x, m21 = r.m21 * m_Scale.y, m22 = r.m22 * m_Scale.z, m23 = m_Position.z,
|
||||
m30 = 0f, m31 = 0f, m32 = 0f, m33 = 1f,
|
||||
};
|
||||
return m;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a9334f8228fdda948b8ae3a9533d0060
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user