Ok ...here we are
Morphing is an old technique that moves vertex to match next figure . We could make a function ....something like an add on to morph differents keys of a figure. In the past it was absolutely necesary that both figures had the same quantity of vertex to work ...I've seen somewhere advanced programms that this is not necesary, but in this example I show you the old school technique .
In this example, you can flay with cursor keys to get closer to the object to see how vertes move to match the othe figure .
// Project: 3D Morfing
// Created: 2018-12-24
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "3D Morfing" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
`LOADING TWO POSE KEYS ...we could load more morphing keys but for this example is ok
loadobject(1000,"pose2.x") `first pose key
loadobject(2000,"pose1.x") `second pose key
cloneobject(3000,2000) `last pose key
loadimage(1,"face.png")
SetObjectVisible(2000,0) `hiding second pose
setobjectimage(1000,1,0)
setobjectimage(3000,1,0)
mesh = CreateMemblockFromObjectMesh(1000,1)
`getting object vertex quantity of the model to manage vertex data
verts1= GetMemblockInt( mesh, 0 )
ob=0
for i = 0 to verts1-1
inc ob
x#=GetMeshMemblockVertexX(mesh,i)
y#=GetMeshMemblockVertexy(mesh,i)
z#=GetMeshMemblockVertexz(mesh,i)
createobjectbox(ob,1,1,1) `we make litle boxes as vertex just to show vertex position and transformation
setobjectcolor(ob,255,0,0,0)
setobjectposition(ob,x#,y#,z#)
setobjectvisible(ob,1) `WE CAN DEACTIVATE VERTEX VISIBILITY IN FINAL CODE
next
se=ob
mesh = CreateMemblockFromObjectMesh(2000,1)
verts2= GetMemblockInt( mesh, 0 )
for i = 0 to verts2-1
inc se
x#=GetMeshMemblockVertexX(mesh,i)
y#=GetMeshMemblockVertexy(mesh,i)
z#=GetMeshMemblockVertexz(mesh,i)
createobjectbox(se,1,1,1)
setobjectcolor(se,0,0,255,0)
setobjectposition(se ,x#,y#,z#)
setobjectvisible(se,1) `WE CAN DEACTIVATE VERTEX VISIBILITY IN FINAL CODE
next
setcameraposition(1,100,0,-250)
SetCameraLookAt(1,100,0,0,0)
`sky
createobjectsphere(5000,1000,10,10)
SetObjectColorEmissive(5000,0,0,50)
SetObjectCullMode(5000,0)
do
gosub camara
`comparing vertex of first pose and second pose
for i= 1 to verts
x#=getobjectx(i) `first pose x
y#=getobjecty(i) `first pose y
z#=getobjectz(i) `first pose z
nx#=getobjectx(i+verts) `second pose x
ny#=getobjecty(i+verts) `second pose y
nz#=getobjectz(i+verts) `second pose z
`if vertex do not match, they move until they match when we press Spacebar
if ( GetRawKeyState( 32 ) )
if x#>nx# then moveobjectlocalx( i+verts,0.1)
if x#<nx# then moveobjectlocalx( i+verts,-0.1 )
if y#>ny# then moveobjectlocaly(i+verts,0.1)
if y#<ny# then moveobjectlocaly(i+verts,-0.1)
if z#>nz# then moveobjectlocalz( i+verts,0.1)
if z#<nz# then moveobjectlocalz( i+verts,-0.1)
endif
next
mesh = CreateMemblockFromObjectMesh(3000,1)
`getting object vertex quantity
verts= GetMemblockInt( mesh, 0 )
for i = 0 to verts-1
x#=Getobjectx(i+1+verts)
y#=getobjecty(i+1+verts)
z#=getobjectz(i+1+verts)
SetMeshMemblockVertexPosition(mesh,i,150+x#,y#,z#) `we add here 150+ x# just to show differences between the two figures
next
//fixing mesh
SetObjectMeshFromMemblock(3000,1,mesh)
print ("Fly with cursor and mouse ")
print ("When ready press Sapcebar to see the morphing")
Sync()
loop
camara:
if ( GetRawKeyState( 38 ) ) then MoveCameraLocalZ( 1, 3 )
if ( GetRawKeyState( 40 ) ) then MoveCameraLocalZ( 1, -3 )
if ( GetRawKeyState( 37 ) ) then MoveCameraLocalX( 1, -3 )
if ( GetRawKeyState( 39 ) ) then MoveCameraLocalX( 1, 3 )
if ( GetPointerPressed() )
startx# = GetPointerX()
starty# = GetPointerY()
angx# = GetCameraAngleX(1)
angy# = GetCameraAngleY(1)
pressed = 1
endif
if ( GetPointerState() = 1 )
fDiffX# = (GetPointerX() - startx#)/1.0
fDiffY# = (GetPointerY() - starty#)/1.0
newX# = angx# + fDiffY#
if ( newX# > 89 ) then newX# = 89
if ( newX# < -89 ) then newX# = -89
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
endif
return
I'm not a grumpy grandpa