Fancy3D引擎sample学习(D&E)

1、drawboard

在三个画板内画三角形。

db = _DrawBoard.new(w, h) 创建画板指定宽高

指定画板的种类,db:drawImage(...) db:draw3DImage(...) db:drawBillBoard(...)

最后在_rd:useDrawBoard(db, color)   和 _rd:resetDrawBoard() 之间画三角形

_rd:drawTriangle(x1,y1,x2,y2,x3,y3,color)

PS:貌似最后画三角形的时候,三角形的x,y都是相对于画板中的。

 1 ---- DrawBoard Sample ----
 2 
 3 _dofile("cameraControl.lua")
 4 
 5 db = _DrawBoard.new(1000, 1000)
 6 
 7 function draw()
 8     _rd:drawTriangle(200, 0, 50, 200, 350, 200, _Color.Blue)
 9     _rd:fillTriangle(200, 0, 50, 200, 350, 200, _Color.Red)
10 end
11 
12 function boardDraw()
13     _rd:drawAxis(500)
14     db:drawImage(0, 0, 200, 200)
15     db:draw3DImage(0, 0, 0, 20, 10, 10, 30, 30, 15)
16     db:drawBillboard(50, 50, 0, 5, 5)
17 end
18 
19 _app:onIdle(boardDraw)
20 
21 _rd:drawAxis(500)
22 _rd:useDrawBoard(db, _Color.White)
23 draw()
24 _rd:resetDrawBoard()
25 
26 db:saveToFile("board.bmp", 0)
View Code

 2、drawboard-blocker

观察girl在四个挡板中的动作,当出现半透明遮挡后,改变颜色

_rd.blockColor = _Color.XX

_rd.blockee = true  设置半透明遮挡后,当物体有遮挡出现时会呈现XX颜色

3、drawboard-copy2image

程序主要为了说明_DrawBoard类是特殊的_Image类

img = _Image.new(db) 相当于复制这个_DrawBoard到img中

PS:

local x = _rd.w / 2
local y = _rd.h / 2     -- 这里就是找到屏幕的中心点

img:drawImage(x - 200, y - 150, x + 200, y + 150)   然后设置img的大小

 1 _dofile('cameracontrol.lua')
 2 _rd.camera.eye = _Vector3.new(10, 10, 10)
 3 
 4 cube = _mf:createCube()
 5 teapot = _mf:createTeapot()
 6 plane = _mf:createPlane()
 7 
 8 sky = _SkyLight.new()
 9 sky.direction = _Vector3.new(-1, -1, -1)
10 sky.color = _Color.Green
11 
12 _rd:useLight(_AmbientLight.new())
13 _rd:useLight(sky)
14 
15 db = _DrawBoard.new(400, 300)
16 _rd:useDrawBoard(db, _Color.Gray)
17     _rd:pushMatrix3D(_Matrix3D.new():setScaling(20, 20, 20))
18         plane:drawMesh()
19     _rd:popMatrix3D()
20 
21     _rd:pushMatrix3D(_Matrix3D.new():setTranslation(-2, 0, 1))
22         cube:drawMesh()
23     _rd:popMatrix3D()
24 
25     _rd:pushMatrix3D(_Matrix3D.new():setTranslation(2, 0, 1))
26         teapot:drawMesh()
27     _rd:popMatrix3D()
28 _rd:resetDrawBoard()
29 _rd:popLight()
30 _rd:popLight()
31 
32 img = _Image.new(db)
33 db = nil
34 _gc()
35 
36 _app:onIdle(function(e)
37     local x = _rd.w / 2
38     local y = _rd.h / 2
39     img:drawImage(x - 200, y - 150, x + 200, y + 150)
40 end)
View Code

 4、drawboard-edge

_rd.edge = true  -- 设置是否描边

_rd.edgeColor = _Color.XX -- 设置描边颜色

5、drawboard-glow

_rd.glowFactor = number 设置亮度,范围【0,1】

