public GameObject GetGameObject(GeometricObject.Type type, GeometricObject go, CollideType collideType = CollideType.None) { GameObject gao = null; if (type == GeometricObject.Type.Collide) { gao = new GameObject("Element Spheres @ " + Offset); gao.layer = LayerMask.NameToLayer("Collide"); gao.transform.localPosition = Vector3.zero; if (spheres.Value != null) { Vector3[] verts = go.verticesCollide.Value?.GetVectors(go.ScaleFactor); GeometricObjectElementCollideSphereArray.GeometricElementCollideSphere[] sphere = spheres.Value.spheres; for (int i = 0; i < num_spheres; i++) { GameObject sphere_gao = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere_gao.name = "Sphere " + i; sphere_gao.transform.SetParent(gao.transform); MeshRenderer mr = sphere_gao.GetComponent <MeshRenderer>(); sphere_gao.transform.localPosition = verts[sphere[i].centerPoint]; sphere_gao.transform.localScale = Vector3.one * sphere[i].radius * 2; // default Unity sphere radius is 0.5 sphere_gao.layer = LayerMask.NameToLayer("Collide"); BillboardBehaviour bill = sphere_gao.AddComponent <BillboardBehaviour>(); bill.mode = BillboardBehaviour.LookAtMode.CameraPosXYZ; mr.material = MapLoader.Loader.collideMaterial; GameMaterial gmt = spheres.Value.spheres[i].material.Value; if (gmt != null && gmt.collideMaterial.Value != null) { gmt.collideMaterial.Value.SetMaterial(mr); } if (collideType != CollideType.None) { Color col = mr.material.color; mr.material = MapLoader.Loader.collideTransparentMaterial; mr.material.color = new Color(col.r, col.g, col.b, col.a * 0.7f); switch (collideType) { case CollideType.ZDD: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdd")); break; case CollideType.ZDE: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break; case CollideType.ZDM: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break; case CollideType.ZDR: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break; } } } } } return(gao); }
public GameObject GetGameObject(GeometricObject.Type type, GeometricObject go, CollideType collideType = CollideType.None) { GameObject gao = null; if (type == GeometricObject.Type.Collide) { gao = new GameObject("Element Boxes @ " + Offset); gao.layer = LayerMask.NameToLayer("Collide"); gao.transform.localPosition = Vector3.zero; if (boxes.Value != null) { Vector3[] verts = go.verticesCollide.Value?.GetVectors(go.ScaleFactor); GeometricObjectElementCollideAlignedBoxArray.GeometricElementCollideAlignedBox[] b = boxes.Value.boxes; for (int i = 0; i < num_boxes; i++) { GameObject box_gao = GameObject.CreatePrimitive(PrimitiveType.Cube); box_gao.layer = LayerMask.NameToLayer("Collide"); box_gao.name = "Box " + i; box_gao.transform.SetParent(gao.transform); MeshFilter mf = box_gao.GetComponent <MeshFilter>(); MeshRenderer mr = box_gao.GetComponent <MeshRenderer>(); Vector3 center = Vector3.Lerp(verts[b[i].minVertex], verts[b[i].maxVertex], 0.5f); box_gao.transform.localPosition = center; box_gao.transform.localScale = verts[b[i].maxVertex] - verts[b[i].minVertex]; mr.material = MapLoader.Loader.collideMaterial; GameMaterial gmt = boxes.Value.boxes[i].material.Value; if (gmt != null && gmt.collideMaterial.Value != null) { gmt.collideMaterial.Value.SetMaterial(mr); } if (collideType != CollideType.None) { Color col = mr.material.color; mr.material = MapLoader.Loader.collideTransparentMaterial; mr.material.color = new Color(col.r, col.g, col.b, col.a * 0.7f); switch (collideType) { case CollideType.ZDD: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdd")); break; case CollideType.ZDE: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break; case CollideType.ZDM: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break; case CollideType.ZDR: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break; } } } } } return(gao); }
public GameObject GetGameObject(GeometricObject.Type type, GeometricObject go, CollideType collideType = CollideType.None) { GameObject gao = null; if (type == GeometricObject.Type.Collide) { gao = new GameObject("Element @ " + Offset); gao.layer = LayerMask.NameToLayer("Collide"); gao.transform.localPosition = Vector3.zero; MeshRenderer mr = gao.AddComponent <MeshRenderer>(); MeshFilter mf = gao.AddComponent <MeshFilter>(); mr.material = MapLoader.Loader.collideMaterial; if (material.Value != null && material.Value is GameMaterial) { GameMaterial gmt = material.Value as GameMaterial; //MapLoader.Loader.print(gmt.collideMaterial); if (gmt.collideMaterial.Value != null) { gmt.collideMaterial.Value.SetMaterial(mr); } } else { MapLoader.Loader.print("Type: " + type_material + " - Ind: " + ind_material); } if (collideType != CollideType.None) { Color col = mr.material.color; mr.material = new Material(MapLoader.Loader.collideTransparentMaterial); mr.material.color = new Color(col.r, col.g, col.b, col.a * 0.7f); switch (collideType) { case CollideType.ZDD: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdd")); break; case CollideType.ZDE: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break; case CollideType.ZDM: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break; case CollideType.ZDR: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break; } } Vector3[] vertices = go.verticesCollide.Value.GetVectors(go.ScaleFactor); int[] tris = triangles.Value.triangles.SelectMany(t => new int[] { t.v2, t.v1, t.v3 }).ToArray(); Mesh mesh = new Mesh(); mesh.vertices = tris.Select(t => vertices[t]).ToArray(); //mesh.normals = go.normals.Value.GetVectors(Int16.MaxValue); //mesh.SetUVs(0, triangles.Value.uvs.Select(u => new Vector3(u.x, u.y, 1f)).ToList()); mesh.triangles = Enumerable.Range(0, tris.Length).ToArray(); mesh.RecalculateNormals(); Vector2[] uvs = new Vector2[mesh.vertexCount]; // Generate simple UVs for collision checkerboard (basically a box projection) Vector3[] normals = null; { List <Vector3> norm = new List <Vector3>(); mesh.GetNormals(norm); normals = norm.ToArray(); // Norm can be GC'd after these curly braces } for (int j = 0; j < uvs.Length; j++) { Vector3 normal = normals[j]; normal = new Vector3(Mathf.Abs(normal.x), Mathf.Abs(normal.y), Mathf.Abs(normal.z)); float biggestNorm = Mathf.Max(normal.x, normal.y, normal.z); float uvX = (mesh.vertices[j].x / 20.0f); float uvY = (mesh.vertices[j].y / 20.0f); float uvZ = (mesh.vertices[j].z / 20.0f); //Debug.Log("Norms: " + normal.x+","+normal.y+","+normal.z); //Debug.Log("Biggest norm: " + biggestNorm); if (biggestNorm == Mathf.Abs(normal.x)) { uvs[j] = new Vector2(uvY, uvZ); } else if (biggestNorm == Mathf.Abs(normal.y)) { uvs[j] = new Vector2(uvX, uvZ); } else if (biggestNorm == Mathf.Abs(normal.z)) { uvs[j] = new Vector2(uvX, uvY); } else { Debug.LogError("HALP"); } } mesh.uv = uvs; mf.mesh = mesh; try { MeshCollider mc = gao.AddComponent <MeshCollider>(); //mc.cookingOptions = MeshColliderCookingOptions.None; //mc.sharedMesh = mf.sharedMesh; } catch (Exception) { } CollideComponent cc = gao.AddComponent <CollideComponent>(); cc.collideROM = this; cc.type = collideType; } return(gao); }