public void OnSceneGUI() { AttachedPathScript pathScript = (AttachedPathScript)target as AttachedPathScript; Event currentEvent = Event.current; if (pathScript.addNodeMode == true) { // If P is pressed, than create a node at selected point (window has to have focus) if (currentEvent.isKey && currentEvent.character == 'p') { Vector3 pathNode = GetTerrainCollisionInEditor(currentEvent); Debug.Log(currentEvent); TerrainPathCell pathNodeCell = new TerrainPathCell(); Debug.Log(pathNode); pathNodeCell.position.x = pathNode.x; pathNodeCell.position.y = pathNode.z; pathNodeCell.heightAtCell = pathNode.y; pathScript.CreatePathNode(pathNodeCell); pathScript.addNodeMode = false; } } if (pathScript.nodeObjects != null && pathScript.nodeObjects.Length != 0) { int n = pathScript.nodeObjects.Length; for (int i = 0; i < n; i++) { PathNodeObjects node = pathScript.nodeObjects[i]; node.position = Handles.PositionHandle(node.position, Quaternion.identity); } } if (GUI.changed) { EditorUtility.SetDirty(pathScript); if ((pathScript.pathFlat || pathScript.isRoad) && (pathScript.isFinalized == false)) { pathScript.CreatePath(pathScript.pathSmooth, true, false); } else if (!pathScript.pathFlat && !pathScript.isRoad) { pathScript.CreatePath(pathScript.pathSmooth, false, false); } else if (pathScript.isFinalized) { pathScript.CreatePath(pathScript.pathSmooth, true, true); } } }
public void CreatePathNode(TerrainPathCell nodeCell) { Vector3 pathPosition = new Vector3((nodeCell.position.x / terData.heightmapResolution) * terData.size.x, nodeCell.heightAtCell * terData.size.y, (nodeCell.position.y / terData.heightmapResolution) * terData.size.z); AddNode(pathPosition, pathWidth); if (pathFlat || isRoad) { CreatePath(pathSmooth, true, false); } else { CreatePath(pathSmooth, false, false); } }
public void OnSceneGUI() { AttachedPathScript pathScript = (AttachedPathScript) target as AttachedPathScript; Event currentEvent = Event.current; if(pathScript.addNodeMode == true) { // If P is pressed, than create a node at selected point (window has to have focus) if(currentEvent.isKey && currentEvent.character == 'p') { Vector3 pathNode = GetTerrainCollisionInEditor(currentEvent); TerrainPathCell pathNodeCell = new TerrainPathCell(); pathNodeCell.position.x = pathNode.x; pathNodeCell.position.y = pathNode.z; pathNodeCell.heightAtCell = pathNode.y; pathScript.CreatePathNode(pathNodeCell); pathScript.addNodeMode = false; } } if (pathScript.nodeObjects != null && pathScript.nodeObjects.Length != 0) { int n = pathScript.nodeObjects.Length; for (int i = 0; i < n; i++) { PathNodeObjects node = pathScript.nodeObjects[i]; node.position = Handles.PositionHandle(node.position, Quaternion.identity); } } if (GUI.changed) { EditorUtility.SetDirty(pathScript); if((pathScript.pathFlat || pathScript.isRoad) && (pathScript.isFinalized == false)) pathScript.CreatePath(pathScript.pathSmooth, true, false); else if(!pathScript.pathFlat && !pathScript.isRoad) pathScript.CreatePath(pathScript.pathSmooth, false, false); else if(pathScript.isFinalized) pathScript.CreatePath(pathScript.pathSmooth, true, true); } }
public void CreatePath(int smoothingLevel, bool flatten, bool road) { MeshFilter meshFilter = (MeshFilter)pathMesh.GetComponent(typeof(MeshFilter)); if (meshFilter == null) { return; } Mesh newMesh = meshFilter.sharedMesh; terrainHeights = terData.GetHeights(0, 0, terData.heightmapResolution, terData.heightmapResolution); pathCells = new ArrayList(); if (newMesh == null) { newMesh = new Mesh(); newMesh.name = "Generated Path Mesh"; meshFilter.sharedMesh = newMesh; } else { newMesh.Clear(); } if (nodeObjects == null || nodeObjects.Length < 2) { return; } int n = nodeObjects.Length; int verticesPerNode = 2 * (smoothingLevel + 1) * 2; int trianglesPerNode = 6 * (smoothingLevel + 1); Vector2[] uvs = new Vector2[(verticesPerNode * (n - 1))]; Vector3[] newVertices = new Vector3[(verticesPerNode * (n - 1))]; int[] newTriangles = new int[(trianglesPerNode * (n - 1))]; nodeObjectVerts = new Vector3[(verticesPerNode * (n - 1))]; int nextVertex = 0; int nextTriangle = 0; int nextUV = 0; // variables for splines and perpendicular extruded points float[] cubicX = new float[n]; float[] cubicZ = new float[n]; Vector3 handle1Tween = new Vector3(); Vector3[] g1 = new Vector3[smoothingLevel + 1]; Vector3[] g2 = new Vector3[smoothingLevel + 1]; Vector3[] g3 = new Vector3[smoothingLevel + 1]; Vector3 oldG2 = new Vector3(); Vector3 extrudedPointL = new Vector3(); Vector3 extrudedPointR = new Vector3(); for (int i = 0; i < n; i++) { cubicX[i] = nodeObjects[i].position.x; cubicZ[i] = nodeObjects[i].position.z; } for (int i = 0; i < n; i++) { g1 = new Vector3[smoothingLevel + 1]; g2 = new Vector3[smoothingLevel + 1]; g3 = new Vector3[smoothingLevel + 1]; extrudedPointL = new Vector3(); extrudedPointR = new Vector3(); if (i == 0) { newVertices[nextVertex] = nodeObjects[0].position; nextVertex++; uvs[0] = new Vector2(0f, 1f); nextUV++; newVertices[nextVertex] = nodeObjects[0].position; nextVertex++; uvs[1] = new Vector2(1f, 1f); nextUV++; continue; } float _widthAtNode = pathWidth; // Interpolate points along the path using splines for direction and bezier curves for heights for (int j = 0; j < smoothingLevel + 1; j++) { // clone the vertex for uvs if (i == 1) { if (j != 0) { newVertices[nextVertex] = newVertices[nextVertex - 2]; nextVertex++; newVertices[nextVertex] = newVertices[nextVertex - 2]; nextVertex++; uvs[nextUV] = new Vector2(0f, 1f); nextUV++; uvs[nextUV] = new Vector2(1f, 1f); nextUV++; } else { oldG2 = nodeObjects[0].position; } } else { newVertices[nextVertex] = newVertices[nextVertex - 2]; nextVertex++; newVertices[nextVertex] = newVertices[nextVertex - 2]; nextVertex++; uvs[nextUV] = new Vector2(0f, 1f); nextUV++; uvs[nextUV] = new Vector2(1f, 1f); nextUV++; } float u = (float)j / (float)(smoothingLevel + 1f); Cubic[] X = calcNaturalCubic(n - 1, cubicX); Cubic[] Z = calcNaturalCubic(n - 1, cubicZ); Vector3 tweenPoint = new Vector3(X[i - 1].eval(u), 0f, Z[i - 1].eval(u)); // Add the current tweenpoint as a path cell TerrainPathCell tC = new TerrainPathCell(); tC.position.x = ((tweenPoint.x - parentTerrain.transform.position.x) / terData.size.x) * terData.heightmapResolution; tC.position.y = ((tweenPoint.z - parentTerrain.transform.position.z) / terData.size.z) * terData.heightmapResolution; tC.heightAtCell = (tweenPoint.y - parentTerrain.transform.position.y) / terData.size.y; pathCells.Add(tC); // update tweened points g2[j] = tweenPoint; g1[j] = oldG2; g3[j] = g2[j] - g1[j]; oldG2 = g2[j]; // Create perpendicular points for vertices extrudedPointL = new Vector3(-g3[j].z, 0, g3[j].x); extrudedPointR = new Vector3(g3[j].z, 0, -g3[j].x); extrudedPointL.Normalize(); extrudedPointR.Normalize(); extrudedPointL *= _widthAtNode; extrudedPointR *= _widthAtNode; // Height at the terrain tweenPoint.y = terrainHeights[(int)(((float)(tweenPoint.z - parentTerrain.transform.position.z) / terData.size.z) * terData.heightmapResolution), (int)(((float)(tweenPoint.x - parentTerrain.transform.position.x) / terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y; // create vertices at the perpendicular points newVertices[nextVertex] = tweenPoint + extrudedPointR; if (!road) { newVertices[nextVertex].y = (((float)terrainHeights[(int)(((float)(newVertices[nextVertex].z - parentTerrain.transform.position.z) / terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[nextVertex].x - parentTerrain.transform.position.x) / terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y) + newVertices[nextVertex - 2].y) / 2f; } else { newVertices[nextVertex].y = (float)terrainHeights[(int)(((float)(newVertices[nextVertex].z - parentTerrain.transform.position.z) / terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[nextVertex].x - parentTerrain.transform.position.x) / terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y; } nodeObjectVerts[nextVertex] = newVertices[nextVertex]; nextVertex++; newVertices[nextVertex] = tweenPoint + extrudedPointL; if (!road) { newVertices[nextVertex].y = (((float)terrainHeights[(int)(((float)(newVertices[nextVertex].z - parentTerrain.transform.position.z) / terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[nextVertex].x - parentTerrain.transform.position.x) / terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y) + newVertices[nextVertex - 2].y) / 2f; } else { newVertices[nextVertex].y = (float)terrainHeights[(int)(((float)(newVertices[nextVertex].z - parentTerrain.transform.position.z) / terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[nextVertex].x - parentTerrain.transform.position.x) / terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y; } nodeObjectVerts[nextVertex] = newVertices[nextVertex]; nextVertex++; uvs[nextUV] = new Vector2(0f, 0f); nextUV++; uvs[nextUV] = new Vector2(1f, 0f); nextUV++; // used later to update the handles if (i == 1) { if (j == 0) { handle1Tween = tweenPoint; } } // flatten mesh if (flatten && !road) { if (newVertices[nextVertex - 1].y < (newVertices[nextVertex - 2].y - 0.0f)) { extrudedPointL *= 1.5f; extrudedPointR *= 1.2f; newVertices[nextVertex - 1] = tweenPoint + extrudedPointL; newVertices[nextVertex - 2] = tweenPoint + extrudedPointR; newVertices[nextVertex - 1].y = newVertices[nextVertex - 2].y; } else if (newVertices[nextVertex - 1].y > (newVertices[nextVertex - 2].y - 0.0f)) { extrudedPointR *= 1.5f; extrudedPointL *= 1.2f; newVertices[nextVertex - 2] = tweenPoint + extrudedPointR; newVertices[nextVertex - 1] = tweenPoint + extrudedPointL; newVertices[nextVertex - 2].y = newVertices[nextVertex - 1].y; } } // Create triangles... newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j); // 0 nextTriangle++; newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j) + 1; // 1 nextTriangle++; newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j) + 2; // 2 nextTriangle++; newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j) + 1; // 1 nextTriangle++; newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j) + 3; // 3 nextTriangle++; newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j) + 2; // 2 nextTriangle++; } } // update handles g2[0] = handle1Tween; g1[0] = nodeObjects[0].position; g3[0] = g2[0] - g1[0]; extrudedPointL = new Vector3(-g3[0].z, 0, g3[0].x); extrudedPointR = new Vector3(g3[0].z, 0, -g3[0].x); extrudedPointL.Normalize(); extrudedPointR.Normalize(); extrudedPointL *= nodeObjects[0].width; extrudedPointR *= nodeObjects[0].width; newVertices[0] = nodeObjects[0].position + extrudedPointR; newVertices[0].y = terrainHeights[(int)(((float)(newVertices[0].z - parentTerrain.transform.position.z) / terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[0].x - parentTerrain.transform.position.x) / terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y; newVertices[1] = nodeObjects[0].position + extrudedPointL; newVertices[1].y = terrainHeights[(int)(((float)(newVertices[1].z - parentTerrain.transform.position.z) / terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[1].x - parentTerrain.transform.position.x) / terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y; if (road) { for (int i = 0; i < newVertices.Length; i++) { newVertices[i].y = terComponent.SampleHeight(newVertices[i]) + (0.05f) + parentTerrain.transform.position.y; } } newMesh.vertices = newVertices; newMesh.triangles = newTriangles; newMesh.uv = uvs; Vector3[] myNormals = new Vector3[newMesh.vertexCount]; for (int p = 0; p < newMesh.vertexCount; p++) { myNormals[p] = Vector3.up; } newMesh.normals = myNormals; TangentSolver(newMesh); // newMesh.RecalculateNormals(); pathCollider.sharedMesh = meshFilter.sharedMesh; pathCollider.smoothSphereCollisions = true; // we don't want to see the mesh if (!road) { pathMesh.GetComponent <Renderer>().enabled = false; } else { pathMesh.GetComponent <Renderer>().enabled = true; } transform.localScale = new Vector3(1, 1, 1); }
public void CreatePathNode(TerrainPathCell nodeCell) { Vector3 pathPosition = new Vector3((nodeCell.position.x/terData.heightmapResolution) * terData.size.x, nodeCell.heightAtCell * terData.size.y , (nodeCell.position.y/terData.heightmapResolution) * terData.size.z); AddNode(pathPosition, pathWidth); if(pathFlat || isRoad) CreatePath(pathSmooth, true, false); else CreatePath(pathSmooth, false, false); }
public void CreatePath(int smoothingLevel, bool flatten, bool road) { MeshFilter meshFilter = (MeshFilter)pathMesh.GetComponent(typeof(MeshFilter)); if(meshFilter == null) return; Mesh newMesh = meshFilter.sharedMesh; terrainHeights = terData.GetHeights(0, 0, terData.heightmapResolution, terData.heightmapResolution); pathCells = new ArrayList(); if (newMesh == null) { newMesh = new Mesh(); newMesh.name = "Generated Path Mesh"; meshFilter.sharedMesh = newMesh; } else newMesh.Clear(); if (nodeObjects == null || nodeObjects.Length < 2) { return; } int n = nodeObjects.Length; int verticesPerNode = 2 * (smoothingLevel + 1) * 2; int trianglesPerNode = 6 * (smoothingLevel + 1); Vector2[] uvs = new Vector2[(verticesPerNode * (n - 1))]; Vector3[] newVertices = new Vector3[(verticesPerNode * (n - 1))]; int[] newTriangles = new int[(trianglesPerNode * (n - 1))]; nodeObjectVerts = new Vector3[(verticesPerNode * (n - 1))]; int nextVertex = 0; int nextTriangle = 0; int nextUV = 0; // variables for splines and perpendicular extruded points float[] cubicX = new float[n]; float[] cubicZ = new float[n]; Vector3 handle1Tween = new Vector3(); Vector3[] g1 = new Vector3[smoothingLevel+1]; Vector3[] g2 = new Vector3[smoothingLevel+1]; Vector3[] g3 = new Vector3[smoothingLevel+1]; Vector3 oldG2 = new Vector3(); Vector3 extrudedPointL = new Vector3(); Vector3 extrudedPointR = new Vector3(); for(int i = 0; i < n; i++) { cubicX[i] = nodeObjects[i].position.x; cubicZ[i] = nodeObjects[i].position.z; } for (int i = 0; i < n; i++) { g1 = new Vector3[smoothingLevel+1]; g2 = new Vector3[smoothingLevel+1]; g3 = new Vector3[smoothingLevel+1]; extrudedPointL = new Vector3(); extrudedPointR = new Vector3(); if (i == 0) { newVertices[nextVertex] = nodeObjects[0].position; nextVertex++; uvs[0] = new Vector2(0f, 1f); nextUV++; newVertices[nextVertex] = nodeObjects[0].position; nextVertex++; uvs[1] = new Vector2(1f, 1f); nextUV++; continue; } float _widthAtNode = pathWidth; // Interpolate points along the path using splines for direction and bezier curves for heights for (int j = 0; j < smoothingLevel + 1; j++) { // clone the vertex for uvs if(i == 1) { if(j != 0) { newVertices[nextVertex] = newVertices[nextVertex-2]; nextVertex++; newVertices[nextVertex] = newVertices[nextVertex-2]; nextVertex++; uvs[nextUV] = new Vector2(0f, 1f); nextUV++; uvs[nextUV] = new Vector2(1f, 1f); nextUV++; } else oldG2 = nodeObjects[0].position; } else { newVertices[nextVertex] = newVertices[nextVertex-2]; nextVertex++; newVertices[nextVertex] =newVertices[nextVertex-2]; nextVertex++; uvs[nextUV] = new Vector2(0f, 1f); nextUV++; uvs[nextUV] = new Vector2(1f, 1f); nextUV++; } float u = (float)j/(float)(smoothingLevel+1f); Cubic[] X = calcNaturalCubic(n-1, cubicX); Cubic[] Z = calcNaturalCubic(n-1, cubicZ); Vector3 tweenPoint = new Vector3(X[i-1].eval(u), 0f, Z[i-1].eval(u)); // Add the current tweenpoint as a path cell TerrainPathCell tC = new TerrainPathCell(); tC.position.x = ((tweenPoint.x - parentTerrain.transform.position.x)/terData.size.x) * terData.heightmapResolution; tC.position.y = ((tweenPoint.z - parentTerrain.transform.position.z)/terData.size.z) * terData.heightmapResolution; tC.heightAtCell = (tweenPoint.y - parentTerrain.transform.position.y)/terData.size.y; pathCells.Add(tC); // update tweened points g2[j] = tweenPoint; g1[j] = oldG2; g3[j] = g2[j] - g1[j]; oldG2 = g2[j]; // Create perpendicular points for vertices extrudedPointL = new Vector3(-g3[j].z, 0, g3[j].x); extrudedPointR = new Vector3(g3[j].z, 0, -g3[j].x); extrudedPointL.Normalize(); extrudedPointR.Normalize(); extrudedPointL *= _widthAtNode; extrudedPointR *= _widthAtNode; // Height at the terrain tweenPoint.y = terrainHeights[(int)(((float)(tweenPoint.z - parentTerrain.transform.position.z)/terData.size.z) * terData.heightmapResolution), (int)(((float)(tweenPoint.x - parentTerrain.transform.position.x)/terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y; // create vertices at the perpendicular points newVertices[nextVertex] = tweenPoint + extrudedPointR; if(!road) newVertices[nextVertex].y = (((float)terrainHeights[(int)(((float)(newVertices[nextVertex].z - parentTerrain.transform.position.z)/terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[nextVertex].x - parentTerrain.transform.position.x)/terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y) + newVertices[nextVertex-2].y)/2f; else newVertices[nextVertex].y = (float)terrainHeights[(int)(((float)(newVertices[nextVertex].z - parentTerrain.transform.position.z)/terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[nextVertex].x- parentTerrain.transform.position.x)/terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y; nodeObjectVerts[nextVertex] = newVertices[nextVertex]; nextVertex++; newVertices[nextVertex] = tweenPoint + extrudedPointL; if(!road) newVertices[nextVertex].y = (((float)terrainHeights[(int)(((float)(newVertices[nextVertex].z - parentTerrain.transform.position.z)/terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[nextVertex].x - parentTerrain.transform.position.x)/terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y) + newVertices[nextVertex-2].y)/2f; else newVertices[nextVertex].y = (float)terrainHeights[(int)(((float)(newVertices[nextVertex].z - parentTerrain.transform.position.z)/terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[nextVertex].x - parentTerrain.transform.position.x)/terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y; nodeObjectVerts[nextVertex] = newVertices[nextVertex]; nextVertex++; uvs[nextUV] = new Vector2(0f, 0f); nextUV++; uvs[nextUV] = new Vector2(1f, 0f); nextUV++; // used later to update the handles if(i == 1) if(j == 0) handle1Tween = tweenPoint; // flatten mesh if(flatten && !road) { if(newVertices[nextVertex-1].y < (newVertices[nextVertex-2].y-0.0f)) { extrudedPointL *= 1.5f; extrudedPointR *= 1.2f; newVertices[nextVertex-1] = tweenPoint + extrudedPointL; newVertices[nextVertex-2] = tweenPoint + extrudedPointR; newVertices[nextVertex-1].y = newVertices[nextVertex-2].y; } else if(newVertices[nextVertex-1].y > (newVertices[nextVertex-2].y-0.0f)) { extrudedPointR *= 1.5f; extrudedPointL *= 1.2f; newVertices[nextVertex-2] = tweenPoint + extrudedPointR; newVertices[nextVertex-1] = tweenPoint + extrudedPointL; newVertices[nextVertex-2].y = newVertices[nextVertex-1].y; } } // Create triangles... newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j); // 0 nextTriangle++; newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j) + 1; // 1 nextTriangle++; newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j) + 2; // 2 nextTriangle++; newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j) + 1; // 1 nextTriangle++; newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j) + 3; // 3 nextTriangle++; newTriangles[nextTriangle] = (verticesPerNode * (i - 1)) + (4 * j) + 2; // 2 nextTriangle++; } } // update handles g2[0] = handle1Tween; g1[0] = nodeObjects[0].position; g3[0] = g2[0] - g1[0]; extrudedPointL = new Vector3(-g3[0].z, 0, g3[0].x); extrudedPointR = new Vector3(g3[0].z, 0, -g3[0].x); extrudedPointL.Normalize(); extrudedPointR.Normalize(); extrudedPointL *= nodeObjects[0].width; extrudedPointR *= nodeObjects[0].width; newVertices[0] = nodeObjects[0].position + extrudedPointR; newVertices[0].y = terrainHeights[(int)(((float)(newVertices[0].z - parentTerrain.transform.position.z)/terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[0].x - parentTerrain.transform.position.x)/terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y; newVertices[1] = nodeObjects[0].position + extrudedPointL; newVertices[1].y = terrainHeights[(int)(((float)(newVertices[1].z - parentTerrain.transform.position.z)/terData.size.z) * terData.heightmapResolution), (int)(((float)(newVertices[1].x - parentTerrain.transform.position.x)/terData.size.x) * terData.heightmapResolution)] * terData.size.y + parentTerrain.transform.position.y; if(road) { for(int i = 0; i < newVertices.Length; i++) { newVertices[i].y = terComponent.SampleHeight(newVertices[i]) + (0.05f) + parentTerrain.transform.position.y; } } newMesh.vertices = newVertices; newMesh.triangles = newTriangles; newMesh.uv = uvs; Vector3[] myNormals = new Vector3[newMesh.vertexCount]; for(int p = 0; p < newMesh.vertexCount; p++) { myNormals[p] = Vector3.up; } newMesh.normals = myNormals; TangentSolver(newMesh); // newMesh.RecalculateNormals(); pathCollider.sharedMesh = meshFilter.sharedMesh; pathCollider.smoothSphereCollisions = true; // we don't want to see the mesh if(!road) pathMesh.renderer.enabled = false; else pathMesh.renderer.enabled = true; transform.localScale = new Vector3(1,1,1); }
public void visualizePath() { GameObject pathMesh = new GameObject(); pathMesh.name = "Path"; //pathMesh.tag = "Road"; pathMesh.AddComponent(typeof(MeshFilter)); pathMesh.AddComponent(typeof(MeshRenderer)); pathMesh.AddComponent("AttachedPathScript"); AttachedPathScript APS = (AttachedPathScript)pathMesh.GetComponent("AttachedPathScript"); APS.pathMesh = pathMesh; APS.parentTerrain = gameObject; //APS.NewPath(); APS.NewPath(); APS.pathWidth = 3; //APS.pathTexture = 1; APS.isRoad = true; APS.pathSmooth = 5; //APS.pathUniform = true; //APS.pathWear = 0.5f; bool check = false; // foreach(Node node in path) { for (int i = 0; i < path.Count; i++) { if (i % 3 == 0 || i == path.Count - 1) { TerrainPathCell pathNodeCell = new TerrainPathCell(); pathNodeCell.position.x = Mathf.RoundToInt((float)((((Node)path[i]).getPosition().x / Terrain.activeTerrain.terrainData.size.x) * Terrain.activeTerrain.terrainData.heightmapResolution)); pathNodeCell.position.y = Mathf.RoundToInt((float)((((Node)path[i]).getPosition().z / Terrain.activeTerrain.terrainData.size.z) * Terrain.activeTerrain.terrainData.heightmapResolution)); pathNodeCell.heightAtCell = (Terrain.activeTerrain.SampleHeight(new Vector3(pathNodeCell.position.x, pathNodeCell.position.y))) / Terrain.activeTerrain.terrainData.size.y; //Debug.Log(pathNodeCell.heightAtCell); //Debug.Log("path node " + pathNodeCell.position); if (!APS.CreatePathNode(pathNodeCell)) { check = true; break; } if (check) { DestroyImmediate(pathMesh); continue; } } } APS.terrainCells = new TerrainPathCell[APS.terData.heightmapResolution * APS.terData.heightmapResolution]; APS.terrainCells = terrainCells; APS.FinalizePath(); APS.pathMesh.renderer.enabled = true; APS.pathMesh.renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); APS.pathMesh.renderer.sharedMaterial.color = Color.grey; //pathMesh.GetComponent<MeshCollider>().convex = true; pathMesh.AddComponent <Rigidbody>() /*.inertiaTensor = Vector3.one*/; pathMesh.GetComponent <Rigidbody>().useGravity = false; pathMesh.transform.Translate(new Vector3(0, 0.2f, 0)); pathMesh.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; pathMesh.AddComponent <CollisionMover>(); for (int i = 0; i < APS.nodeObjects.Length; i++) { GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); sphere.renderer.sharedMaterial.color = Color.blue; sphere.transform.localScale = new Vector3(5, 5, 5); sphere.transform.position = APS.nodeObjects[i].position; sphere.AddComponent <DestroyOnLoad>(); DestroyImmediate(sphere.GetComponent <SphereCollider>()); sphere.transform.parent = pathMesh.transform; } pathMesh.GetComponent <CollisionMover>().check(); }