6、drawboard-hollow

如果在_rd中指定了一个_DrawBoard那么里面的坐标都是相对于这个画板的,比如fillRect()填充矩形

 1 _sys:addPath('res')
 2 
 3 _rd.bgColor = _Color.DarkBlue
 4 
 5 db = _DrawBoard.new(400, 300)
 6 _rd:useDrawBoard(db, 0x80808080)
 7 _rd:fillRect(50, 50, 350, 250, _Color.Red)
 8 
 9 bl = _Blender.new()
10 bl:copy()
11 _rd:useBlender(bl)
12 _rd:fillRect(100, 100, 300, 200, 0x80808080)
13 _rd:popBlender()
14 
15 _rd:resetDrawBoard()
16 
17 _app:onIdle(function(e)
18     local x = _rd.w / 2
19     local y = _rd.h / 2
20     db:drawImage(x - 200, y - 150, x + 200, y + 150)
21 end)
View Code

 7、drawboard-shadow

设置人物动画的阴影效果

_rd.shadowLight = _Vector3.new(1, 1, -1)  -- 设置光的方向

_rd.shadowColor = _Color.XX                    -- 设置阴影的颜色

_rd.shadowCaster = true

msh:drawMesh()     ...

_rd.shadowCaster = false       -- 设置发射投影的meh

_rd.shadowReceiver = true

plane:drawMesh()    ...

_rd.shawReceiver = false         -- 设置接收投影的meh

 1 _dofile( "cameraControl.lua" )
 2 _sys:addPath( "res" );
 3 
 4 msh = _Mesh.new( "face.skn" );
 5 msh:addSubMesh( "body1.skn" );
 6 msh:addSubMesh( "hair.skn" );
 7 msh:addSubMesh( "leg1.skn" );
 8 msh:addSubMesh( "arm.skn" );
 9 
10 anima = msh:attachSkeleton( "girl.skl" ):addAnima( "san1.san" );
11 anima.loop = true;
12 anima:play( );
13 
14 plane = _mf:createPlane( );
15 plane.transform:setScaling( 100, 100, 100 );
16 
17 _rd.shadowLight = _Vector3.new( 1, 1, -1 );
18 _rd.shadowColor = _Color.Black;
19 
20 db = _DrawBoard.new( 800, 600 );
21 _app:onIdle( function( )
22     _rd:drawAxis( 100 );
23 
24     _rd.shadowReceiver = true;                          -- 地面接收产生阴影效果
25     plane:drawMesh( );
26     _rd.shadowReceiver = false;
27     
28     _rd.shadowCaster = true;                            -- 人物投射
29     msh:drawMesh( );
30     _rd.shadowCaster = false;
31 
32     -- Use drawboard.
33     _rd:useDrawBoard( db, _Color.Gray );
34 
35     _rd.shadowReceiver = true;
36     plane:drawMesh( );
37     _rd.shadowReceiver = false;
38     
39     _rd.shadowCaster = true;
40     msh:drawMesh( );
41     _rd.shadowCaster = false;
42     
43     _rd:resetDrawBoard( );
44     
45     db:drawImage( 0, 0, 400, 300 );
46 end )
47 
48 _app:onKeyDown( function( kc )
49     if kc == _System.KeySpace then
50         _sys.gpuSkinning = _sys.gpuSkinning == false;
51         print( "Use GPU:", _sys.gpuSkinning );
52     end
53 end )
View Code

 8、efficiency-meshrender

按A随机添加一百个meh,空格停止动画,S设置阴影效果,DEL删除,R重新设置摄像机位置

PS:onIdle()相当于刷新屏幕的过程;scene:render()调用scene:onRender(function(node)...end)

