Last active
June 2, 2017 05:16
-
-
Save jminor/9828a5df8cbab08b7f1633d21915fe95 to your computer and use it in GitHub Desktop.
Unity SteamVR Simple 3D Sketching Tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Instructions: | |
// Just attach this script to your SteamVR Controller (left or right) and then you can draw in 3D. | |
// After you attach it, a new GameObject called "Sketch Line" will be added as a child. | |
// You'll want to adjust the material and width of the line renderer to make it look nice. | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class VRSimpleSketch : MonoBehaviour { | |
// Keep track of our child object that holds the current line | |
private GameObject _currentChildObject; | |
// ...and the LineRenderer on it. | |
private LineRenderer _lineRenderer; | |
private SteamVR_TrackedController _controller; | |
public bool justOneLine = false; | |
public bool pretendPenDown = false; | |
private bool penDown; | |
private bool penWasDown; | |
// This is called in the Editor when this script is 1st attached to a GameObject | |
void Reset() { | |
if (_currentChildObject == null) { | |
var lines = GetComponentsInChildren<LineRenderer>(); | |
if (lines.Length > 0) { | |
// We already have one (or more) so pick one | |
_lineRenderer = lines[0]; | |
_currentChildObject = _lineRenderer.gameObject; | |
}else{ | |
MakeNewLineObject(); | |
} | |
} | |
_controller = GetComponent<SteamVR_TrackedController>(); | |
} | |
void OnEnable () { | |
Reset(); | |
} | |
void MakeNewLineObject() { | |
if (_currentChildObject != null) { | |
// Copy the previous one | |
_currentChildObject = Instantiate(_currentChildObject); | |
_lineRenderer = _currentChildObject.GetComponent<LineRenderer>(); | |
}else{ | |
// Make a brand new one | |
_currentChildObject = new GameObject("Sketch Line"); | |
_lineRenderer = _currentChildObject.AddComponent<LineRenderer>(); | |
} | |
_currentChildObject.transform.position = transform.position; | |
_currentChildObject.transform.parent = transform; | |
} | |
void Update () { | |
// If there is no _controller, you can use pretendPenDown to simulate it | |
penDown = (_controller != null && _controller.triggerPressed) || pretendPenDown; | |
if (penDown) { | |
if (penWasDown) { | |
ExtendLine(transform.position); | |
}else{ | |
if (!justOneLine) MakeNewLineObject(); | |
StartLine(transform.position); | |
} | |
}else{ | |
if (penWasDown) { | |
// The pen was just released. | |
} | |
} | |
penWasDown = penDown; | |
} | |
public void StartLine(Vector3 startPoint) { | |
var points = new Vector3[1]; | |
points[0] = startPoint; | |
_lineRenderer.positionCount = 1; | |
_lineRenderer.SetPositions(points); | |
} | |
public void ExtendLine(Vector3 newPoint) { | |
int oldPointCount = _lineRenderer.positionCount; | |
// Make an array with enough room for one more point | |
var points = new Vector3[oldPointCount+1]; | |
// Get all the old points | |
_lineRenderer.GetPositions(points); | |
// Add the new one | |
points[oldPointCount] = newPoint; | |
_lineRenderer.positionCount = oldPointCount+1; | |
_lineRenderer.SetPositions(points); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment