s
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 查询贝塞尔点
|
||||
/// </summary>
|
||||
public class FindBezier : ModuleFind<DataBezier> {
|
||||
public readonly float FindRange = 0.02f;
|
||||
/// <summary> 板片资产 </summary>
|
||||
public ModuleAssets<DataPlate> AssetsPlate => ModuleCore.AssetsPlate;
|
||||
|
||||
protected override void Awake() => ModuleCore.FindBezier = this;
|
||||
|
||||
public override bool Find(Vector3 position, out DataBezier bezier) {
|
||||
List<DataPlate> plates = AssetsPlate.Datas;
|
||||
for (int i = 0; i < plates.Count; i++) {
|
||||
Vector3 localPosition = position - plates[i].designPosition;
|
||||
bezier = Find(plates[i], localPosition);
|
||||
if (bezier != null) { return true; }
|
||||
}
|
||||
bezier = null; return false;
|
||||
}
|
||||
|
||||
/// <summary> 查询匹配的边 </summary>
|
||||
private DataBezier Find(DataPlate plate, Vector3 localPosition) {
|
||||
for (int i = 0; i < plate.sides.Count; i++) {
|
||||
DataBezier bezier = Find(plate.sides[i], localPosition);
|
||||
if (bezier != null) { return bezier; }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary> 查询匹配的边 </summary>
|
||||
private DataBezier Find(DataSide side, Vector3 localPosition) {
|
||||
if (side.bezier == Bezier.一阶) { return null; }
|
||||
float aDis = Vector3.Distance(side.aBezier, localPosition);
|
||||
if (aDis < FindRange) { return new DataBezier() { isA = true, side = side }; }
|
||||
float bDis = Vector3.Distance(side.bBezier, localPosition);
|
||||
if (bDis < FindRange) { return new DataBezier() { isA = false, side = side }; }
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5ea40ae623c1a974fa852f60e22f5a37
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 查找点
|
||||
/// </summary>
|
||||
public class FindPoint : ModuleFind<DataPoint> {
|
||||
public readonly float FindRange = 0.01f;
|
||||
/// <summary> 板片资产 </summary>
|
||||
public ModuleAssets<DataPlate> AssetsPlate => ModuleCore.AssetsPlate;
|
||||
|
||||
protected override void Awake() => ModuleCore.FindPoint = this;
|
||||
|
||||
public override bool Find(Vector3 position, out DataPoint point) {
|
||||
List<DataPlate> plates = AssetsPlate.Datas;
|
||||
for (int i = 0; i < plates.Count; i++) {
|
||||
Vector3 localPosition = position - plates[i].designPosition;
|
||||
point = Find(plates[i], localPosition);
|
||||
if (point != null) { return true; }
|
||||
}
|
||||
point = null; return false;
|
||||
}
|
||||
|
||||
/// <summary> 查询匹配的点 </summary>
|
||||
private DataPoint Find(DataPlate plate, Vector3 localPosition) {
|
||||
List<DataPoint> points = plate.points;
|
||||
for (int i = 0; i < points.Count; i++) {
|
||||
float distance = Vector3.Distance(points[i].position, localPosition);
|
||||
if (distance > FindRange) { continue; }
|
||||
return points[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1da1d6857fa49af4dbe4dd67998f54f1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,61 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 查找边
|
||||
/// </summary>
|
||||
public class FindSide : ModuleFind<DataSide> {
|
||||
public readonly float FindRange = 0.01f;
|
||||
/// <summary> 板片资产 </summary>
|
||||
public ModuleAssets<DataPlate> AssetsPlate => ModuleCore.AssetsPlate;
|
||||
|
||||
protected override void Awake() => ModuleCore.FindSide = this;
|
||||
|
||||
public override bool Find(Vector3 position, out DataSide side) {
|
||||
List<DataPlate> plates = AssetsPlate.Datas;
|
||||
for (int i = 0; i < plates.Count; i++) {
|
||||
Vector3 localPosition = position - plates[i].designPosition;
|
||||
side = Find(plates[i], localPosition);
|
||||
if (side != null) { return true; }
|
||||
}
|
||||
side = null; return false;
|
||||
}
|
||||
|
||||
/// <summary> 查询匹配的边 </summary>
|
||||
private DataSide Find(DataPlate plate, Vector3 localPosition) {
|
||||
for (int i = 0; i < plate.sides.Count; i++) {
|
||||
DataSide side = Find(plate.sides[i], localPosition);
|
||||
if (side != null) { return side; }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/// <summary> 查询匹配的边 </summary>
|
||||
private DataSide Find(DataSide side, Vector3 localPosition) {
|
||||
for (int i = 0; i < side.lines.Length; i++) {
|
||||
Vector3 a = side.lines[i].a;
|
||||
Vector3 b = side.lines[i].b;
|
||||
float distance = ProjectDistance(a, b, localPosition);
|
||||
//Debug.Log($"{a} , {b} , {localPosition}");
|
||||
if (distance < FindRange) { return side; }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向量投影法
|
||||
/// 计算点c到线段ab最近的点
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <param name="c"></param>
|
||||
/// <returns>如果不在线段上返回 float.MaxValue</returns>
|
||||
public static float ProjectDistance(Vector3 a, Vector3 b, Vector3 c) {
|
||||
Vector3 ab = b - a;
|
||||
Vector3 ac = c - a;
|
||||
Vector3 p = Vector3.Project(ac, ab);
|
||||
if (ab.normalized != p.normalized) { return float.MaxValue; }
|
||||
if (ab.magnitude < p.magnitude) { return float.MaxValue; }
|
||||
return Vector3.Distance(c, p + a);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f9b522b1c221a4643b94ce865ac8ecaa
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 查找模块
|
||||
/// </summary>
|
||||
public abstract class ModuleFind<Data> : MonoBehaviour {
|
||||
/// <summary> 必须要初始化 </summary>
|
||||
protected abstract void Awake();
|
||||
/// <summary> 核心模块 </summary>
|
||||
protected virtual ModuleCore ModuleCore => ModuleCore.I;
|
||||
|
||||
/// <summary> 查询 </summary>
|
||||
public abstract bool Find(Vector3 position, out Data data);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b7a6d69ae9d8c86438ea713a619b8afe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user