protected virtual void RealizeMesh(MeshDetail meshDetail) { if (string.IsNullOrEmpty(meshDetail.MeshPath)) { return; } gameObject.transform.localScale = meshDetail.ObjectScale; GameObject meshObject = Resources.Load <GameObject>(meshDetail.MeshPath); if (meshObject == null) { throw new ApplicationException("Could not find a mesh object at path " + meshDetail.MeshPath); } GameObject instance = (GameObject)Instantiate(meshObject, transform.position, transform.rotation); instance.name = "Mesh"; instance.transform.position = transform.position + meshDetail.MeshOffset; instance.transform.localScale = meshDetail.MeshScale; instance.transform.parent = gameObject.transform; Animator animator = instance.GetComponent <Animator>(); if (animator == null) { return; } RuntimeAnimatorController controller = Resources.Load <RuntimeAnimatorController>(meshDetail.AnimationControllerPath); animator.runtimeAnimatorController = controller; }
protected virtual void RealizeMeshRenderer(MeshDetail meshDetail) { if (string.IsNullOrEmpty(meshDetail.MeshPath)) { return; } Renderer renderer = GetRendererInChildren(); if (renderer == null) { throw new InvalidOperationException("Could not find a MeshRenderer in any children of game object " + gameObject.name); } if (meshDetail.MaterialDetails.IsNullOrEmpty()) { return; } List <Material> materials = new List <Material>(); for (int i = 0; i < meshDetail.MaterialDetails.Count; i++) { MaterialDetail current = meshDetail.MaterialDetails[i]; Material newMaterial = ParseMaterial(current); materials.Add(newMaterial); } renderer.materials = materials.ToArray(); }
protected virtual void RealizeMeshCollider(MeshDetail meshDetail) { if (string.IsNullOrEmpty(meshDetail.MeshPath)) { return; } Mesh mesh = Resources.Load <Mesh>(meshDetail.MeshPath); if (mesh == null) { throw new ApplicationException("Could not find a mesh object at path " + meshDetail.MeshPath); } MeshCollider collider = GetComponentInChildren <MeshCollider>(); if (collider == null) { throw new ApplicationException("Could not find a MeshCollider in any children of game object " + gameObject.name); } Mesh instance = Instantiate(mesh); collider.sharedMesh = instance; }
protected virtual GameObject RealizeMeshDetails(MeshDetail meshDetail) { GameObject result = RealizeMesh(meshDetail); RealizeMeshRenderer(meshDetail); return(result); }
protected virtual void RealizeMeshCollider(MeshDetail meshDetail, GameObject target = null) { if (string.IsNullOrEmpty(meshDetail.MeshPath)) { return; } Mesh mesh = Resources.Load <Mesh>(meshDetail.MeshPath); if (mesh == null) { throw new ApplicationException("Could not find a mesh object at path " + meshDetail.MeshPath); } MeshCollider collider = GetMeshCollider(target); Mesh instance = Instantiate(mesh); collider.sharedMesh = instance; }
public override void ShowChildDetail(ObjectDetail detail) { MeshDetail md = detail as MeshDetail; if (md.showSubMesh) { foreach (var child in md.subMeshList) { GUILayout.BeginHorizontal(); GUILayout.Space(170); if (GUILayout.Button(child.name, GUILayout.Width(245))) { SelectObject(child.meshObject); } string vertexCount = "" + child.vertexCount; GUILayout.Label(vertexCount, GUILayout.Width(80)); string tranCount = "" + child.tranCount; GUILayout.Label(tranCount, GUILayout.Width(80)); GUILayout.Label(child.format, GUILayout.Width(200)); GUILayout.EndHorizontal(); } } }
public override void AddObjectDetail(Object obj, Object refObj, Object detailRefObj) { Mesh mesh = obj as Mesh; if (mesh == null) { return; } MeshDetail detail = null; foreach (var checker in CheckList) { if (checker.assetPath == AssetDatabase.GetAssetPath(obj)) { detail = checker as MeshDetail; } } if (detail == null) { detail = new MeshDetail(obj, this); } detail.AddSubMesh(mesh, this); detail.AddObjectReference(refObj, detailRefObj); }
protected virtual void RealizeMeshDetails(MeshDetail meshDetail) { RealizeMesh(meshDetail); RealizeMeshRenderer(meshDetail); }
private void OnButtonSubMeshCountClick(ObjectDetail detail) { MeshDetail md = detail as MeshDetail; md.showSubMesh = !md.showSubMesh; }
private void GenerateMesh(float tileW, MeshFilter meshFilter, MeshDetail meshDetail, float localScale) { int width; int height; float spacing; if (meshDetail == MeshDetail.HIGH) { width = height = 8; } else if (meshDetail == MeshDetail.MED) { width = height = 8; } else { // low detail width = height = 8; } spacing = (tileW / (float)width) / localScale; Vector3[] verts = new Vector3[(width + 1) * (height + 1)]; Vector3[] norms = new Vector3[(width + 1) * (height + 1)]; Vector2[] uvs = new Vector2[(width + 1) * (height + 1)]; int[] tris = new int[3 * 2 * (width * height)]; // 3 points per triangle, 2 triangles per quad, width * height quads int i = 0; for (float x = 0; x < width + 1; x++) { for (int z = 0; z < height + 1; z++) { //default case verts[i].x = (x - width / 2.0f) * spacing; verts[i].y = 0; verts[i].z = (z - height / 2.0f) * spacing; norms[i] = new Vector3(0.0f, 1.0f, 0.0f); uvs[i].x = x / (float)width; uvs[i].y = z / (float)height; i++; } } int t = 0; for (int n = 0; n < verts.Length; n++) { if (n % (width + 1) == width) { continue; } if (n / (height + 1) == height) { continue; } tris[t] = n; tris[t + 1] = n + 1; tris[t + 2] = n + width + 1; tris[t + 3] = n + 1; tris[t + 4] = n + width + 2; tris[t + 5] = n + width + 1; t += 6; } Mesh mesh = new Mesh(); mesh.vertices = verts; mesh.normals = norms; mesh.uv = uvs; mesh.triangles = tris; mesh.name = "not null"; meshFilter.mesh = mesh; // spacing = (tileW / (float)width) / localScale; // // Vector3[] verts = new Vector3[(width + 3) * (height + 3)]; // Vector3[] norms = new Vector3[(width + 3) * (height + 3)]; // Vector2[] uvs = new Vector2[(width + 3) * (height + 3)]; // int[] tris = new int[3 * 2 * ((width + 1) * (height + 1))]; // 3 points per triangle, 2 triangles per quad, width * height quads // // int i = 0; // for (float x = 0; x < width + 3; x++) // { // for (int z = 0; z < height + 3; z++) // { // /* // * mesh of size 32x32 has verts of 33x33 // * make mesh with verts 35x35 // * shift up and to the left one unit // * if vert is in first row, move down one extra unit // * if vert is in last row, move up one extra unit // * if vert is first of the row, move right one unit // * if vert is last of the row, move left one unit // */ // // //default case // verts[i].x = (x - width / 2.0f) * spacing - spacing; // verts[i].y = 0; // verts[i].z = (z - height / 2.0f) * spacing + spacing; // // if (z == 0) //first row, move down one unit // { // verts[i].x = (x - width / 2.0f) * spacing - spacing; // verts[i].y = 0; // verts[i].z = (z - height / 2.0f) * spacing; // } // if (z == height + 2) //last row, move up one unit // { // verts[i].x = (x - width / 2.0f) * spacing - spacing; // verts[i].y = 0; // verts[i].z = (z - height / 2.0f) * spacing + 2 * spacing; // } // if (x == 0) //first of the row, move right one unit // { // verts[i].x = (x - width / 2.0f) * spacing; // verts[i].y = 0; // verts[i].z = (z - height / 2.0f) * spacing + spacing; // } // if (x == width + 2) //last of the row, move left one unit // { // verts[i].x = (x - width / 2.0f) * spacing - 2 * spacing; // verts[i].y = 0; // verts[i].z = (z - height / 2.0f) * spacing + spacing; // } // norms[i] = new Vector3(0.0f,1.0f,0.0f); // uvs[i].x = x / (float)width; // uvs[i].y = z / (float)height; // // i++; // } // } // int t = 0; // for (int n = 0; n < verts.Length; n++) // { // if (n % (width + 3) == width + 2) // continue; // if (n / (height + 3) == height + 2) // continue; // tris[t ] = n; // tris[t+1] = n + 1; // tris[t+2] = n + width + 1; // tris[t+3] = n + 1; // tris[t+4] = n + width + 2; // tris[t+5] = n + width + 1; // t += 6; // } // Mesh mesh = new Mesh (); // mesh.vertices = verts; // mesh.normals = norms; // mesh.uv = uvs; // mesh.triangles = tris; // mesh.name = "not null"; // meshFilter.mesh = mesh; }
public void Regenerate(float tileW, MeshFilter meshFilter, MeshDetail meshDetail, float localScale) { GenerateMesh(tileW, meshFilter, meshDetail, localScale); StartCoroutine(SetHeights(meshFilter, localScale)); }