I don't know if this is any good because i'm not entirely sure what you're trying to achieve in the long run, but I think this thing might be easier to work out if you do it on a quad by quad basis as opposed to tri tri as you seemed to be (?).
I added a new array (Pvertices) to store the vertex indexes created by the create terrain function and then used those to reference the terrain mesh. Most of the GradePaintTerrainMesh I changed, but I didn't copy the UVs of the terrain (i forgot :). Also, I don't know if it was intentional but u only store the colour red of each vertex in your function.
; level test Graphics3D 640,480,16,2 Global width = 32 Global seed = 3 Dim plasma#(width,width) Dim PVertices(width,width) GeneratePlasma(width,width,1,seed) SquarePlasma(width,0.005) SetBuffer BackBuffer() terr = CreateTerrainMesh(width,width) b0 = LoadBrush("efx_sample_05.jpg",1+8) b1 = LoadBrush("grass.bmp",1+8) terr = GradePaintTerrainMesh(terr, 0.00001, 90, 90, 90, b0, b1, b2, b3, b4) ScaleEntity terr,1,4,1 EntityFX terr,2 EntityColor terr,0,0,255 Global cam_camera = CreateCamera() PositionEntity cam_camera,10,40,10 sun= CreateLight() PositionEntity sun,10,40,10 RotateEntity sun,45,0,0 While Not KeyHit(1) lev_docamera() UpdateWorld RenderWorld Flip Wend End Function LEV_docamera() CamSpd# = .50 MoveEntity(CAM_Camera, Float(KeyDown(205) - KeyDown(203)) * CamSpd, 0, Float(KeyDown(200) - KeyDown(208)) * CamSpd) If MouseDown(1) Then WireFrame 1 Else WireFrame 0 EndIf If MouseDown(2) TurnSpeed# = 0.8 TurnEntity(CAM_Camera, Float(MouseYSpeed()) * TurnSpeed#, 0, 0, False) TurnEntity(CAM_Camera, 0, -Float(MouseXSpeed()) * TurnSpeed#, 0, True) Else MouseXSpeed() : MouseYSpeed() EndIf End Function Function GeneratePlasma(width, height, grain#, seed) SeedRnd(seed) Dim plasma#(width, height) For j = 0 To width - 1 For i = 0 To height - 1 plasma#(j, i) = -1.0 Next Next x_max = Ceil(Float(width - 1)/2.0) ; mid-point x y_max = Ceil(Float(height - 1)/2.0) ; mid-point y plasma#(0, 0) = Rnd(0.0, 1.0) plasma#(x_max, 0) = Rnd(0.0, 1.0) plasma#(0, y_max) = Rnd(0.0, 1.0) plasma#(x_max, y_max) = Rnd(0.0, 1.0) RecursePlasma(0, 0, x_max, y_max, grain#, 1.0) For y = 0 To y_max plasma#(width - 1, y) = plasma#(0, y) Next RecursePlasma(x_max, 0, width - 1, y_max, grain#, 1.0) For x = 0 To width - 1 plasma#(x, height - 1) = plasma#(x, 0) Next RecursePlasma(x_max, y_max, width - 1, height - 1, grain#, 1.0) For y = (y_max + 1) To height - 1 plasma#(0, y) = plasma#(width - 1, y) Next RecursePlasma(0, y_max, x_max, height - 1, grain#, 1.0) End Function ;--------------------------------------------------------------------- Function MidPointDisplace#(x0, y0, x1, y1, x2, y2, grain#, level#) r# = Rnd(-grain#, grain#)/level# r# = (plasma#(x0, y0) + plasma#(x2, y2))/2.0 + r# If r# < 0.0 Then r# = 0.0 If r# > 1.0 Then r# = 1.0 plasma#(x1, y1) = r# Return r# End Function ;--------------------------------------------------------------------- Function RecursePlasma(x0, y0, x2, y2, grain#, level#) If (x2 - x0 < 2) And (y2 - y0 < 2) Then Return level# = 2.0*level# x1 = Ceil((x0 + x2)/2.0) y1 = Ceil((y0 + y2)/2.0) v# = plasma#(x1, y0) If v# = -1.0 Then v# = MidPointDisplace#(x0, y0, x1, y0, x2, y0, grain#, level#) i# = v# v# = plasma#(x2, y1) If v# = -1.0 Then v# = MidPointDisplace#(x2, y0, x2, y1, x2, y2, grain#, level#) i# = i# + v# v# = plasma#(x1, y2) If v# = -1.0 Then v# = MidPointDisplace#(x0, y2, x1, y2, x2, y2, grain#, level#) i# = i# + v# v# = plasma#(x0, y1) If v# = -1.0 Then v# = MidPointDisplace#(x0, y0, x0, y1, x0, y2, grain#, level#) i# = i# + v# If plasma#(x1, y1) = -1.0 Then plasma#(x1, y1) = i#/4.0 RecursePlasma(x0, y0, x1, y1, grain#, level#) RecursePlasma(x1, y0, x2, y1, grain#, level#) RecursePlasma(x1, y1, x2, y2, grain#, level#) RecursePlasma(x0, y1, x1, y2, grain#, level#) End Function ; ------------------------------------------------------------------------ Function SquarePlasma(width,value#) yMax# = 0 tempx = 0 tempy = 0 For x = 0 To width - 1 For y = 0 To width -1 x1# = plasma(x,y) x2# = plasma(x+1,y) x3# = plasma(x,y+1) x4# = plasma(x+1,y+1) av# = (x1+x2+x3+x4) / Float(4) If Abs(av-x1) <= value Then plasma(x,y) = x1 plasma(x+1,y) = x1 plasma(x,y+1) = x1 plasma(x+1,y+1) = x1 EndIf If x1 > yMax Then yMax = x1 tempx = x tempy = y EndIf Next Next plasma(tempx,tempy) = ymax plasma(tempx+1,tempy) = ymax plasma(tempx,tempy+1) = ymax plasma(tempx+1,tempy+1) = ymax End Function ;---------------------------------------------------------------------------- Function CreateTerrainMesh(x,y) ;x = how many horizontal vertices ;y = how many vertical vertices mesh = CreateMesh() surf = CreateSurface(mesh) If x < 2 Or y < 2 Then Return mesh ;in case user gives a bad number simply return the mesh For tempx = 1 To x For tempy = 1 To y PVertices(tempx,tempy)=AddVertex(surf,tempx*4,plasma#(tempx,tempy)*10,tempy*4) Next Next For v0 = 0 To (x*y)-y-2 For Z = 1 To x*y If v0 = z*y-1 Then v0 = v0 + 1 Next v1 = V0 + 1 v2 = v1 + y v3 = v0 + y AddTriangle(surf,v0,v1,v2) AddTriangle(surf,v0,v2,v3) Next UpdateNormals(mesh) Return mesh End Function ; ----------------------------------- thanks to mrsnidesmin Function GradePaintTerrainMesh%(m%, g1#, g2#, g3#, g4#, bGrad0%, bGrad1%, bGrad2%, bGrad3%, bGrad4%) m2% = CreateMesh() s_g0% = CreateSurface(m2) s_g1% = CreateSurface(m2) For i% = 1 To CountSurfaces(m) s% = GetSurface(m, i) For tempx = 1 To width-1 For tempy = 1 To width-1 If plasma(tempx,tempy)=plasma(tempx,tempy+1) And plasma(tempx,tempy)=plasma(tempx+1,tempy) And plasma(tempx,tempy)=plasma(tempx+1,tempy+1) v0=AddVertex(s_g0,tempx*4,plasma#(tempx,tempy)*10,tempy*4) v1=AddVertex(s_g0,tempx*4,plasma#(tempx,tempy+1)*10,tempy*4+4) v2=AddVertex(s_g0,tempx*4+4,plasma#(tempx+1,tempy+1)*10,tempy*4+4) v3=AddVertex(s_g0,tempx*4+4,plasma#(tempx+1,tempy)*10,tempy*4) AddTriangle(s_g0,v0,v1,v2) AddTriangle(s_g0,v0,v2,v3) VertexNormal s_g0,v0,VertexNX(s,PVertices(tempx,tempy)),VertexNY(s,PVertices(tempx,tempy)),VertexNZ(s,PVertices(tempx,tempy)) VertexNormal s_g0,v1,VertexNX(s,PVertices(tempx,tempy+1)),VertexNY(s,PVertices(tempx,tempy+1)),VertexNZ(s,PVertices(tempx,tempy+1)) VertexNormal s_g0,v2,VertexNX(s,PVertices(tempx+1,tempy+1)),VertexNY(s,PVertices(tempx+1,tempy+1)),VertexNZ(s,PVertices(tempx+1,tempy+1)) VertexNormal s_g0,v3,VertexNX(s,PVertices(tempx+1,tempy)),VertexNY(s,PVertices(tempx+1,tempy)),VertexNZ(s,PVertices(tempx+1,tempy)) VertexColor s_g0,v0,VertexRed(s,PVertices(tempx,tempy)),VertexGreen(s,PVertices(tempx,tempy)),VertexBlue(s,PVertices(tempx,tempy)) VertexColor s_g0,v1,VertexRed(s,PVertices(tempx,tempy+1)),VertexGreen(s,PVertices(tempx,tempy+1)),VertexBlue(s,PVertices(tempx,tempy+1)) VertexColor s_g0,v2,VertexRed(s,PVertices(tempx+1,tempy+1)),VertexGreen(s,PVertices(tempx+1,tempy+1)),VertexBlue(s,PVertices(tempx+1,tempy+1)) VertexColor s_g0,v3,VertexRed(s,PVertices(tempx+1,tempy)),VertexGreen(s,PVertices(tempx+1,tempy)),VertexBlue(s,PVertices(tempx+1,tempy)) Else v0=AddVertex(s_g1,tempx*4,plasma#(tempx,tempy)*10,tempy*4) v1=AddVertex(s_g1,tempx*4,plasma#(tempx,tempy+1)*10,tempy*4+4) v2=AddVertex(s_g1,tempx*4+4,plasma#(tempx+1,tempy+1)*10,tempy*4+4) v3=AddVertex(s_g1,tempx*4+4,plasma#(tempx+1,tempy)*10,tempy*4) AddTriangle(s_g1,v0,v1,v2) AddTriangle(s_g1,v0,v2,v3) VertexNormal s_g1,v0,VertexNX(s,PVertices(tempx,tempy)),VertexNY(s,PVertices(tempx,tempy)),VertexNZ(s,PVertices(tempx,tempy)) VertexNormal s_g1,v1,VertexNX(s,PVertices(tempx,tempy+1)),VertexNY(s,PVertices(tempx,tempy+1)),VertexNZ(s,PVertices(tempx,tempy+1)) VertexNormal s_g1,v2,VertexNX(s,PVertices(tempx+1,tempy+1)),VertexNY(s,PVertices(tempx+1,tempy+1)),VertexNZ(s,PVertices(tempx+1,tempy+1)) VertexNormal s_g1,v3,VertexNX(s,PVertices(tempx+1,tempy)),VertexNY(s,PVertices(tempx+1,tempy)),VertexNZ(s,PVertices(tempx+1,tempy)) VertexColor s_g1,v0,VertexRed(s,PVertices(tempx,tempy)),VertexGreen(s,PVertices(tempx,tempy)),VertexBlue(s,PVertices(tempx,tempy)) VertexColor s_g1,v1,VertexRed(s,PVertices(tempx,tempy+1)),VertexGreen(s,PVertices(tempx,tempy+1)),VertexBlue(s,PVertices(tempx,tempy+1)) VertexColor s_g1,v2,VertexRed(s,PVertices(tempx+1,tempy+1)),VertexGreen(s,PVertices(tempx+1,tempy+1)),VertexBlue(s,PVertices(tempx+1,tempy+1)) VertexColor s_g1,v3,VertexRed(s,PVertices(tempx+1,tempy)),VertexGreen(s,PVertices(tempx+1,tempy)),VertexBlue(s,PVertices(tempx+1,tempy)) End If Next Next Next PaintSurface s_g0, bGrad0 PaintSurface s_g1, bGrad1 FreeEntity m Return m2 End Function
I added a new array (Pvertices) to store the vertex indexes created by the create terrain function and then used those to reference the terrain mesh. Most of the GradePaintTerrainMesh I changed, but I didn't copy the UVs of the terrain (i forgot :). Also, I don't know if it was intentional but u only store the colour red of each vertex in your function.