├── DrawLineManager.cs.meta ├── GraphicsLineRenderer.cs.meta ├── LICENSE ├── README.md ├── DrawLineManager.cs └── GraphicsLineRenderer.cs /DrawLineManager.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 854abeda705c7834e8dd7de76d5896ff 3 | timeCreated: 1488157906 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /GraphicsLineRenderer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6efc7bd3231c60e4ead9b02070dc2800 3 | timeCreated: 1488240018 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 scobot 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # VRDrawLine 2 | Unity 3D Codes for "Building Tilt Brush from Scratch" YouTube tutorial by Fuseman 3 | https://www.youtube.com/watch?v=eMJATZI0A7c 4 | 5 | This tutorial is GREAT and Fuseman is super kind to share his knowledge and put this out there. You should watch the video, lots of good stuff in there and it will help you figure out how/where to use this code. 6 | 7 | The ONE problem I had with Fuseman's video, there was no link to the custom Unity scripts he was using! I searched everywhere and could not find them, so, I painstakingly screen captured the video and typed each line of code as he had it (spelling errors and all). AND IT WORKS! Follow the video tutorial, and just use these scripts. 8 | 9 | He also uses code from Bartek Drozdz so I feel I should mention that (as I don't know who did what, I just transcribed the code!) 10 | http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/ 11 | 12 | If you are interested in creating your own VR drawing program in Unity, and are frustrated that you cannot find a good code to start with, I hope this will help. It helped me! 13 | 14 | NOTE: These codes only cover the "drawing" part of the video, not the color picker part. Feel free to continue if that is your thing. 15 | 16 | NOTE: Copy these scripts into your UNITY PROJECTS > "project name" > ASSETS > "folder name you set up in Unity for such things" 17 | NOTE: I do not know what the "DrawLineManager.cs.meta" and "GraphicsLineRenderer.cs.meta" files are for, but they were in the Project folder, so I kept them. 18 | -------------------------------------------------------------------------------- /DrawLineManager.cs: -------------------------------------------------------------------------------- 1 | /*Unity 3D Codes for "Building Tilt Brush from Scratch" YouTube tutorial by Fuseman 2 | https://www.youtube.com/watch?v=eMJATZI0A7c 3 | 4 | He also uses code from Bartek Drozdz so I feel I should mention that (as I don't know who did what, I just transcribed the code!) 5 | http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/ 6 | */ 7 | 8 | using System.Collections; 9 | using System.Collections.Generic; 10 | using UnityEngine; 11 | 12 | public class DrawLineManager : MonoBehaviour { 13 | 14 | public Material lMat; 15 | 16 | public SteamVR_TrackedObject trackedObj; 17 | 18 | //private LineRenderer currLine; 19 | private GraphicsLineRenderer currLine; 20 | 21 | private int numClicks = 0; 22 | 23 | // Update is called once per frame 24 | void Update () { 25 | SteamVR_Controller.Device device = SteamVR_Controller.Input((int)trackedObj.index); 26 | if (device.GetTouchDown (SteamVR_Controller.ButtonMask.Trigger)) { 27 | GameObject go = new GameObject (); 28 | //currLine = go.AddComponent (); 29 | go.AddComponent (); 30 | go.AddComponent (); 31 | currLine = go.AddComponent (); 32 | 33 | currLine.lmat = lMat; 34 | 35 | //currLine.SetWidth (.1f, .1f); 36 | currLine.SetWidth (.1f); 37 | numClicks = 0; 38 | 39 | } else if (device.GetTouch (SteamVR_Controller.ButtonMask.Trigger)) { 40 | //currLine.SetVertexCount (numClicks + 1); 41 | //currLine.SetPosition (numClicks, trackedObj.transform.position); 42 | 43 | currLine.AddPoint(trackedObj.transform.position); 44 | numClicks++; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /GraphicsLineRenderer.cs: -------------------------------------------------------------------------------- 1 | /*Unity 3D Codes for "Building Tilt Brush from Scratch" YouTube tutorial by Fuseman 2 | https://www.youtube.com/watch?v=eMJATZI0A7c 3 | 4 | He also uses code from Bartek Drozdz so I feel I should mention that (as I don't know who did what, I just transcribed the code!) 5 | http://www.everyday3d.com/blog/index.php/2010/03/15/3-ways-to-draw-3d-lines-in-unity3d/ 6 | */ 7 | 8 | using UnityEngine; 9 | using System.Collections.Generic; 10 | 11 | /* 12 | class Point { 13 | public Vector3 p; 14 | public Point next; 15 | } 16 | */ 17 | 18 | [RequireComponent (typeof(MeshRenderer))] 19 | [RequireComponent (typeof(MeshFilter))] 20 | public class GraphicsLineRenderer : MonoBehaviour { 21 | 22 | public Material lmat; 23 | 24 | private Mesh ml; 25 | 26 | private Vector3 s; 27 | 28 | private float lineSize = .1f; 29 | 30 | private bool firstQuad = true; 31 | 32 | void Start () { 33 | ml = GetComponent ().mesh; 34 | GetComponent ().material = lmat; 35 | } 36 | 37 | public void SetWidth(float width) { 38 | lineSize = width; 39 | } 40 | 41 | public void AddPoint(Vector3 point){ 42 | if(s != Vector3.zero){ 43 | AddLine(ml, MakeQuad(s, point, lineSize, firstQuad)); 44 | firstQuad = false; 45 | } 46 | 47 | s = point; 48 | } 49 | 50 | 51 | Vector3[] MakeQuad(Vector3 s, Vector3 e, float w, bool all) { 52 | w = w / 2; 53 | 54 | Vector3[] q; 55 | if (all) 56 | q = new Vector3[4]; 57 | else 58 | q = new Vector3[2]; 59 | 60 | Vector3 n = Vector3.Cross (s, e); 61 | Vector3 l = Vector3.Cross (n, e - s); 62 | l.Normalize (); 63 | 64 | if (all) { 65 | q [0] = transform.InverseTransformPoint (s + l * w); 66 | q [1] = transform.InverseTransformPoint (s + l * -w); 67 | q [2] = transform.InverseTransformPoint (e + l * w); 68 | q [3] = transform.InverseTransformPoint (e + l * -w); 69 | }else{ 70 | q [0] = transform.InverseTransformPoint (e + l * w); 71 | q [1] = transform.InverseTransformPoint (s + l * -w); 72 | } 73 | return q; 74 | } 75 | 76 | void AddLine(Mesh m, Vector3[] quad){ 77 | int vl = m.vertices.Length; 78 | 79 | Vector3[] vs = m.vertices; 80 | vs = resizeVertices (vs, 2 * quad.Length); 81 | 82 | for (int i = 0; i < 2*quad.Length; i+=2){ 83 | vs[vl + i] = quad[i/2]; 84 | vs[vl + i+1] = quad[i/2]; 85 | } 86 | 87 | Vector2[] uvs = m.uv; 88 | uvs = resizeUVs(uvs, 2*quad.Length); 89 | 90 | if (quad.Length == 4){ 91 | uvs [vl] = Vector2.zero; 92 | uvs [vl + 1] = Vector2.zero; 93 | uvs [vl + 2] = Vector2.right; 94 | uvs [vl + 3] = Vector2.right; 95 | uvs [vl + 4] = Vector2.up; 96 | uvs [vl + 5] = Vector2.up; 97 | uvs [vl + 6] = Vector2.one; 98 | uvs [vl + 7] = Vector2.one; 99 | } else { 100 | if (vl %8 == 0) { 101 | uvs [vl] = Vector2.zero; 102 | uvs [vl + 1] = Vector2.zero; 103 | uvs [vl + 2] = Vector2.right; 104 | uvs [vl + 3] = Vector2.right; 105 | } else { 106 | uvs [vl] = Vector2.up; 107 | uvs [vl + 1] = Vector2.up; 108 | uvs [vl + 2] = Vector2.one; 109 | uvs [vl + 3] = Vector2.one; 110 | } 111 | } 112 | 113 | int tl = m.triangles.Length; 114 | 115 | int[] ts = m.triangles; 116 | ts = resizeTraingles(ts, 12); //!misspelling of triangles !!! 117 | 118 | if (quad.Length ==2) 119 | vl -= 4; 120 | 121 | //front facing quad 122 | ts[tl] = vl; 123 | ts[tl+1] = vl+2; 124 | ts[tl+2] = vl+4; 125 | 126 | ts[tl+3] = vl+2; 127 | ts[tl+4] = vl+6; 128 | ts[tl+5] = vl+4; 129 | 130 | //back facing quad 131 | ts[tl+6] = vl+5; 132 | ts[tl+7] = vl+3; 133 | ts[tl+8] = vl+1; 134 | 135 | ts[tl+9] = vl+5; 136 | ts[tl+10]= vl+7; 137 | ts[tl+11]= vl+3; 138 | 139 | m.vertices = vs; 140 | m.uv = uvs; 141 | m.triangles = ts; 142 | m.RecalculateBounds(); 143 | m.RecalculateNormals(); 144 | 145 | } 146 | 147 | Vector3[] resizeVertices(Vector3[] ovs, int ns) { 148 | Vector3[] nvs = new Vector3[ovs.Length + ns]; 149 | for (int i = 0; i < ovs.Length; i++) nvs [i] = ovs [i]; 150 | return nvs; 151 | } 152 | 153 | Vector2[] resizeUVs(Vector2[] uvs, int ns){ 154 | Vector2[] nvs = new Vector2[uvs.Length + ns]; 155 | for (int i = 0; i < uvs.Length; i++) nvs [i] = uvs [i]; 156 | return nvs; 157 | } 158 | 159 | int[] resizeTraingles(int[]ovs, int ns){ //!!Misspelled triangles !!! 160 | int[] nvs = new int[ovs.Length+ns]; 161 | for (int i = 0; i < ovs.Length; i++) nvs [i] = ovs [i]; 162 | return nvs; 163 | } 164 | } 165 | 166 | 167 | 168 | //Not used in video but was created when new script was created. 169 | //I do not think you need this. But if so, place a beginning of code. It works without though. 170 | //using System.Collections; --------------------------------------------------------------------------------