From 2aa29dfc5cc500a3ea5ff9464f15d682f1db7381 Mon Sep 17 00:00:00 2001 From: bottlefish <781230111@qq.com> Date: Mon, 14 Oct 2024 13:30:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E9=97=B4=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Prototype/佩佩/佩佩视觉模块.unity | 189 ++++++++++-------- Assets/Scripts/FixSystem/Cable/Plug.cs | 5 +- Assets/Scripts/FixSystem/Cable/Socket.cs | 8 + .../Scope/ErrorWaveformVisualizer.cs | 113 +++++++++++ .../Scope/ErrorWaveformVisualizer.cs.meta | 11 + .../FixSystem/Scope/OscilloscopeController.cs | 72 +++---- .../FixSystem/Scope/TargetPointData.cs | 3 +- .../FixSystem/Scope/WaveformVisualizer.cs | 40 ++-- 8 files changed, 285 insertions(+), 156 deletions(-) create mode 100644 Assets/Scripts/FixSystem/Scope/ErrorWaveformVisualizer.cs create mode 100644 Assets/Scripts/FixSystem/Scope/ErrorWaveformVisualizer.cs.meta diff --git a/Assets/Prototype/佩佩/佩佩视觉模块.unity b/Assets/Prototype/佩佩/佩佩视觉模块.unity index 7caf32fab..a84550699 100644 --- a/Assets/Prototype/佩佩/佩佩视觉模块.unity +++ b/Assets/Prototype/佩佩/佩佩视觉模块.unity @@ -625,6 +625,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c2a37d0f43cb6f14c8011e55a756c448, type: 3} m_Name: m_EditorClassIdentifier: + targetData: {fileID: 0} --- !u!114 &7986263 MonoBehaviour: m_ObjectHideFlags: 0 @@ -2343,6 +2344,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c2a37d0f43cb6f14c8011e55a756c448, type: 3} m_Name: m_EditorClassIdentifier: + targetData: {fileID: 0} --- !u!114 &144553464 MonoBehaviour: m_ObjectHideFlags: 0 @@ -4879,6 +4881,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c2a37d0f43cb6f14c8011e55a756c448, type: 3} m_Name: m_EditorClassIdentifier: + targetData: {fileID: 0} --- !u!114 &265611527 MonoBehaviour: m_ObjectHideFlags: 0 @@ -5700,14 +5703,14 @@ GameObject: m_Component: - component: {fileID: 302414626} - component: {fileID: 302414628} - - component: {fileID: 302414627} + - component: {fileID: 302414629} m_Layer: 0 m_Name: WaveformVisualizer (3) m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!4 &302414626 Transform: m_ObjectHideFlags: 0 @@ -5723,31 +5726,6 @@ Transform: m_Children: [] m_Father: {fileID: 1634739324} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &302414627 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 302414625} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: cc37ef954feb9fa449d9a9bc1c28f118, type: 3} - m_Name: - m_EditorClassIdentifier: - samples: 1024 - amplitude: 1 - spatialFrequency: 1 - temporalFrequency: 0.1 - targetWaveformNoiseAmplitude: 0 - noiseAmplitude: 0.5 - clarity: 0 - noiseColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} - clearColor: {r: 0, g: 1, b: 0, a: 1} - waveformType: 0 - amplitudeModulationFrequency: 0.5 - amplitudeModulationDepth: 0.5 - phaseShift: 0 --- !u!120 &302414628 LineRenderer: serializedVersion: 2 @@ -5851,6 +5829,18 @@ LineRenderer: m_UseWorldSpace: 0 m_Loop: 0 m_ApplyActiveColorSpace: 1 +--- !u!114 &302414629 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 302414625} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 252307b91ef91c648b65b4cdf9ae497b, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &306249772 GameObject: m_ObjectHideFlags: 0 @@ -8942,6 +8932,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c2a37d0f43cb6f14c8011e55a756c448, type: 3} m_Name: m_EditorClassIdentifier: + targetData: {fileID: 0} --- !u!114 &585617292 MonoBehaviour: m_ObjectHideFlags: 0 @@ -11603,6 +11594,7 @@ GameObject: - component: {fileID: 697311006} - component: {fileID: 697311005} - component: {fileID: 697311004} + - component: {fileID: 697311007} m_Layer: 5 m_Name: SocketSpeak m_TagString: Untagged @@ -11641,6 +11633,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c2a37d0f43cb6f14c8011e55a756c448, type: 3} m_Name: m_EditorClassIdentifier: + targetData: {fileID: 0} --- !u!114 &697311005 MonoBehaviour: m_ObjectHideFlags: 0 @@ -11679,6 +11672,23 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 697311002} m_CullTransparentMesh: 1 +--- !u!114 &697311007 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 697311002} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b7407f67123179648be95c4ebf1d6281, type: 3} + m_Name: + m_EditorClassIdentifier: + waveformType: 1 + isCorrect: 0 + errorWaveformType: 6 + randomShift: 0 + initialRandomPhase: 0 --- !u!1 &708923414 GameObject: m_ObjectHideFlags: 0 @@ -14164,7 +14174,6 @@ GameObject: - component: {fileID: 951186777} - component: {fileID: 951186779} - component: {fileID: 951186778} - - component: {fileID: 951186780} m_Layer: 0 m_Name: "\u8BB0\u5FC6\u6A21\u5757 (1)" m_TagString: Untagged @@ -14255,22 +14264,6 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 ---- !u!114 &951186780 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 951186776} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: b7407f67123179648be95c4ebf1d6281, type: 3} - m_Name: - m_EditorClassIdentifier: - waveformType: 0 - isCorrect: 0 - errorWaveformType: 6 - randomShift: 0 --- !u!1 &960673425 GameObject: m_ObjectHideFlags: 0 @@ -15135,6 +15128,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c2a37d0f43cb6f14c8011e55a756c448, type: 3} m_Name: m_EditorClassIdentifier: + targetData: {fileID: 0} --- !u!114 &1079123348 MonoBehaviour: m_ObjectHideFlags: 0 @@ -16321,6 +16315,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: e52fef95146aaeb41a90b1faaa1a2e6e, type: 3} m_Name: m_EditorClassIdentifier: + currentSocket: {fileID: 0} initialSocket: {fileID: 1079123347} plugSprite: {fileID: 1826528114} --- !u!114 &1132009645 @@ -16373,12 +16368,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: a013b1ad22dec80488f1f76e949b2de9, type: 3} m_Name: m_EditorClassIdentifier: - targetPoints: - - {fileID: 951186777} - - {fileID: 1715975300} - - {fileID: 1738809421} - maxDistance: 1 waveformVisualizer: {fileID: 1424895148} + errorWaveformVisualizer: {fileID: 302414629} phaseSlider: {fileID: 590855577} --- !u!1 &1141076858 GameObject: @@ -16692,6 +16683,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c2a37d0f43cb6f14c8011e55a756c448, type: 3} m_Name: m_EditorClassIdentifier: + targetData: {fileID: 0} --- !u!114 &1177703836 MonoBehaviour: m_ObjectHideFlags: 0 @@ -17112,15 +17104,15 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: cc37ef954feb9fa449d9a9bc1c28f118, type: 3} m_Name: m_EditorClassIdentifier: - samples: 1024 amplitude: 1 spatialFrequency: 1 temporalFrequency: 0.1 - targetWaveformNoiseAmplitude: 0 noiseAmplitude: 0.5 clarity: 0 noiseColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} clearColor: {r: 1, g: 0.5508574, b: 0, a: 1} + waveformData: [] + displayData: [] waveformType: 0 amplitudeModulationFrequency: 0.5 amplitudeModulationDepth: 0.5 @@ -18312,6 +18304,7 @@ GameObject: - component: {fileID: 1261765725} - component: {fileID: 1261765724} - component: {fileID: 1261765723} + - component: {fileID: 1261765726} m_Layer: 5 m_Name: SocketLogic m_TagString: Untagged @@ -18350,6 +18343,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c2a37d0f43cb6f14c8011e55a756c448, type: 3} m_Name: m_EditorClassIdentifier: + targetData: {fileID: 0} --- !u!114 &1261765724 MonoBehaviour: m_ObjectHideFlags: 0 @@ -18388,6 +18382,23 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1261765721} m_CullTransparentMesh: 1 +--- !u!114 &1261765726 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1261765721} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b7407f67123179648be95c4ebf1d6281, type: 3} + m_Name: + m_EditorClassIdentifier: + waveformType: 2 + isCorrect: 0 + errorWaveformType: 6 + randomShift: 0 + initialRandomPhase: 0 --- !u!1 &1272879182 GameObject: m_ObjectHideFlags: 0 @@ -21750,6 +21761,7 @@ GameObject: - component: {fileID: 1451543710} - component: {fileID: 1451543709} - component: {fileID: 1451543708} + - component: {fileID: 1451543711} m_Layer: 5 m_Name: SocketMemory m_TagString: Untagged @@ -21788,6 +21800,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c2a37d0f43cb6f14c8011e55a756c448, type: 3} m_Name: m_EditorClassIdentifier: + targetData: {fileID: 0} --- !u!114 &1451543709 MonoBehaviour: m_ObjectHideFlags: 0 @@ -21826,6 +21839,23 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1451543706} m_CullTransparentMesh: 1 +--- !u!114 &1451543711 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1451543706} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b7407f67123179648be95c4ebf1d6281, type: 3} + m_Name: + m_EditorClassIdentifier: + waveformType: 0 + isCorrect: 0 + errorWaveformType: 6 + randomShift: 0 + initialRandomPhase: 0 --- !u!1 &1476216892 GameObject: m_ObjectHideFlags: 0 @@ -25334,7 +25364,6 @@ GameObject: - component: {fileID: 1715975300} - component: {fileID: 1715975302} - component: {fileID: 1715975301} - - component: {fileID: 1715975303} m_Layer: 0 m_Name: "\u5DE6\u773C (1)" m_TagString: Untagged @@ -25425,22 +25454,6 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 ---- !u!114 &1715975303 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1715975299} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: b7407f67123179648be95c4ebf1d6281, type: 3} - m_Name: - m_EditorClassIdentifier: - waveformType: 1 - isCorrect: 0 - errorWaveformType: 6 - randomShift: 0 --- !u!21 &1720709099 Material: serializedVersion: 8 @@ -26176,7 +26189,6 @@ GameObject: - component: {fileID: 1738809421} - component: {fileID: 1738809423} - component: {fileID: 1738809422} - - component: {fileID: 1738809424} m_Layer: 0 m_Name: "\u53F3\u773C (1)" m_TagString: Untagged @@ -26267,22 +26279,6 @@ SpriteRenderer: m_WasSpriteAssigned: 1 m_MaskInteraction: 0 m_SpriteSortPoint: 0 ---- !u!114 &1738809424 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1738809420} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: b7407f67123179648be95c4ebf1d6281, type: 3} - m_Name: - m_EditorClassIdentifier: - waveformType: 0 - isCorrect: 1 - errorWaveformType: 6 - randomShift: 0 --- !u!1 &1749292993 GameObject: m_ObjectHideFlags: 0 @@ -28553,15 +28549,15 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: cc37ef954feb9fa449d9a9bc1c28f118, type: 3} m_Name: m_EditorClassIdentifier: - samples: 1024 amplitude: 1 spatialFrequency: 1 temporalFrequency: 0.1 - targetWaveformNoiseAmplitude: 0 noiseAmplitude: 0.5 clarity: 0 noiseColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} clearColor: {r: 0, g: 1, b: 0, a: 1} + waveformData: [] + displayData: [] waveformType: 0 amplitudeModulationFrequency: 0.5 amplitudeModulationDepth: 0.5 @@ -31170,6 +31166,7 @@ GameObject: - component: {fileID: 2031366196} - component: {fileID: 2031366195} - component: {fileID: 2031366194} + - component: {fileID: 2031366197} m_Layer: 5 m_Name: SocketEye m_TagString: Untagged @@ -31208,6 +31205,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: c2a37d0f43cb6f14c8011e55a756c448, type: 3} m_Name: m_EditorClassIdentifier: + targetData: {fileID: 0} --- !u!114 &2031366195 MonoBehaviour: m_ObjectHideFlags: 0 @@ -31246,6 +31244,23 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2031366192} m_CullTransparentMesh: 1 +--- !u!114 &2031366197 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2031366192} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b7407f67123179648be95c4ebf1d6281, type: 3} + m_Name: + m_EditorClassIdentifier: + waveformType: 0 + isCorrect: 1 + errorWaveformType: 6 + randomShift: 0 + initialRandomPhase: 0 --- !u!1 &2033809932 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/FixSystem/Cable/Plug.cs b/Assets/Scripts/FixSystem/Cable/Plug.cs index 9be2eaa18..83cca0ebc 100644 --- a/Assets/Scripts/FixSystem/Cable/Plug.cs +++ b/Assets/Scripts/FixSystem/Cable/Plug.cs @@ -12,7 +12,8 @@ public class Plug : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUp private Vector3 startPosition; private Quaternion startRotation; private bool isDragging = false; - private Socket currentSocket = null; + [HideInInspector] + public Socket currentSocket = null; private Image image; private Vector3 pickupOffset = new Vector3(0, 0, 0); private Quaternion pickupRotationOffset = Quaternion.Euler(0, 0, 30); @@ -180,4 +181,4 @@ public class Plug : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUp float snappingRange = 1.0f; // You might need to adjust this for WorldSpace Canvas Gizmos.DrawWireSphere(transform.position, snappingRange); } -} \ No newline at end of file +} diff --git a/Assets/Scripts/FixSystem/Cable/Socket.cs b/Assets/Scripts/FixSystem/Cable/Socket.cs index 34a9d43d7..9c805d2a6 100644 --- a/Assets/Scripts/FixSystem/Cable/Socket.cs +++ b/Assets/Scripts/FixSystem/Cable/Socket.cs @@ -7,12 +7,20 @@ public class Socket : MonoBehaviour, ISocket public Vector2 InsertionPoint => transform.position; private Plug plug=null; private YarnOption _option; + [HideInInspector] + public TargetPointData targetData; // 新增属性 public YarnOption Option { set => _option = value; } + + public void Start() + { + targetData=transform.GetComponent(); + + } public void Update() { if(plug!=null) diff --git a/Assets/Scripts/FixSystem/Scope/ErrorWaveformVisualizer.cs b/Assets/Scripts/FixSystem/Scope/ErrorWaveformVisualizer.cs new file mode 100644 index 000000000..68b51d52c --- /dev/null +++ b/Assets/Scripts/FixSystem/Scope/ErrorWaveformVisualizer.cs @@ -0,0 +1,113 @@ +using UnityEngine; + +[RequireComponent(typeof(LineRenderer))] +public class ErrorWaveformVisualizer : MonoBehaviour +{ + private int samples = 1024; + private float amplitude = 0.5f; + + private LineRenderer lineRenderer; + private SpriteRenderer spriteRenderer; + private float spriteWidth; + private float spriteHeight; + + private float[] errorWaveformData; + private WaveformVisualizer waveformVisualizer; + + private int[] spikeIndices; + + void Start() + { + lineRenderer = GetComponent(); + lineRenderer.positionCount = samples; + lineRenderer.useWorldSpace = false; + + spriteRenderer = GetComponentInParent(); + if (spriteRenderer != null) + { + spriteWidth = spriteRenderer.bounds.size.x; + spriteHeight = spriteRenderer.bounds.size.y; + lineRenderer.sortingLayerID = spriteRenderer.sortingLayerID; + lineRenderer.sortingOrder = spriteRenderer.sortingOrder + 1; + } + else + { + Debug.LogError("Could not find SpriteRenderer in parent object."); + } + + errorWaveformData = new float[samples]; + + // 定义突刺的位置索引(在波形的 1/4 和 3/4 位置) + spikeIndices = new int[] { samples / 4, samples * 3 / 4 }; + } + + public void SetWaveformVisualizer(WaveformVisualizer visualizer) + { + waveformVisualizer = visualizer; + + if (waveformVisualizer.displayData.Length != samples) + { + Debug.LogError("WaveformVisualizer data length does not match samples."); + return; + } + } + + void Update() + { + if (waveformVisualizer != null) + { + // 更新错误波形数据,使其与基础波形一致 + for (int i = 0; i < samples; i++) + { + errorWaveformData[i] = waveformVisualizer.displayData[i]; + } + + DrawErrorWaveform(); + } + } + + void DrawErrorWaveform() + { + // 绘制波形和突刺 + for (int i = 0; i < samples; i++) + { + float x = ((float)i / (samples - 1)) * spriteWidth - (spriteWidth / 2f); + float y = errorWaveformData[i]; + + // 检查当前索引是否为突刺位置 + // if (System.Array.IndexOf(spikeIndices, i) >= 0) + // { + // // 获取波形在该点的振幅 + // float baseAmplitude = waveformVisualizer.displayData[i]; + + // // if(baseAmplitude-amplitude/3<0) + // // { + // // y=amplitude/3; + // // } + // if(Mathf.Abs(baseAmplitude)-amplitude/2<0) + // { + // if(baseAmplitude<0) + // { + // y=-amplitude/2; + + // } + // else + // { + // y=amplitude/2; + // } + + // } + // } + + y *= (spriteHeight / 2f); + + Vector3 position = new Vector3(x, y, 0); + lineRenderer.SetPosition(i, position); + } + + lineRenderer.startColor = Color.red; + lineRenderer.endColor = Color.red; + lineRenderer.startWidth = 0.05f; + lineRenderer.endWidth = 0.05f; + } +} diff --git a/Assets/Scripts/FixSystem/Scope/ErrorWaveformVisualizer.cs.meta b/Assets/Scripts/FixSystem/Scope/ErrorWaveformVisualizer.cs.meta new file mode 100644 index 000000000..2d5bd1a77 --- /dev/null +++ b/Assets/Scripts/FixSystem/Scope/ErrorWaveformVisualizer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 252307b91ef91c648b65b4cdf9ae497b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/FixSystem/Scope/OscilloscopeController.cs b/Assets/Scripts/FixSystem/Scope/OscilloscopeController.cs index 759e445bb..21b7d389d 100644 --- a/Assets/Scripts/FixSystem/Scope/OscilloscopeController.cs +++ b/Assets/Scripts/FixSystem/Scope/OscilloscopeController.cs @@ -4,51 +4,49 @@ using RainbowArt.CleanFlatUI; public class OscilloscopeController : MonoBehaviour { - public List targetPoints; // 目标点列表 - public float maxDistance = 1f; // 最大检测距离 - public WaveformVisualizer waveformVisualizer; + public ErrorWaveformVisualizer errorWaveformVisualizer; + public SliderCircular phaseSlider; - //public WaveformVisualizer errorWaveformVisualizer; // 错误波形 + private float phaseShiftMax = Mathf.PI * 2f; + private Plug plug; - public SliderCircular phaseSlider; // 控制相位偏移的旋钮 - - private float phaseShiftMax = Mathf.PI * 3f; // 最大相位偏移量 - - private Transform closestTarget; // 最近的目标点 - private float clarity; // 清晰度(根据距离计算) - - private bool isCaptured = false; // 是否已拍照(冻结波形) + void Start() + { + plug = FindObjectOfType(); + errorWaveformVisualizer.SetWaveformVisualizer(waveformVisualizer); + } void Update() { - if (!isCaptured) + if (plug != null) { - DetectClosestTarget(); + var currentSocket = plug.currentSocket; - if (closestTarget != null) + if (currentSocket != null && currentSocket.targetData != null) { - var targetData = closestTarget.GetComponent(); + var targetData = currentSocket.targetData; if (targetData != null) { - waveformVisualizer.clarity = clarity; waveformVisualizer.waveformType = targetData.waveformType; - - // 处理相位偏移 + waveformVisualizer.clarity = 1; float phaseShift = 0f; if (phaseSlider != null) { float sliderPhase = (phaseSlider.Value / 10f) * phaseShiftMax; - phaseShift = sliderPhase + targetData.initialRandomPhase; // 加上随机初始相位 + phaseShift = sliderPhase + targetData.initialRandomPhase; } + waveformVisualizer.SetPhaseShift(phaseShift); + waveformVisualizer.gameObject.SetActive(true); - if (phaseShift > 1) + if (targetData.isCorrect) { - waveformVisualizer.SetPhaseShift(phaseShift); + errorWaveformVisualizer.gameObject.SetActive(false); } else { - waveformVisualizer.SetPhaseShift(0); + errorWaveformVisualizer.SetWaveformVisualizer(waveformVisualizer); + errorWaveformVisualizer.gameObject.SetActive(true); } } } @@ -58,31 +56,13 @@ public class OscilloscopeController : MonoBehaviour } } } + void ResetVisualizer() { - waveformVisualizer.clarity = 0f; waveformVisualizer.waveformType = WaveformType.Noise; + waveformVisualizer.clarity = 0; waveformVisualizer.ResetWaveform(); + waveformVisualizer.gameObject.SetActive(true); + errorWaveformVisualizer.gameObject.SetActive(false); } - void DetectClosestTarget() - { - float minDistance = float.MaxValue; - closestTarget = null; - clarity = 0f; - - foreach (var target in targetPoints) - { - float distance = Vector2.Distance(transform.position, target.position); - if (distance <= maxDistance && distance < minDistance) - { - minDistance = distance; - closestTarget = target; - } - } - - if (closestTarget != null) - { - clarity = Mathf.Clamp01(1f - (minDistance / maxDistance)); - } - } -} \ No newline at end of file +} diff --git a/Assets/Scripts/FixSystem/Scope/TargetPointData.cs b/Assets/Scripts/FixSystem/Scope/TargetPointData.cs index 93a0d06a7..5233f835d 100644 --- a/Assets/Scripts/FixSystem/Scope/TargetPointData.cs +++ b/Assets/Scripts/FixSystem/Scope/TargetPointData.cs @@ -15,4 +15,5 @@ public class TargetPointData : MonoBehaviour randomShift = Random.Range(4, 8); initialRandomPhase = randomShift*Mathf.PI * 3f*0.1f; // 随机初始相位 } -} \ No newline at end of file +} + diff --git a/Assets/Scripts/FixSystem/Scope/WaveformVisualizer.cs b/Assets/Scripts/FixSystem/Scope/WaveformVisualizer.cs index 4536da73e..e105299b4 100644 --- a/Assets/Scripts/FixSystem/Scope/WaveformVisualizer.cs +++ b/Assets/Scripts/FixSystem/Scope/WaveformVisualizer.cs @@ -10,18 +10,18 @@ public enum WaveformType Composite, Spike, Pulse, - Error // 新增错误波形类型 } [RequireComponent(typeof(LineRenderer))] public class WaveformVisualizer : MonoBehaviour { - public int samples = 1024; - public float amplitude = 1f; + private int samples = 1024; + [HideInInspector] + public float amplitude = 0.4f; public float spatialFrequency = 1f; public float temporalFrequency = 0.1f; - public float targetWaveformNoiseAmplitude = 0f; + private float targetWaveformNoiseAmplitude = 0.1f; public float noiseAmplitude = 0.5f; [Range(0f, 1f)] [HideInInspector] @@ -34,12 +34,13 @@ public class WaveformVisualizer : MonoBehaviour private SpriteRenderer spriteRenderer; private float spriteWidth; private float spriteHeight; - [HideInInspector] public float[] waveformData; private float[] noiseData; - private float[] displayData; + [HideInInspector] + public float[] displayData; private float[] antiWaveformData; + //private float[] errorWaveformData; // 错误波形数据 [HideInInspector] public WaveformType waveformType = WaveformType.Sine; @@ -47,9 +48,9 @@ public class WaveformVisualizer : MonoBehaviour public float amplitudeModulationFrequency = 0.5f; public float amplitudeModulationDepth = 0.5f; - public float phaseShift = 0f; + public float phaseShift = 0f; // 相位偏移 - private bool isCaptured = false; + private bool isCaptured = false; // 是否已拍照(冻结波形) void Start() { @@ -63,7 +64,7 @@ public class WaveformVisualizer : MonoBehaviour spriteWidth = spriteRenderer.bounds.size.x; spriteHeight = spriteRenderer.bounds.size.y; lineRenderer.sortingLayerID = spriteRenderer.sortingLayerID; - lineRenderer.sortingOrder = spriteRenderer.sortingOrder + 1; + lineRenderer.sortingOrder = spriteRenderer.sortingOrder + 2; } else { @@ -74,8 +75,9 @@ public class WaveformVisualizer : MonoBehaviour noiseData = new float[samples]; displayData = new float[samples]; antiWaveformData = new float[samples]; + //errorWaveformData = new float[samples]; - DrawWaveform(); + DrawWaveform(); // 初始化波形 } void Update() @@ -97,6 +99,7 @@ public class WaveformVisualizer : MonoBehaviour { displayData[i] += antiWaveformData[i]; } + //displayData[i] += errorWaveformData[i]; // 叠加错误波形 } DrawWaveform(); } @@ -124,7 +127,7 @@ public class WaveformVisualizer : MonoBehaviour { float time = Time.time * temporalFrequency; float amplitudeModulation = 1f + amplitudeModulationDepth * Mathf.Sin(2f * Mathf.PI * amplitudeModulationFrequency * Time.time); - float angle = t * Mathf.PI * 2f * spatialFrequency + time + phaseOffset; + float angle = t * Mathf.PI * 2f * spatialFrequency + phaseOffset; float value = 0f; @@ -154,9 +157,6 @@ public class WaveformVisualizer : MonoBehaviour case WaveformType.Spike: value = Random.Range(0f, 1f) > 0.98f ? Random.Range(0.5f, 1f) : 0f; break; - case WaveformType.Error: // 错误波形逻辑 - value = Random.Range(0f, 1f) > 0.95f ? Random.Range(-1f, 1f) : 0f; - break; default: value = 0f; break; @@ -170,10 +170,9 @@ public class WaveformVisualizer : MonoBehaviour for (int i = 0; i < samples; i++) { float t = (float)i / samples; - antiWaveformData[i] = GetWaveformValue(t, phaseShift) * clarity; + antiWaveformData[i] = -GetWaveformValue(t, phaseShift) * clarity; } } - void DrawWaveform() { for (int i = 0; i < samples; i++) @@ -181,11 +180,9 @@ public class WaveformVisualizer : MonoBehaviour float x = ((float)i / (samples - 1)) * spriteWidth - (spriteWidth / 2f); float amplitudeScale = Mathf.Lerp(0.2f, 1f, clarity); float y = displayData[i] * amplitudeScale * (spriteHeight / 2f); - Vector3 position = new Vector3(x, y, 0); lineRenderer.SetPosition(i, position); } - Color waveColor = Color.Lerp(noiseColor, clearColor, clarity); lineRenderer.startColor = waveColor; @@ -196,16 +193,19 @@ public class WaveformVisualizer : MonoBehaviour lineRenderer.endWidth = startWidth; } + + + // 拍照功能 public void SetCaptured(bool captured) { isCaptured = captured; } - + // 应用相位偏移 public void SetPhaseShift(float phaseShiftValue) { phaseShift = phaseShiftValue; } - + // 重置波形 public void ResetWaveform() { isCaptured = false;