I want to move verts up or down on a terrain and the only way I have found uses a memblock.
In this demo keys Z and X move the selected vert down or up but it lags due to having to apply the
whole memblock to the whole terrain after each movement.
There must be a faster way. Any ideas anyone?
Cheers.
Here's the code and the demo.
// Project: vertex manipulation
// Created: 22-11-04
SetErrorMode(2)
//SetVSync(1)
// make fullscreen
width# = GetMaxDeviceWidth()
height# = GetMaxDeviceHeight()
SetWindowSize(width#,height#,1)
SetVirtualResolution(width#,height#)
SetSyncRate(60,0)
SetScissor(0,0,0,0)
UseNewDefaultFonts(1)
SetPrintSize(height#/30)
SetGenerateMipmaps(1)
SetAmbientColor(50,50,50)
SetSkyBoxVisible(1)
SetSunActive(0)
SetAmbientColor(50,50,50)
SetSkyboxVisible(1)
SetSkyBoxHorizonColor(30,30,30)
SetSkyBoxSkyColor(30,30,30)
light1=1
CreatePointLight(light1,3000,5000,3000,10000,255,255,255)
SetPointLightMode(light1,1)
// xxxxxxxxxxxxxxxxxxxx camera setup
cam1=1
SetCameraRange(cam1, 0.1, 50000 )
SetCameraposition(cam1,8351,260,-318)
SetCameraRotation(cam1,0,310,0)
RotateCameraLocalX(cam1,25)
man1=CreateObjectsphere(2,6,12)
SetObjectposition(man1,8351,260,-318)
setobjectvisible(man1,0)
SetObjectRotation(man1,0,310,0)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx terrain
island1=CreateObjectFromHeightMap("island7HM.png",1000,50,1000,1,0)
island1tex=LoadImage("island5TEX.png")
SetObjectImage(island1,island1tex,1)
SetObjectScalePermanent(island1,8,8,8)
mesh=CreateMemblockFromObjectMesh(island1,1)
verts=GetMemblockInt(mesh,0)
SetObjectCollisionMode(island1,1)
raytarget=createobjectbox(111,111,111) // invisible box for getting ray end co-ords
setobjectvisible(raytarget,0)
fireball=CreateObjectSphere(10,20,20) // invisible sphere placed where the ray hit the terrain
SetObjectCollisionMode(fireball,0)
setobjectvisible(fireball,0)
fireballpos#=CreateVector3(0,0,0)
vertball=CreateObjectSphere(50,20,20) // semi-visible sphere placed at the nearest vert
SetObjectColor(vertball,255,255,255,50)
SetObjectTransparency(vertball,1)
SetObjectColorEmissive(vertball,255,255,255)
SetObjectCollisionMode(vertball,0)
vertballpos#=CreateVector3(1000000,1000000,1000000)
vertpos#=CreateVector3(0,0,0)
SetRawMousePosition(width#/2,height#/2)
do
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx camera control
mouseX# = (GetRawMouseX()-width#/2)*0.2
mouseY# = (GetRawMouseY()-height#/2)*0.2
RotateCameraLocalX(cam1,mouseY#)
RotateObjectLocalY(man1,mouseX#)
SetCameraRotation(cam1,GetCameraAngleX(cam1),GetObjectAngleY(man1),0)
If GetCameraAngleX(cam1)>80 Then SetCameraRotation(cam1,80,GetCameraAngleY(cam1),0)
If GetCameraAngleX(cam1)<-80 Then SetCameraRotation(cam1,-80,GetCameraAngleY(cam1),0)
if GetRawKeyState(87) then MoveObjectLocalZ(man1,20) // W forward
if GetRawKeyState(83) then MoveObjectLocalZ(man1,-20) // S backward
if GetRawKeyState(65) then MoveObjectLocalX(man1,-20) // A strafe left
if GetRawKeyState(68) then MoveObjectLocalX(man1,20) // D strafe right
if GetRawKeyState(81) then MoveObjectLocalY(man1,-10) // Q down
if GetRawKeyState(69) then MoveObjectLocalY(man1,10) // E up
SetCameraPosition(cam1,GetObjectX(man1),GetObjectY(man1),GetObjectZ(man1))
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
if GetRawMouseLeftPressed() // select a vertex on the terrain
setobjectposition(raytarget,GetCameraWorldX(cam1),GetCameraWorldY(cam1),GetCameraWorldZ(cam1))
setobjectrotation(raytarget,GetCameraAngleX(cam1),GetCameraAngleY(cam1),GetCameraAngleZ(cam1))
moveobjectlocalZ(raytarget,15000) // ray1 starts at camera and ends at an invisible target box (raytarget)
ray1=ObjectRayCast(0,GetCameraWorldX(cam1),GetCameraWorldY(cam1),GetCameraWorldZ(cam1),GetObjectWorldX(raytarget),GetObjectWorldY(raytarget),GetObjectWorldZ(raytarget))
targetHit=GetObjectRayCastHitID(0)
if targetHit > 0 and targetHit <> raytarget
XhitPos#=GetObjectRayCastX(0)
YhitPos#=GetObjectRayCastY(0)
ZhitPos#=GetObjectRayCastZ(0)
setobjectposition(fireball,XhitPos#,YhitPos#,ZhitPos#) // position invisible sphere (fireball) at ray hit pos
setobjectposition(vertball,0,100000,0)
SetVector3(fireballpos#,GetObjectWorldX(fireball),GetObjectWorldY(fireball),GetObjectWorldZ(fireball))
newvertdist#=1000010.0
oldvertdist#=1000000.0
for i=0 to verts -1 // check all terrain verts to find the nearest to fireball then place vertball there
x#=GetMeshMemblockVertexX(mesh,i)
y#=GetMeshMemblockVertexY(mesh,i)
z#=GetMeshMemblockVertexZ(mesh,i)
SetVector3(vertballpos#,x#,y#,z#)
newvertdist#=GetVector3Distance(fireballpos#,vertballpos#)
if newvertdist#<oldvertdist#
oldvertdist#=newvertdist#
setobjectposition(vertball,x#,y#,z#)
vert=i // get the closest vertex ID
endif
next
endif
endif
if GetRawKeyState(90) // Z key
SetMeshMemblockVertexPosition(mesh,vert,GetMeshMemblockVertexX(mesh,vert),GetMeshMemblockVertexY(mesh,vert)-1,GetMeshMemblockVertexZ(mesh,vert))
SetObjectMeshFromMemblock(island1,1,mesh)
endif
if GetRawKeyState(88) // X key
SetMeshMemblockVertexPosition(mesh,vert,GetMeshMemblockVertexX(mesh,vert),GetMeshMemblockVertexY(mesh,vert)+1,GetMeshMemblockVertexZ(mesh,vert))
SetObjectMeshFromMemblock(island1,1,mesh)
endif
camX=getcameraX(cam1)
camY=getcameraY(cam1)
camZ=getcameraZ(cam1)
framerate=ScreenFPS()
Print(" "+ str(framerate))
Print(" co-ords X "+str(camX)+" Y "+str(camY)+" Z "+str(camZ))
print(" LMB - select vertex "+str(vert))
Print(" WSAD QE mouse - camera")
Print(" Z X - moves vertex down/up")
SetRawMousePosition(width#/2,height#/2)
Sync()
if GetrawKeyState(27) then end
loop
BASIC appeared in May 1964. Lightning flashed, the wind roared and the Earth moved.
And nine months later I was born.
So here I am.
I am Basic.
Code is in my genes.