├── Assets ├── Plugins.meta ├── Plugins │ ├── Editor.meta │ └── Editor │ │ ├── JetBrains.meta │ │ └── JetBrains │ │ ├── Unity3DRider.cs │ │ └── Unity3DRider.cs.meta ├── UnityUtils.meta └── UnityUtils │ ├── Collada.meta │ ├── Collada │ ├── Editor.meta │ ├── Editor │ │ ├── ColladaExportTest.cs │ │ └── ColladaExportTest.cs.meta │ ├── ExportToCollada.cs │ ├── ExportToCollada.cs.meta │ ├── Materials.meta │ ├── Materials │ │ ├── No Name.mat │ │ └── No Name.mat.meta │ ├── collada_schema_1_4.cs │ └── collada_schema_1_4.cs.meta │ ├── HMesh.meta │ ├── HMesh │ ├── ConvexHull.cs │ ├── ConvexHull.cs.meta │ ├── DelaunayTriangulation2D.cs │ ├── DelaunayTriangulation2D.cs.meta │ ├── EarClipping.cs │ ├── EarClipping.cs.meta │ ├── Face.cs │ ├── Face.cs.meta │ ├── HMesh.cs │ ├── HMesh.cs.meta │ ├── HMeshMath.cs │ ├── HMeshMath.cs.meta │ ├── HMeshOptimizer.cs │ ├── HMeshOptimizer.cs.meta │ ├── HMeshRenderer.cs │ ├── HMeshRenderer.cs.meta │ ├── HMeshSimplification.cs │ ├── HMeshSimplification.cs.meta │ ├── Halfedge.cs │ ├── Halfedge.cs.meta │ ├── Heap.cs │ ├── Heap.cs.meta │ ├── IBounds.cs │ ├── IBounds.cs.meta │ ├── IntPair.cs │ ├── IntPair.cs.meta │ ├── Test.meta │ ├── Test │ │ ├── EarClipTest.cs │ │ ├── EarClipTest.cs.meta │ │ ├── Editor.meta │ │ ├── Editor │ │ │ ├── EarClipTestEditor.cs │ │ │ ├── EarClipTestEditor.cs.meta │ │ │ ├── HMeshUTest.cs │ │ │ ├── HMeshUTest.cs.meta │ │ │ ├── Resources.meta │ │ │ └── Resources │ │ │ │ ├── Materials.meta │ │ │ │ ├── Materials │ │ │ │ ├── defaultMat.mat │ │ │ │ └── defaultMat.mat.meta │ │ │ │ ├── hmeshbug.obj │ │ │ │ ├── hmeshbug.obj.meta │ │ │ │ ├── hmeshbug.txt │ │ │ │ ├── hmeshbug.txt.meta │ │ │ │ ├── hmeshbug2.obj │ │ │ │ ├── hmeshbug2.obj.meta │ │ │ │ ├── hmeshbug2.txt │ │ │ │ ├── hmeshbug2.txt.meta │ │ │ │ ├── hmeshbug3.obj │ │ │ │ ├── hmeshbug3.obj.meta │ │ │ │ ├── hmeshbug3.txt │ │ │ │ └── hmeshbug3.txt.meta │ │ ├── test.json │ │ └── test.json.meta │ ├── TestApp.meta │ ├── TestApp │ │ ├── GUIController.cs │ │ ├── GUIController.cs.meta │ │ ├── HMeshTestScene.unity │ │ └── HMeshTestScene.unity.meta │ ├── UI.meta │ ├── UI │ │ ├── Editor.meta │ │ ├── Editor │ │ │ ├── HMeshARendererEditor.cs │ │ │ ├── HMeshARendererEditor.cs.meta │ │ │ ├── HMeshFilterOperationEditor.cs │ │ │ ├── HMeshFilterOperationEditor.cs.meta │ │ │ ├── HMeshRendererEditor.cs │ │ │ └── HMeshRendererEditor.cs.meta │ │ ├── HMeshARenderer.cs │ │ ├── HMeshARenderer.cs.meta │ │ ├── HMeshFilter.cs │ │ ├── HMeshFilter.cs.meta │ │ ├── HMeshFilterOperation.cs │ │ ├── HMeshFilterOperation.cs.meta │ │ ├── HMeshLineRenderer.cs │ │ └── HMeshLineRenderer.cs.meta │ ├── Vertex.cs │ ├── Vertex.cs.meta │ ├── test.json │ └── test.json.meta │ ├── KDTree.cs │ ├── KDTree.cs.meta │ ├── Math.meta │ ├── Math │ ├── Bounds2D.cs │ ├── Bounds2D.cs.meta │ ├── Editor.meta │ ├── Editor │ │ ├── MathTest.cs │ │ ├── MathTest.cs.meta │ │ ├── Plane3DTest.cs │ │ └── Plane3DTest.cs.meta │ ├── LineSegment.cs │ ├── LineSegment.cs.meta │ ├── LineSegment2D.cs │ ├── LineSegment2D.cs.meta │ ├── Matrix3x3.cs │ ├── Matrix3x3.cs.meta │ ├── Matrix3x3D.cs │ ├── Matrix3x3D.cs.meta │ ├── Matrix4x4D.cs │ ├── Matrix4x4D.cs.meta │ ├── Plane3D.cs │ ├── Plane3D.cs.meta │ ├── Polygon3D.cs │ ├── Polygon3D.cs.meta │ ├── Ray2DD.cs │ ├── Ray2DD.cs.meta │ ├── RayD.cs │ ├── RayD.cs.meta │ ├── TangentSolver.cs │ ├── TangentSolver.cs.meta │ ├── Triangle3D.cs │ ├── Triangle3D.cs.meta │ ├── Vector2D.cs │ ├── Vector2D.cs.meta │ ├── Vector2i.cs │ ├── Vector2i.cs.meta │ ├── Vector3d.cs │ ├── Vector3d.cs.meta │ ├── Vector3i.cs │ ├── Vector3i.cs.meta │ ├── Vector4D.cs │ └── Vector4D.cs.meta │ ├── Obj.meta │ ├── Obj │ ├── ExportToObj.cs │ └── ExportToObj.cs.meta │ ├── SVG.meta │ ├── SVG │ ├── BSPTree.cs │ ├── BSPTree.cs.meta │ ├── Editor.meta │ ├── Editor │ │ ├── BSPTreeTest.cs │ │ └── BSPTreeTest.cs.meta │ ├── SVGDocument.cs │ ├── SVGDocument.cs.meta │ ├── SVGExportProps.cs │ ├── SVGExportProps.cs.meta │ ├── SVGGroup.cs │ ├── SVGGroup.cs.meta │ ├── SVGNode.cs │ ├── SVGNode.cs.meta │ ├── SVGPolygon.cs │ ├── SVGPolygon.cs.meta │ ├── Test.mat │ ├── Test.mat.meta │ ├── test.json │ └── test.json.meta │ ├── Scene.meta │ ├── Scene │ ├── LineSegmentRenderer.cs │ └── LineSegmentRenderer.cs.meta │ ├── ThirdParty.meta │ ├── ThirdParty │ ├── Priority Queue.meta │ ├── Priority Queue │ │ ├── FastPriorityQueue.cs │ │ ├── FastPriorityQueue.cs.meta │ │ ├── FastPriorityQueueNode.cs │ │ ├── FastPriorityQueueNode.cs.meta │ │ ├── GenericPriorityQueue.cs │ │ ├── GenericPriorityQueue.cs.meta │ │ ├── GenericPriorityQueueNode.cs │ │ ├── GenericPriorityQueueNode.cs.meta │ │ ├── IFixedSizePriorityQueue.cs │ │ ├── IFixedSizePriorityQueue.cs.meta │ │ ├── IPriorityQueue.cs │ │ ├── IPriorityQueue.cs.meta │ │ ├── LICENSE.txt │ │ ├── LICENSE.txt.meta │ │ ├── Priority Queue.csproj │ │ ├── Priority Queue.csproj.meta │ │ ├── Priority Queue.nuspec │ │ ├── Priority Queue.nuspec.meta │ │ ├── Properties.meta │ │ ├── Properties │ │ │ ├── AssemblyInfo.cs │ │ │ └── AssemblyInfo.cs.meta │ │ ├── SimplePriorityQueue.cs │ │ ├── SimplePriorityQueue.cs.meta │ │ ├── StablePriorityQueue.cs │ │ ├── StablePriorityQueue.cs.meta │ │ ├── StablePriorityQueueNode.cs │ │ ├── StablePriorityQueueNode.cs.meta │ │ ├── packages.config │ │ └── packages.config.meta │ ├── SimpleJSON.cs │ └── SimpleJSON.cs.meta │ ├── extensions.meta │ └── extensions │ ├── BinaryReaderExt.cs │ ├── BinaryReaderExt.cs.meta │ ├── DebugExt.cs │ ├── DebugExt.cs.meta │ ├── Ext.cs │ ├── Ext.cs.meta │ ├── MeshDebug.cs │ ├── MeshDebug.cs.meta │ ├── Vector2Ext.cs │ ├── Vector2Ext.cs.meta │ ├── Vector3Ext.cs │ └── Vector3Ext.cs.meta ├── LICENSE ├── LICENSE - Thirdparty └── README.md /Assets/Plugins.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cc776d668db934197817fb4e564b36b9 3 | folderAsset: yes 4 | timeCreated: 1516608241 5 | licenseType: Pro 6 | DefaultImporter: 7 | externalObjects: {} 8 | userData: 9 | assetBundleName: 10 | assetBundleVariant: 11 | -------------------------------------------------------------------------------- /Assets/Plugins/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9c0c149ed76394ffe8171548b251baac 3 | folderAsset: yes 4 | timeCreated: 1516608241 5 | licenseType: Pro 6 | DefaultImporter: 7 | externalObjects: {} 8 | userData: 9 | assetBundleName: 10 | assetBundleVariant: 11 | -------------------------------------------------------------------------------- /Assets/Plugins/Editor/JetBrains.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 934b9cebb059e4293a8b8353b09ce500 3 | folderAsset: yes 4 | timeCreated: 1516608241 5 | licenseType: Pro 6 | DefaultImporter: 7 | externalObjects: {} 8 | userData: 9 | assetBundleName: 10 | assetBundleVariant: 11 | -------------------------------------------------------------------------------- /Assets/Plugins/Editor/JetBrains/Unity3DRider.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a391c4f58d2d54711ad60e3779d39f72 3 | timeCreated: 1516608241 4 | licenseType: Pro 5 | MonoImporter: 6 | externalObjects: {} 7 | serializedVersion: 2 8 | defaultReferences: [] 9 | executionOrder: 0 10 | icon: {instanceID: 0} 11 | userData: 12 | assetBundleName: 13 | assetBundleVariant: 14 | -------------------------------------------------------------------------------- /Assets/UnityUtils.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 43d26481003054518b332291bf377394 3 | folderAsset: yes 4 | timeCreated: 1516608071 5 | licenseType: Pro 6 | DefaultImporter: 7 | externalObjects: {} 8 | userData: 9 | assetBundleName: 10 | assetBundleVariant: 11 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Collada.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7b18b890dc1a842e0b037e64a0ad24cd 3 | folderAsset: yes 4 | timeCreated: 1491122660 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Collada/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ca5e2964db7bb465c88e81f70a3b483b 3 | folderAsset: yes 4 | timeCreated: 1491122822 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Collada/Editor/ColladaExportTest.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using UnityEngine; 3 | using UnityEditor; 4 | 5 | public class ColladaExportTest : MonoBehaviour { 6 | 7 | [MenuItem("Collada/ExportTest")] 8 | static void Start () 9 | { 10 | var g = Selection.gameObjects; 11 | ExportToCollada export = new ExportToCollada("/Users/mnob/programming/Unity/dtu_3d_model/UnityProject/Assets/scripts/test/ColTest/"); 12 | foreach (var go in g){ 13 | if (go == null) 14 | { 15 | continue; 16 | } 17 | var mfs = go.GetComponentsInChildren(); 18 | foreach (var mf in mfs){ 19 | if (mf == null) 20 | { 21 | continue; 22 | } 23 | var mats = mf.GetComponent().sharedMaterials; 24 | export.AddMeshWithMaterials(mf.sharedMesh,mats, mf.transform.localToWorldMatrix, mf.name); 25 | } 26 | } 27 | export.Save("/Users/mnob/programming/Unity/dtu_3d_model/UnityProject/Assets/scripts/test/ColTest/test2.dae"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Collada/Editor/ColladaExportTest.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7223f9b18f3084196839052ef4ea19bf 3 | timeCreated: 1491123366 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Collada/ExportToCollada.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 531afc7e789c9476785f677146b50eda 3 | timeCreated: 1491123385 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Collada/Materials.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7384382e8279b4e65abdce1ad6bba9bd 3 | folderAsset: yes 4 | timeCreated: 1491133899 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Collada/Materials/No Name.mat: -------------------------------------------------------------------------------- 1 | %YAML 1.1 2 | %TAG !u! tag:unity3d.com,2011: 3 | --- !u!21 &2100000 4 | Material: 5 | serializedVersion: 6 6 | m_ObjectHideFlags: 0 7 | m_PrefabParentObject: {fileID: 0} 8 | m_PrefabInternal: {fileID: 0} 9 | m_Name: No Name 10 | m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0} 11 | m_ShaderKeywords: 12 | m_LightmapFlags: 5 13 | m_EnableInstancingVariants: 0 14 | m_DoubleSidedGI: 0 15 | m_CustomRenderQueue: -1 16 | stringTagMap: {} 17 | disabledShaderPasses: [] 18 | m_SavedProperties: 19 | serializedVersion: 3 20 | m_TexEnvs: 21 | - _BumpMap: 22 | m_Texture: {fileID: 0} 23 | m_Scale: {x: 1, y: 1} 24 | m_Offset: {x: 0, y: 0} 25 | - _DetailAlbedoMap: 26 | m_Texture: {fileID: 0} 27 | m_Scale: {x: 1, y: 1} 28 | m_Offset: {x: 0, y: 0} 29 | - _DetailMask: 30 | m_Texture: {fileID: 0} 31 | m_Scale: {x: 1, y: 1} 32 | m_Offset: {x: 0, y: 0} 33 | - _DetailNormalMap: 34 | m_Texture: {fileID: 0} 35 | m_Scale: {x: 1, y: 1} 36 | m_Offset: {x: 0, y: 0} 37 | - _EmissionMap: 38 | m_Texture: {fileID: 0} 39 | m_Scale: {x: 1, y: 1} 40 | m_Offset: {x: 0, y: 0} 41 | - _MainTex: 42 | m_Texture: {fileID: 0} 43 | m_Scale: {x: 1, y: 1} 44 | m_Offset: {x: 0, y: 0} 45 | - _MetallicGlossMap: 46 | m_Texture: {fileID: 0} 47 | m_Scale: {x: 1, y: 1} 48 | m_Offset: {x: 0, y: 0} 49 | - _OcclusionMap: 50 | m_Texture: {fileID: 0} 51 | m_Scale: {x: 1, y: 1} 52 | m_Offset: {x: 0, y: 0} 53 | - _ParallaxMap: 54 | m_Texture: {fileID: 0} 55 | m_Scale: {x: 1, y: 1} 56 | m_Offset: {x: 0, y: 0} 57 | m_Floats: 58 | - _BumpScale: 1 59 | - _Cutoff: 0.5 60 | - _DetailNormalMapScale: 1 61 | - _DstBlend: 0 62 | - _GlossMapScale: 1 63 | - _Glossiness: 0.5 64 | - _GlossyReflections: 1 65 | - _Metallic: 0 66 | - _Mode: 0 67 | - _OcclusionStrength: 1 68 | - _Parallax: 0.02 69 | - _SmoothnessTextureChannel: 0 70 | - _SpecularHighlights: 1 71 | - _SrcBlend: 1 72 | - _UVSec: 0 73 | - _ZWrite: 1 74 | m_Colors: 75 | - _Color: {r: 1, g: 1, b: 1, a: 1} 76 | - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} 77 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Collada/Materials/No Name.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e759fb0036b2c4659982a5af21dbe406 3 | timeCreated: 1491133899 4 | licenseType: Pro 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Collada/collada_schema_1_4.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1e034c6eff9b448c4b087ba1b0e01c18 3 | timeCreated: 1491122534 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cfc196431ccea4b3c89b40b8b506eea0 3 | folderAsset: yes 4 | timeCreated: 1443282179 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/ConvexHull.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | /* 5 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 6 | * 7 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 8 | * License: MIT 9 | */ 10 | 11 | // Find Convex Hull in 2d (using only x and y) 12 | public class ConvexHull { 13 | // Aka Jarvis march 14 | public static List GiftWrapping(List positions) { 15 | // Find leftmost point 16 | int leftMostPoint = 0; 17 | for (int i=1;i res = new List(); 25 | do { 26 | res.Add(positions[pointOnHull]); 27 | endPoint = pointOnHull==0?1:0; 28 | for (int j = 0;j GiftWrappingXZ(List positions) { 40 | // Find leftmost point 41 | int leftMostPoint = 0; 42 | for (int i=1;i res = new List(); 50 | do { 51 | res.Add(positions[pointOnHull]); 52 | endPoint = pointOnHull==0?1:0; 53 | for (int j = 0;j AklToussaintHeuristic(List positions){ 64 | int maxX = 0, maxY = 0, minX = 0, minY = 0; 65 | 66 | 67 | for (int i=0;i positions[maxX].x){ 72 | maxX = i; 73 | } 74 | if (positions[i].y < positions[minY].y){ 75 | minY = i; 76 | } 77 | if (positions[i].y > positions[maxY].y){ 78 | maxY = i; 79 | } 80 | } 81 | List points = new List(); 82 | points.Add(minX); 83 | if (!points.Contains(minY)){ 84 | points.Add(minY); 85 | } 86 | if (!points.Contains(maxX)){ 87 | points.Add(maxX); 88 | } 89 | if (!points.Contains(maxY)){ 90 | points.Add(maxY); 91 | } 92 | 93 | if (points.Count<=2){ 94 | return positions; 95 | } 96 | 97 | /*Gizmos.color = Color.green; 98 | for (int i=0;i res = new List(); 103 | 104 | for (int i=0;i AklToussaintHeuristicXZ(List positions){ 122 | int maxX = 0, maxZ = 0, minX = 0, minZ = 0; 123 | 124 | 125 | for (int i=0;i positions[maxX].x){ 130 | maxX = i; 131 | } 132 | if (positions[i].y < positions[minZ].z){ 133 | minZ = i; 134 | } 135 | if (positions[i].y > positions[maxZ].z){ 136 | maxZ = i; 137 | } 138 | } 139 | List points = new List(); 140 | points.Add(minX); 141 | if (!points.Contains(minZ)){ 142 | points.Add(minZ); 143 | } 144 | if (!points.Contains(maxX)){ 145 | points.Add(maxX); 146 | } 147 | if (!points.Contains(maxZ)){ 148 | points.Add(maxZ); 149 | } 150 | 151 | if (points.Count<=2){ 152 | return positions; 153 | } 154 | 155 | /*Gizmos.color = Color.green; 156 | for (int i=0;i res = new List(); 161 | 162 | for (int i=0;i positions) { 24 | HMesh mesh = new HMesh(); 25 | Stack flipStack = new Stack(); 26 | 27 | var outerVerts = CreateBoundingTriangle(mesh, positions); 28 | 29 | foreach (var pos in positions) 30 | { 31 | var triangle = FindTriangleWithinPoint(mesh, pos); 32 | AddEdgesToFlipStack(triangle, flipStack); 33 | bool inserted = InsertPointInTriangle(triangle, pos,flipStack); 34 | if (!inserted) 35 | { 36 | continue; 37 | } 38 | while (flipStack.Count > 0){ 39 | var e = flipStack.Pop(); 40 | if (IsLocalDelaunay(e)) continue; 41 | var opp = e.opp; 42 | var check1 = opp.next; 43 | var check2 = check1.next; 44 | var check3 = e.next; 45 | var check4 = check3.next; 46 | if (!e.FlipPrecondition()) continue; 47 | flipStack.Push(check1); 48 | flipStack.Push(check2); 49 | flipStack.Push(check3); 50 | flipStack.Push(check4); 51 | e.Flip(); 52 | } 53 | } 54 | 55 | foreach (var vert in outerVerts) 56 | { 57 | foreach (var he in vert.Circulate()) 58 | { 59 | //he.face.Dissolve(); 60 | } 61 | } 62 | 63 | return mesh; 64 | } 65 | 66 | static bool IsLocalDelaunay(Halfedge e){ 67 | if (e.IsBoundary()){ 68 | return true; 69 | } 70 | var p1 = e.vert.positionD; 71 | var p2 = e.next.vert.positionD; 72 | var p3 = e.next.next.vert.positionD; 73 | var p4 = e.opp.next.vert.positionD; 74 | if (projectPlane == ProjectionPlane.XZ) 75 | { 76 | return HMeshMath.InCircleXZ(p1, p2, p3, p4) || HMeshMath.InCircleXZ(p3, p1, p4, p2); 77 | } 78 | else 79 | { 80 | Debug.LogError("Not implemented"); 81 | return false; 82 | } 83 | } 84 | 85 | static bool InsertPointInTriangle(Face triangle, Vector3D point, Stack flipStack) 86 | { 87 | var minVertexDistance2 = minVertexDistance * minVertexDistance; 88 | foreach (var he in triangle.Circulate()) 89 | { 90 | if ((he.vert.positionD - point).sqrMagnitude < minVertexDistance2) 91 | { 92 | return false; 93 | } 94 | } 95 | var vertex =triangle.Split(); 96 | vertex.positionD = point; 97 | 98 | foreach (var e in vertex.Circulate()) 99 | { 100 | flipStack.Push(e); 101 | } 102 | 103 | return true; 104 | } 105 | 106 | static List CreateBoundingTriangle(HMesh mesh, List position) { 107 | BoundsD b = new BoundsD(position[0], Vector3D.zero); 108 | for (int i=1;i boundingVertices = new List(); 122 | foreach (var he in face.Circulate()){ 123 | boundingVertices.Add(he.vert); 124 | } 125 | return boundingVertices; 126 | } 127 | 128 | static void AddEdgesToFlipStack(Face f, Stack flipStack){ 129 | foreach (var he in f.Circulate()){ 130 | flipStack.Push(he); 131 | } 132 | } 133 | 134 | static bool IsPointInTriangle(Face face, Vector3D pos) 135 | { 136 | var edges = face.Circulate(); 137 | var p1 = edges[0].vert.positionD; 138 | var p2 = edges[1].vert.positionD; 139 | var p3 = edges[2].vert.positionD; 140 | 141 | if (projectPlane == ProjectionPlane.XZ) 142 | { 143 | if (HMeshMath.RightOfXZ(p1, p2, pos) && 144 | HMeshMath.RightOfXZ(p2, p3, pos) && 145 | HMeshMath.RightOfXZ(p3, p1, pos)) 146 | { 147 | return true; 148 | } 149 | } 150 | else 151 | { 152 | Debug.LogError("Not implemented"); 153 | } 154 | return false; 155 | } 156 | 157 | static Face FindTriangleWithinPoint(HMesh mesh, Vector3D pos){ 158 | foreach (var face in mesh.GetFacesRaw()){ 159 | if (IsPointInTriangle(face, pos)) 160 | { 161 | return face; 162 | } 163 | } 164 | return null; 165 | } 166 | 167 | public static HMesh TriangulateOrderedTriangles(List triangles) 168 | { 169 | var mesh = Triangulate(triangles); 170 | 171 | string vectorsStr = "{"; 172 | foreach (var t in triangles) 173 | { 174 | vectorsStr+="new Vector3D"+t.ToString("R")+",\n"; 175 | } 176 | vectorsStr+="}"; 177 | Debug.Log(vectorsStr); 178 | 179 | Bounds b = new Bounds(); 180 | bool initialized = false; 181 | foreach (var vert in mesh.GetVerticesRaw()) 182 | { 183 | if (vert.IsBoundary()) 184 | { 185 | continue; 186 | } 187 | if (!initialized) 188 | { 189 | initialized = true; 190 | b = new Bounds(vert.position, Vector3.zero); 191 | } 192 | else 193 | { 194 | b.Encapsulate(vert.position); 195 | } 196 | } 197 | 198 | // build acceleration structure 199 | KDTree vertexTree = new KDTree(7, b); 200 | foreach (var vert in mesh.GetVerticesRaw()) 201 | { 202 | if (vert.IsBoundary()) 203 | { 204 | continue; 205 | } 206 | vertexTree.Insert(vert, new Bounds(vert.position,Vector3.zero)); 207 | 208 | } 209 | Debug.Log(vertexTree.Debug()); 210 | // enforce triangle flipping 211 | for (int i = 0; i < triangles.Count; i = i + 3) 212 | { 213 | var p0 = triangles[i]; 214 | var p1 = triangles[i+1]; 215 | var p2 = triangles[i+2]; 216 | 217 | var v0 = FindClosestVertex(vertexTree,p0.ToVector3()); 218 | var v1 = FindClosestVertex(vertexTree,p1.ToVector3()); 219 | var v2 = FindClosestVertex(vertexTree,p2.ToVector3()); 220 | 221 | if (v0 == null) 222 | { 223 | Debug.Log("Cannot find "+p0.ToString("R")); 224 | } else { 225 | Debug.Log(Vector3.Distance(p0.ToVector3(), v0.position)); 226 | } 227 | if (v1 == null) 228 | { 229 | Debug.Log("Cannot find "+p1.ToString("R")); 230 | } else 231 | { 232 | Debug.Log(Vector3.Distance(p1.ToVector3(), v1.position)); 233 | } 234 | if (v2 == null) 235 | { 236 | Debug.Log("Cannot find "+p2.ToString("R")); 237 | } 238 | else 239 | { 240 | Debug.Log(Vector3.Distance(p2.ToVector3(), v2.position)); 241 | } 242 | 243 | } 244 | 245 | return mesh; 246 | } 247 | 248 | static Vertex FindClosestVertex(KDTree vertexTree, Vector3 pos) 249 | { 250 | var res = vertexTree.Query(new Bounds(pos, Vector3.one * (float)(minVertexDistance*1.000001))); 251 | double minDist = double.MaxValue; 252 | Vertex currentV = null; 253 | foreach (var v in res) 254 | { 255 | foreach (var vv in v.values) 256 | { 257 | double dist = Vector3.Distance(pos, vv.value.position); 258 | if (dist < minDist) 259 | { 260 | minDist = dist; 261 | currentV = vv.value; 262 | } 263 | } 264 | } 265 | return currentV; 266 | } 267 | } 268 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/DelaunayTriangulation2D.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ee20893acc5084f189148bf0ed04fbeb 3 | timeCreated: 1469714656 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/EarClipping.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | /* 5 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 6 | * 7 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 8 | * License: MIT 9 | */ 10 | 11 | // based on https://github.com/prideout/polygon.js/blob/master/js/polygon.js 12 | public class EarClipping : MonoBehaviour { 13 | // currently discards y value 14 | public static List Tesselate(Face face){ 15 | if (face.NoEdges() == 3){ 16 | var r = new List(); 17 | r.Add(face); 18 | return r; 19 | } 20 | 21 | List newFaces = new List(); 22 | newFaces.Add(face); 23 | while (face.NoEdges() > 3){ 24 | var he = FindEar(face); 25 | if (he==null){ 26 | Debug.LogWarning("Warning - cannot cut ear"); 27 | return newFaces; 28 | } 29 | var otherFace = face.Cut(he.prev.vert,he.next.vert); 30 | newFaces.Add(otherFace); 31 | if (otherFace.NoEdges()>3){ 32 | face = otherFace; 33 | } 34 | } 35 | return newFaces; 36 | } 37 | 38 | static Halfedge FindEar(Face face){ 39 | float minAngle = 99999; 40 | Halfedge minHe = null; 41 | foreach (var he in face.Circulate()){ 42 | Vector3 from3 = he.prev.vert.position; 43 | Vector3 this3 = he.vert.position; 44 | Vector3 next3 = he.next.vert.position; 45 | Vector2 from2 = new Vector2(from3.x, from3.z); 46 | Vector2 this2 = new Vector2(this3.x, this3.z); 47 | Vector2 next2 = new Vector2(next3.x, next3.z); 48 | Vector2 from = from2 - this2; 49 | Vector2 to = next2 - this2; 50 | if (Vector3.Cross(from, to).z < 0){ 51 | bool valid = he.prev.vert != he.next.vert; // special case where two halfedges are extruded into a face from a single vertex 52 | Halfedge h = he.next.next; 53 | while (h != he.prev && valid){ 54 | Vector2 p = new Vector2(h.vert.position.x,h.vert.position.z); 55 | if (PointInTriangle(p, from2, this2, next2)){ 56 | valid = false; 57 | } 58 | h = h.next; 59 | } 60 | 61 | if (valid){ 62 | var angle = Vector2.Angle(from, to); 63 | if (angle < minAngle){ 64 | minAngle = angle; 65 | minHe = he; 66 | } 67 | } 68 | } 69 | } 70 | return minHe; 71 | } 72 | 73 | public static bool PointInTriangle(Vector2 p, Vector2 p0, Vector2 p1, Vector2 p2) 74 | { 75 | var s = p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y; 76 | var t = p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y; 77 | 78 | if ((s < 0) != (t < 0)) 79 | return false; 80 | 81 | var A = -p1.y * p2.x + p0.y * (p2.x - p1.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y; 82 | if (A < 0.0) 83 | { 84 | s = -s; 85 | t = -t; 86 | A = -A; 87 | } 88 | return s > 0 && t > 0 && (s + t) <= A; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/EarClipping.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c2c46ad3d02d44389a5251837e6a19c4 3 | timeCreated: 1470226031 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/Face.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d7f3f52fa33774c66af7b212d83c8f7e 3 | timeCreated: 1443282417 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/HMesh.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 53703eeeecddf4dd7b71ad898187635d 3 | timeCreated: 1443282416 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/HMeshMath.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | 10 | public static class HMeshMath { 11 | // determines if the point p3 lies to the left of the line spanned by p1 and p2. 12 | public static bool LeftOfXY(Vector3D p1, Vector3D p2, Vector3D p3){ 13 | Matrix3x3D mat = Matrix3x3D.zero; 14 | mat.SetRow(0,new Vector3D(1,p1.x, p1.y)); 15 | mat.SetRow(1,new Vector3D(1,p2.x, p2.y)); 16 | mat.SetRow(2,new Vector3D(1,p3.x, p3.y)); 17 | return mat.determinant > 0; 18 | } 19 | 20 | // determines if the point p3 lies to the left of the line spanned by p1 and p2. 21 | public static bool LeftOfXZ(Vector3D p1, Vector3D p2, Vector3D p3){ 22 | Matrix3x3D mat = Matrix3x3D.zero; 23 | mat.SetRow(0,new Vector3D(1,p1.x, p1.z)); 24 | mat.SetRow(1,new Vector3D(1,p2.x, p2.z)); 25 | mat.SetRow(2,new Vector3D(1,p3.x, p3.z)); 26 | return mat.determinant > 0; 27 | } 28 | 29 | // determines if the point p3 lies to the right of the line spanned by p1 and p2. 30 | public static bool RightOfXY(Vector3D p1, Vector3D p2, Vector3D p3){ 31 | return LeftOfXY(p2,p1,p3); 32 | } 33 | 34 | // determines if the point p3 lies to the right of the line spanned by p1 and p2. 35 | public static bool RightOfXZ(Vector3D p1, Vector3D p2, Vector3D p3){ 36 | return LeftOfXZ(p2,p1,p3); 37 | } 38 | 39 | // InCircle(p1,p2,p3,p4) determines if point p4 lies inside of the circumcircle of points p1, p2 and p3, 40 | // where it is assumed that the points p1, p2 and p3 are in counterclockwise order. 41 | public static bool InCircleXY(Vector3D p1,Vector3D p2,Vector3D p3,Vector3D p4){ 42 | Matrix4x4D m = Matrix4x4D.identity; 43 | Vector3D[] a = { 44 | p1,p2,p3,p4 45 | }; 46 | for (int i=0;i<4;i++){ 47 | m.SetRow(i, new Vector4D(a[i].x,a[i].y,a[i].x*a[i].x+a[i].y*a[i].y,1)); 48 | } 49 | return m.determinant<0; 50 | } 51 | 52 | public static bool InCircleXZ(Vector3D p1,Vector3D p2,Vector3D p3,Vector3D p4){ 53 | Matrix4x4D m = Matrix4x4D.identity; 54 | Vector3D[] a = { 55 | p1,p2,p3,p4, 56 | }; 57 | for (int i=0;i<4;i++){ 58 | m.SetRow(i, new Vector4D(a[i].x,a[i].z,(a[i].x*a[i].x) + (a[i].z*a[i].z),1)); 59 | } 60 | return m.determinant<0; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/HMeshMath.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 642129093861146a9ac69763d8042841 3 | timeCreated: 1469714656 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/HMeshOptimizer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3dd33bedbae0643f28d02dd311ca5252 3 | timeCreated: 1487144835 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/HMeshRenderer.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | using UnityEngine; 8 | using System.Collections.Generic; 9 | 10 | public enum HMeshRendererType { 11 | Full, 12 | Normals, 13 | BoundaryEdges, 14 | Vertices 15 | } 16 | 17 | public enum OptimizationStrategy 18 | { 19 | MinimizeDihedralAngle, 20 | MaximizeMinAngle, 21 | OptimizeValency 22 | } 23 | 24 | public class HMeshRenderer : MonoBehaviour { 25 | 26 | public HMesh hmesh; 27 | 28 | public Mesh mesh; 29 | 30 | public OptimizationStrategy optStategy; 31 | public bool OptimizeFaceLabelContrain = true; 32 | public bool OptimizeFaceNormalContrain = true; 33 | public float OptimizeMinMaxAngleThreshold = 0.1f; 34 | [TextArea(3,10)] 35 | public string objfile; 36 | 37 | public Color[] colors = new Color[] 38 | { 39 | Color.red, 40 | Color.green, 41 | Color.blue, 42 | Color.grey, 43 | Color.cyan 44 | }; 45 | 46 | public HMeshRendererType renderType = HMeshRendererType.Full; 47 | 48 | public MeshFilter meshFilter; 49 | 50 | Mesh GetMesh(){ 51 | if (mesh == null){ 52 | mesh = new Mesh(); 53 | mesh.name = "HMesh-to-mesh"; 54 | } 55 | return mesh; 56 | } 57 | 58 | void OnDrawGizmos() { 59 | var mesh = GetMesh(); 60 | 61 | for (int i = 0; i < Mathf.Min(colors.Length,mesh.subMeshCount); i++) 62 | { 63 | Gizmos.color = colors[i]; 64 | Gizmos.DrawWireMesh(mesh, i, transform.position, transform.rotation, transform.localScale); 65 | } 66 | } 67 | 68 | // Update is called once per frame 69 | public Mesh UpdateMesh () { 70 | Mesh mesh = GetMesh(); 71 | 72 | Debug.Log("UpdateMesh"); 73 | 74 | List lines = new List(); 75 | List normals = new List(); 76 | List> indices = new List>(); 77 | HMesh hmesh = this.hmesh; 78 | int vertexId = 0; 79 | foreach (var face in hmesh.GetFacesRaw()) 80 | { 81 | int materialIndex = face.label % colors.Length; 82 | while (indices.Count <= materialIndex) 83 | { 84 | indices.Add(new List()); 85 | } 86 | if (renderType == HMeshRendererType.Normals){ 87 | lines.Add(face.GetCenter().ToVector3()); 88 | lines.Add((face.GetCenter()+face.GetNormal()).ToVector3()); 89 | normals.Add(Vector3.up); 90 | normals.Add(Vector3.up); 91 | indices[materialIndex].Add(vertexId); 92 | vertexId++; 93 | indices[materialIndex].Add(vertexId); 94 | vertexId++; 95 | } else { 96 | foreach (var edge in face.Circulate()){ 97 | if (renderType == HMeshRendererType.BoundaryEdges){ 98 | if (!edge.IsBoundary()){ 99 | continue; 100 | } 101 | } 102 | lines.Add(edge.prev.vert.position); 103 | lines.Add(edge.vert.position); 104 | normals.Add(Vector3.up); 105 | normals.Add(Vector3.up); 106 | indices[materialIndex].Add(vertexId); 107 | vertexId++; 108 | indices[materialIndex].Add(vertexId); 109 | vertexId++; 110 | } 111 | } 112 | } 113 | mesh.Clear(); 114 | mesh.vertices = lines.ToArray(); 115 | mesh.normals = normals.ToArray(); 116 | var meshTopology = renderType == HMeshRendererType.Vertices ? MeshTopology.Points : MeshTopology.Lines; 117 | mesh.subMeshCount = indices.Count; 118 | for (int i = 0; i < indices.Count; i++) 119 | { 120 | mesh.SetIndices(indices[i].ToArray(), meshTopology, i); 121 | } 122 | 123 | mesh.RecalculateBounds(); 124 | mesh.UploadMeshData(false); 125 | 126 | if (meshFilter == null) 127 | { 128 | meshFilter = GetComponent(); 129 | } 130 | if (meshFilter != null) 131 | { 132 | meshFilter.mesh = mesh; 133 | } 134 | 135 | return mesh; 136 | } 137 | 138 | public void CreateHMesh(){ 139 | hmesh = HMesh.CreateTestMesh(); 140 | } 141 | 142 | public void CreateHMeshTriangle(){ 143 | hmesh = HMesh.CreateTestMeshTriangle(); 144 | } 145 | 146 | public void CreateHMeshQuad(){ 147 | hmesh = HMesh.CreateTestMeshQuad(); 148 | } 149 | 150 | public HMesh CreateHMeshNGon(int n) 151 | { 152 | hmesh = HMesh.CreateTestMeshNGon(n); 153 | return hmesh; 154 | } 155 | 156 | public HMesh Triangulate(bool step=false) 157 | { 158 | if (step) 159 | { 160 | var faces = new List(hmesh.GetFacesRaw()); 161 | faces.Sort((face, face1) => 162 | { 163 | return -face.Circulate().Count + face1.Circulate().Count; 164 | }); 165 | Debug.Log("Triangulate "+faces[0]); 166 | faces[0].Triangulate(true); 167 | } else { 168 | hmesh.Triangulate(step); 169 | } 170 | Debug.Log("Valid hmesh "+hmesh.IsValid()); 171 | 172 | return hmesh; 173 | } 174 | 175 | public void ApplyTransform(){ 176 | var localToWorld = transform.localToWorldMatrix; 177 | foreach (var vert in hmesh.GetVerticesRaw()){ 178 | vert.position = localToWorld.MultiplyPoint(vert.position); 179 | } 180 | transform.position = Vector3.zero; 181 | transform.localScale = Vector3.one; 182 | transform.rotation = Quaternion.identity; 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/HMeshRenderer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0a28f097815f54c36bca65296ce344ca 3 | timeCreated: 1464958870 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/HMeshSimplification.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9af31e05bbe9d4316a2503b273d1b39a 3 | timeCreated: 1486995526 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/Halfedge.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0fee44bdc5b5d47178972861ce28c99f 3 | timeCreated: 1443282416 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/Heap.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using System; 4 | using System.Linq; 5 | 6 | /* 7 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 8 | * 9 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 10 | * License: MIT 11 | */ 12 | 13 | public abstract class Heap : IEnumerable 14 | { 15 | private const int InitialCapacity = 0; 16 | private const int GrowFactor = 2; 17 | private const int MinGrow = 1; 18 | 19 | private int _capacity = InitialCapacity; 20 | private T[] _heap = new T[InitialCapacity]; 21 | private int _tail = 0; 22 | 23 | public int Count { get { return _tail; } } 24 | public int Capacity { get { return _capacity; } } 25 | 26 | protected Comparer Comparer { get; private set; } 27 | protected abstract bool Dominates(T x, T y); 28 | 29 | protected Heap() : this(Comparer.Default) 30 | { 31 | } 32 | 33 | protected Heap(Comparer comparer) : this(Enumerable.Empty(), comparer) 34 | { 35 | } 36 | 37 | protected Heap(IEnumerable collection) 38 | : this(collection, Comparer.Default) 39 | { 40 | } 41 | 42 | protected Heap(IEnumerable collection, Comparer comparer) 43 | { 44 | if (collection == null) throw new ArgumentNullException("collection"); 45 | if (comparer == null) throw new ArgumentNullException("comparer"); 46 | 47 | Comparer = comparer; 48 | 49 | foreach (var item in collection) 50 | { 51 | if (Count == Capacity) 52 | Grow(); 53 | 54 | _heap[_tail++] = item; 55 | } 56 | 57 | for (int i = Parent(_tail - 1); i >= 0; i--) 58 | BubbleDown(i); 59 | } 60 | 61 | public void Add(T item) 62 | { 63 | if (Count == Capacity) 64 | Grow(); 65 | 66 | _heap[_tail++] = item; 67 | BubbleUp(_tail - 1); 68 | } 69 | 70 | private void BubbleUp(int i) 71 | { 72 | if (i == 0 || Dominates(_heap[Parent(i)], _heap[i])) 73 | return; //correct domination (or root) 74 | 75 | Swap(i, Parent(i)); 76 | BubbleUp(Parent(i)); 77 | } 78 | 79 | public T GetMin() 80 | { 81 | if (Count == 0) throw new InvalidOperationException("Heap is empty"); 82 | return _heap[0]; 83 | } 84 | 85 | public T ExtractDominating() 86 | { 87 | if (Count == 0) throw new InvalidOperationException("Heap is empty"); 88 | T ret = _heap[0]; 89 | _tail--; 90 | Swap(_tail, 0); 91 | BubbleDown(0); 92 | return ret; 93 | } 94 | 95 | private void BubbleDown(int i) 96 | { 97 | int dominatingNode = Dominating(i); 98 | if (dominatingNode == i) return; 99 | Swap(i, dominatingNode); 100 | BubbleDown(dominatingNode); 101 | } 102 | 103 | private int Dominating(int i) 104 | { 105 | int dominatingNode = i; 106 | dominatingNode = GetDominating(YoungChild(i), dominatingNode); 107 | dominatingNode = GetDominating(OldChild(i), dominatingNode); 108 | 109 | return dominatingNode; 110 | } 111 | 112 | private int GetDominating(int newNode, int dominatingNode) 113 | { 114 | if (newNode < _tail && !Dominates(_heap[dominatingNode], _heap[newNode])) 115 | return newNode; 116 | else 117 | return dominatingNode; 118 | } 119 | 120 | private void Swap(int i, int j) 121 | { 122 | T tmp = _heap[i]; 123 | _heap[i] = _heap[j]; 124 | _heap[j] = tmp; 125 | } 126 | 127 | private static int Parent(int i) 128 | { 129 | return (i + 1)/2 - 1; 130 | } 131 | 132 | private static int YoungChild(int i) 133 | { 134 | return (i + 1)*2 - 1; 135 | } 136 | 137 | private static int OldChild(int i) 138 | { 139 | return YoungChild(i) + 1; 140 | } 141 | 142 | private void Grow() 143 | { 144 | int newCapacity = _capacity*GrowFactor + MinGrow; 145 | var newHeap = new T[newCapacity]; 146 | Array.Copy(_heap, newHeap, _capacity); 147 | _heap = newHeap; 148 | _capacity = newCapacity; 149 | } 150 | 151 | public IEnumerator GetEnumerator() 152 | { 153 | return _heap.Take(Count).GetEnumerator(); 154 | } 155 | 156 | IEnumerator IEnumerable.GetEnumerator() 157 | { 158 | return GetEnumerator(); 159 | } 160 | } 161 | 162 | public class MaxHeap : Heap 163 | { 164 | public MaxHeap() 165 | : this(Comparer.Default) 166 | { 167 | } 168 | 169 | public MaxHeap(Comparer comparer) 170 | : base(comparer) 171 | { 172 | } 173 | 174 | public MaxHeap(IEnumerable collection, Comparer comparer) 175 | : base(collection, comparer) 176 | { 177 | } 178 | 179 | public MaxHeap(IEnumerable collection) : base(collection) 180 | { 181 | } 182 | 183 | protected override bool Dominates(T x, T y) 184 | { 185 | return Comparer.Compare(x, y) >= 0; 186 | } 187 | } 188 | 189 | public class MinHeap : Heap 190 | { 191 | public MinHeap() 192 | : this(Comparer.Default) 193 | { 194 | } 195 | 196 | public MinHeap(Comparer comparer) 197 | : base(comparer) 198 | { 199 | } 200 | 201 | public MinHeap(IEnumerable collection) : base(collection) 202 | { 203 | } 204 | 205 | public MinHeap(IEnumerable collection, Comparer comparer) 206 | : base(collection, comparer) 207 | { 208 | } 209 | 210 | protected override bool Dominates(T x, T y) 211 | { 212 | return Comparer.Compare(x, y) <= 0; 213 | } 214 | } 215 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/Heap.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: be2ed888a38d449b1a227a086e8d6cb3 3 | timeCreated: 1443283943 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/IBounds.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | public interface IBounds 9 | { 10 | BoundsD GetBoundsD(); 11 | } 12 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/IBounds.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 51ef7cc95ec9441c99ac2f1fa3125d65 3 | timeCreated: 1480509037 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/IntPair.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | public struct IntPair : System.IEquatable{ 8 | public readonly int first; 9 | public readonly int second; 10 | public IntPair(int first, int second){ 11 | this.first = first; 12 | this.second = second; 13 | } 14 | 15 | public override bool Equals (object obj) 16 | { 17 | return base.Equals (obj); 18 | } 19 | 20 | public override int GetHashCode () 21 | { 22 | return first ^ second; 23 | } 24 | 25 | bool System.IEquatable.Equals(IntPair obj){ 26 | return first == obj.first && second == obj.second; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/IntPair.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5d55be74e6a3040fa8a34611368e7954 3 | timeCreated: 1443282416 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/Test.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 32b33968fb1954721b847b28dfa3d3ba 3 | folderAsset: yes 4 | timeCreated: 1470228205 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/Test/EarClipTest.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | /* 4 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 5 | * 6 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 7 | * License: MIT 8 | */ 9 | 10 | public class EarClipTest : MonoBehaviour { 11 | void OnDrawGizmosSelected () { 12 | Gizmos.color = Color.green; 13 | for (int i=0;i(); 13 | renderer.hmesh = mesh; 14 | renderer.UpdateMesh(); 15 | } 16 | if (GUILayout.Button("Create and triangulate")){ 17 | var mesh = CreateTestMeshPolygon(earClipTest); 18 | foreach (var f in mesh.GetFaces()){ 19 | EarClipping.Tesselate(f); 20 | } 21 | var renderer = earClipTest.GetComponent(); 22 | renderer.hmesh = mesh; 23 | renderer.UpdateMesh(); 24 | } 25 | } 26 | 27 | public static HMesh CreateTestMeshPolygon(EarClipTest clip) { 28 | HMesh mesh = new HMesh(); 29 | 30 | Transform t = clip.transform; 31 | 32 | var face = mesh.CreateFace(); 33 | 34 | var edges = new Halfedge[t.childCount]; 35 | var verts = new List(); 36 | for (int i=0;i(); 33 | hmeshRenderer.hmesh = hmesh; 34 | hmeshRenderer.UpdateMesh(); 35 | } 36 | 37 | // Update is called once per frame 38 | public void UpdateMenu () { 39 | meshButtons.gameObject.SetActive(menu.value == 0); 40 | vertexButtons.gameObject.SetActive(menu.value == 1); 41 | edgeButtons.gameObject.SetActive(menu.value == 2); 42 | faceButtons.gameObject.SetActive(menu.value == 3); 43 | } 44 | 45 | public void CreateTestMesh() 46 | { 47 | hmesh.CreateTriangle(new Vector3(0,0,0), new Vector3(0,1,0), new Vector3(1,0,0)); 48 | hmeshRenderer.UpdateMesh(); 49 | } 50 | 51 | public void ClearMesh() 52 | { 53 | hmesh.Clear(); 54 | hmeshRenderer.UpdateMesh(); 55 | } 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/TestApp/GUIController.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 29554f9c0e4d44cf3845fc04a68afe00 3 | timeCreated: 1485436267 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/TestApp/HMeshTestScene.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mortennobel/UnityUtils/e153e2582f4f6ae4e3c6bc920e13ef4b11c7b9c4/Assets/UnityUtils/HMesh/TestApp/HMeshTestScene.unity -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/TestApp/HMeshTestScene.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: eba5f3cce5f0b402c858aac914a82dd2 3 | timeCreated: 1485435798 4 | licenseType: Pro 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 17fa865216bac41f69186a7bc7ffabfc 3 | folderAsset: yes 4 | timeCreated: 1491336947 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c185cd119c15c48379f9669722bff56f 3 | folderAsset: yes 4 | timeCreated: 1464960432 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/Editor/HMeshARendererEditor.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEditor; 3 | 4 | [CustomEditor(typeof(HMeshLineRenderer))] 5 | public class HMeshARendererEditor : Editor { 6 | public override void OnInspectorGUI() 7 | { 8 | HMeshARenderer myTarget = (HMeshARenderer)target; 9 | DrawDefaultInspector(); 10 | if (GUILayout.Button("UpdateMesh")){ 11 | myTarget.UpdateMesh(); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/Editor/HMeshARendererEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ad0663f4b63dd434482ead4dd8c34db8 3 | timeCreated: 1464960451 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/Editor/HMeshFilterOperationEditor.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEditor; 3 | 4 | [CustomEditor(typeof(HMeshFilterOperation))] 5 | public class HMeshFilterOperationEditor : Editor { 6 | public override void OnInspectorGUI(){ 7 | HMeshFilterOperation myTarget = (HMeshFilterOperation)target; 8 | var filter = myTarget.GetComponent(); 9 | if (filter == null) 10 | { 11 | Debug.LogWarning("HMeshFilter not found"); 12 | return; 13 | } 14 | DrawDefaultInspector(); 15 | if (GUILayout.Button("CreateTriangle")) 16 | { 17 | CreateTriangle(filter); 18 | } 19 | if (GUILayout.Button("CreateQuad")) 20 | { 21 | CreateQuad(filter); 22 | } 23 | if (GUILayout.Button("CreateNGon")) 24 | { 25 | CreateNGon(filter,myTarget.edges); 26 | } 27 | if (GUILayout.Button("ToMesh")) 28 | { 29 | CreateHMesh(filter, myTarget.meshMaterials); 30 | } 31 | } 32 | 33 | private void CreateHMesh(HMeshFilter filter, Material[] myTargetMeshMaterial) 34 | { 35 | GameObject GO = new GameObject("HMesh_To_Mesh"); 36 | var meshFilter = GO.AddComponent(); 37 | meshFilter.mesh = filter.hMesh.Export(true); 38 | var meshRenderer = GO.AddComponent(); 39 | meshRenderer.materials = myTargetMeshMaterial; 40 | } 41 | 42 | private void CreateTriangle(HMeshFilter filter) 43 | { 44 | filter.SetHMesh(HMesh.CreateTestMeshTriangle(true)); 45 | } 46 | 47 | private void CreateQuad(HMeshFilter filter) 48 | { 49 | filter.SetHMesh(HMesh.CreateTestMeshQuad()); 50 | } 51 | 52 | private void CreateNGon(HMeshFilter filter,int n) 53 | { 54 | filter.SetHMesh(HMesh.CreateTestMeshNGon(n)); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/Editor/HMeshFilterOperationEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6c4cd4a86009748c08c460e6c0d5120e 3 | timeCreated: 1465208537 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/Editor/HMeshRendererEditor.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 98618a0cf96294c2eaa047811fead33a 3 | timeCreated: 1464960451 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/HMeshARenderer.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | /* 4 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 5 | * 6 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 7 | * License: MIT 8 | */ 9 | 10 | public abstract class HMeshARenderer : MonoBehaviour 11 | { 12 | public abstract void UpdateMesh(); 13 | } 14 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/HMeshARenderer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2ab5bfb3752fe47b6a03e5a5c51d468c 3 | timeCreated: 1491376220 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/HMeshFilter.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | /* 4 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 5 | * 6 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 7 | * License: MIT 8 | */ 9 | 10 | 11 | public class HMeshFilter : MonoBehaviour 12 | { 13 | 14 | public HMesh hMesh; 15 | 16 | 17 | public void SetHMesh(HMesh m) 18 | { 19 | hMesh = m; 20 | 21 | foreach (var c in GetComponents()) 22 | { 23 | c.UpdateMesh(); 24 | } 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/HMeshFilter.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 702b7c55e57a143aabde015baca29088 3 | timeCreated: 1491336853 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/HMeshFilterOperation.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | /* 4 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 5 | * 6 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 7 | * License: MIT 8 | */ 9 | 10 | // When added the component can be modified using HMeshFilterOperationEditor 11 | [RequireComponent(typeof(HMeshFilter))] 12 | public class HMeshFilterOperation : MonoBehaviour 13 | { 14 | public Material[] meshMaterials; 15 | 16 | public int edges = 4; 17 | } 18 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/HMeshFilterOperation.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 029a7053380994f8d8d466b7e2c95bc4 3 | timeCreated: 1465203932 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/HMeshLineRenderer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | 4 | /* 5 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 6 | * 7 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 8 | * License: MIT 9 | */ 10 | 11 | [RequireComponent(typeof(LineSegmentRenderer))] 12 | [RequireComponent(typeof(HMeshFilter))] 13 | public class HMeshLineRenderer : HMeshARenderer 14 | { 15 | 16 | public bool showNormals = false; 17 | public float normalLength = 1; 18 | 19 | public override void UpdateMesh() 20 | { 21 | var lr = GetComponent(); 22 | var mf = GetComponent(); 23 | if (lr == null || mf == null) 24 | { 25 | Debug.LogWarning("Was null"); 26 | return; 27 | } 28 | if (mf.hMesh == null) 29 | { 30 | Debug.LogWarning("HMesh was null"); 31 | return; 32 | } 33 | 34 | var pos = new List(); 35 | foreach (var l in mf.hMesh.GetHalfedgesRaw()) 36 | { 37 | pos.Add(l.prev.vert.position); 38 | pos.Add(l.vert.position); 39 | } 40 | 41 | if (showNormals) 42 | { 43 | foreach (var f in mf.hMesh.GetFacesRaw()) 44 | { 45 | var c = f.GetCenter(); 46 | var n = f.GetNormal(); 47 | pos.Add(c.ToVector3()); 48 | pos.Add((c+n*normalLength).ToVector3()); 49 | } 50 | } 51 | 52 | lr.positions = pos; 53 | lr.UpdateMesh(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/UI/HMeshLineRenderer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0dce58d0f3fb64150bd8f26470db986a 3 | timeCreated: 1491336277 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/Vertex.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a088c2957f77c417ead420c82432370d 3 | timeCreated: 1443282416 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/test.json: -------------------------------------------------------------------------------- 1 | { 2 | "face": "23893", 3 | "faces": [{ "id": "23893", "label": "0", "he": "71679" }], 4 | "halfedges": [ 5 | { "id": "71679", "opp": "-1", "next": "71678", "label": "0", "face": "23893", "vert": "12803" }, 6 | { "id": "71678", "opp": "-1", "next": "71679", "label": "0", "face": "23893", "vert": "12804" } 7 | ], 8 | "vertices": [ 9 | { 10 | "id": "12803", 11 | "he": "71678", 12 | "label": "1", 13 | "position": ["-170.819931030273", "36.7980766296387", "183.168167114258"] 14 | }, 15 | { 16 | "id": "12804", 17 | "he": "71679", 18 | "label": "1", 19 | "position": ["-174.299926757813", "36.7980766296387", "183.168167114258"] 20 | } 21 | ] 22 | } -------------------------------------------------------------------------------- /Assets/UnityUtils/HMesh/test.json.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a89d90816f81849a7864296a889aa9ab 3 | timeCreated: 1492418730 4 | licenseType: Pro 5 | TextScriptImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/UnityUtils/KDTree.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2ac2da75ede4c4fd9a6d51839bd88c4f 3 | timeCreated: 1468169584 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: dbae241117eb84893b614af1caa071da 3 | folderAsset: yes 4 | timeCreated: 1466505586 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Bounds2D.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | 10 | public struct BoundsD { 11 | 12 | private Vector3D m_Center; 13 | private Vector3D m_Extents; 14 | 15 | /// 16 | /// The center of the bounding box. 17 | /// 18 | public Vector3D center 19 | { 20 | get 21 | { 22 | return this.m_Center; 23 | } 24 | set 25 | { 26 | this.m_Center = value; 27 | } 28 | } 29 | 30 | /// 31 | /// The total size of the box. This is always twice as large as the extents. 32 | /// 33 | public Vector3D size 34 | { 35 | get 36 | { 37 | return this.m_Extents * 2f; 38 | } 39 | set 40 | { 41 | this.m_Extents = value * 0.5f; 42 | } 43 | } 44 | 45 | /// 46 | /// The extents of the box. This is always half of the size. 47 | /// 48 | public Vector3D extents 49 | { 50 | get 51 | { 52 | return this.m_Extents; 53 | } 54 | set 55 | { 56 | this.m_Extents = value; 57 | } 58 | } 59 | 60 | /// 61 | /// The minimal point of the box. This is always equal to center-extents. 62 | /// 63 | public Vector3D min 64 | { 65 | get 66 | { 67 | return this.center - this.extents; 68 | } 69 | set 70 | { 71 | this.SetMinMax(value, this.max); 72 | } 73 | } 74 | 75 | /// 76 | /// The maximal point of the box. This is always equal to center+extents. 77 | /// 78 | public Vector3D max 79 | { 80 | get 81 | { 82 | return this.center + this.extents; 83 | } 84 | set 85 | { 86 | this.SetMinMax(this.min, value); 87 | } 88 | } 89 | 90 | /// 91 | /// Creates new Bounds with a given center and total size. Bound extents will be half the given size. 92 | /// 93 | /// 94 | /// 95 | public BoundsD(Vector3D center, Vector3D size) 96 | { 97 | this.m_Center = center; 98 | this.m_Extents = size * 0.5; 99 | } 100 | 101 | public static bool operator ==(BoundsD lhs, BoundsD rhs) 102 | { 103 | if (lhs.center == rhs.center) 104 | return lhs.extents == rhs.extents; 105 | return false; 106 | } 107 | 108 | public static bool operator !=(BoundsD lhs, BoundsD rhs) 109 | { 110 | return !(lhs == rhs); 111 | } 112 | 113 | public override int GetHashCode() 114 | { 115 | return this.center.GetHashCode() ^ this.extents.GetHashCode() << 2; 116 | } 117 | 118 | public override bool Equals(object other) 119 | { 120 | if (!(other is Bounds)) 121 | return false; 122 | Bounds bounds = (Bounds) other; 123 | if (this.center.Equals((object) bounds.center)) 124 | return this.extents.Equals((object) bounds.extents); 125 | return false; 126 | } 127 | 128 | /// 129 | /// Sets the bounds to the min and max value of the box. 130 | /// 131 | /// 132 | /// 133 | public void SetMinMax(Vector3D min, Vector3D max) 134 | { 135 | this.extents = (max - min) * 0.5; 136 | this.center = min + this.extents; 137 | } 138 | 139 | /// 140 | /// Grows the Bounds to include the point. 141 | /// 142 | /// 143 | public void Encapsulate(Vector3D point) 144 | { 145 | this.SetMinMax(Vector3D.Min(this.min, point), Vector3D.Max(this.max, point)); 146 | } 147 | 148 | /// 149 | /// Grow the bounds to encapsulate the bounds. 150 | /// 151 | /// 152 | public void Encapsulate(BoundsD bounds) 153 | { 154 | this.Encapsulate(bounds.center - bounds.extents); 155 | this.Encapsulate(bounds.center + bounds.extents); 156 | } 157 | 158 | /// 159 | /// Expand the bounds by increasing its size by amount along each side. 160 | /// 161 | /// 162 | public void Expand(double amount) 163 | { 164 | amount *= 0.5; 165 | this.extents += new Vector3D(amount, amount, amount); 166 | } 167 | 168 | /// 169 | /// Expand the bounds by increasing its size by amount along each side. 170 | /// 171 | /// 172 | public void Expand(Vector3D amount) 173 | { 174 | this.extents += amount * 0.5f; 175 | } 176 | 177 | /// 178 | /// Does another bounding box intersect with this bounding box? 179 | /// 180 | /// 181 | public bool Intersects(BoundsD bounds) 182 | { 183 | if ((double) this.min.x <= (double) bounds.max.x && (double) this.max.x >= (double) bounds.min.x && ((double) this.min.y <= (double) bounds.max.y && (double) this.max.y >= (double) bounds.min.y) && (double) this.min.z <= (double) bounds.max.z) 184 | return (double) this.max.z >= (double) bounds.min.z; 185 | return false; 186 | } 187 | 188 | /// 189 | /// Is point contained in the bounding box? 190 | /// 191 | /// 192 | public bool Contains(Vector3D point) 193 | { 194 | for (int i = 0; i < 3; i++) 195 | { 196 | if (point[i] < this.min[i] || 197 | point[i] > this.max[i]) 198 | { 199 | return false; 200 | } 201 | } 202 | return true; 203 | } 204 | 205 | public override string ToString() 206 | { 207 | return "Bounds {"+m_Center+", "+m_Extents+"} min max {"+this.min+", "+this.max+"}"; 208 | } 209 | 210 | public Bounds ToBounds() 211 | { 212 | return new Bounds(center.ToVector3(), size.ToVector3()); 213 | } 214 | 215 | public void DebugDraw(Color color, float duration = 0) 216 | { 217 | DebugExt.DrawBox(min.ToVector3(),max.ToVector3(), color, duration); 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Bounds2D.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6939da4d56d894ae69bb76dee6bc6010 3 | timeCreated: 1480507706 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 05973ad09afe54a6b9c7909214c06fb9 3 | folderAsset: yes 4 | timeCreated: 1490126651 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Editor/MathTest.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using NUnit.Framework; 4 | using UnityEngine; 5 | 6 | [TestFixture] 7 | public class MathTest { 8 | 9 | [Test] 10 | public void TestIsParallelDist() 11 | { 12 | Vector3D d1 = new Vector3D(0,0,1); 13 | Vector3D d2 = new Vector3D(0,0.002,1); 14 | double treshold = 0.002; 15 | Assert.True(Vector3D.IsParallelDist(d1,d2,treshold*treshold)); 16 | Assert.False(Vector3D.IsParallelDist(d1,d2,treshold*treshold*treshold)); 17 | 18 | d1 = -d1; 19 | Assert.True(Vector3D.IsParallelDist(d1,d2,treshold*treshold)); 20 | Assert.False(Vector3D.IsParallelDist(d1,d2,treshold*treshold*treshold)); 21 | 22 | d1 = new Vector3D(0,0,2); 23 | d2 = new Vector3D(0,0.002,2); 24 | Assert.True(Vector3D.IsParallelDist(d1,d2,treshold*treshold)); 25 | Assert.False(Vector3D.IsParallelDist(d1,d2,treshold*treshold*treshold)); 26 | 27 | d1 = new Vector3D(0,0,2); 28 | d2 = new Vector3D(0,0,2); 29 | Assert.True(Vector3D.IsParallelDist(d1,d2,treshold*treshold)); 30 | 31 | d1 = new Vector3D(0,0,2); 32 | d2 = new Vector3D(0,2,0); 33 | Assert.False(Vector3D.IsParallelDist(d1,d2,treshold*treshold)); 34 | } 35 | 36 | [Test] 37 | public void TestProjectPointLine() 38 | { 39 | LineSegment lineSegment = new LineSegment(new Vector3D(2, 2, 2), new Vector3D(4, 2, 2)); 40 | var pos = lineSegment.ProjectPoint(new Vector3D(3, 4, 2)); 41 | Assert.True(Mathf.Approximately(0, (float)Vector3D.Distance(pos, new Vector3D(3, 2, 2)))); 42 | 43 | pos = lineSegment.ProjectPoint(new Vector3D(2, 4, 2)); 44 | Assert.True(Mathf.Approximately(0, (float)Vector3D.Distance(pos, new Vector3D(2, 2, 2)))); 45 | 46 | pos = lineSegment.ProjectPoint(new Vector3D(0, 4, 2)); 47 | Assert.True(Mathf.Approximately(0, (float)Vector3D.Distance(pos, new Vector3D(2, 2, 2)))); 48 | 49 | pos = lineSegment.ProjectPoint(new Vector3D(4, 4, 2)); 50 | Assert.True(Mathf.Approximately(0, (float)Vector3D.Distance(pos, new Vector3D(4, 2, 2)))); 51 | 52 | pos = lineSegment.ProjectPoint(new Vector3D(6, 4, 2)); 53 | Assert.True(Mathf.Approximately(0, (float)Vector3D.Distance(pos, new Vector3D(4, 2, 2)))); 54 | 55 | } 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Editor/MathTest.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 08700893d43c049d5b3457de36284ce4 3 | timeCreated: 1496217537 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Editor/Plane3DTest.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4ba5aca9f16214ea99379e8e316f88fa 3 | timeCreated: 1490126658 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/LineSegment.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | //using MB.Algodat; 10 | 11 | public class LineSegment { 12 | public Vector3D first{ 13 | get{ 14 | return array [0]; 15 | } 16 | } 17 | public Vector3D second{ 18 | get { 19 | return array[1]; 20 | } 21 | } 22 | Vector3D[] array = new Vector3D[2]; 23 | public bool flag = false; 24 | 25 | public LineSegment(Vector3D first_, Vector3D second_){ 26 | array[0] = first_; 27 | array[1] = second_; 28 | //Range = new Range(System.Math.Min(first_.x, second_.x), System.Math.Max(first_.x, second_.x)); 29 | } 30 | 31 | public double Length(){ 32 | return (first - second).magnitude; 33 | } 34 | 35 | public bool IsSameDirection(LineSegment other, double thresholdSqr = 0.002*0.002){ 36 | Vector3D dirThis = (second - first); 37 | Vector3D dirOther = (other.second - other.first); 38 | 39 | return Vector3D.IsParallel(dirThis, dirOther) 40 | && Vector3D.Dot (dirThis, dirOther) >= 0.99f; // this number just have to be close to one since we are already testing for parallelism 41 | } 42 | 43 | /** 44 | * Return distance from first point 45 | */ 46 | public bool Intersects(Vector3D point, out double distance) 47 | { 48 | Vector3D dirThis = (second - first).normalized; 49 | Vector3D dirOther = (point - first); 50 | 51 | if (dirOther.sqrMagnitude < 1E-10) 52 | { 53 | distance = 0; 54 | return true; 55 | } 56 | 57 | distance = Vector3D.Dot(dirThis, dirOther); 58 | return Vector3D.IsParallel((second - first), dirOther); 59 | } 60 | 61 | // http://www.realtimerendering.com/intersections.html (ay/ray: (after Goldman, Graphics Gems) 62 | public bool FindIntersections(LineSegment other, out Vector3D pos1, out Vector3D pos2, out bool parallel) 63 | { 64 | // L1(t1) = o1 + d1*t1 65 | // L2(t2) = o2 + d2*t2 66 | // The solution is: 67 | // t1 = Determinant{(o2-o1),d2,d1 X d2} / ||d1 X d2||^2 68 | // and 69 | // t2 = Determinant{(o2-o1),d1,d1 X d2} / ||d1 X d2||^2 70 | // 71 | // If the lines are parallel, the denominator ||d1 X d2||^2 is 0. 72 | Vector3D o1 = first; 73 | Vector3D o2 = other.first; 74 | Vector3D d1 = (second - first).normalized; 75 | Vector3D d2 = (other.second - other.first).normalized; 76 | double denominator = Vector3D.Cross(d1, d2).sqrMagnitude; 77 | if (Mathf.Approximately((float) denominator, 0)) 78 | { 79 | parallel = true; 80 | pos1 = Vector3D.zero; 81 | pos2 = Vector3D.zero; 82 | return false; 83 | } 84 | var mat1 = new Matrix3x3D(o2 - o1, d2, Vector3D.Cross(d1, d2)); 85 | var mat2 = new Matrix3x3D(o2 - o1, d1, Vector3D.Cross(d1, d2)); 86 | double det1 = mat1.determinant; 87 | double det2 = mat2.determinant; 88 | double t1 = det1 / denominator; 89 | double t2 = det2 / denominator; 90 | pos1 = o1 + d1 * t1; 91 | pos2 = o2 + d2 * t2; 92 | parallel = false; 93 | return Mathf.Approximately((float)pos1.x, (float)pos2.x) && 94 | Mathf.Approximately((float)pos1.y, (float)pos2.y) && 95 | Mathf.Approximately((float)pos1.z, (float)pos2.z) && 96 | t1 >=0 && 97 | t2 >=0 && 98 | t1*t1 <= (second - first).sqrMagnitude && 99 | t2*t2 <= (other.second - other.first).sqrMagnitude; 100 | 101 | } 102 | 103 | /*public Range Range 104 | { 105 | get; 106 | set; 107 | }*/ 108 | 109 | Vector3D ProjectPointLine(Vector3D point, Vector3D lineStart, Vector3D lineEnd) 110 | { 111 | Vector3D rhs = point - lineStart; 112 | Vector3D vector3 = lineEnd - lineStart; 113 | double magnitude2 = vector3.sqrMagnitude; 114 | Vector3D lhs = vector3; 115 | if ( magnitude2 < 9.99999997475243E-07) 116 | { 117 | return lineStart; // line has zero magnitude - just return start position 118 | } 119 | 120 | double dotProduct = Vector3D.Dot(lhs, rhs); 121 | double num = System.Math.Min(System.Math.Max(dotProduct, 0.0), magnitude2); 122 | return lineStart + lhs * num/magnitude2; 123 | } 124 | 125 | double DistancePointLine(Vector3D point, Vector3D lineStart, Vector3D lineEnd) 126 | { 127 | return Vector3D.Magnitude(ProjectPointLine(point, lineStart, lineEnd) - point); 128 | } 129 | 130 | public Vector3D ProjectPoint(Vector3D point) 131 | { 132 | return ProjectPointLine(point, first, second); 133 | } 134 | 135 | public double DistancePoint(Vector3D point) 136 | { 137 | return DistancePointLine(point, first, second); 138 | } 139 | 140 | public LineSegment2D To2D(int skip = 1){ 141 | if (skip == 1) { 142 | return new LineSegment2D (new Vector2D (first.x, first.z), new Vector2D (second.x, second.z)); 143 | } 144 | Debug.LogError("Not supported");; 145 | return null; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/LineSegment.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5814e100b7c394ead9a1393e173a873b 3 | timeCreated: 1443508260 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/LineSegment2D.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | 10 | // http://martin-thoma.com/how-to-check-if-two-line-segments-intersect/ 11 | public class LineSegment2D { 12 | public Vector2D first{ 13 | get{ 14 | return array [0]; 15 | } 16 | } 17 | public Vector2D second{ 18 | get { 19 | return array[1]; 20 | } 21 | } 22 | Vector2D[] array = new Vector2D[2]; 23 | 24 | public LineSegment2D(Vector2D first_, Vector2D second_){ 25 | array[0] = first_; 26 | array[1] = second_; 27 | } 28 | 29 | public double Length(){ 30 | return (first - second).magnitude; 31 | } 32 | 33 | public Vector2D Direction(){ 34 | var dir = (second-first); 35 | var magnitude = dir.magnitude; 36 | if (magnitude>0){ 37 | dir = dir * (1.0f/magnitude); 38 | } 39 | return dir; 40 | } 41 | 42 | public static bool doesIntersect(double l1x1, double l1y1, double l1x2, double l1y2, double l2x1, double l2y1, double l2x2, 43 | double l2y2) { 44 | double denom = ((l2y2 - l2y1) * (l1x2 - l1x1)) - ((l2x2 - l2x1) * (l1y2 - l1y1)); 45 | 46 | if (denom == 0.0f) { 47 | return false; 48 | } 49 | 50 | double ua = (((l2x2 - l2x1) * (l1y1 - l2y1)) - ((l2y2 - l2y1) * (l1x1 - l2x1))) / denom; 51 | double ub = (((l1x2 - l1x1) * (l1y1 - l2y1)) - ((l1y2 - l1y1) * (l1x1 - l2x1))) / denom; 52 | 53 | return ((ua >= 0.0f) && (ua <= 1.0f) && (ub >= 0.0f) && (ub <= 1.0f)); 54 | } 55 | 56 | public static bool doesIntersect(LineSegment2D ln1, LineSegment2D ln2) { 57 | double denom = ((ln2.second.y - ln2.first.y) * (ln1.second.x - ln1.first.x)) 58 | - ((ln2.second.x - ln2.first.x) * (ln1.second.y - ln1.first.y)); 59 | 60 | if (denom == 0.0) { 61 | return false; 62 | } 63 | 64 | double ua = ((ln2.second.x - ln2.first.x) * (ln1.first.y - ln2.first.y)) 65 | - ((ln2.second.y - ln2.first.y) * (ln1.first.x - ln2.first.x)) / denom; 66 | double ub = ((ln1.second.x - ln1.first.x) * (ln1.first.y - ln2.first.y)) 67 | - ((ln1.second.y - ln1.first.y) * (ln1.first.x - ln2.first.x)) / denom; 68 | 69 | return ((ua >= 0.0f) && (ua <= 1.0f) && (ub >= 0.0f) && (ub <= 1.0f)); 70 | } 71 | 72 | public static bool getIntersection(LineSegment2D line, double x1, double y1, double x2, double y2, out Vector2D res) { 73 | res = Vector2D.zero; 74 | double denom = ((line.second.y - line.first.y) * (x2 - x1)) 75 | - ((line.second.x - line.first.x) * (y2 - y1)); 76 | double nume_a = ((line.second.x - line.first.x) * (y1 - line.first.y)) 77 | - ((line.second.y - line.first.y) * (x1 - line.first.x)); 78 | double nume_b = ((x2 - x1) * (y1 - line.first.y)) - ((y2 - y1) * (x1 - line.first.x)); 79 | 80 | if (denom == 0.0f) { 81 | return false; 82 | } 83 | 84 | double ua = nume_a / denom; 85 | double ub = nume_b / denom; 86 | 87 | if ((ua >= 0.0f) && (ua <= 1.0f) && (ub >= 0.0f) && (ub <= 1.0f)) { 88 | 89 | // Get the intersection point. 90 | double intersectX = (line.first.x + ua * (line.second.x - line.first.x)); 91 | double intersectY = (line.first.y + ua * (line.second.y - line.first.y)); 92 | 93 | res = new Vector2D(intersectX, intersectY); 94 | return true; 95 | } 96 | 97 | return false; 98 | } 99 | 100 | public bool RayIntersect(Ray2DD l2, out Vector2D res) { 101 | LineSegment2D l1 = this; 102 | bool b = RayIntersect(l1.first.x, l1.first.y, l1.second.x, l1.second.y, 103 | l2.origin.x, l2.origin.y, l2.origin.x + l2.direction.x*1000000, l2.origin.y + l2.direction.y*1000000, out res); 104 | if (b){ 105 | return (first - res).magnitude + (second - res).magnitude <= (first - second).magnitude + 0.0001f;; 106 | } 107 | return false; 108 | } 109 | 110 | public bool LineIntersect(LineSegment2D l2, out Vector2D res) { 111 | LineSegment2D l1 = this; 112 | return LineIntersect(l1.first.x, l1.first.y, l1.second.x, l1.second.y, 113 | l2.first.x, l2.first.y, l2.second.x, l2.second.y, out res); 114 | } 115 | 116 | static bool RayIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, out Vector2D res) { 117 | res = Vector2D.zero; 118 | double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1); 119 | if (denom == 0.0) { // Lines are parallel. 120 | return false; 121 | } 122 | double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denom; 123 | double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denom; 124 | 125 | // Get the intersection point. 126 | res = new Vector2D ((x1 + ua * (x2 - x1)), (y1 + ua * (y2 - y1))); 127 | return true; 128 | } 129 | 130 | public static bool LineIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, out Vector2D res) { 131 | res = Vector2D.zero; 132 | double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1); 133 | if (System.Math.Abs(denom) < Mathf.Epsilon) { // Lines are approx parallel. 134 | return false; 135 | } 136 | double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denom; 137 | double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denom; 138 | if (ua >= 0.0 && ua <= 1.0 && ub >= 0.0 && ub <= 1.0) { 139 | // Get the intersection point. 140 | res = new Vector2D((x1 + ua * (x2 - x1)), (y1 + ua * (y2 - y1))); 141 | return true; 142 | } 143 | 144 | return false; 145 | } 146 | 147 | public double MinimumDistanceSqr(Vector2D point) 148 | { 149 | double t; 150 | return MinimumDistanceSqr(point, out t); 151 | } 152 | 153 | public double MinimumDistanceSqr(Vector2D point, out double t) 154 | { 155 | t = 0; 156 | // Return minimum distance between line segment vw and point p 157 | double l2 = (first - second).sqrMagnitude; // i.e. |w-v|^2 - avoid a sqrt 158 | if (l2 == 0.0) return (point - first).magnitude; // v == w case 159 | // Consider the line extending the segment, parameterized as v + t (w - v). 160 | // We find projection of point p onto the line. 161 | // It falls where t = [(p-v) . (w-v)] / |w-v|^2 162 | t = Vector2D.Dot(point- first, second - first) / l2; 163 | if (t < 0.0) return (point- first).magnitude; // Beyond the 'v' end of the segment 164 | else if (t > 1.0) return (point- second).magnitude; // Beyond the 'w' end of the segment 165 | var projection = first + t * (second - first); // Projection falls on the segment 166 | return (point- projection).sqrMagnitude; 167 | } 168 | 169 | public double MinimumDistance(Vector2D point) 170 | { 171 | double t; 172 | return System.Math.Sqrt(MinimumDistanceSqr(point, out t)); 173 | } 174 | 175 | public double MinimumDistance(Vector2D point, out double t) 176 | { 177 | return System.Math.Sqrt(MinimumDistanceSqr(point, out t)); 178 | } 179 | 180 | public override string ToString() 181 | { 182 | return "{"+first+"} - {"+second+"}"; 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/LineSegment2D.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ed5d4f998ed164cd18d2dd826a12f721 3 | timeCreated: 1443508199 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Matrix3x3.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | 10 | public struct Matrix3x3 { 11 | // Matrices in unity are column major. Data is accessed as: row + (column*3). 12 | public float[] data; 13 | 14 | public Matrix3x3(float[] data){ 15 | this.data = new float[9]; 16 | for (int i=0;i<9;i++){ 17 | this.data[i] = data[i]; 18 | } 19 | } 20 | // 21 | // Static Properties 22 | // 23 | public static Matrix3x3 identity { 24 | get{ 25 | return new Matrix3x3(new float[]{ 26 | 1,0,0, 27 | 0,1,0, 28 | 0,0,1 29 | }); 30 | } 31 | } 32 | 33 | public static Matrix3x3 zero { 34 | get{ 35 | return new Matrix3x3(new float[]{ 36 | 0,0,0, 37 | 0,0,0, 38 | 0,0,0 39 | }); 40 | } 41 | } 42 | 43 | // 44 | // Properties 45 | // 46 | public float determinant { 47 | get{ 48 | var a = this; 49 | return a[0,0] * (a[2,2] * a[1,1] - a[1,2] * a[2,1]) 50 | + a[0,1] * (a[1,2] * a[2,0] - a[2,2] * a[1,0]) 51 | + a[0,2] * (a[2,1] * a[1,0] - a[1,1] * a[2,0]); 52 | } 53 | } 54 | 55 | public bool isIdentity { 56 | get{ 57 | return this == identity; 58 | } 59 | } 60 | 61 | public Matrix3x3 transpose { 62 | get { 63 | Matrix3x3 n = Matrix3x3.zero; 64 | for (int i=0;i<3;i++){ 65 | for (int j=0;j<3;j++){ 66 | n[j,i] = this[i,j]; 67 | } 68 | } 69 | return n; 70 | } 71 | } 72 | 73 | public float this [int row, int column] { 74 | get{ 75 | return data[row + (column*3)]; 76 | } 77 | set{ 78 | data[row + (column*3)] = value; 79 | } 80 | } 81 | 82 | 83 | // 84 | // Methods 85 | // 86 | public override bool Equals (object other){ 87 | if (!GetType().IsInstanceOfType(other)){ 88 | return false; 89 | } 90 | var mat = (Matrix3x3)other; 91 | return mat == this; 92 | } 93 | 94 | public void SetColumn (int column, Vector3 v){ 95 | for (int r=0;r<3;r++){ 96 | this[r,column] = v[r]; 97 | } 98 | } 99 | 100 | public void SetRow (int r, Vector3 v){ 101 | for (int c=0;c<3;c++){ 102 | this[r,c] = v[c]; 103 | } 104 | } 105 | 106 | 107 | // 108 | // Operators 109 | // 110 | public static bool operator == (Matrix3x3 lhs, Matrix3x3 rhs){ 111 | for (int i=0;i<9;i++){ 112 | if (lhs.data[i] != rhs.data[i]){ 113 | return false; 114 | } 115 | } 116 | return true; 117 | } 118 | 119 | public static bool operator != (Matrix3x3 lhs, Matrix3x3 rhs){ 120 | return !(lhs == rhs); 121 | } 122 | 123 | public override string ToString() 124 | { 125 | return string.Format("{0:0.00} {0:0.00} {0:0.00}\n{0:0.00} {0:0.00} {0:0.00}\n{0:0.00} {0:0.00} {0:0.00}", 126 | this[0, 0], this[0, 1], this[0, 2], 127 | this[1, 0], this[1, 1], this[1, 2], 128 | this[2, 0], this[2, 1], this[2, 2]); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Matrix3x3.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9b18219ce845d47aab2f6fba9d97adbd 3 | timeCreated: 1469714712 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Matrix3x3D.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using System; 9 | 10 | public struct Matrix3x3D { 11 | // Matrices in unity are column major. Data is accessed as: row + (column*3). 12 | public double[] data; 13 | 14 | public Matrix3x3D(double[] data){ 15 | this.data = new double[9]; 16 | for (int i=0;i<9;i++){ 17 | this.data[i] = data[i]; 18 | } 19 | } 20 | 21 | public Matrix3x3D(Vector3D col1, Vector3D col2, Vector3D col3) 22 | { 23 | this.data = new double[9]; 24 | SetColumn(0,col1); 25 | SetColumn(1,col2); 26 | SetColumn(2,col3); 27 | } 28 | 29 | // 30 | // Static Properties 31 | // 32 | public static Matrix3x3D identity { 33 | get{ 34 | return new Matrix3x3D(new double[]{ 35 | 1,0,0, 36 | 0,1,0, 37 | 0,0,1 38 | }); 39 | } 40 | } 41 | 42 | public static Matrix3x3D zero { 43 | get{ 44 | return new Matrix3x3D(new double[]{ 45 | 0,0,0, 46 | 0,0,0, 47 | 0,0,0 48 | }); 49 | } 50 | } 51 | 52 | // 53 | // Properties 54 | // 55 | public double determinant { 56 | get{ 57 | var a = this; 58 | return a[0,0] * (a[2,2] * a[1,1] - a[1,2] * a[2,1]) 59 | + a[0,1] * (a[1,2] * a[2,0] - a[2,2] * a[1,0]) 60 | + a[0,2] * (a[2,1] * a[1,0] - a[1,1] * a[2,0]); 61 | } 62 | } 63 | 64 | /*public Matrix4x4 inverse { 65 | get; 66 | }*/ 67 | 68 | public bool isIdentity { 69 | get{ 70 | return this == identity; 71 | } 72 | } 73 | 74 | public Matrix3x3D transpose { 75 | get { 76 | Matrix3x3D n = Matrix3x3D.zero; 77 | for (int i=0;i<3;i++){ 78 | for (int j=0;j<3;j++){ 79 | n[j,i] = this[i,j]; 80 | } 81 | } 82 | return n; 83 | } 84 | } 85 | 86 | // 87 | // Indexer 88 | // 89 | /*public float this [int index] { 90 | get; 91 | set; 92 | }*/ 93 | 94 | public double this [int row, int column] { 95 | get{ 96 | return data[row + (column*3)]; 97 | } 98 | set{ 99 | data[row + (column*3)] = value; 100 | } 101 | } 102 | 103 | 104 | // 105 | // Methods 106 | // 107 | public override bool Equals (object other){ 108 | if (!GetType().IsInstanceOfType(other)){ 109 | return false; 110 | } 111 | var mat = (Matrix3x3D)other; 112 | return mat == this; 113 | } 114 | 115 | public void SetColumn (int column, Vector3D v){ 116 | for (int r=0;r<3;r++){ 117 | this[r,column] = v[r]; 118 | } 119 | } 120 | 121 | public void SetRow (int r, Vector3D v){ 122 | for (int c=0;c<3;c++){ 123 | this[r,c] = v[c]; 124 | } 125 | } 126 | 127 | public Vector3D MultiplyPoint(Vector3D v) 128 | { 129 | Vector3D res = new Vector3D(); 130 | for (int r = 0; r < 3; r++) 131 | { 132 | double dotSum = 0; 133 | for (int c = 0; c < 3; c++) 134 | { 135 | dotSum += this[r, c] * v[c]; 136 | } 137 | dotSum += this[r, 3]; 138 | res[r] = dotSum; 139 | } 140 | return res; 141 | } 142 | 143 | public Vector3D MultiplyVector(Vector3D v) 144 | { 145 | Vector3D res = new Vector3D(); 146 | for (int r = 0; r < 3; r++) 147 | { 148 | double dotSum = 0; 149 | for (int c = 0; c < 3; c++) 150 | { 151 | dotSum += this[r, c] * v[c]; 152 | } 153 | res[r] = dotSum; 154 | } 155 | return res; 156 | } 157 | 158 | /*public Vector4 GetColumn (int i); 159 | 160 | public override int GetHashCode (); 161 | 162 | public Vector4 GetRow (int i); 163 | 164 | 165 | 166 | public Vector3 MultiplyPoint3x4 (Vector3 v); 167 | 168 | public Vector3 MultiplyVector (Vector3 v); 169 | 170 | public void SetColumn (int i, Vector4 v); 171 | 172 | public void SetRow (int i, Vector4 v); 173 | 174 | public void SetTRS (Vector3 pos, Quaternion q, Vector3 s); 175 | 176 | public override string ToString (); 177 | 178 | public string ToString (string format); 179 | */ 180 | // 181 | // Operators 182 | // 183 | public static bool operator == (Matrix3x3D lhs, Matrix3x3D rhs){ 184 | for (int i=0;i<9;i++){ 185 | if (lhs.data[i] != rhs.data[i]){ 186 | return false; 187 | } 188 | } 189 | return true; 190 | } 191 | 192 | public static bool operator != (Matrix3x3D lhs, Matrix3x3D rhs){ 193 | return !(lhs == rhs); 194 | } 195 | 196 | //public static Matrix4x4 operator * (Matrix4x4 lhs, Matrix4x4 rhs); 197 | 198 | //public static Vector4 operator * (Matrix4x4 lhs, Vector4 v); 199 | 200 | public override string ToString() 201 | { 202 | return String.Format("{0:0.00} {1:0.00} {2:0.00}\n{3:0.00} {4:0.00} {5:0.00}\n{6:0.00} {7:0.00} {8:0.00}", 203 | this[0, 0], this[0, 1], this[0, 2], 204 | this[1, 0], this[1, 1], this[1, 2], 205 | this[2, 0], this[2, 1], this[2, 2]); 206 | } 207 | } 208 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Matrix3x3D.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 48dd853d66261487780d14a6ae5079ff 3 | timeCreated: 1469714712 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Matrix4x4D.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using System; 9 | 10 | public struct Matrix4x4D { 11 | // Matrices in unity are column major. Data is accessed as: row + (column*3). 12 | public double[] data; 13 | 14 | public Matrix4x4D(double[] data){ 15 | this.data = new double[16]; 16 | for (int i=0;i<16;i++){ 17 | this.data[i] = data[i]; 18 | } 19 | } 20 | 21 | public Matrix4x4D(Vector4D col1, Vector4D col2, Vector4D col3, Vector4D col4) 22 | { 23 | this.data = new double[16]; 24 | SetColumn(0,col1); 25 | SetColumn(1,col2); 26 | SetColumn(2,col3); 27 | SetColumn(4,col4); 28 | } 29 | 30 | // 31 | // Static Properties 32 | // 33 | public static Matrix4x4D identity { 34 | get{ 35 | return new Matrix4x4D(new double[]{ 36 | 1,0,0,0, 37 | 0,1,0,0, 38 | 0,0,1,0, 39 | 0,0,0,1 40 | }); 41 | } 42 | } 43 | 44 | public static Matrix4x4D zero { 45 | get{ 46 | return new Matrix4x4D(new double[]{ 47 | 0,0,0,0, 48 | 0,0,0,0, 49 | 0,0,0,0, 50 | 0,0,0,0 51 | }); 52 | } 53 | } 54 | 55 | // 56 | // Properties 57 | // 58 | public double determinant { 59 | get 60 | { 61 | var m = this; 62 | var SubFactor00 = m[2,2] * m[3,3] - m[3,2] * m[2,3]; 63 | var SubFactor01 = m[2,1] * m[3,3] - m[3,1] * m[2,3]; 64 | var SubFactor02 = m[2,1] * m[3,2] - m[3,1] * m[2,2]; 65 | var SubFactor03 = m[2,0] * m[3,3] - m[3,0] * m[2,3]; 66 | var SubFactor04 = m[2,0] * m[3,2] - m[3,0] * m[2,2]; 67 | var SubFactor05 = m[2,0] * m[3,1] - m[3,0] * m[2,1]; 68 | 69 | var DetCof = new Vector4D( 70 | + (m[1,1] * SubFactor00 - m[1,2] * SubFactor01 + m[1,3] * SubFactor02), 71 | - (m[1,0] * SubFactor00 - m[1,2] * SubFactor03 + m[1,3] * SubFactor04), 72 | + (m[1,0] * SubFactor01 - m[1,1] * SubFactor03 + m[1,3] * SubFactor05), 73 | - (m[1,0] * SubFactor02 - m[1,1] * SubFactor04 + m[1,2] * SubFactor05)); 74 | 75 | return 76 | m[0,0] * DetCof[0] + m[0,1] * DetCof[1] + 77 | m[0,2] * DetCof[2] + m[0,3] * DetCof[3]; 78 | } 79 | } 80 | 81 | /*public Matrix4x4 inverse { 82 | get; 83 | }*/ 84 | 85 | public bool isIdentity { 86 | get{ 87 | return this == identity; 88 | } 89 | } 90 | 91 | public Matrix4x4D transpose { 92 | get { 93 | Matrix4x4D n = Matrix4x4D.zero; 94 | for (int i=0;i<4;i++){ 95 | for (int j=0;j<4;j++){ 96 | n[j,i] = this[i,j]; 97 | } 98 | } 99 | return n; 100 | } 101 | } 102 | 103 | // 104 | // Indexer 105 | // 106 | /*public float this [int index] { 107 | get; 108 | set; 109 | }*/ 110 | 111 | public double this [int row, int column] { 112 | get{ 113 | return data[row + (column*4)]; 114 | } 115 | set{ 116 | data[row + (column*4)] = value; 117 | } 118 | } 119 | 120 | 121 | // 122 | // Methods 123 | // 124 | public override bool Equals (object other){ 125 | if (!GetType().IsInstanceOfType(other)){ 126 | return false; 127 | } 128 | var mat = (Matrix4x4D)other; 129 | return mat == this; 130 | } 131 | 132 | 133 | public void SetColumn (int column, Vector4D v){ 134 | for (int r=0;r<4;r++){ 135 | this[r,column] = v[r]; 136 | } 137 | } 138 | 139 | public void SetRow (int r, Vector4D v){ 140 | for (int c=0;c<4;c++){ 141 | this[r,c] = v[c]; 142 | } 143 | } 144 | 145 | /*public Vector4 GetColumn (int i); 146 | 147 | public override int GetHashCode (); 148 | 149 | public Vector4 GetRow (int i); 150 | 151 | public Vector3 MultiplyPoint (Vector3 v); 152 | 153 | public Vector3 MultiplyPoint3x4 (Vector3 v); 154 | 155 | public Vector3 MultiplyVector (Vector3 v); 156 | 157 | public void SetColumn (int i, Vector4 v); 158 | 159 | public void SetRow (int i, Vector4 v); 160 | 161 | public void SetTRS (Vector3 pos, Quaternion q, Vector3 s); 162 | 163 | public override string ToString (); 164 | 165 | public string ToString (string format); 166 | */ 167 | // 168 | // Operators 169 | // 170 | public static bool operator == (Matrix4x4D lhs, Matrix4x4D rhs){ 171 | for (int i=0;i<16;i++){ 172 | if (lhs.data[i] != rhs.data[i]){ 173 | return false; 174 | } 175 | } 176 | return true; 177 | } 178 | 179 | public static bool operator != (Matrix4x4D lhs, Matrix4x4D rhs){ 180 | return !(lhs == rhs); 181 | } 182 | 183 | //public static Matrix4x4 operator * (Matrix4x4 lhs, Matrix4x4 rhs); 184 | 185 | //public static Vector4 operator * (Matrix4x4 lhs, Vector4 v); 186 | 187 | public override string ToString() 188 | { 189 | return String.Format("{0:0.00} {1:0.00} {2:0.00} {3:0.00}\n{4:0.00} {5:0.00} {6:0.00} {7:0.00}\n{8:0.00} {9:0.00} {10:0.00} {11:0.00}\n{12:0.00} {13:0.00} {14:0.00} {15:0.00}", 190 | this[0, 0], this[0, 1], this[0, 2],this[0, 3], 191 | this[1, 0], this[1, 1], this[1, 2],this[1, 3], 192 | this[2, 0], this[2, 1], this[2, 2],this[2, 3], 193 | this[3, 0], this[3, 1], this[3, 2],this[3, 3] 194 | 195 | ); 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Matrix4x4D.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a91bd80f6a9d045c5a8c989d3621209c 3 | timeCreated: 1469714712 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Plane3D.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | 10 | public struct Plane3D 11 | { 12 | double m_Distance; 13 | Vector3D m_Normal; 14 | // 15 | // Properties 16 | // 17 | public double distance 18 | { 19 | get 20 | { 21 | return this.m_Distance; 22 | } 23 | set 24 | { 25 | this.m_Distance = value; 26 | } 27 | } 28 | 29 | public Vector3D normal 30 | { 31 | get 32 | { 33 | return this.m_Normal; 34 | } 35 | set 36 | { 37 | this.m_Normal = value; 38 | } 39 | } 40 | 41 | // 42 | // Constructors 43 | // 44 | 45 | public Plane3D (Vector3D inNormal, double d) 46 | { 47 | this.m_Normal = Vector3D.Normalize (inNormal); 48 | this.m_Distance = d; 49 | } 50 | 51 | public Plane3D (Vector3D inNormal, Vector3D inPoint) 52 | { 53 | this.m_Normal = Vector3D.Normalize (inNormal); 54 | this.m_Distance = -Vector3D.Dot (m_Normal, inPoint); 55 | } 56 | 57 | // 58 | // Methods 59 | // 60 | public double GetDistanceToPoint (Vector3D inPt) 61 | { 62 | return Vector3D.Dot (this.normal, inPt) + distance; 63 | } 64 | 65 | public bool GetSide (Vector3D inPt) 66 | { 67 | return GetDistanceToPoint( inPt) > 0f; 68 | } 69 | 70 | public bool Raycast (RayD ray, out double enter) 71 | { 72 | double num = Vector3D.Dot (ray.direction, normal); 73 | double num2 = -Vector3D.Dot (ray.origin, normal) - distance; 74 | if (Mathf.Approximately ((float)num, 0f)) 75 | { 76 | enter = 0f; 77 | return false; 78 | } 79 | enter = num2 / num; 80 | return enter > 0f; 81 | } 82 | 83 | public bool SameSide (Vector3D inPt0, Vector3D inPt1) 84 | { 85 | double distanceToPoint = this.GetDistanceToPoint (inPt0); 86 | double distanceToPoint2 = this.GetDistanceToPoint (inPt1); 87 | return (distanceToPoint > 0f && distanceToPoint2 > 0f) || (distanceToPoint <= 0f && distanceToPoint2 <= 0f); 88 | } 89 | 90 | public void Set3Points (Vector3D a, Vector3D b, Vector3D c) 91 | { 92 | this.normal = Vector3D.Normalize (Vector3D.Cross (b - a, c - a)); 93 | this.distance = -Vector3D.Dot (this.normal, a); 94 | } 95 | 96 | public void SetNormalAndPosition (Vector3D inNormal, Vector3D inPoint) 97 | { 98 | this.normal = Vector3D.Normalize (inNormal); 99 | this.distance = -Vector3D.Dot (inNormal, inPoint); 100 | } 101 | 102 | public Vector3D ProjectAlongYAxis(double x, double z) 103 | { 104 | double y = -(+m_Distance + x * m_Normal.x + z * m_Normal.z) / m_Normal.y; 105 | return new Vector3D(x,y,z); 106 | } 107 | 108 | public void DebugDraw() 109 | { 110 | DebugExt.DrawCross((normal*distance).ToVector3(),Color.green,1,10); 111 | } 112 | 113 | public override string ToString() 114 | { 115 | return "Plane normal: " + normal.ToString("R") + " dist " + distance.ToString("R"); 116 | } 117 | 118 | // return NaN if line is parallel with plane 119 | public Vector3D LineIntersection(Vector3D lineFrom, Vector3D lineTo) 120 | { 121 | var ray = new RayD(lineFrom, (lineTo - lineFrom).normalized); 122 | double enter; 123 | double num = Vector3D.Dot (ray.direction, normal); 124 | double num2 = -Vector3D.Dot (ray.origin, normal) - distance; 125 | if (num == 0) 126 | { 127 | return Vector3D.one * double.NaN; 128 | } 129 | enter = num2 / num; 130 | return ray.GetPointAt(enter); 131 | } 132 | 133 | 134 | } -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Plane3D.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b0ef85632319c4815937d7d36b8c6621 3 | timeCreated: 1430391966 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Polygon3D.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2134b9f919e549a3835ac63981b080c6 3 | timeCreated: 1490772239 -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Ray2DD.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | public struct Ray2DD{ 9 | 10 | // 11 | // Properties 12 | // 13 | public Vector2D direction { 14 | get; 15 | set; 16 | } 17 | 18 | public Vector2D origin { 19 | get; 20 | set; 21 | } 22 | 23 | // 24 | // Constructors 25 | // 26 | public Ray2DD (Vector2D origin, Vector2D direction){ 27 | this.origin = origin; 28 | this.direction = direction; 29 | } 30 | 31 | // 32 | // Methods 33 | // 34 | public Vector2D GetPoint (double distance){ 35 | return origin + direction*distance; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Ray2DD.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 66fe1313b4e0b48c39ddd1d1735fe310 3 | timeCreated: 1515005968 4 | licenseType: Pro 5 | MonoImporter: 6 | externalObjects: {} 7 | serializedVersion: 2 8 | defaultReferences: [] 9 | executionOrder: 0 10 | icon: {instanceID: 0} 11 | userData: 12 | assetBundleName: 13 | assetBundleVariant: 14 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/RayD.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | 10 | public class RayD { 11 | public Vector3D origin; 12 | public Vector3D direction; 13 | 14 | public RayD(Vector3D origin, Vector3D direction){ 15 | this.origin = origin; 16 | this.direction = direction; 17 | } 18 | 19 | 20 | public Vector3D GetPointAt(double d) 21 | { 22 | return origin + direction * d; 23 | } 24 | 25 | // return the distance to the line 26 | public double LinePointDistance(Vector3D p) 27 | { 28 | if (Vector3D.Distance(p, origin) < Mathf.Epsilon) 29 | { 30 | return 0.0; 31 | } 32 | // Notation from https://math.stackexchange.com/a/1300565 33 | var p1 = origin; 34 | var p2 = origin + direction; 35 | var resVector = (p - p1) - (Vector3D.Dot(p - p1, p2 - p1) / (p2 - p1).sqrMagnitude) * (p2 - p1); 36 | return resVector.magnitude; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/RayD.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: caccb698d579a425b841dc8bd1720dd8 3 | timeCreated: 1515005992 4 | licenseType: Pro 5 | MonoImporter: 6 | externalObjects: {} 7 | serializedVersion: 2 8 | defaultReferences: [] 9 | executionOrder: 0 10 | icon: {instanceID: 0} 11 | userData: 12 | assetBundleName: 13 | assetBundleVariant: 14 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/TangentSolver.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | /* 9 | Derived from 10 | Lengyel, Eric. "Computing Tangent Space Basis Vectors for an Arbitrary Mesh". Terathon Software 3D Graphics Library, 2001. 11 | [url]http://www.terathon.com/code/tangent.html[/url] 12 | */ 13 | 14 | using UnityEngine; 15 | //using UnityEditor; 16 | 17 | public class TangentSolver : MonoBehaviour { 18 | 19 | public static void Solve(Mesh theMesh) { 20 | int vertexCount = theMesh.vertexCount; 21 | Vector3[] vertices = theMesh.vertices; 22 | Vector3[] normals = theMesh.normals; 23 | Vector2[] texcoords = theMesh.uv; 24 | int[] triangles = theMesh.triangles; 25 | int triangleCount = triangles.Length/3; 26 | 27 | Vector4[] tangents = new Vector4[vertexCount]; 28 | Vector3[] tan1 = new Vector3[vertexCount]; 29 | Vector3[] tan2 = new Vector3[vertexCount]; 30 | 31 | int tri = 0; 32 | 33 | for (int i = 0; i < (triangleCount); i++) { 34 | 35 | int i1 = triangles[tri]; 36 | int i2 = triangles[tri+1]; 37 | int i3 = triangles[tri+2]; 38 | 39 | Vector3 v1 = vertices[i1]; 40 | Vector3 v2 = vertices[i2]; 41 | Vector3 v3 = vertices[i3]; 42 | 43 | Vector2 w1 = texcoords[i1]; 44 | Vector2 w2 = texcoords[i2]; 45 | Vector2 w3 = texcoords[i3]; 46 | 47 | float x1 = v2.x - v1.x; 48 | float x2 = v3.x - v1.x; 49 | float y1 = v2.y - v1.y; 50 | float y2 = v3.y - v1.y; 51 | float z1 = v2.z - v1.z; 52 | float z2 = v3.z - v1.z; 53 | 54 | float s1 = w2.x - w1.x; 55 | float s2 = w3.x - w1.x; 56 | float t1 = w2.y - w1.y; 57 | float t2 = w3.y - w1.y; 58 | 59 | float r = 1.0f / (s1 * t2 - s2 * t1); 60 | Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r); 61 | Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r); 62 | 63 | tan1[i1] += sdir; 64 | tan1[i2] += sdir; 65 | tan1[i3] += sdir; 66 | 67 | tan2[i1] += tdir; 68 | tan2[i2] += tdir; 69 | tan2[i3] += tdir; 70 | 71 | tri += 3; 72 | 73 | } 74 | 75 | 76 | 77 | for (int i = 0; i < (vertexCount); i++) { 78 | 79 | Vector3 n = normals[i]; 80 | Vector3 t = tan1[i]; 81 | 82 | // Gram-Schmidt orthogonalize 83 | Vector3.OrthoNormalize(ref n, ref t); 84 | 85 | tangents[i].x = t.x; 86 | tangents[i].y = t.y; 87 | tangents[i].z = t.z; 88 | 89 | // Calculate handedness 90 | tangents[i].w = ( Vector3.Dot(Vector3.Cross(n, t), tan2[i]) < 0.0f ) ? -1.0f : 1.0f; 91 | 92 | } 93 | 94 | theMesh.tangents = tangents; 95 | 96 | } 97 | 98 | } -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/TangentSolver.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7f8121a392ab440cfbc826c959f39c53 3 | timeCreated: 1447099314 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Triangle3D.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ba32c13558d93422bab75a0892055209 3 | timeCreated: 1490090070 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Vector2D.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | 10 | 11 | public struct Vector2D { 12 | public double x; 13 | public double y; 14 | 15 | public Vector2D(Vector2 vector){ 16 | x = vector.x; 17 | y = vector.y; 18 | } 19 | 20 | public Vector2D(double x, double y){ 21 | this.x = x; 22 | this.y = y; 23 | } 24 | 25 | public Vector2 ToVector2(){ 26 | return new Vector2((float)x,(float)y); 27 | } 28 | 29 | public Vector2D Copy(){ 30 | return new Vector2D(x,y); 31 | } 32 | 33 | public Vector2D Rotate(double radians) { 34 | double sin = System.Math.Sin(radians); 35 | double cos = System.Math.Cos(radians); 36 | 37 | Vector2D v = this; 38 | 39 | double tx = v.x; 40 | double ty = v.y; 41 | v.x = (cos * tx) - (sin * ty); 42 | v.y = (sin * tx) + (cos * ty); 43 | return v; 44 | } 45 | 46 | public double this[int i] 47 | { 48 | get { return i==0?x:y; } 49 | set { 50 | switch (i){ 51 | case 0: 52 | x = value; 53 | break; 54 | case 1: 55 | y = value; 56 | break; 57 | 58 | default: 59 | throw new System.Exception("Out of range "+i); 60 | } 61 | } 62 | } 63 | 64 | public bool Equals(Vector2D p) { 65 | // If parameter is null return false: 66 | if ((object)p == null) 67 | { 68 | return false; 69 | } 70 | 71 | // Return true if the fields match: 72 | return (x == p.x) && (y == p.y); 73 | } 74 | 75 | public static Vector2D operator +(Vector2D c1, Vector2D c2) { 76 | return new Vector2D(c1.x + c2.x, c1.y + c2.y); 77 | } 78 | 79 | public static Vector2D operator +(Vector2D c1, Vector2 c2) { 80 | return new Vector2D(c1.x + c2.x, c1.y + c2.y); 81 | } 82 | 83 | public static Vector2D operator -(Vector2D c1, Vector2D c2) { 84 | return new Vector2D(c1.x - c2.x, c1.y - c2.y); 85 | } 86 | 87 | public static Vector2D operator -(Vector2D c1, Vector2 c2) { 88 | return new Vector2D(c1.x - c2.x, c1.y - c2.y); 89 | } 90 | 91 | public static Vector2D operator /(Vector2D c1, double i) { 92 | return new Vector2D(c1.x / i, c1.y / i); 93 | } 94 | 95 | public static Vector2D operator *(Vector2D c1, double i) { 96 | return new Vector2D(c1.x * i, c1.y * i); 97 | } 98 | 99 | public static Vector2D operator *(double i, Vector2D c1) { 100 | return new Vector2D(c1.x * i, c1.y * i); 101 | } 102 | 103 | public static Vector2D operator *(Vector2D c1, Vector2 c2) { 104 | return new Vector2D(c1.x * c2.x, c1.y * c2.y); 105 | } 106 | 107 | 108 | public static Vector2D operator - (Vector2D c1){ 109 | return new Vector2D(-c1.x, -c1.y); 110 | } 111 | 112 | public static bool operator ==(Vector2D c1, Vector2D c2) { 113 | return c1.x == c2.x && c1.y == c2.y; 114 | } 115 | 116 | public static bool operator !=(Vector2D c1, Vector2D c2) { 117 | return !(c1 == c2); 118 | } 119 | 120 | public override int GetHashCode() 121 | { 122 | return (x + y).GetHashCode(); 123 | } 124 | /* 125 | public Vec2d Clamp(Vec2d min, Vec2d max){ 126 | 127 | return new Vec2d(Mathf.Clamp(x, min.x, max.x), 128 | Mathf.Clamp(y, min.y, max.y)); 129 | }*/ 130 | 131 | public static Vector2D one{ 132 | get { 133 | return new Vector2D(1,1); 134 | } 135 | } 136 | 137 | public static Vector2D zero{ 138 | get { 139 | return new Vector2D(0,0); 140 | } 141 | } 142 | 143 | public static double Dot(Vector2D v1, Vector2D v2){ 144 | return v1.x * v2.x + v1.y * v2.y; 145 | } 146 | 147 | public static double Distance (Vector2D v1, Vector2D v2){ 148 | return (v1 - v2).magnitude; 149 | } 150 | 151 | 152 | public override bool Equals (object obj) 153 | { 154 | if (obj == null) 155 | return false; 156 | if (ReferenceEquals (this, obj)) 157 | return true; 158 | if (obj.GetType () != typeof(Vector2D)) 159 | return false; 160 | Vector2D other = (Vector2D)obj; 161 | return x == other.x && y == other.y; 162 | } 163 | 164 | 165 | // 166 | // Properties 167 | // 168 | public double magnitude 169 | { 170 | get 171 | { 172 | 173 | return System.Math.Sqrt (this.x * this.x + this.y * this.y); 174 | } 175 | } 176 | 177 | public double sqrMagnitude 178 | { 179 | get 180 | { 181 | return this.x * this.x + this.y * this.y; 182 | } 183 | } 184 | 185 | public override string ToString() 186 | { 187 | return "["+x+","+y+"]"; 188 | } 189 | 190 | // http://stackoverflow.com/a/1501725/420250 191 | public static double LineSegmentPointDistance(Vector2D v, Vector2D w, Vector2D p) { 192 | // Return minimum distance between line segment vw and point p 193 | double l2 = (v-w).sqrMagnitude; // i.e. |w-v|^2 - avoid a sqrt 194 | if (l2 == 0.0) return Vector2D.Distance(p, v); // v == w case 195 | // Consider the line extending the segment, parameterized as v + t (w - v). 196 | // We find projection of point p onto the line. 197 | // It falls where t = [(p-v) . (w-v)] / |w-v|^2 198 | // We clamp t from [0,1] to handle points outside the segment vw. 199 | double t = System.Math.Max(0.0, System.Math.Min(1.0, Vector2D.Dot(p - v, w - v) / l2)); 200 | Vector2D projection = v + t * (w - v); // Projection falls on the segment 201 | return Vector2D.Distance(p, projection); 202 | } 203 | } -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Vector2D.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ae8442d5de0bd4fb3b00f50e0709591c 3 | timeCreated: 1429535906 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Vector2i.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | 10 | /// 11 | /// A integer 2D vector class 12 | /// 13 | [System.Serializable] 14 | public class Vector2i { 15 | public int x; 16 | public int y; 17 | 18 | public Vector2i(){} 19 | 20 | public Vector2i(int x,int y){ 21 | this.x = x; 22 | this.y = y; 23 | } 24 | 25 | public Vector2i(Vector2 v){ 26 | x = (int)v.x; 27 | y = (int)v.y; 28 | } 29 | 30 | public static Vector2i Max(Vector2i a, Vector2i b){ 31 | return new Vector2i (Mathf.Max (a.x,b.x), Mathf.Max (a.y,b.y)); 32 | } 33 | 34 | public static Vector2i Min(Vector2i a, Vector2i b){ 35 | return new Vector2i (Mathf.Min (a.x,b.x), Mathf.Min (a.y,b.y)); 36 | } 37 | 38 | public static Vector2i operator +(Vector2i c1,Vector2i c2){ 39 | return new Vector2i(c1.x+c2.x, c1.y+c2.y); 40 | } 41 | 42 | public static Vector2 operator +(Vector2i c1,Vector2 c2){ 43 | return new Vector3(c1.x+c2.x, c1.y+c2.y); 44 | } 45 | 46 | public static Vector2 operator +(Vector2 c1,Vector2i c2){ 47 | return new Vector3(c1.x+c2.x, c1.y+c2.y); 48 | } 49 | 50 | public static Vector2i operator -(Vector2i c1,Vector2i c2){ 51 | return new Vector2i(c1.x-c2.x, c1.y-c2.y); 52 | } 53 | 54 | public static Vector2i operator *(Vector2i c1,int c2){ 55 | return new Vector2i(c1.x*c2, c1.y*c2); 56 | } 57 | 58 | public static Vector2 operator *(Vector2i c1,float c2){ 59 | return new Vector2(c1.x*c2, c1.y*c2); 60 | } 61 | 62 | public static Vector2i operator *(int c1,Vector2i c2){ 63 | return new Vector2i(c1*c2.x, c1*c2.y); 64 | } 65 | 66 | public static Vector2 operator *(float c1,Vector2i c2){ 67 | return new Vector2(c1*c2.x, c1*c2.y); 68 | } 69 | 70 | // allow callers to initialize 71 | public int this[int idx] 72 | { 73 | get { return idx==0?x:y; } 74 | set { 75 | switch (idx){ 76 | case 0: 77 | x = value; 78 | break; 79 | default: 80 | y = value; 81 | break; 82 | } 83 | } 84 | } 85 | 86 | public Vector2 toVector2(){ 87 | return new Vector2(x,y); 88 | } 89 | 90 | public float magnitude{ 91 | get { return Mathf.Sqrt(x*x+y*y);} 92 | } 93 | 94 | public override bool Equals(System.Object obj) 95 | { 96 | // If parameter is null return false. 97 | if (obj == null) 98 | { 99 | return false; 100 | } 101 | 102 | // If parameter cannot be cast to Point return false. 103 | Vector2i p = obj as Vector2i; 104 | if ((System.Object)p == null) 105 | { 106 | return false; 107 | } 108 | 109 | // Return true if the fields match: 110 | return (x == p.x) && (y == p.y); 111 | } 112 | 113 | public bool Equals(Vector2i p) 114 | { 115 | // If parameter is null return false: 116 | if ((object)p == null) 117 | { 118 | return false; 119 | } 120 | 121 | // Return true if the fields match: 122 | return (x == p.x) && (y == p.y); 123 | } 124 | 125 | public static bool operator ==(Vector2i a, Vector2i b) 126 | { 127 | // If both are null, or both are same instance, return true. 128 | if (System.Object.ReferenceEquals(a, b)) 129 | { 130 | return true; 131 | } 132 | 133 | // If one is null, but not both, return false. 134 | if (((object)a == null) || ((object)b == null)) 135 | { 136 | return false; 137 | } 138 | 139 | // Return true if the fields match: 140 | return a.x == b.x && a.y == b.y; 141 | } 142 | 143 | public static bool operator !=(Vector2i a, Vector2i b) 144 | { 145 | return !(a == b); 146 | } 147 | 148 | public override int GetHashCode() 149 | { 150 | return x ^ y; 151 | } 152 | 153 | public override string ToString() { 154 | return "Vector2i {"+x+", "+y+"}"; 155 | } 156 | 157 | 158 | } 159 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Vector2i.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1a9585c1f01744d0aaeee3f819160a9e 3 | timeCreated: 1428846381 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Vector3d.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 567b1bced925f4530854c2912d73f03c 3 | timeCreated: 1430387220 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Vector3i.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | 10 | /// 11 | /// A integer 2D vector class 12 | /// 13 | [System.Serializable] 14 | public class Vector3i { 15 | public int x; 16 | public int y; 17 | public int z; 18 | 19 | public Vector3i(){} 20 | 21 | public Vector3i(int x,int y,int z){ 22 | this.x = x; 23 | this.y = y; 24 | this.z = z; 25 | } 26 | 27 | public static Vector3i Max(Vector3i a, Vector3i b){ 28 | return new Vector3i (Mathf.Max (a.x,b.x), Mathf.Max (a.y,b.y), Mathf.Max (a.z,b.z)); 29 | } 30 | 31 | public static Vector3i Min(Vector3i a, Vector3i b){ 32 | return new Vector3i (Mathf.Min (a.x,b.x), Mathf.Min (a.y,b.y), Mathf.Min (a.z,b.z)); 33 | } 34 | 35 | public static Vector3i operator +(Vector3i c1,Vector3i c2){ 36 | return new Vector3i(c1.x+c2.x, c1.y+c2.y, c1.z+c2.z); 37 | } 38 | 39 | public static Vector3i operator -(Vector3i c1,Vector3i c2){ 40 | return new Vector3i(c1.x-c2.x, c1.y-c2.y, c1.z-c2.z); 41 | } 42 | 43 | public static Vector3i operator *(Vector3i c1,int c2){ 44 | return new Vector3i(c1.x*c2, c1.y*c2, c1.z*c2); 45 | } 46 | 47 | public static Vector3i operator /(Vector3i c1,int c2){ 48 | return new Vector3i(c1.x/c2, c1.y/c2, c1.z/c2); 49 | } 50 | 51 | public static Vector3i operator *(int c1,Vector3i c2){ 52 | return new Vector3i(c1*c2.x, c1*c2.y, c1*c2.z); 53 | } 54 | 55 | // allow callers to initialize 56 | public int this[int idx] 57 | { 58 | get { return idx==0?x:(idx==1?y:z); } 59 | set { 60 | switch (idx){ 61 | case 0: 62 | x = value; 63 | break; 64 | case 1: 65 | y = value; 66 | break; 67 | default: 68 | z = value; 69 | break; 70 | } 71 | } 72 | } 73 | 74 | public float magnitude{ 75 | get { return Mathf.Sqrt(x*x+y*y+z*z);} 76 | } 77 | 78 | public override bool Equals(System.Object obj) 79 | { 80 | // If parameter is null return false. 81 | if (obj == null) 82 | { 83 | return false; 84 | } 85 | 86 | // If parameter cannot be cast to Point return false. 87 | Vector3i p = obj as Vector3i; 88 | if ((System.Object)p == null) 89 | { 90 | return false; 91 | } 92 | 93 | // Return true if the fields match: 94 | return (x == p.x) && (y == p.y) && (z == p.z); 95 | } 96 | 97 | public bool Equals(Vector3i p) 98 | { 99 | // If parameter is null return false: 100 | if ((object)p == null) 101 | { 102 | return false; 103 | } 104 | 105 | // Return true if the fields match: 106 | return (x == p.x) && (y == p.y) && (z == p.z); 107 | } 108 | 109 | public static bool operator ==(Vector3i a, Vector3i b) 110 | { 111 | // If both are null, or both are same instance, return true. 112 | if (System.Object.ReferenceEquals(a, b)) 113 | { 114 | return true; 115 | } 116 | 117 | // If one is null, but not both, return false. 118 | if (((object)a == null) || ((object)b == null)) 119 | { 120 | return false; 121 | } 122 | 123 | // Return true if the fields match: 124 | return a.x == b.x && a.y == b.y && a.z == b.z; 125 | } 126 | 127 | public static bool operator !=(Vector3i a, Vector3i b) 128 | { 129 | return !(a == b); 130 | } 131 | 132 | public override int GetHashCode() 133 | { 134 | return x ^ y ^ z; 135 | } 136 | 137 | public override string ToString() { 138 | return "Vector3i {"+x+", "+y+", "+z+"}"; 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Vector3i.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 875f55ed0bca647b79b4e88a1c09fe9d 3 | timeCreated: 1428846381 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Math/Vector4D.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 5e2f8a3536d0248e3aa8139aab356fdb 3 | timeCreated: 1490648205 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Obj.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a37ad901fe2244046b238c3cd25d451b 3 | folderAsset: yes 4 | timeCreated: 1491123445 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Obj/ExportToObj.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using System.Collections.Generic; 9 | using System.IO; 10 | using UnityEngine; 11 | 12 | // Generic obj export 13 | public class ExportToObj 14 | { 15 | public List positions; 16 | public List normals; 17 | public List uvs; 18 | public List indices; 19 | 20 | private List objectNames; 21 | private List objectIndices; 22 | 23 | private bool exportNormals; 24 | private bool exportUvs; 25 | public bool flipX = true; 26 | 27 | public string name; 28 | 29 | public ExportToObj(bool exportNormals = true, bool exportUvs = true) 30 | { 31 | this.exportNormals = exportNormals; 32 | this.exportUvs= exportUvs; 33 | positions = new List(); 34 | normals = new List(); 35 | uvs = new List(); 36 | indices = new List(); 37 | objectNames = new List(); 38 | objectIndices = new List(); 39 | } 40 | 41 | public void AddObject(string objectName) 42 | { 43 | objectNames.Add(objectName); 44 | objectIndices.Add(indices.Count); 45 | } 46 | 47 | public void AddMesh(Mesh mesh, Matrix4x4 mat) 48 | { 49 | if (mesh == null) 50 | { 51 | return; 52 | } 53 | for (int i = 0; i < mesh.subMeshCount; i++) 54 | { 55 | AddMesh(mesh,i,mat); 56 | } 57 | } 58 | 59 | public void AddTriangle(Vector3[] mPosition, Vector3[] mNormals = null, Vector2[] mUvs = null) 60 | { 61 | int offset = positions.Count; 62 | indices.Add(offset); 63 | indices.Add(offset+1); 64 | indices.Add(offset+2); 65 | 66 | positions.Add(mPosition[0]); 67 | positions.Add(mPosition[1]); 68 | positions.Add(mPosition[2]); 69 | 70 | if (exportNormals) 71 | { 72 | if (mNormals == null) 73 | { 74 | Debug.LogError("mNormals is null"); 75 | return; 76 | } 77 | normals.Add(mNormals[0]); 78 | normals.Add(mNormals[1]); 79 | normals.Add(mNormals[2]); 80 | } 81 | if (exportUvs) 82 | { 83 | if (mUvs == null) 84 | { 85 | Debug.LogError("mUvs is null"); 86 | return; 87 | } 88 | uvs.Add(mUvs[0]); 89 | uvs.Add(mUvs[1]); 90 | uvs.Add(mUvs[2]); 91 | } 92 | } 93 | 94 | public void AddMesh(Mesh mesh, int subMesh, Matrix4x4 mat) 95 | { 96 | var normalTrans = mat.inverse.transpose; 97 | int offset = positions.Count; 98 | var tris = mesh.GetTriangles(subMesh); 99 | var mVertices = mesh.vertices; 100 | var mNormals = mesh.normals; 101 | var mUvs = mesh.uv; 102 | for (int i = 0; i < tris.Length; i = i + 3) 103 | { 104 | indices.Add(i+offset); 105 | indices.Add(i+offset+1); 106 | indices.Add(i+offset+2); 107 | 108 | positions.Add(mVertices[tris[i]]); 109 | positions.Add(mVertices[tris[i+1]]); 110 | positions.Add(mVertices[tris[i+2]]); 111 | 112 | if (exportNormals) 113 | { 114 | if (mNormals == null) 115 | { 116 | normals.Add(Vector3.zero); 117 | normals.Add(Vector3.zero); 118 | normals.Add(Vector3.zero); 119 | } 120 | else 121 | { 122 | normals.Add(mNormals[tris[i]]); 123 | normals.Add(mNormals[tris[i+1]]); 124 | normals.Add(mNormals[tris[i+2]]); 125 | } 126 | 127 | } 128 | if (exportUvs) 129 | { 130 | if (mUvs.Length == 0) 131 | { 132 | uvs.Add(Vector2.zero); 133 | uvs.Add(Vector2.zero); 134 | uvs.Add(Vector2.zero); 135 | } 136 | else 137 | { 138 | uvs.Add(mUvs[tris[i]]); 139 | uvs.Add(mUvs[tris[i+1]]); 140 | uvs.Add(mUvs[tris[i+2]]); 141 | } 142 | 143 | } 144 | } 145 | for (int i = offset; i < positions.Count; i++) 146 | { 147 | positions[i] = mat.MultiplyPoint(positions[i]); 148 | } 149 | for (int i = offset; i < normals.Count; i++) 150 | { 151 | normals[i] = normalTrans.MultiplyVector(normals[i]); 152 | } 153 | } 154 | 155 | public string ToString() 156 | { 157 | StringWriter st = new StringWriter(); 158 | Save(st); 159 | return st.GetStringBuilder().ToString(); 160 | } 161 | 162 | public void Save(TextWriter writer) 163 | { 164 | writer.Write("# Custom geometry export (UNITY) OBJ File: '"+name+"'\n"); 165 | writer.Write("o "); 166 | writer.Write(name); 167 | writer.Write("\n"); 168 | float xScale = flipX?-1:0; 169 | foreach (var v in positions){ 170 | writer.Write("v {0} {1} {2}\n", v.x*xScale,v.y,v.z); 171 | } 172 | foreach (var u in uvs){ 173 | writer.Write("vt {0} {1}\n", 1.0f-u.x,u.y); 174 | } 175 | foreach (var n in normals){ 176 | writer.Write("v {0} {1} {2}\n", n.x*xScale,n.y,n.z); 177 | } 178 | var formatStr = "f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n"; 179 | if (exportNormals && exportUvs == false) 180 | { 181 | formatStr = "f {0}//{0} {1}//{1} {2}//{2}\n"; 182 | } 183 | if (exportNormals == false && exportUvs ) 184 | { 185 | formatStr = "f {0}/{0} {1}/{1} {2}/{2}\n"; 186 | } 187 | if (exportNormals == false && exportUvs == false ) 188 | { 189 | formatStr = "f {0} {1} {2}\n"; 190 | } 191 | int objectId = 0; 192 | for (int i=0;i objectId && objectIndices[objectId] == i) 194 | { 195 | writer.Write("o "); 196 | writer.Write(objectNames[objectId]); 197 | writer.Write("\n"); 198 | objectId++; 199 | } 200 | if (flipX) 201 | { 202 | // inverse winding order 203 | writer.Write(formatStr, 204 | indices[0+i]+1, 205 | indices[2+i]+1, 206 | indices[1+i]+1); 207 | } 208 | else 209 | { 210 | writer.Write(formatStr, 211 | indices[0+i]+1, 212 | indices[1+i]+1, 213 | indices[2+i]+1); 214 | } 215 | } 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Obj/ExportToObj.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: c786ab9eae90b4dce9975ee9fd333c51 3 | timeCreated: 1491072302 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 29e234278bdba4b74836575089548d3f 3 | folderAsset: yes 4 | timeCreated: 1490035309 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/BSPTree.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0425bea241b00411e95b179d1d121f5d 3 | timeCreated: 1490102684 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/Editor.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4ac3798d398754b7783bbbedb8f1f59d 3 | folderAsset: yes 4 | timeCreated: 1490035325 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/Editor/BSPTreeTest.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | [TestFixture] 4 | public class BSPTreeTest { 5 | 6 | [Test] 7 | public void BuildBSPTree() 8 | { 9 | var triangles = new[] 10 | { 11 | new Vector3D(-1.000000, 0.000000, 0.000000),new Vector3D(1.000000, 0.000000, 0.000000),new Vector3D(1.000000, -1.000000, 0.000000), 12 | new Vector3D(0.750382, -0.453671, 1.597113),new Vector3D(1.044405, 0.284186, 0.820524),new Vector3D(1.177693, -0.022709, 0.454081), 13 | new Vector3D(-0.370747, -1.054776, 1.736725),new Vector3D(1.539376, -0.188849, 2.000000),new Vector3D(0.406594, -2.000000, 2.000000) 14 | }; 15 | /* 16 | BSPTree bspTree = new BSPTree(); 17 | Assert.Null(bspTree.node,"No root node"); 18 | bspTree.Insert(new Triangle3D( triangles[0],triangles[1],triangles[2])); 19 | Assert.NotNull(bspTree.node, "First insert as root node"); 20 | Assert.Null(bspTree.node.left, "First insert has empty left"); 21 | Assert.Null(bspTree.node.right, "First insert has empty right"); 22 | bspTree.Insert(new Triangle3D( triangles[3],triangles[4],triangles[5])); 23 | Assert.Null(bspTree.node.left,"Not inserted to the left"); 24 | Assert.NotNull(bspTree.node.right,"Inserted to the right"); 25 | Assert.Null(bspTree.node.right.left,"Newly inserted node has no children"); 26 | Assert.Null(bspTree.node.right.right,"Newly inserted node has no children"); 27 | bspTree.Insert(new Triangle3D( triangles[6],triangles[7],triangles[8])); 28 | Assert.Null(bspTree.node.left,"Not inserted to the left"); 29 | Assert.NotNull(bspTree.node.right,"Inserted to the right"); 30 | Assert.NotNull(bspTree.node.right.left,"Newly inserted node is split"); 31 | Assert.NotNull(bspTree.node.right.right,"Newly inserted node is split"); 32 | bspTree.ToList();*/ 33 | } 34 | 35 | [Test] 36 | public void BuildBSPTreeExample() 37 | { 38 | /* 39 | var triangles = new[] 40 | { 41 | new Vector3D(3.6, -3.6, -6.4), new Vector3D(2.2, -1.2, -6.0), new Vector3D(2.2, -2.6, -7.4), 42 | new Vector3D(3.6, -3.6, -6.4), new Vector3D(3.6, -2.2, -5.0), new Vector3D(2.2, -1.2, -6.0), 43 | new Vector3D(3.6, -2.2, -5.0), new Vector3D(5.0, -2.6, -7.4), new Vector3D(5.0, -1.2, -6.0), 44 | new Vector3D(3.6, -2.2, -5.0), new Vector3D(3.6, -3.6, -6.4), new Vector3D(5.0, -2.6, -7.4), 45 | new Vector3D(3.4, 0.2, -7.5), new Vector3D(2.7, -1.1, -4.6), new Vector3D(4.5, 0.2, -5.9) 46 | }; 47 | BSPTree bspTree = new BSPTree(); 48 | for (int i = 0; i < triangles.Length; i = i + 3) 49 | { 50 | bspTree.Insert(new Triangle3D( triangles[i],triangles[i+1],triangles[i+2])); 51 | } 52 | bspTree.ToList(); 53 | */ 54 | } 55 | } 56 | 57 | /* 58 | using System.Collections; 59 | using System.Collections.Generic; 60 | using UnityEngine; 61 | using NUnit.Framework; 62 | 63 | [TestFixture] 64 | public class Plane3DTest 65 | { 66 | [Test] 67 | public void TestPlaneDistance() 68 | { 69 | Random.InitState(123); 70 | for (int i = 0; i < 1000; i++) 71 | { 72 | Vector3 direction = Random.onUnitSphere; 73 | float dist = Random.Range(-9999, 9999); 74 | Vector3D directionD = new Vector3D(direction); 75 | Plane3D planeD = new Plane3D(directionD, dist); 76 | Plane plane = new Plane(direction, dist); 77 | for (int j = 0; j < 10; j++) 78 | { 79 | Vector3 pos = Random.insideUnitSphere * 99999; 80 | Vector3D posD = new Vector3D(pos); 81 | double val1 = plane.GetDistanceToPoint(pos); 82 | double val2 = planeD.GetDistanceToPoint(posD); 83 | Assert.AreEqual(val1, val2, 0.02, "GetDistanceToPoint " + plane); 84 | Assert.AreEqual(plane.GetSide(pos), planeD.GetSide(posD), "GetSide " + plane); 85 | } 86 | } 87 | } 88 | */ -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/Editor/BSPTreeTest.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3827ab2c4c61540d49d39dfaadff9a70 3 | timeCreated: 1490173316 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/SVGDocument.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using System.IO; 9 | using System.Xml; 10 | 11 | public class SVGDocument : SVGNode 12 | { 13 | public int width; 14 | public int height; 15 | 16 | public XmlDocument doc; 17 | 18 | public SVGDocument(int width, int height) : base(null) 19 | { 20 | 21 | this.width = width; 22 | this.height = height; 23 | doc = new XmlDocument( ); 24 | 25 | //(1) the xml declaration is recommended, but not mandatory 26 | XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration( "1.0", "UTF-8", null ); 27 | XmlElement root = doc.DocumentElement; 28 | doc.InsertBefore( xmlDeclaration, root ); 29 | 30 | //(2) string.Empty makes cleaner code 31 | XmlElement svg = doc.CreateElement( string.Empty, "svg", string.Empty ); 32 | xmlElement = svg; 33 | svg.SetAttribute("xmlns", "http://www.w3.org/2000/svg"); 34 | svg.SetAttribute("version", "1.2"); 35 | svg.SetAttribute("baseProfile", "tiny"); 36 | svg.SetAttribute("viewBox", "0 0 "+width+" "+height); 37 | svg.SetAttribute("shape-rendering", "crispEdges"); 38 | 39 | doc.AppendChild( svg ); 40 | 41 | XmlElement element2 = doc.CreateElement( string.Empty, "desc", string.Empty ); 42 | XmlText text1 = doc.CreateTextNode( "SVG Export by Morten Nobel-Joergensen" ); 43 | element2.AppendChild(text1); 44 | svg.AppendChild( element2 ); 45 | } 46 | 47 | public string ToString() 48 | { 49 | StringWriter w = new StringWriter(); 50 | doc.Save(w); 51 | return w.ToString(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/SVGDocument.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9441465c2c1a54d2582ee990a176b1c9 3 | timeCreated: 1490035567 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/SVGExportProps.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | 10 | [RequireComponent(typeof(MeshFilter))] 11 | public class SVGExportProps : MonoBehaviour 12 | { 13 | public Vector3 baseDirection = new Vector3(0,1,0); 14 | 15 | public void OnDrawGizmosSelected() 16 | { 17 | var meshFilter = GetComponent(); 18 | var meshRenderer = GetComponent(); 19 | 20 | Gizmos.color = Color.cyan; 21 | Gizmos.matrix = Matrix4x4.identity; 22 | Gizmos.DrawWireCube(meshRenderer.bounds.center,meshRenderer.bounds.size); 23 | var direction = transform.TransformVector(baseDirection); 24 | Gizmos.DrawLine(meshRenderer.bounds.center,meshRenderer.bounds.center+direction*meshRenderer.bounds.size.magnitude); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/SVGExportProps.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 32c22dc78f26a48f783eefe2f4a78e55 3 | timeCreated: 1490299314 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/SVGGroup.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | public class SVGGroup : SVGNode{ 9 | 10 | public SVGGroup(SVGNode parent, string name) : base(parent) 11 | { 12 | var doc = GetDocument(); 13 | xmlElement = doc.CreateElement("g"); 14 | parent.xmlElement.AppendChild(xmlElement); 15 | xmlElement.SetAttribute("name", name); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/SVGGroup.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ae799626c058f45c9b5ea0625475df87 3 | timeCreated: 1490035773 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/SVGNode.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using System.Collections.Generic; 9 | using System.Xml; 10 | using UnityEngine; 11 | 12 | public class SVGNode 13 | { 14 | public readonly SVGNode parent; 15 | 16 | public XmlElement xmlElement; 17 | 18 | protected XmlDocument GetDocument() 19 | { 20 | SVGNode n = parent; 21 | while (n.parent != null) 22 | { 23 | n = n.parent; 24 | } 25 | SVGDocument doc = n as SVGDocument; 26 | return doc.doc; 27 | } 28 | 29 | public SVGNode(SVGNode parent) 30 | { 31 | this.parent = parent; 32 | } 33 | 34 | public SVGGroup CreateGroup(string name) 35 | { 36 | return new SVGGroup(this, name); 37 | } 38 | 39 | public SVGPolygon CreatePolygon(Color fill, List vertices, bool debugOutput = false) 40 | { 41 | return new SVGPolygon(this, fill, vertices, debugOutput); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/SVGNode.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 689c6df8144074fe9b6cabbf6d5c1706 3 | timeCreated: 1490035736 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/SVGPolygon.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using System.Collections.Generic; 9 | using System.IO; 10 | using UnityEngine; 11 | 12 | public class SVGPolygon : SVGNode 13 | { 14 | public SVGPolygon(SVGNode svgNode, Color fill, List vertices, bool debugOutput = false) 15 | :base(svgNode) 16 | { 17 | var doc = GetDocument(); 18 | xmlElement = doc.CreateElement("polygon"); 19 | parent.xmlElement.AppendChild(xmlElement); 20 | xmlElement.SetAttribute("fill", ColorToHex(fill)); 21 | if (debugOutput) 22 | { 23 | xmlElement.SetAttribute("stroke", "black"); 24 | xmlElement.SetAttribute("stroke-width", "1"); 25 | } 26 | 27 | //xmlElement.SetAttribute("stroke-linecap", "round"); 28 | StringWriter sw = new StringWriter(); 29 | bool first = true; 30 | foreach (var v in vertices) 31 | { 32 | if (first) 33 | { 34 | first = false; 35 | } 36 | else 37 | { 38 | sw.Write(" "); 39 | } 40 | sw.Write("{0},{1}", v.x.ToString("R"), v.y.ToString("R")); 41 | } 42 | xmlElement.SetAttribute("points", sw.ToString()); 43 | } 44 | 45 | string ColorToHex(Color c) 46 | { 47 | return string.Format("#{0:X2}{1:X2}{2:X2}", 48 | Mathf.RoundToInt(c.r * 255), 49 | Mathf.RoundToInt(c.g * 255), 50 | Mathf.RoundToInt(c.b * 255)); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/SVGPolygon.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 74a99dd9835a84d64a3dffba8412a220 3 | timeCreated: 1490035536 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/Test.mat: -------------------------------------------------------------------------------- 1 | %YAML 1.1 2 | %TAG !u! tag:unity3d.com,2011: 3 | --- !u!21 &2100000 4 | Material: 5 | serializedVersion: 6 6 | m_ObjectHideFlags: 0 7 | m_PrefabParentObject: {fileID: 0} 8 | m_PrefabInternal: {fileID: 0} 9 | m_Name: Test 10 | m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0} 11 | m_ShaderKeywords: _EMISSION 12 | m_LightmapFlags: 1 13 | m_EnableInstancingVariants: 0 14 | m_DoubleSidedGI: 0 15 | m_CustomRenderQueue: -1 16 | stringTagMap: {} 17 | disabledShaderPasses: [] 18 | m_SavedProperties: 19 | serializedVersion: 3 20 | m_TexEnvs: 21 | - _BumpMap: 22 | m_Texture: {fileID: 0} 23 | m_Scale: {x: 1, y: 1} 24 | m_Offset: {x: 0, y: 0} 25 | - _DetailAlbedoMap: 26 | m_Texture: {fileID: 0} 27 | m_Scale: {x: 1, y: 1} 28 | m_Offset: {x: 0, y: 0} 29 | - _DetailMask: 30 | m_Texture: {fileID: 0} 31 | m_Scale: {x: 1, y: 1} 32 | m_Offset: {x: 0, y: 0} 33 | - _DetailNormalMap: 34 | m_Texture: {fileID: 0} 35 | m_Scale: {x: 1, y: 1} 36 | m_Offset: {x: 0, y: 0} 37 | - _EmissionMap: 38 | m_Texture: {fileID: 0} 39 | m_Scale: {x: 1, y: 1} 40 | m_Offset: {x: 0, y: 0} 41 | - _MainTex: 42 | m_Texture: {fileID: 0} 43 | m_Scale: {x: 1, y: 1} 44 | m_Offset: {x: 0, y: 0} 45 | - _MetallicGlossMap: 46 | m_Texture: {fileID: 0} 47 | m_Scale: {x: 1, y: 1} 48 | m_Offset: {x: 0, y: 0} 49 | - _OcclusionMap: 50 | m_Texture: {fileID: 0} 51 | m_Scale: {x: 1, y: 1} 52 | m_Offset: {x: 0, y: 0} 53 | - _ParallaxMap: 54 | m_Texture: {fileID: 0} 55 | m_Scale: {x: 1, y: 1} 56 | m_Offset: {x: 0, y: 0} 57 | m_Floats: 58 | - _BumpScale: 1 59 | - _Cutoff: 0.5 60 | - _DetailNormalMapScale: 1 61 | - _DstBlend: 0 62 | - _GlossMapScale: 1 63 | - _Glossiness: 0.5 64 | - _GlossyReflections: 1 65 | - _Metallic: 0 66 | - _Mode: 0 67 | - _OcclusionStrength: 1 68 | - _Parallax: 0.02 69 | - _SmoothnessTextureChannel: 0 70 | - _SpecularHighlights: 1 71 | - _SrcBlend: 1 72 | - _UVSec: 0 73 | - _ZWrite: 1 74 | m_Colors: 75 | - _Color: {r: 0, g: 0, b: 0, a: 1} 76 | - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} 77 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/Test.mat.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 745e1f1113b584f0b8cd975517307b5f 3 | timeCreated: 1490045073 4 | licenseType: Pro 5 | NativeFormatImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/test.json: -------------------------------------------------------------------------------- 1 | {'Name': 'BSPTree', 2 | 'Node': 3 | {'plane':'Plane normal: (1, 0, 0) dist 1.4300000667572021', 4 | 'triangles':'', 5 | 'left':{'plane':'Plane normal: (0, 1, 0) dist 0.81999999284744263', 6 | 'triangles':'', 7 | 'left':null, 8 | 'right':{'plane':'Plane normal: (0, 1, 0) dist 1.3200000524520874', 9 | 'triangles':'', 10 | 'left':null, 11 | 'right':{'plane':'Plane normal: (0, 1, 0) dist 0.31999993324279785', 12 | 'triangles':'', 13 | 'left':null, 14 | 'right':null 15 | } 16 | } 17 | }, 18 | 'right':{'plane':'Plane normal: (1, 0, 0) dist 1.9300000667572021', 19 | 'triangles':'', 20 | 'left':{'plane':'Plane normal: (0, 1, 0) dist 0.81999999284744263', 21 | 'triangles':'', 22 | 'left':null, 23 | 'right':{'plane':'Plane normal: (0, 1, 0) dist 1.3200000524520874', 24 | 'triangles':'', 25 | 'left':null, 26 | 'right':{'plane':'Plane normal: (0, 1, 0) dist 0.31999993324279785', 27 | 'triangles':'', 28 | 'left':null, 29 | 'right':null 30 | } 31 | } 32 | }, 33 | 'right':{'plane':'Plane normal: (1, 0, 0) dist 0.93000006675720215', 34 | 'triangles':'', 35 | 'left':{'plane':'Plane normal: (0, 1, 0) dist 0.81999999284744263', 36 | 'triangles':'', 37 | 'left':null, 38 | 'right':{'plane':'Plane normal: (0, 1, 0) dist 1.3200000524520874', 39 | 'triangles':'', 40 | 'left':null, 41 | 'right':{'plane':'Plane normal: (0, 1, 0) dist 0.31999993324279785', 42 | 'triangles':'', 43 | 'left':null, 44 | 'right':null 45 | } 46 | } 47 | }, 48 | 'right':{'plane':'Plane normal: (0, 1, 0) dist 0.81999999284744263', 49 | 'triangles':'', 50 | 'left':null, 51 | 'right':{'plane':'Plane normal: (0, 1, 0) dist 1.3200000524520874', 52 | 'triangles':'', 53 | 'left':null, 54 | 'right':{'plane':'Plane normal: (0, 1, 0) dist 0.31999993324279785', 55 | 'triangles':'', 56 | 'left':null, 57 | 'right':null 58 | } 59 | } 60 | } 61 | } 62 | } 63 | }} -------------------------------------------------------------------------------- /Assets/UnityUtils/SVG/test.json.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a803452737d34dd09b08c460a5a013fb 3 | timeCreated: 1490615694 4 | TextScriptImporter: 5 | userData: 6 | assetBundleName: 7 | assetBundleVariant: 8 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Scene.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 3e28b32d4a23f4567a475838c2d89da3 3 | folderAsset: yes 4 | timeCreated: 1491394967 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Scene/LineSegmentRenderer.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using System.Collections.Generic; 9 | using UnityEngine; 10 | 11 | [RequireComponent(typeof(MeshFilter))] 12 | public class LineSegmentRenderer : MonoBehaviour { 13 | 14 | public List positions; 15 | 16 | public float lineWidth = 0.1f; 17 | 18 | public void UpdateMesh() 19 | { 20 | if (positions.Count % 2 != 0) 21 | { 22 | Debug.LogError("Invalid position count - should come in pair (for each line segment)"); 23 | return; 24 | } 25 | 26 | List meshPositions = new List(); 27 | List meshIndices = new List(); 28 | 29 | float l = lineWidth *0.5f; 30 | 31 | for (int j=0;j 64000) 48 | { 49 | break; 50 | } 51 | int vertCount = meshPositions.Count; 52 | meshIndices.Add(vertCount); 53 | meshIndices.Add(vertCount+1); 54 | meshIndices.Add(vertCount+2); 55 | meshIndices.Add(vertCount); 56 | meshIndices.Add(vertCount+2); 57 | meshIndices.Add(vertCount+3); 58 | 59 | meshPositions.Add((from - t*l + b*l).RotateAround(from, normal, i*120)); 60 | meshPositions.Add((to - t*l + b*l).RotateAround(from, normal, i*120)); 61 | meshPositions.Add((to + t*l + b*l).RotateAround(from, normal, i*120)); 62 | meshPositions.Add((from + t*l + b*l).RotateAround(from, normal, i*120)); 63 | } 64 | } 65 | var meshFilter = GetComponent(); 66 | if (meshFilter == null) 67 | { 68 | Debug.LogError("MeshFilter == null"); 69 | return; 70 | } 71 | var mesh = meshFilter.sharedMesh; 72 | if (mesh == null) 73 | { 74 | mesh = new Mesh(); 75 | } 76 | mesh.SetTriangles(new List(), 0); 77 | mesh.SetVertices(meshPositions); 78 | mesh.SetTriangles(meshIndices, 0); 79 | meshFilter.sharedMesh = mesh; 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /Assets/UnityUtils/Scene/LineSegmentRenderer.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 826eda1e9fdaf4132a58d8cd8b5001cb 3 | timeCreated: 1491394981 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a4355ae78479443e38a264607702fc11 3 | folderAsset: yes 4 | timeCreated: 1517991878 5 | licenseType: Pro 6 | DefaultImporter: 7 | externalObjects: {} 8 | userData: 9 | assetBundleName: 10 | assetBundleVariant: 11 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7943ac055e52445ebae6a15a60498d6a 3 | folderAsset: yes 4 | timeCreated: 1516608073 5 | licenseType: Pro 6 | DefaultImporter: 7 | externalObjects: {} 8 | userData: 9 | assetBundleName: 10 | assetBundleVariant: 11 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/FastPriorityQueue.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 50568a89fafd64c49b3a0c720e5c5cb0 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/FastPriorityQueueNode.cs: -------------------------------------------------------------------------------- 1 | namespace Priority_Queue 2 | { 3 | public class FastPriorityQueueNode 4 | { 5 | /// 6 | /// The Priority to insert this node at. Must be set BEFORE adding a node to the queue (ideally just once, in the node's constructor). 7 | /// Should not be manually edited once the node has been enqueued - use queue.UpdatePriority() instead 8 | /// 9 | public float Priority { get; protected internal set; } 10 | 11 | /// 12 | /// Represents the current position in the queue 13 | /// 14 | public int QueueIndex { get; internal set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/FastPriorityQueueNode.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 1735ee73795c744ef8bc89d9c5713369 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/GenericPriorityQueue.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 6be706ee1484948479ccd063b54fa371 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/GenericPriorityQueueNode.cs: -------------------------------------------------------------------------------- 1 | namespace Priority_Queue 2 | { 3 | public class GenericPriorityQueueNode 4 | { 5 | /// 6 | /// The Priority to insert this node at. Must be set BEFORE adding a node to the queue (ideally just once, in the node's constructor). 7 | /// Should not be manually edited once the node has been enqueued - use queue.UpdatePriority() instead 8 | /// 9 | public TPriority Priority { get; protected internal set; } 10 | 11 | /// 12 | /// Represents the current position in the queue 13 | /// 14 | public int QueueIndex { get; internal set; } 15 | 16 | /// 17 | /// Represents the order the node was inserted in 18 | /// 19 | public long InsertionIndex { get; internal set; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/GenericPriorityQueueNode.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a1333bd0725064ddfb8e8fbff3d13201 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/IFixedSizePriorityQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Priority_Queue 4 | { 5 | /// 6 | /// A helper-interface only needed to make writing unit tests a bit easier (hence the 'internal' access modifier) 7 | /// 8 | internal interface IFixedSizePriorityQueue : IPriorityQueue 9 | where TPriority : IComparable 10 | { 11 | /// 12 | /// Resize the queue so it can accept more nodes. All currently enqueued nodes are remain. 13 | /// Attempting to decrease the queue size to a size too small to hold the existing nodes results in undefined behavior 14 | /// 15 | void Resize(int maxNodes); 16 | 17 | /// 18 | /// Returns the maximum number of items that can be enqueued at once in this queue. Once you hit this number (ie. once Count == MaxSize), 19 | /// attempting to enqueue another item will cause undefined behavior. 20 | /// 21 | int MaxSize { get; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/IFixedSizePriorityQueue.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: a2b5fbba128834f3799cd7b5f61a0c58 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/IPriorityQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Priority_Queue 5 | { 6 | /// 7 | /// The IPriorityQueue interface. This is mainly here for purists, and in case I decide to add more implementations later. 8 | /// For speed purposes, it is actually recommended that you *don't* access the priority queue through this interface, since the JIT can 9 | /// (theoretically?) optimize method calls from concrete-types slightly better. 10 | /// 11 | public interface IPriorityQueue : IEnumerable 12 | where TPriority : IComparable 13 | { 14 | /// 15 | /// Enqueue a node to the priority queue. Lower values are placed in front. Ties are broken by first-in-first-out. 16 | /// See implementation for how duplicates are handled. 17 | /// 18 | void Enqueue(TItem node, TPriority priority); 19 | 20 | /// 21 | /// Removes the head of the queue (node with minimum priority; ties are broken by order of insertion), and returns it. 22 | /// 23 | TItem Dequeue(); 24 | 25 | /// 26 | /// Removes every node from the queue. 27 | /// 28 | void Clear(); 29 | 30 | /// 31 | /// Returns whether the given node is in the queue. 32 | /// 33 | bool Contains(TItem node); 34 | 35 | /// 36 | /// Removes a node from the queue. The node does not need to be the head of the queue. 37 | /// 38 | void Remove(TItem node); 39 | 40 | /// 41 | /// Call this method to change the priority of a node. 42 | /// 43 | void UpdatePriority(TItem node, TPriority priority); 44 | 45 | /// 46 | /// Returns the head of the queue, without removing it (use Dequeue() for that). 47 | /// 48 | TItem First { get; } 49 | 50 | /// 51 | /// Returns the number of nodes in the queue. 52 | /// 53 | int Count { get; } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/IPriorityQueue.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 83ece48dfa12d4e36a270d233fbd9001 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Daniel "BlueRaja" Pflughoeft 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/LICENSE.txt.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b1f0b44eb6e934b379f91f9c6aa35041 3 | timeCreated: 1487161024 4 | licenseType: Pro 5 | TextScriptImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/Priority Queue.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {1531C1EA-BD53-41D1-A34B-CFCDF79D2651} 8 | Library 9 | Properties 10 | Priority_Queue 11 | Priority Queue 12 | v4.5 13 | 512 14 | NET_VERSION_4_5 15 | $(DefineConstants);$(CustomConstants) 16 | 17 | 18 | 19 | 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | NET_VERSION_4_5 28 | $(DefineConstants);$(CustomConstants) 29 | false 30 | 31 | 32 | pdbonly 33 | true 34 | bin\Release\ 35 | TRACE 36 | prompt 37 | 4 38 | NET_VERSION_4_5 39 | $(DefineConstants);$(CustomConstants) 40 | false 41 | 42 | 43 | pdbonly 44 | true 45 | TRACE;NET_VERSION_4_5 46 | prompt 47 | 4 48 | false 49 | bin\Release\net45\ 50 | v4.5 51 | bin\Release\net45\Priority Queue.xml 52 | 53 | 54 | pdbonly 55 | true 56 | TRACE 57 | prompt 58 | 4 59 | false 60 | bin\Release\net20\ 61 | v2.0 62 | bin\Release\net20\Priority Queue.xml 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 89 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/Priority Queue.csproj.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 29486b448e60744edbbfb35a667adb05 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/Priority Queue.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | OptimizedPriorityQueue 5 | 4.0.0 6 | Highly Optimized Priority Queue 7 | BlueRaja 8 | BlueRaja 9 | https://github.com/BlueRaja/High-Speed-Priority-Queue-for-C-Sharp/blob/master/LICENSE.txt 10 | https://github.com/BlueRaja/High-Speed-Priority-Queue-for-C-Sharp 11 | false 12 | A highly optimized Priority Queue for path-finding and related applications 13 | 4.0 release! Added generic priority-types for SimplePriorityQueue 14 | Copyright 2016 15 | C# priority-queue pathfinding optimized 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/Priority Queue.nuspec.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 157d3c85879b5470bbf2d4e01df67bf2 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/Properties.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: fb56b7ff81e754ee8be240bdbf2f3448 3 | folderAsset: yes 4 | timeCreated: 1487161022 5 | licenseType: Pro 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Highly Optimized Priority Queue")] 9 | [assembly: AssemblyDescription("A highly optimized Priority Queue for path-finding and related applications")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Highly Optimized Priority Queue")] 13 | [assembly: AssemblyCopyright("Copyright © BlueRaja 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("3eee6b54-af8a-494b-9121-3d46ed09a58b")] 24 | 25 | [assembly: InternalsVisibleToAttribute("Priority Queue Tests")] 26 | 27 | // Version information for an assembly consists of the following four values: 28 | // 29 | // Major Version 30 | // Minor Version 31 | // Build Number 32 | // Revision 33 | // 34 | // You can specify all the values or you can default the Build and Revision Numbers 35 | // by using the '*' as shown below: 36 | // [assembly: AssemblyVersion("1.0.*")] 37 | [assembly: AssemblyVersion("4.0.0")] 38 | [assembly: AssemblyFileVersion("4.0.0")] -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/Properties/AssemblyInfo.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 2b4ed801d1d5042d4b4eaba0dc20626d 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/SimplePriorityQueue.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 83aea537b311c470aa104958bf970dee 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/StablePriorityQueue.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 907a0e18a3be543f388a2e2fe2dbeb30 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/StablePriorityQueueNode.cs: -------------------------------------------------------------------------------- 1 | namespace Priority_Queue 2 | { 3 | public class StablePriorityQueueNode : FastPriorityQueueNode 4 | { 5 | /// 6 | /// Represents the order the node was inserted in 7 | /// 8 | public long InsertionIndex { get; internal set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/StablePriorityQueueNode.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cfeb41780c1644de0810483e72ad08f0 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/Priority Queue/packages.config.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b8a811b5014b645ef962f6baef970bae 3 | timeCreated: 1487161022 4 | licenseType: Pro 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /Assets/UnityUtils/ThirdParty/SimpleJSON.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7f5f32985d6f0459dbf9c56318f582ef 3 | timeCreated: 1516608076 4 | licenseType: Pro 5 | MonoImporter: 6 | externalObjects: {} 7 | serializedVersion: 2 8 | defaultReferences: [] 9 | executionOrder: 0 10 | icon: {instanceID: 0} 11 | userData: 12 | assetBundleName: 13 | assetBundleVariant: 14 | -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b3638aecb614f46eda30dea138f0cd6f 3 | folderAsset: yes 4 | timeCreated: 1426854455 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/BinaryReaderExt.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System; 3 | 4 | /* 5 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 6 | * 7 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 8 | * License: MIT 9 | */ 10 | 11 | public static class BinaryReaderExt { 12 | public static int ReadInt32BE(this BinaryReader b) 13 | { 14 | var a32 = b.ReadBytes(4); 15 | Array.Reverse(a32); 16 | return BitConverter.ToInt32(a32,0); 17 | } 18 | 19 | public static Int16 ReadInt16BE(this BinaryReader b) 20 | { 21 | var a16 = b.ReadBytes(2); 22 | Array.Reverse(a16); 23 | return BitConverter.ToInt16(a16, 0); 24 | } 25 | public static Int64 ReadInt64BE(this BinaryReader b) 26 | { 27 | var a64 = b.ReadBytes(8); 28 | Array.Reverse(a64); 29 | return BitConverter.ToInt64(a64, 0); 30 | } 31 | public static UInt32 ReadUInt32BE(this BinaryReader b) 32 | { 33 | var a32 = b.ReadBytes(4); 34 | Array.Reverse(a32); 35 | return BitConverter.ToUInt32(a32, 0); 36 | } 37 | 38 | public static float ReadFloatBE(this BinaryReader b) 39 | { 40 | var a32 = b.ReadBytes(4); 41 | Array.Reverse(a32); 42 | return BitConverter.ToSingle(a32, 0); 43 | } 44 | public static double ReadDoubleBE(this BinaryReader b) 45 | { 46 | var a32 = b.ReadBytes(8); 47 | Array.Reverse(a32); 48 | return BitConverter.ToDouble(a32, 0); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/BinaryReaderExt.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 695abd181eeaf4c9dae870e4e2f96dc7 3 | timeCreated: 1429282096 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/DebugExt.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | /* 4 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 5 | * 6 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 7 | * License: MIT 8 | */ 9 | 10 | public static class DebugExt { 11 | public static void DrawPlane (Plane plane, Vector3 centerPosition, Color color, float duration = 0, float length = 10.0f) 12 | { 13 | #if UNITY_EDITOR 14 | // pick a valid tangent value 15 | Vector3 tangent = Vector3.Dot( plane.normal, Vector3.up) < 0.5f ? Vector3.up : Vector3.left; 16 | 17 | 18 | Vector3 t1 = Vector3.Cross(plane.normal, tangent).normalized; 19 | Vector3 t2 = Vector3.Cross(plane.normal, t1).normalized; 20 | 21 | Vector3 position = centerPosition; 22 | 23 | DebugExt.DrawCross(position, color, length/10, duration); 24 | Debug.DrawLine(position +( t1 + t2)*length, position +( t1 - t2)*length,color,duration); 25 | Debug.DrawLine(position +(- t1 + t2)*length, position +(- t1 - t2)*length,color,duration); 26 | Debug.DrawLine(position +( t1 + t2)*length, position +(- t1 + t2)*length,color,duration); 27 | Debug.DrawLine(position +( t1 - t2)*length, position +(- t1 - t2)*length,color,duration); 28 | #endif 29 | } 30 | 31 | public static void DrawBounds (Bounds bounds, Color color, float duration = 0) 32 | { 33 | #if UNITY_EDITOR 34 | DrawBox(bounds.min, bounds.max, color, duration); 35 | #endif 36 | } 37 | 38 | public static void DrawBox (Vector3 from, Vector3 to, Color color, float duration = 0) 39 | { 40 | #if UNITY_EDITOR 41 | Debug.DrawLine(from, new Vector3(from.x, from.y, to.z),color,duration); 42 | Debug.DrawLine(from, new Vector3(from.x, to.y, from.z),color,duration); 43 | Debug.DrawLine(from, new Vector3(to.x, from.y, from.z),color,duration); 44 | 45 | Debug.DrawLine(new Vector3(to.x, to.y, from.z), to,color,duration); 46 | Debug.DrawLine(new Vector3(to.x, from.y, to.z), to,color,duration); 47 | Debug.DrawLine(new Vector3(from.x, to.y, to.z), to,color,duration); 48 | 49 | Debug.DrawLine(new Vector3(to.x, from.y, from.z), new Vector3(to.x, from.y, to.z),color,duration); 50 | Debug.DrawLine(new Vector3(to.x, from.y, to.z),new Vector3(from.x, from.y, to.z), color,duration); 51 | Debug.DrawLine(new Vector3(from.x, from.y, to.z), new Vector3(from.x, to.y, to.z),color,duration); 52 | 53 | Debug.DrawLine(new Vector3(from.x, to.y, from.z),new Vector3(from.x, to.y, from.z), color,duration); 54 | Debug.DrawLine(new Vector3(from.x, to.y, from.z), new Vector3(to.x, to.y, from.z),color,duration); 55 | Debug.DrawLine(new Vector3(to.x, to.y, from.z),new Vector3(to.x, from.y, from.z), color,duration); 56 | Debug.DrawLine(new Vector3(from.x, to.y, from.z),new Vector3(from.x, to.y, to.z), color,duration); 57 | #endif 58 | } 59 | 60 | public static void DrawCross(Vector3 position, Color color, float size = 1, float duration = 0){ 61 | #if UNITY_EDITOR 62 | Debug.DrawLine(position+Vector3.down*size, position+Vector3.up*size,color,duration); 63 | Debug.DrawLine(position+Vector3.left*size, position+Vector3.right*size,color,duration); 64 | Debug.DrawLine(position+Vector3.forward*size, position+Vector3.back*size,color,duration); 65 | #endif 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/DebugExt.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 13817b6a70f8747b2b00414490e5c6ea 3 | timeCreated: 1426854461 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/Ext.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections.Generic; 3 | 4 | /* 5 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 6 | * 7 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 8 | * License: MIT 9 | */ 10 | 11 | // http://stackoverflow.com/questions/15281616/take-slice-from-2d-array-int-using-linq-in-c-sharp 12 | public static class Ext 13 | { 14 | public static string NameFull(this GameObject go){ 15 | string name = go.name; 16 | while (go.transform.parent) { 17 | go = go.transform.parent.gameObject; 18 | name = go.name + "/" + name; 19 | } 20 | return name; 21 | } 22 | 23 | // warning - will also find prefab objects 24 | public static GameObject[] FindAllObjectsWithTag(string tag){ 25 | List res = new List (); 26 | GameObject[] gameObjects = Resources.FindObjectsOfTypeAll(); 27 | foreach (var go in gameObjects) { 28 | if (go.tag == tag) { 29 | if (!res.Contains (go)) { 30 | res.Add (go); 31 | } 32 | } 33 | } 34 | return res.ToArray (); 35 | } 36 | 37 | // warning - will also find prefab objects 38 | public static GameObject[] FindInactiveObjectsWithTag(string tag){ 39 | List res = new List (); 40 | GameObject[] gameObjects = Resources.FindObjectsOfTypeAll(); 41 | foreach (var go in gameObjects) { 42 | if (go.tag == tag && !go.activeSelf) { 43 | if (!res.Contains (go)) { 44 | res.Add (go); 45 | } 46 | } 47 | } 48 | return res.ToArray (); 49 | } 50 | 51 | public static T[] Slice(this T[] source, int fromIdx, int toIdx) 52 | { 53 | T[] ret = new T[toIdx - fromIdx + 1]; 54 | for(int srcIdx=fromIdx, dstIdx = 0; srcIdx <= toIdx; srcIdx++) 55 | { 56 | ret[dstIdx++] = source[srcIdx]; 57 | } 58 | return ret; 59 | } 60 | public static T[,] Slice(this T[,] source, int fromIdxRank0, int toIdxRank0, int fromIdxRank1, int toIdxRank1) 61 | { 62 | T[,] ret = new T[toIdxRank0 - fromIdxRank0 + 1, toIdxRank1 - fromIdxRank1 + 1]; 63 | 64 | for(int srcIdxRank0=fromIdxRank0, dstIdxRank0 = 0; srcIdxRank0 <= toIdxRank0; srcIdxRank0++, dstIdxRank0++) 65 | { 66 | for(int srcIdxRank1=fromIdxRank1, dstIdxRank1 = 0; srcIdxRank1 <= toIdxRank1; srcIdxRank1++, dstIdxRank1++) 67 | { 68 | ret[dstIdxRank0, dstIdxRank1] = source[srcIdxRank0, srcIdxRank1]; 69 | } 70 | } 71 | return ret; 72 | } 73 | 74 | public static void DestroyAllChildred(this GameObject go){ 75 | var children = new List(); 76 | foreach (Transform child in go.transform) children.Add(child.gameObject); 77 | children.ForEach(child => GameObject.DestroyImmediate(child)); 78 | } 79 | 80 | public static double DotD (Vector3 lhs, Vector3 rhs) 81 | { 82 | return lhs.x * (double)rhs.x + lhs.y * (double)rhs.y + lhs.z * (double)rhs.z; 83 | } 84 | 85 | 86 | public static bool RaycastD (this Plane plane, Ray ray, out double enter) 87 | { 88 | double num = DotD (ray.direction, plane.normal); 89 | double num2 = -DotD (ray.origin, plane.normal) - plane.distance; 90 | if (Mathf.Approximately ((float)num, 0f)) 91 | { 92 | enter = 0f; 93 | return false; 94 | } 95 | enter = num2 / num; 96 | return enter > 0f; 97 | } 98 | 99 | private static System.Random rng = new System.Random(); 100 | 101 | public static void Shuffle(this IList list) 102 | { 103 | int n = list.Count; 104 | while (n > 1) { 105 | n--; 106 | int k = rng.Next(n + 1); 107 | T value = list[k]; 108 | list[k] = list[n]; 109 | list[n] = value; 110 | } 111 | } 112 | } -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/Ext.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 60abf7479d8424a2686b486ad85b6263 3 | timeCreated: 1426858041 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/MeshDebug.cs: -------------------------------------------------------------------------------- 1 | /* 2 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 3 | * 4 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 5 | * License: MIT 6 | */ 7 | 8 | using UnityEngine; 9 | 10 | /// 11 | /// Utility class that let you see normals and tangent vectors for a mesh. 12 | /// This is really useful when debugging mesh appearance. 13 | /// 14 | /// Simply drag this component into the game object containing the MeshFilter 15 | /// that needs to be debugged. 16 | /// 17 | [RequireComponent (typeof (MeshFilter))] 18 | public class MeshDebug : MonoBehaviour { 19 | public bool showNormals = true; 20 | public bool showTangents = true; 21 | public float displayLengthScale = 1.0f; 22 | 23 | public Color normalColor = Color.red; 24 | public Color tangentColor = Color.blue; 25 | 26 | public int maxDebugVertices = 100; 27 | 28 | void OnDrawGizmosSelected() { 29 | MeshFilter meshFilter = GetComponent(); 30 | if (meshFilter==null){ 31 | Debug.LogWarning("Cannot find MeshFilter"); 32 | return; 33 | } 34 | displayLengthScale = Mathf.Max(Mathf.Epsilon,displayLengthScale);// ensure never negative length 35 | Mesh mesh = meshFilter.sharedMesh; 36 | if (mesh==null){ 37 | if (Application.isPlaying){ 38 | mesh = meshFilter.mesh; 39 | } 40 | Debug.LogWarning("Cannot find mesh"); 41 | return; 42 | } 43 | bool doShowNormals = showNormals && mesh.normals.Length==mesh.vertices.Length; 44 | bool doShowTangents = showTangents && mesh.tangents.Length==mesh.vertices.Length; 45 | int maxCount = maxDebugVertices; 46 | foreach (int idx in mesh.triangles){ 47 | maxCount--; 48 | if (maxCount<0){ 49 | break; 50 | } 51 | Vector3 vertex = transform.TransformPoint(mesh.vertices[idx]); 52 | 53 | if (doShowNormals){ 54 | Vector3 normal = transform.TransformDirection(mesh.normals[idx]); 55 | Gizmos.color = normalColor; 56 | Gizmos.DrawLine(vertex, vertex+normal*displayLengthScale); 57 | } 58 | if (doShowTangents){ 59 | Vector3 tangent = transform.TransformDirection(mesh.tangents[idx]); 60 | Gizmos.color = tangentColor; 61 | Gizmos.DrawLine(vertex, vertex+tangent*displayLengthScale); 62 | } 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/MeshDebug.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 675b2a5cd59974f54b4a13a03359bf61 3 | timeCreated: 1428995181 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/Vector2Ext.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | /* 4 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 5 | * 6 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 7 | * License: MIT 8 | */ 9 | 10 | public static class Vector2Ext { 11 | public static Vector2 Rotate(this Vector2 v, float radians) { 12 | float sin = Mathf.Sin(radians); 13 | float cos = Mathf.Cos(radians); 14 | Vector2 res = v; 15 | float tx = res.x; 16 | float ty = res.y; 17 | res.x = (cos * tx) - (sin * ty); 18 | res.y = (sin * tx) + (cos * ty); 19 | return res; 20 | } 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/Vector2Ext.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 14c74d704c8354b598fb709c30e50c7e 3 | timeCreated: 1429214666 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/Vector3Ext.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | /* 4 | * UnityUtils (https://github.com/mortennobel/UnityUtils) 5 | * 6 | * Created by Morten Nobel-Jørgensen ( http://www.nobel-joergnesen.com/ ) 7 | * License: MIT 8 | */ 9 | 10 | public static class Vector3Ext { 11 | // rotate a point arount a line (defined by a pivot point and a direction) 12 | public static Vector3 RotateAround(this Vector3 inputPoint, Vector3 pivotPoint, Vector3 direction, float degrees) { 13 | 14 | if (degrees == 0) 15 | { 16 | return inputPoint; 17 | } 18 | inputPoint = inputPoint - pivotPoint; 19 | 20 | inputPoint = Quaternion.AngleAxis(degrees, direction) * inputPoint; 21 | 22 | inputPoint = inputPoint + pivotPoint; 23 | return inputPoint; 24 | } 25 | 26 | 27 | // Create an orthonormal basis given a normal 28 | // http://jcgt.org/published/0006/01/01/ 29 | public static void BranchlessONB(this Vector3 n, out Vector3 b1, out Vector3 b2) 30 | { 31 | float sign = Mathf.Sign( n.z); 32 | float a = -1.0f / (sign + n.z); 33 | float b = n.x * n.y * a; 34 | b1 = new Vector3(1.0f + sign * n.x * n.x * a, sign * b, -sign * n.x); 35 | b2 = new Vector3(b, sign + n.y * n.y * a, -n.y); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Assets/UnityUtils/extensions/Vector3Ext.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 8fcc64d7ef72346eaaaa4be143140b34 3 | timeCreated: 1429214666 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) 2018 Morten Nobel-Jørgensen 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. -------------------------------------------------------------------------------- /LICENSE - Thirdparty: -------------------------------------------------------------------------------- 1 | Simple JSON 2 | 3 | * The MIT License (MIT) 4 | * 5 | * Copyright (c) 2012-2017 Markus Göbel (Bunny83) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in all 15 | * copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | * SOFTWARE. 24 | 25 | __ 26 | 27 | Priority Queue 28 | 29 | The MIT License (MIT) 30 | 31 | Copyright (c) 2013 Daniel "BlueRaja" Pflughoeft 32 | 33 | Permission is hereby granted, free of charge, to any person obtaining a copy 34 | of this software and associated documentation files (the "Software"), to deal 35 | in the Software without restriction, including without limitation the rights 36 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 37 | copies of the Software, and to permit persons to whom the Software is 38 | furnished to do so, subject to the following conditions: 39 | 40 | The above copyright notice and this permission notice shall be included in 41 | all copies or substantial portions of the Software. 42 | 43 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 44 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 45 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 46 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 47 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 48 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 49 | THE SOFTWARE. 50 | 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Unity Utils 2 | 3 | Collada: 4 | - Simple support for ColladaExport 5 | 6 | Extensions: 7 | - Extension classes Unity classes 8 | 9 | HMesh: 10 | - Halfedge data structure. For procedural mesh generation. 11 | - Experimental 12 | 13 | Math: 14 | - Miscellaneous math functions 15 | 16 | Obj: 17 | - Wavefront OBJ file exporter 18 | 19 | Scene: 20 | - Custom renderes 21 | 22 | SVG: 23 | - Export to SVG 24 | - Experimental 25 | 26 | --------------------------------------------------------------------------------