public GameObject GetGameObject(GameObject parent, int index, CollideType collideType = CollideType.None) { GameObject gao = GameObject.CreatePrimitive(PrimitiveType.Sphere); gao.name = "Sphere " + index; gao.transform.SetParent(parent.transform); MeshRenderer mr = gao.GetComponent <MeshRenderer>(); gao.transform.localPosition = new Vector3(x / 256f, z / 256f, y / 256f); gao.transform.localScale = Vector3.one * (radius / 256f) * 2; // default Unity sphere radius is 0.5 gao.layer = LayerMask.NameToLayer("Collide"); BillboardBehaviour bill = gao.AddComponent <BillboardBehaviour>(); bill.mode = BillboardBehaviour.LookAtMode.CameraPosXYZ; GameMaterial gm = (Load as R2PS1Loader).levelHeader.gameMaterials?[gameMaterial]; CollideMaterial cm = gm?.collideMaterial; if (cm != null) { mr.material = cm.CreateMaterial(); } else { mr.material = new Material(MapLoader.Loader.collideMaterial); } 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.ZDM: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break; case CollideType.ZDE: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break; case CollideType.ZDR: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break; } } CollideComponent cc = gao.AddComponent <CollideComponent>(); cc.collidePS1 = this; cc.type = collideType; return(gao); }
public GameObject GetGameObject(GameObject parent, int index, CollideType collideType = CollideType.None) { GameObject gao = GameObject.CreatePrimitive(PrimitiveType.Cube); gao.name = "Box " + index + " - " + Offset; gao.transform.SetParent(parent.transform); MeshRenderer mr = gao.GetComponent <MeshRenderer>(); Vector3 minVertex = new Vector3(x0, z0, y0) / R2PS1Loader.CoordinateFactor; Vector3 maxVertex = new Vector3(x1, z1, y1) / R2PS1Loader.CoordinateFactor; Vector3 center = Vector3.Lerp(minVertex, maxVertex, 0.5f); gao.transform.localPosition = center; gao.transform.localScale = maxVertex - minVertex; gao.layer = LayerMask.NameToLayer("Collide"); GameMaterial gm = (Load as R2PS1Loader).levelHeader.gameMaterials?[gameMaterial]; CollideMaterial cm = gm?.collideMaterial; if (cm != null) { mr.material = cm.CreateMaterial(); } else { mr.material = new Material(MapLoader.Loader.collideMaterial); } 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.ZDM: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdm")); break; case CollideType.ZDE: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zde")); break; case CollideType.ZDR: mr.material.SetTexture("_MainTex", Resources.Load <Texture2D>("Textures/zdr")); break; } } CollideComponent cc = gao.AddComponent <CollideComponent>(); cc.collidePS1 = this; cc.type = collideType; return(gao); }
public GameObject GetGameObject(CollideType collideType = CollideType.None) { GameObject gao = new GameObject("ZdxEntry @ " + Offset); gao.layer = LayerMask.NameToLayer("Collide"); if (spheres != null) { for (int i = 0; i < spheres.Length; i++) { spheres[i].GetGameObject(gao, i, collideType: collideType); } } if (boxes != null) { for (int i = 0; i < boxes.Length; i++) { boxes[i].GetGameObject(gao, i, collideType: collideType); } } 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]; CollideComponent cc = box_gao.AddComponent <CollideComponent>(); cc.collideROM = this; cc.type = collideType; cc.index = i; 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 = 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; } } } } } 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 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; CollideComponent cc = sphere_gao.AddComponent <CollideComponent>(); cc.collideROM = this; cc.type = collideType; cc.index = i; 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 = 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; } } } } } return(gao); }
public void UpdateViewCollision(bool viewCollision) { if (perso.collSet != null) { CollSet c = perso.collSet; if (collSetObjects == null) { collSetObjects = new Dictionary <CollideType, GameObject[]>(); foreach (KeyValuePair <CollideType, ZdxList> zdxListKV in c.zdxList) { CollideType type = zdxListKV.Key; ZdxList zdxList = zdxListKV.Value; if (zdxList == null) { continue; } collSetObjects[type] = new GameObject[zdxList.entries?.Length ?? 0]; for (int i = 0; i < zdxList.entries?.Length; i++) { ZdxEntry zdx = zdxList.entries[i]; collSetObjects[type][i] = zdx.GetGameObject(type); collSetObjects[type][i].transform.SetParent(transform); collSetObjects[type][i].transform.localPosition = Vector3.zero; collSetObjects[type][i].transform.localRotation = Quaternion.identity; collSetObjects[type][i].transform.localScale = Vector3.one; } } } foreach (KeyValuePair <CollideType, GameObject[]> entry in collSetObjects) { if (entry.Value != null && entry.Value.Length > 0) { foreach (GameObject gao in entry.Value) { if (gao != null) { gao.SetActive(false); } } } } if (viewCollision) { if (state != null && c.activationList != null) { foreach (KeyValuePair <CollideType, short> activationZone in state.zoneZdx) { CollideType type = activationZone.Key; short cur_activation = activationZone.Value; if (cur_activation == -1) { continue; } if (!collSetObjects.ContainsKey(type) || collSetObjects[type] == null) { continue; } if (cur_activation < 0 || cur_activation >= c.activationList.num_activationZones) { continue; } if (c.activationList.activationZones[cur_activation].num_activations == 0) { continue; } for (int i = 0; i < c.activationList.activationZones[cur_activation].activations.Length; i++) { uint ind_zdx = c.activationList.activationZones[cur_activation].activations[i]; if (collSetObjects[type].Length <= ind_zdx) { continue; } GameObject gao = collSetObjects[type][ind_zdx]; if (gao == null) { continue; } gao.SetActive(true); } } } } } }
public GameObject GetGameObject(Type type, CollideType collideType = CollideType.None) { GameObject gao = new GameObject("GeometricObject @ " + Offset); if (type == Type.Visual) { gao.layer = LayerMask.NameToLayer("Visual"); } else { gao.layer = LayerMask.NameToLayer("Collide"); } gao.name = gao.name + " - S0:" + scaleFactor + " - S1:" + factor_1 + " - U0:" + string.Format("{0:X4}", unk0) + " - U1:" + string.Format("{0:X4}", unk1) + " - U2:" + string.Format("{0:X4}", hasVertexColors) + " - U3:" + string.Format("{0:X4}", lookAtMode); MapLoader l = MapLoader.Loader; if (type == Type.Visual) { if (elementsVisual.Value != null) { // First, reset vertex buffer if (Settings.s.platform == Settings.Platform.N64) { foreach (GeometricObjectElementVisualList.GeometricElementListEntry entry in elementsVisual.Value.elements) { if (entry.element.Value is GeometricObjectElementTriangles) { GeometricObjectElementTriangles el = entry.element.Value as GeometricObjectElementTriangles; el.ResetVertexBuffer(); } } } //gao.transform.position = new Vector3(UnityEngine.Random.Range(-100f, 100f), UnityEngine.Random.Range(-100f, 100f), UnityEngine.Random.Range(-100f, 100f)); foreach (GeometricObjectElementVisualList.GeometricElementListEntry entry in elementsVisual.Value.elements) { /*if (entry.element.Value == null) { * l.print("Visual element null: " + entry.element.type); * }*/ if (entry.element.Value != null) { GameObject child = null; if (entry.element.Value is GeometricObjectElementTriangles) { GeometricObjectElementTriangles el = entry.element.Value as GeometricObjectElementTriangles; child = el.GetGameObject(type, this); } else if (entry.element.Value is GeometricObjectElementSprites) { GeometricObjectElementSprites el = entry.element.Value as GeometricObjectElementSprites; child = el.GetGameObject(type, this); } if (child != null) { child.transform.SetParent(gao.transform); child.transform.localPosition = Vector3.zero; } } } } if (lookAtMode != 0) { BillboardBehaviour billboard = gao.AddComponent <BillboardBehaviour>(); billboard.mode = (BillboardBehaviour.LookAtMode)lookAtMode; } } else { if (elementsCollide.Value != null) { foreach (GeometricObjectElementCollideList.GeometricElementListEntry entry in elementsCollide.Value.elements) { if (entry.element.Value == null) { l.print("Collide element null: " + entry.element.type); } if (entry.element.Value != null) { GameObject child = null; if (entry.element.Value is GeometricObjectElementCollideTriangles) { GeometricObjectElementCollideTriangles el = entry.element.Value as GeometricObjectElementCollideTriangles; child = el.GetGameObject(type, this, collideType: collideType); } else if (entry.element.Value is GeometricObjectElementCollideSpheres) { GeometricObjectElementCollideSpheres el = entry.element.Value as GeometricObjectElementCollideSpheres; child = el.GetGameObject(type, this, collideType: collideType); } else if (entry.element.Value is GeometricObjectElementCollideAlignedBoxes) { GeometricObjectElementCollideAlignedBoxes el = entry.element.Value as GeometricObjectElementCollideAlignedBoxes; child = el.GetGameObject(type, this, collideType: collideType); } if (child != null) { child.transform.SetParent(gao.transform); child.transform.localPosition = Vector3.zero; } } } } } 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); }