private IEnumerator CreateSplineFittedMeshesCoroutine(GameObject mazeObjects, MazeFrameSplines mazeFrameSplines, GameObject prefabInst, int objPerCurveSegment, List <Color> colorList) { MazeObjectsPlaced = 0; // First, setup the shared materials that will be modified. Dictionary <int, Material> materials = new Dictionary <int, Material>(); foreach (MazeFrameSplines.SplineSegment seg in mazeFrameSplines.SplineSegments) { if (seg.shortestPathInd.Count == 1) { if (materials.ContainsKey(seg.shortestPathInd[0])) { continue; } Material mat = Object.Instantiate(prefabInst.GetComponent <MeshRenderer>().material); mat.SetColor("_EmissionColor", GetColorBasedOnPathIndex(seg.shortestPathInd, ref colorList)); materials.Add(seg.shortestPathInd[0], mat); } } Material materialBlack = Object.Instantiate(prefabInst.GetComponent <MeshRenderer>().material); materialBlack.SetColor("_EmissionColor", Color.black); //List<Color> colorList = new List<Color>(); //Dictionary<List<int>, Material> materials = new Dictionary<List<int>, Material>(); //foreach (MazeFrameSplines.SplineSegment seg in mazeFrameSplines.SplineSegments) //{ // if (materials.ContainsListKeyWithContents(seg.shortestPathInd)) { continue; } // Material mat = Object.Instantiate(prefabInst.GetComponent<MeshRenderer>().material); // mat.SetColor("_EmissionColor", GetColorBasedOnPathIndex(seg.shortestPathInd, ref colorList)); // materials.Add(seg.shortestPathInd, mat); //} // Per spline, create gameobject holding the mesh //Dictionary<string, GameObject> processedPrefabInstances = new Dictionary<string, GameObject>(); int count = 0; foreach (MazeFrameSplines.SplineSegment segment in mazeFrameSplines.SplineSegments) { GameObject obj = Object.Instantiate(prefabInst, mazeObjects.transform) as GameObject; string junctionAffix = ""; if (segment.isJunction) { junctionAffix = "junction_" + segment.JunctionID + "_"; } obj.name = junctionAffix + "spline_" + segment.spline.startPoint.ToString() + "_" + segment.spline.endPoint.ToString(); obj.layer = 10; // Attached SplineFittedMesh script and initilaze with prefab obj.AddComponent(typeof(SplineFittedMesh)); SplineFittedMesh splineMesh = obj.GetComponent <SplineFittedMesh>(); splineMesh.SetSplineAxis(Vector3.forward); splineMesh.SetSpline(segment.spline); splineMesh.SetNMeshesPerSegment(objPerCurveSegment); Mesh[] meshes; if (mazeFrameSplines.ShortestPathInd.Count > 1) { meshes = new Mesh[] { Instantiate(obj.GetComponent <MeshFilter>().mesh), Instantiate(obj.GetComponent <MeshFilter>().mesh) }; } else { meshes = new Mesh[] { Instantiate(obj.GetComponent <MeshFilter>().mesh) }; } splineMesh.SetMeshBase(meshes); // Set segment color to first color if only one path, or determine multiple colors below if (mazeFrameSplines.ShortestPathInd.Count == 1) { obj.GetComponent <MeshRenderer>().sharedMaterial = materials[0]; } else { // Determine segment color based on start/end neighbors if segment color itself is ambigious Material[] mats = new Material[2]; if (segment.shortestPathInd.Count == 1) { mats[0] = materials[segment.shortestPathInd[0]]; mats[1] = materials[segment.shortestPathInd[0]]; } else { // Start if (segment.shortestPathIndAtStart.Count == 1) { mats[0] = materials[segment.shortestPathIndAtStart[0]]; } else { if (segment.shortestPathIndAtEnd.Count == 1) { mats[0] = materials[segment.shortestPathIndAtEnd[0]]; } else { mats[0] = materialBlack; } } // End if (segment.shortestPathIndAtEnd.Count == 1) { mats[1] = materials[segment.shortestPathIndAtEnd[0]]; } else { if (segment.shortestPathIndAtStart.Count == 1) { mats[1] = materials[segment.shortestPathIndAtStart[0]]; } else { mats[1] = materialBlack; } } } if (mats[0] == materialBlack && mats[1] == materialBlack && segment.shortestPathIndAtStart.EqualContents(segment.shortestPathIndAtEnd) && segment.shortestPathIndAtStart.Count == 2 && segment.shortestPathIndAtEnd.Count == 2) { mats[0] = materials[segment.shortestPathInd[0]]; mats[1] = materials[segment.shortestPathInd[1]]; } // Assign to object obj.GetComponent <MeshRenderer>().sharedMaterials = mats; } // Increment global counter MazeObjectsPlaced++; // Yield count++; if ((count % 200) == 0) { yield return(null); } } // Destroy processed prefab instances Object.Destroy(prefabInst); }
void Update() { List <Vector3> pList = new List <Vector3>(); for (int i = 0; i < GetComponent <Transform>().childCount; i++) { if (GetComponent <Transform>().GetChild(i).gameObject.activeSelf) { pList.Add(GetComponent <Transform>().GetChild(i).position); } } Vector3[] pArray = pList.ToArray(); Spline spline = new Spline(pArray); spline.SetRMFStartNormal(Vector3.up); spline.SetRMFEndNormal(Vector3.up); spline.ComputeRotationMinimizingFrames(); int nSample = 100; Vector3 pOut; Vector3 prevpOut = pArray[0]; for (int i = 0; i < nSample + 1; i++) { float t = ((1f / nSample) * i); pOut = spline.GetPointOnSpline(t); Debug.DrawLine(prevpOut, pOut, Color.yellow); prevpOut = pOut; Vector3 tOut = spline.GetTangentToPointOnSpline(t).normalized; Debug.DrawRay(pOut, tOut, Color.cyan); Vector3 nOut = spline.DefaultGetNormalAtT(t); Debug.DrawRay(pOut, nOut * 2, Color.red); } //Debug.DrawLine(spline.GetPointOnSpline(0.84f), Vector3.zero, Color.red); //Debug.DrawLine(spline.GetPointOnSpline(0.8f), Vector3.zero, Color.red); count++; if (count < 2) { string meshName = "mesh-" + gameObject.name; //GameObject prefabCyl = Resources.Load("Prefabs/" + "test/testz") as GameObject; GameObject prefabCyl = Resources.Load("Prefabs/MazeBar-20-X/MazeBar-20-1") as GameObject; if (GameObject.Find(meshName) != null) { Destroy(GameObject.Find(meshName)); } obj = Instantiate(prefabCyl, GameObject.Find("holderobj").transform) as GameObject; obj.name = meshName; Utilities.MeshResize(obj, new Vector3(2, .2f, 1)); //Utilities.MeshReplicate(obj, 1, Vector3.forward); //obj.transform.rotation = Quaternion.Euler(0, 90, 0); obj.AddComponent(typeof(SplineFittedMesh)); SplineFittedMesh sfMesh = obj.GetComponent <SplineFittedMesh>(); //sfMesh.SetSplineAxis(new Vector3(0, 0, 1f)); sfMesh.SetSplineAxis(new Vector3(0, 0, 1)); sfMesh.SetSpline(spline); Mesh mesh = obj.GetComponent <MeshFilter>().mesh; sfMesh.SetMeshBase(mesh); } }
private IEnumerator CheckLevelBuiltStatus() { LevelIsPresent = false; LevelBuiltProgressPercentage = 0; while (!mazeFrameMeshesArePresent) { yield return(null); } // Fetch spline fitted meshes from maze objects List <SplineFittedMesh> splineFittedMeshes = new List <SplineFittedMesh>(mazeObjects.transform.childCount); foreach (Transform child in mazeObjects.transform) { SplineFittedMesh childSFM = child.gameObject.GetComponent <SplineFittedMesh>(); if (childSFM != null) { splineFittedMeshes.Add(childSFM); } } // Get progress int currBuilt = 0; while (currBuilt < splineFittedMeshes.Count) { currBuilt = 0; foreach (SplineFittedMesh sfm in splineFittedMeshes) { if (sfm.FitFinished) { currBuilt++; } } LevelBuiltProgressPercentage = 0.5f + (currBuilt / (float)splineFittedMeshes.Count / 2); yield return(null); } // Finished! //// Combine junctions //Dictionary<string, List<GameObject>> junctionObjects = new Dictionary<string, List<GameObject>>(); //foreach (Transform child in mazeObjects.transform) //{ // // Parse name // string[] identifier = child.name.Split(new char[] { '_' }, System.StringSplitOptions.RemoveEmptyEntries); // if (identifier[0] == "junction") // { // if (!junctionObjects.ContainsKey(identifier[1])) { junctionObjects.Add(identifier[1], new List<GameObject>()); } // junctionObjects[identifier[1]].Add(child.gameObject); // } //} //foreach (string junction in junctionObjects.Keys) //{ // GameObject junctionGO = new GameObject("spline-junction-" + junction); // junctionGO.layer = 10; // junctionGO.transform.parent = mazeObjects.transform; // junctionGO.AddComponent<MeshFilter>(); // junctionGO.AddComponent<MeshRenderer>(); // junctionGO.GetComponent<MeshRenderer>().material = junctionObjects[junction][0].GetComponent<MeshRenderer>().sharedMaterial; // for (int i = 0; i < junctionObjects[junction].Count; i++) // { // junctionObjects[junction][i].transform.parent = junctionGO.transform; // // Work around for "z-fighting"... // //junctionObjects[junction][i].transform.position += Vector3.one * 0.001f * i; // } // Utilities.MergeChildrenOfParent(junctionGO, true, true, true); //} // add all sfm' mesh colliders... int count = 0; foreach (Transform child in mazeObjects.transform) { child.gameObject.AddComponent <MeshCollider>(); count++; if ((count % 200) == 0) { yield return(null); } } LevelIsPresent = true; }