Page 1 of 1

Letter 'A' Animation - mesh morphing

Posted: Tue Mar 26, 2013 9:26 am
by applePi
example about making a 3D 'A' letter using an equation and then animating it by changing its hight continuously. the equation runs from about -1.3 to 1.3, and we reflect this to a plane vertices from -20 to 20 width and length
if we want to texture the whole figure with one picture just change the line 80 to
txv = txv + 1/width*0.5
and line 83 to
txu = txu + 1/length*0.5
try other textures.
Image
visit http://www.benjoffe.com/code/tools/functions3d/examples for more 3D curves.

Code: Select all

InitEngine3D()
InitSprite()
InitKeyboard()
Declare morph()
Global width=20
Global length=20 :Global wireFrame.b = 0
Global i,j,y.f,x.f,z.f,r.f,delta.f = 0.01
ExamineDesktops()
win = OpenWindow(#PB_Any, 0, 0, DesktopWidth(0), DesktopHeight(0), "Press W for wire/solid frame", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(win),0,0,DesktopWidth(0), DesktopHeight(0))
Add3DArchive("/", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/3D/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "Examples/Sources\Data", #PB_3DArchive_FileSystem)

cam=CreateCamera(#PB_Any,0,0,100,100)
MoveCamera(cam,0,20,-15,#PB_Absolute)
CameraLookAt(cam,0,0,0)
;CameraRenderMode(cam,#PB_Camera_Wireframe)
CreateLight(#PB_Any,RGB(255,255,255),0,20,-20)

CreateMaterial(0, LoadTexture(0, "wood.jpg"))
;MaterialShadingMode(0, #PB_Material_Wireframe)
MaterialCullingMode(0, #PB_Material_NoCulling)
DisableMaterialLighting(0, #True)

Define mesh=CreateMesh(#PB_Any,#PB_Mesh_TriangleList ,#PB_Mesh_Dynamic)
SetMeshMaterial(mesh, MaterialID(0))
morph()  ; call mesh construction
node = CreateNode(#PB_Any,0,0,0)
AttachNodeObject(node, MeshID(mesh))
ScaleNode(node, 5, 5, 5)
MoveNode(node,0,1,0)
RotateNode(node,0,90,0)

;oooooooooooooooooooooooooooooooooooooooooooooo

Repeat
  ExamineKeyboard()
  If KeyboardReleased(#PB_Key_W) ; display wire frame for the material
      If wireFrame=0
      MaterialShadingMode(0, #PB_Material_Wireframe)
      wireFrame ! 1
         ElseIf wireFrame=1
           MaterialShadingMode(0, #PB_Material_Solid)
           wireFrame ! 1
      EndIf
   EndIf
    rotx.f+0.5:roty.f+0.5
  ;deciding up or down animation , delta is the rate of morphing  
  If r>=3
    delta = -0.01
  ElseIf r<=-3
    delta = 0.01
  EndIf
  
  r.f = r.f + delta ; decide how much y will be up or down
  UpdateMesh(mesh,0)
  morph() ; call mesh construction
  RotateNode(node,0,roty,0)
  RenderWorld()
  
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow
End

Procedure morph()
  
  t=0
  txu.f=0: txv.f=0 ; variables for uv texturing
  For i=-20 To length-1
  For j=-20 To width-1
    x=i:z=j
    x=x*1/15: z=z*1/15  ; because the A generator functions run from -1.3 to 1.3
    ; A generator function
    y = ((1-Sign(-x-0.9+Abs(z*2)))/3*(Sign(0.9-x)+1)/3)*(Sign(x+0.65)+1)/2 -((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.9-x)+1)/3) + ((1-Sign(-x-0.39+Abs(z*2)))/3*(Sign(0.6-x)+1)/3)*(Sign(x-0.35)+1)/2
    y = y*r ; r is the morphing factor
    MeshVertexPosition(x,y,z) 
    MeshVertexTextureCoordinate(txu.f, txv.f)
    MeshVertexNormal(x, y, z)
    txv = txv + 1/width ; texture coordinates
  Next j
  txv = 0
  txu = txu + 1/length ; texture coordinates
Next i
t=0
For i = -20 To length-2
  For j = -20 To width-2
          
          MeshFace(t,t+1,t + width*2+1)
          MeshFace(t,t + width*2+1,t + width*2)
          t+1
     Next
     t+1
Next  
FinishMesh(#False)
EndProcedure

Re: Letter 'A' Animation - mesh morphing

Posted: Wed Mar 27, 2013 7:30 am
by dige
Cool! :-)

Re: Letter 'A' Animation - mesh morphing

Posted: Wed Mar 27, 2013 7:44 am
by davido
Grade: A± :)

Very nice, thank you.

Re: Letter 'A' Animation - mesh morphing

Posted: Wed Mar 27, 2013 10:23 am
by jamirokwai
Nice!