3D CAMERA, ANIMATION LOOP

1 vote · 0 comments

~ 3D CAMERA ANIMATION LOOP, LOOKS LIKE THE MOAI_MILKYWAY

raw ·
copy
· download
---------------------------------------------------------------- -- Copyright (c) 2010-2011 Zipline Games, Inc. -- All Rights Reserved. -- <!-- m --><a class="postlink" href="http://getmoai.com">http://getmoai.com</a><!-- m --> ---------------------------------------------------------------- MOAISim.openWindow ( "MilkyWay", 600, 600 ) MOAIGfxDevice.getFrameBuffer():setClearColor(0,0,0,1) viewport = MOAIViewport.new () viewport:setSize ( 600, 600 ) viewport:setScale ( 600, 600 ) layer = MOAILayer.new () layer:setViewport ( viewport ) MOAISim.pushRenderPass ( layer ) camera = MOAICamera.new () camera:setLoc ( 0, 0, camera:getFocalLength ( 1000)) camera:setFarPlane(1500) local moaiTexture= MOAITexture.new() moaiTexture:setFilter ( MOAITexture.GL_LINEAR_MIPMAP_LINEAR ) --smooth moaiTexture:load( 'moai.png', MOAIImage.PREMULTIPLY_ALPHA ) function makeBoxMesh ( xMin, yMin, zMin, xMax, yMax, zMax, texture ) local function pushPoint ( points, x, y, z ) local point = {} point.x = x point.y = y point.z = z table.insert ( points, point ) end local function writeTri ( vbo, p1, p2, p3, uv1, uv2, uv3 ) vbo:writeFloat ( p1.x, p1.y, p1.z ) vbo:writeFloat ( uv1.x, uv1.y ) vbo:writeColor32 ( 1, 1, 1 ) vbo:writeFloat ( p2.x, p2.y, p2.z ) vbo:writeFloat ( uv2.x, uv2.y ) vbo:writeColor32 ( 1, 1, 1 ) vbo:writeFloat ( p3.x, p3.y, p3.z ) vbo:writeFloat ( uv3.x, uv3.y ) vbo:writeColor32 ( 1, 1, 1 ) end local function writeFace ( vbo, p1, p2, p3, p4, uv1, uv2, uv3, uv4 ) writeTri ( vbo, p1, p2, p4, uv1, uv2, uv4 ) writeTri ( vbo, p2, p3, p4, uv2, uv3, uv4 ) end local p = {} pushPoint ( p, xMin, yMax, zMax ) -- p1 pushPoint ( p, xMin, yMin, zMax ) -- p2 pushPoint ( p, xMax, yMin, zMax ) -- p3 pushPoint ( p, xMax, yMax, zMax ) -- p4 pushPoint ( p, xMin, yMax, zMin ) -- p5 pushPoint ( p, xMin, yMin, zMin ) -- p6 pushPoint ( p, xMax, yMin, zMin ) -- p7 pushPoint ( p, xMax, yMax, zMin ) -- p8 local uv = {} pushPoint ( uv, 0, 0, 0 ) pushPoint ( uv, 0, 1, 0 ) pushPoint ( uv, 1, 1, 0 ) pushPoint ( uv, 1, 0, 0 ) local vertexFormat = MOAIVertexFormat.new () vertexFormat:declareCoord ( 1, MOAIVertexFormat.GL_FLOAT, 3 ) vertexFormat:declareUV ( 2, MOAIVertexFormat.GL_FLOAT, 2 ) vertexFormat:declareColor ( 3, MOAIVertexFormat.GL_UNSIGNED_BYTE ) local vbo = MOAIVertexBuffer.new () vbo:setFormat ( vertexFormat ) vbo:reserveVerts ( 36 ) writeFace ( vbo, p [ 1 ], p [ 2 ], p [ 3 ], p [ 4 ], uv [ 1 ], uv [ 2 ], uv [ 3 ], uv [ 4 ]) writeFace ( vbo, p [ 4 ], p [ 3 ], p [ 7 ], p [ 8 ], uv [ 1 ], uv [ 2 ], uv [ 3 ], uv [ 4 ]) writeFace ( vbo, p [ 8 ], p [ 7 ], p [ 6 ], p [ 5 ], uv [ 1 ], uv [ 2 ], uv [ 3 ], uv [ 4 ]) writeFace ( vbo, p [ 5 ], p [ 6 ], p [ 2 ], p [ 1 ], uv [ 1 ], uv [ 2 ], uv [ 3 ], uv [ 4 ]) writeFace ( vbo, p [ 5 ], p [ 1 ], p [ 4 ], p [ 8 ], uv [ 1 ], uv [ 2 ], uv [ 3 ], uv [ 4 ]) writeFace ( vbo, p [ 2 ], p [ 6 ], p [ 7 ], p [ 3 ], uv [ 1 ], uv [ 2 ], uv [ 3 ], uv [ 4 ]) vbo:bless () local mesh = MOAIMesh.new () mesh:setTexture ( moaiTexture ) mesh:setVertexBuffer ( vbo ) mesh:setPrimType ( MOAIMesh.GL_TRIANGLES ) return mesh end function makeCube ( size, texture ) size = size * 0.5 return makeBoxMesh ( -size, -size, -size, size, size, size, texture ) end --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* local buffer_viewport = MOAIViewport.new () buffer_viewport:setSize ( 600, 600) buffer_viewport:setScale (600, 600 ) local buffer_layer = MOAILayer.new () buffer_layer:setViewport ( buffer_viewport ) buffer_layer:setCamera ( camera ) buffer_layer:setPartitionCull2D(false) local buffer_texture = MOAIFrameBufferTexture.new () buffer_texture:init ( 600, 600 ) buffer_texture:setRenderTable ({ buffer_layer }) MOAIRenderMgr.setBufferTable ({buffer_texture}) local partition = MOAIPartition.new () buffer_layer:setPartition ( partition ) --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* --******************************************************************************* local propTable = {} local width = 600 / 100 for i = 1 , 100 do for j = 1, 100 do local cnt = #propTable+1 local mesh = makeCube ( width, 'lisa' ) propTable[cnt] = MOAIProp.new () propTable[cnt]:setDeck ( mesh ) propTable[cnt]:setShader ( MOAIShaderMgr.getShader ( MOAIShaderMgr.MESH_SHADER )) propTable[cnt]:setCullMode ( MOAIProp.CULL_BACK ) propTable[cnt]:setLoc(math.random(-200,200),math.random(-200,200),math.random(-10000,0)) propTable[cnt]:setRot(math.random(360),math.random(360)) --propTable[cnt]:moveRot ( 360, 360, 0, 20 ) --propTable[cnt]:setBlendMode(MOAIProp2D.GL_ZERO, MOAIProp2D.GL_ONE_MINUS_SRC_COLOR) --propTable[cnt]:seekColor(math.random(100)/100,math.random(100)/100,math.random(100)/100,5,5) --propTable[cnt]:setLoc(i*width-300,j*width-300,0) --propTable[cnt]:moveLoc ( 1000,0,0 , 4, MOAIEaseType.LINEAR ) --propTable[cnt]:setVisible(false) propTable[cnt]:setColor(math.random(100)/100,math.random(100)/100,math.random(100)/100,1) partition:insertProp ( propTable[cnt] ) end end ---*************************************************************** ---*************************************************************** ---*************************************************************** ---*************************************************************** local prop_camera = MOAIProp.new () prop_camera:setLoc(0,0,0) partition:insertProp ( prop_camera ) prop_camera:setVisible(false) camera:setParent(prop_camera) --local myCamera = MOAITransform2D.new () -- myCamera:setAttrLink ( MOAIProp2D.INHERIT_LOC, propA, MOAIProp2D.TRANSFORM_TRAIT ) -- camera:setAttrLink ( MOAIProp2D.INHERIT_LOC, myCamera, MOAIProp2D.TRANSFORM_TRAIT ) local F ={} local thread = MOAICoroutine.new () function F.threadFunc () -- set up sprite here local action; while true do prop_camera:moveRot ( 720, 720, 0, 60,MOAIEaseType.LINEAR ) action = prop_camera:moveLoc (0,0,-10000, 60, MOAIEaseType.LINEAR ) while action:isBusy () do coroutine:yield () end -- spin lock until action is done action = prop_camera:moveRot ( 0, 180, 180, 5,MOAIEaseType.LINEAR ) while action:isBusy () do coroutine:yield () end prop_camera:moveRot ( 720, 720, 0, 60,MOAIEaseType.LINEAR ) action = prop_camera:moveLoc (0,0,10000, 60, MOAIEaseType.LINEAR ) while action:isBusy () do coroutine:yield () end action = prop_camera:moveRot ( 0, 180, 180, 5,MOAIEaseType.LINEAR ) while action:isBusy () do coroutine:yield () end coroutine:yield () end end thread:run ( F.threadFunc ) ---********************************************* --BUFFER OUTPUT local out_gfxQuad = MOAIGfxQuad2D.new () out_gfxQuad:setTexture ( buffer_texture ) out_gfxQuad:setRect ( -300, -300, 300, 300 ) local out_prop = MOAIProp2D.new () out_prop:setDeck ( out_gfxQuad ) layer:insertProp ( out_prop ) local function timerfps() local fps = MOAISim.getPerformance() --if fps > 40 then -- print('','CULLED> FPS',MOAISim.getPerformance(),'LOC',prop_camera:getLoc()) --else print('','> FPS',MOAISim.getPerformance(),'LOC',prop_camera:getLoc()) --end end local timer = MOAITimer.new() timer:setSpan(0.5) timer:setMode(MOAITimer.LOOP) timer:setListener(MOAITimer.EVENT_TIMER_END_SPAN,timerfps,0) timer:start()

Be the first to comment

Sign in with OpenID