117 lines
3.2 KiB
C#
117 lines
3.2 KiB
C#
using Fusion;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using BNG;
|
|
|
|
public class MarkerEraser : NetworkBehaviour
|
|
{
|
|
public float EraseRadius = 0.1f; // شعاع پاک کردن خطوط
|
|
public Transform EraserTransform; // ترانسفورم تخته پاک کن
|
|
public List<LineRenderer> allLines = new List<LineRenderer>();
|
|
public static MarkerEraser instance;
|
|
|
|
private void Update()
|
|
{
|
|
isDrawing = GetComponent<MarkerGrab>().grabed;
|
|
if(isDrawing)
|
|
{
|
|
EraseNearbyLines();
|
|
}
|
|
|
|
}
|
|
public void Awake()
|
|
{
|
|
instance = this;
|
|
EraserTransform = transform;
|
|
}
|
|
public bool isDrawing = false;
|
|
public void StartDrawing()
|
|
{
|
|
isDrawing = true;
|
|
|
|
}
|
|
|
|
public void StopDrawing()
|
|
{
|
|
isDrawing = false;
|
|
}
|
|
|
|
|
|
private void EraseNearbyLines()
|
|
{
|
|
for (int lineIndex = allLines.Count - 1; lineIndex >= 0; lineIndex--)
|
|
{
|
|
LineRenderer line = allLines[lineIndex];
|
|
if (line == null) continue;
|
|
|
|
List<Vector3> newPositions = new List<Vector3>();
|
|
List<Vector3> segment = new List<Vector3>();
|
|
List<List<Vector3>> segments = new List<List<Vector3>>();
|
|
bool isErasing = false;
|
|
|
|
for (int i = 0; i < line.positionCount; i++)
|
|
{
|
|
Vector3 point = line.GetPosition(i);
|
|
if (Vector3.Distance(EraserTransform.position, point) > EraseRadius)
|
|
{
|
|
if (isErasing)
|
|
{
|
|
if (segment.Count > 0)
|
|
{
|
|
segments.Add(new List<Vector3>(segment));
|
|
segment.Clear();
|
|
}
|
|
isErasing = false;
|
|
}
|
|
segment.Add(point);
|
|
}
|
|
else
|
|
{
|
|
isErasing = true;
|
|
if (segment.Count > 0)
|
|
{
|
|
segments.Add(new List<Vector3>(segment));
|
|
segment.Clear();
|
|
}
|
|
}
|
|
}
|
|
if (segment.Count > 0)
|
|
segments.Add(segment);
|
|
|
|
allLines.RemoveAt(lineIndex);
|
|
Destroy(line.gameObject);
|
|
|
|
foreach (var seg in segments)
|
|
{
|
|
if (seg.Count > 1)
|
|
{
|
|
|
|
CreateNewLine(seg,line);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CreateNewLine(List<Vector3> points,LineRenderer line)
|
|
{
|
|
GameObject newLineObj = new GameObject("SplitLine");
|
|
LineRenderer newLine = newLineObj.AddComponent<LineRenderer>();
|
|
newLine.positionCount = points.Count;
|
|
newLine.material=line.material;
|
|
newLine.startColor=line.startColor;
|
|
newLine.endColor=line.endColor;
|
|
newLine.SetPositions(points.ToArray());
|
|
newLine.startWidth = 0.02f;
|
|
newLine.endWidth = 0.02f;
|
|
allLines.Add(newLine);
|
|
}
|
|
|
|
public void RegisterLine(LineRenderer line)
|
|
{
|
|
if (!allLines.Contains(line))
|
|
{
|
|
allLines.Add(line);
|
|
}
|
|
}
|
|
}
|