中间版本
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<TargetPointData>();
|
||||
|
||||
}
|
||||
public void Update()
|
||||
{
|
||||
if(plug!=null)
|
||||
|
||||
@@ -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>();
|
||||
lineRenderer.positionCount = samples;
|
||||
lineRenderer.useWorldSpace = false;
|
||||
|
||||
spriteRenderer = GetComponentInParent<SpriteRenderer>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 252307b91ef91c648b65b4cdf9ae497b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -4,51 +4,49 @@ using RainbowArt.CleanFlatUI;
|
||||
|
||||
public class OscilloscopeController : MonoBehaviour
|
||||
{
|
||||
public List<Transform> 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<Plug>();
|
||||
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<TargetPointData>();
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,4 +15,5 @@ public class TargetPointData : MonoBehaviour
|
||||
randomShift = Random.Range(4, 8);
|
||||
initialRandomPhase = randomShift*Mathf.PI * 3f*0.1f; // 随机初始相位
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user