LineRendererコンポーネントを使って空中で描けるペンを作成します。

Udonスクリプト作成

サンプルの PenLineとSimplePenをU#に書き直し

Udon Graph

デカすぎるので画像の添付は省略

U#

PenLineBySharpとSimplePenBySharp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
public class SimplePenBySharp : UdonSharpBehaviour
{
    private PenLineBySharp line;
 
    private bool isDrawing;
 
    private Vector3 startPosition;
 
    public float minMoveDistance = 0.1f;
 
    private Vector3[] points;
 
    private LineRenderer lineRenderer;
 
    public Transform penTip;
 
    private int currentIndex = -1;
 
    public int pointsPerUpdate = 10;
 
    public Transform linesContainer;
 
    [UdonSynced]
    private int nextLineIndex = 0;
 
    private GameObject[] pool;
 
    private LineRenderer[] linePool;
 
    void Start()
    {
        linePool = linesContainer.GetComponentsInChildren< LineRenderer>();
    }
 
    public override bool OnOwnershipRequest(VRCPlayerApi requestingPlayer, VRCPlayerApi requestedOwner)
    {
        return true;
    }
 
    public override void OnPickupUseDown()
    {
        // Get new Line from Pool
        lineRenderer = linePool[nextLineIndex];
        line = (PenLineBySharp)lineRenderer.GetComponent(typeof(UdonBehaviour));
        Networking.SetOwner(Networking.LocalPlayer, line.gameObject);
        line.gameObject.SetActive(true);
 
        // Increment next Line Index
        if (nextLineIndex++ >= linePool.Length)
        {
            nextLineIndex = 0;
        }
 
        // Reset Variables
        isDrawing = true;
        lineRenderer.positionCount = 2;
 
        startPosition = penTip.position;
        currentIndex = 0;
 
        // Initialize Line with 2 Points at Pen Tip
        for (int i = 0; i < 2; i++)
        {
            lineRenderer.SetPosition(i, penTip.position);
        }
    }
 
    private void Update()
    {
        //Debug.Log(items.Length);
 
        // Are we drawing?
        if (!isDrawing) return;
 
        // Has the pen moved enough?
        if (Vector3.Distance(penTip.position, startPosition) > minMoveDistance)
        {
            // Increase Points in Line
            lineRenderer.positionCount = currentIndex+1;
 
            // Add Point to Line
            lineRenderer.SetPosition(currentIndex, penTip.position);
            startPosition = penTip.position;
 
            // Increment currentIndex
            currentIndex++;
 
            // Group Point Updates to reduce Network Traffic
            if ((currentIndex % pointsPerUpdate) == 0)
            {
                line.OnUpdate();
            }
        }
    }
 
    public override void OnPickupUseUp()
    {
        isDrawing = false;
        line.OnFinish();
    }
}
 
public class PenLineBySharp : UdonSharpBehaviour
{
    private bool isDown;
 
    [UdonSynced]
    private Vector3[] points = new Vector3[10];
 
    public LineRenderer lineRenderer;
 
    public void OnFinish()
    {
        lineRenderer.Simplify(0.005f);
        OnUpdate();
    }
 
    public void OnUpdate()
    {
        points = new Vector3[lineRenderer.positionCount];
        lineRenderer.GetPositions(points);
 
        RequestSerialization();
    }
 
    public override void OnDeserialization()
    {
        // Set LinePositions from Synced Data
        lineRenderer.positionCount = points.Length;
        lineRenderer.SetPositions(points);
    }
 
    public override bool OnOwnershipRequest(VRCPlayerApi requestingPlayer, VRCPlayerApi requestedOwner)
    {
        return true;
    }
}

シーン作成

うーん。特に解説することがない・・・他の記事と解説が被るので省略します。

さいごに

サンプルコードをU#への書き直し作業は過去最大で大変でした。特にエラーが発生してもログに残らないのは辛すぎます。try-catch構文も使えないので、エラー発生時にログを残すことが出来なかったのでバグの特定に時間がかかりました。U#辛すぎますね・・

ペンを作成する

投稿ナビゲーション


コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)