node是场景中的结点,例如:mesh

  1 --efficiency-meshrender
  2 -- Press the 'a' key add 100 role,press the 'space' key control role action,press 'delete' key delete all role,press 's' key control shadow,press 'r' reset camera
  3 -- In the upper-left corner of the screen to record the number of the role
  4 _dofile('cameracontrol.lua')
  5 _sys:addPath('res')
  6 scene = _Scene.new();
  7 font1 = _Font.new('Arial', 12)
  8 font2 = _Font.new('Arial', 12)
  9 plane = _mf:createPlane()
 10 _mf:paintDiffuse(plane, _Color.Gray)
 11 mat1 = _Matrix3D.new():setScaling(400, 400, 400)
 12 animaflag = false
 13 shflag = false
 14 num, count, tab = 100, 0, {}
 15 
 16 _rd.bgColor = _Color.DarkBlue
 17 
 18 function getmyMesh()
 19     local role = _Mesh.new()
 20     role:addSubMesh('arm.skn')
 21     role:addSubMesh('face.skn')
 22     role:addSubMesh('hair.skn')
 23     role:addSubMesh('body2.skn')
 24     role:addSubMesh('leg2.skn')
 25     skl = _Skeleton.new('girl.skl')
 26     role:attachSkeleton(skl)
 27     san = skl:addAnima('idle.san')
 28     san.loop = true
 29     san.name = 'idle'
 30     san:play()
 31     return role
 32 end
 33 function addMesh(num)
 34     for i = 1, num do
 35         local s = getmyMesh()
 36         tab[#tab + 1] = scene:add(s, _Matrix3D.new():setTranslation(_random(-150, 150), _random(-150, 150), 0))
 37     end
 38 end
 39 function delNode()
 40     for i = 1, #tab do
 41         scene:del(tab[i])
 42     end
 43 end
 44 function animaStopOrPlay()
 45     for i = 1, #tab do
 46         local san = tab[i].mesh.skeleton:getAnima('idle')
 47         if not animaflag then
 48             san:stop()
 49         elseif animaflag then 
 50             san.loop = true
 51             san:play()
 52         end
 53     end
 54 end
 55 function reset()
 56     _rd.camera.eye.x = 100
 57     _rd.camera.eye.y = 100
 58     _rd.camera.eye.z = 100
 59     _rd.camera.look.x = 0
 60     _rd.camera.look.y = 0
 61     _rd.camera.look.z = 0
 62 end
 63 
 64 scene:onRender(function(node)
 65     if node.mesh then
 66         if shflag then
 67             _rd.shadowCaster = true
 68             _rd.shadowLight = _Vector3.new(0, 1, -1);
 69         end
 70         count = count + 1
 71         node.mesh:drawMesh()
 72         if shflag then
 73             _rd.shadowCaster = false
 74         end
 75     end
 76 end)
 77 
 78 _app:onKeyDown(function(x)
 79         if x == _System.KeyA then
 80             addMesh(num)
 81         elseif x == _System.KeySpace then
 82             animaStopOrPlay()
 83             animaflag = not animaflag
 84         elseif x == _System.KeyDel then
 85             delNode()
 86         elseif x == _System.KeyR then
 87             reset()
 88         elseif x == _System.KeyS then
 89             shflag = not shflag
 90         end
 91 end)
 92 
 93 _app:onIdle(function(elapse)
 94     _rd:drawAxis(50)
 95     count = 0
 96     if shflag then
 97         _rd.shadowReceiver = true
 98     end
 99     _rd:pushMatrix3D(mat1)
100         plane:drawMesh()
101     _rd:popMatrix3D()
102     if shflag then
103         _rd.shadowReceiver = false
104     end
105     scene:render();
106     font1:drawText(5, 20, 0, 0, 'The role number is ' .. count)
107     font2:drawText(5, 0, 0, 0, "Press the 'a' key add 100 role,press the 'space' key control role action,press 'delete' key delete all role,press 's' key control shadow,press 'r' reset camera")
108 end)
View Code

 

posted @ 2013-11-23 14:24  丿聪丶  阅读(436)  评论(1)    收藏  举报