├── .gitattributes ├── .gitignore ├── LineOfSight.cs ├── README.md ├── docs ├── LineOfSight_breakout.gif └── LineOfSight_motion.gif └── samples └── lineofsight_demo.unitypackage /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /LineOfSight.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Runtime.InteropServices; 5 | using UnityEngine; 6 | 7 | public class LineOfSight : MonoBehaviour 8 | { 9 | 10 | public int subdivisions = 20; 11 | public int maxIterations = 2; 12 | public float fovBegin = -30; 13 | public float fovEnd = 30; 14 | 15 | Mesh lightMesh; 16 | List vertices = new List(); 17 | const float EPSYLON = 0.00001f; 18 | 19 | void Start() 20 | { 21 | lightMesh = GetComponent().mesh; 22 | } 23 | 24 | void CastRays(float startAngle, float stopAngle = 2 * Mathf.PI, int iter = 0) 25 | { 26 | float step = (((Mathf.PI * 2f) - startAngle) + stopAngle) % (Mathf.PI * 2f) / (float)subdivisions; 27 | float angle = startAngle; 28 | int rays = subdivisions + 1; 29 | RaycastHit[] hits = new RaycastHit[rays]; 30 | 31 | // ################################ 32 | // STEP 1 : SHOOT RAYCASTS 33 | // ################################ 34 | 35 | for (int i = 0; i < rays; i++) 36 | { 37 | float x = Mathf.Sin(angle); 38 | float y = Mathf.Cos(angle); 39 | int ignoreHolder = ~(1 << 2); // Ignore objects wearing the "Ignore Raycast" mask 40 | Physics.Raycast(transform.position, new Vector3(x, 0, y), out hits[i], Mathf.Infinity, ignoreHolder); 41 | 42 | angle = (angle + step) % (2f * Mathf.PI); 43 | } 44 | 45 | // ################################ 46 | // STEP 2 : SUBDIVIDE IF NECESSARY 47 | // ################################ 48 | 49 | Vector3 hitPoint = transform.InverseTransformPoint(hits[0].point); 50 | vertices.Add(new Vector2(hitPoint.x, hitPoint.z)); 51 | 52 | for (int i = 1; i < rays; i++) 53 | { 54 | if (hits[i].transform != null && hits[i - 1].transform != null) 55 | if (hits[i].transform != hits[i - 1].transform) 56 | if (iter < maxIterations - 1) 57 | { 58 | float start = (startAngle + (float)(i - 1) * step) % (2f * Mathf.PI); 59 | float stop = (start + step) % (2f * Mathf.PI); 60 | CastRays(start, stop, iter + 1); 61 | } 62 | 63 | hitPoint = transform.InverseTransformPoint(hits[i].point); 64 | vertices.Add(new Vector2(hitPoint.x, hitPoint.z)); 65 | } 66 | } 67 | 68 | void Update() 69 | { 70 | vertices.Clear(); 71 | 72 | // ######################################## 73 | // PHASE 1 : CALCULATE VERTICES 74 | // ######################################## 75 | 76 | float from = (Quaternion.Euler(0, fovBegin, 0) * transform.rotation).eulerAngles.y * Mathf.Deg2Rad; 77 | float to = (Quaternion.Euler(0, fovEnd, 0) * transform.rotation).eulerAngles.y * Mathf.Deg2Rad; 78 | 79 | Profiler.BeginSample("Recursive raycasting"); 80 | 81 | CastRays(from, to); 82 | 83 | Profiler.EndSample(); 84 | 85 | Profiler.BeginSample("Duplicates removing"); 86 | 87 | for (int i = vertices.Count - 1; i > 0; i--) 88 | { 89 | Vector2 relative = new Vector2(vertices[i - 1].x - vertices[i].x, vertices[i - 1].y - vertices[i].y); 90 | if (relative.SqrMagnitude() < EPSYLON) 91 | { 92 | vertices.RemoveAt(i); 93 | } 94 | } 95 | 96 | Profiler.EndSample(); 97 | 98 | Profiler.BeginSample("Vertices interpolation"); 99 | 100 | // Calculate intermediate vertices (we need a multiple of 3) 101 | vertices.Add(new Vector3(0, 0, 0)); 102 | int differenceToMultiple = Mathf.Abs((3 - ((vertices.Count) % 3)) % 3); 103 | for (int i = 1; i <= differenceToMultiple; i++) 104 | { 105 | vertices.Add(new Vector2((float)(i) * vertices[0].x / (float)(differenceToMultiple + 1), (float)(i) * vertices[0].y / (float)(differenceToMultiple + 1))); 106 | } 107 | 108 | Profiler.EndSample(); 109 | 110 | // ############################################ 111 | // PHASE 2 : GENERATE MESH TRIANGLES 112 | // ############################################ 113 | 114 | Profiler.BeginSample("Triangulation"); 115 | 116 | Triangulator triangulator = new Triangulator(vertices); 117 | int[] triangles = triangulator.Triangulate(); 118 | 119 | Profiler.EndSample(); 120 | 121 | 122 | // ############################################ 123 | // PHASE 3 : UPDATE MESH VERTICES AND TRIANGLES 124 | // ############################################ 125 | 126 | Profiler.BeginSample("Mesh updating"); 127 | 128 | Vector3[] vertices3 = new Vector3[vertices.Count]; 129 | for (int i = 0; i < vertices3.Length; i++) 130 | { 131 | vertices3[i] = new Vector3(vertices[i].x, 0f, vertices[i].y); 132 | } 133 | 134 | 135 | lightMesh.triangles = new int[0]; 136 | lightMesh.vertices = vertices3; 137 | lightMesh.triangles = triangles; 138 | 139 | lightMesh.normals = new Vector3[vertices3.Length]; 140 | lightMesh.uv = new Vector2[vertices3.Length]; 141 | 142 | // Use this instead if you want lightning on the mesh 143 | // lightMesh.RecalculateNormals(); 144 | 145 | Profiler.EndSample(); 146 | } 147 | 148 | } 149 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Unity - Line of sight mesh generator 2 | ================= 3 | 4 | ##What is this ? 5 | 6 | This piece of code generates a mesh representing the line of sight of a player in a real time 3D Unity Engine scene. 7 | 8 | A picture is worth a thousand words: 9 | 10 | ![](docs/LineOfSight_motion.gif) 11 | 12 | ##How does it works ? 13 | 14 | Since this raycasting script focusses on outer edges detection, it works best with angular objects. 15 | 16 | First, let's say we cast 20 rays. Then we compare each pair of rays: if they hit a different scene object, we cast 20 other rays between these 2 rays and so on in a recursive way. When we are done (or we reached the number of maximum iterations), we generate a mesh with all the rays hitpoints using a [triangulation algorithm](http://wiki.unity3d.com/index.php?title=Triangulator). 17 | 18 | ![](docs/LineOfSight_breakout.gif) 19 | *From the first to the last iteration of raycasting algorithm, the mesh is generated from the hitpoints* 20 | 21 | Note: your scene must be closed or it won't work! (the rays must hit something sometime) 22 | 23 | ##Try it! 24 | 25 | Check out the demo Unity Package in the samples folder to try it yourself. 26 | 27 | -------------------------------------------------------------------------------- /docs/LineOfSight_breakout.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eoger/unity-lineofsight/b6446eb82515a221ed2768ad2645cbdcf001d60e/docs/LineOfSight_breakout.gif -------------------------------------------------------------------------------- /docs/LineOfSight_motion.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eoger/unity-lineofsight/b6446eb82515a221ed2768ad2645cbdcf001d60e/docs/LineOfSight_motion.gif -------------------------------------------------------------------------------- /samples/lineofsight_demo.unitypackage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eoger/unity-lineofsight/b6446eb82515a221ed2768ad2645cbdcf001d60e/samples/lineofsight_demo.unitypackage --------------------------------------------------------------------------------