首页 > 技术文章 > Fancy3D引擎sample学习(F&G&I)

hlwangcong 2013-11-25 13:21 原文

1、file

file1 = _File.new()  创建一个_File

file1:create('filename.text')  如果创建成功返回true

file1:write('文本内容')      在文件中写

file1:close()                 关闭文件

file1:open('file.txt')     打开文件

text = file1:read()      返回文件中的内容     -- print(text)  如果在读之前,关闭了文件,那么结果为空

 1 file1 = _File.new();
 2 result = false;
 3 result = file1:create('file.txt');
 4 if result == true then
 5     print('create file.txt ok')
 6 end
 7 file1 = file1:write('sucess');
 8 file1:close();
 9 file2 = _File.new();
10 result = false;
11 result = file2:open('file.txt');
12 if result == true then
13     print('open file.txt ok')
14 end
15 text = file2:read();
16 print(text);
17 file2:close();
18 
19 fe = _File.new()
20 
21 fe:write('Fancy guo')
22 fe:close()
23 print(fe:read())
View Code

 2、filter

filter1:addWord("abc")  添加敏感词

filter1:addFile("filter.txt") 添加敏感词文件

filter1.replacer = "****"  添加敏感词被替换成的字符串

filter1:filter(stringname) 对字符串进行敏感词过滤,返回字符串

 1 -----------------------------
 2 ---filter Sample
 3 ---写于7月10日
 4 ---written by syf
 5 -----------------------------
 6 _sys:addPath('res')
 7 
 8 print("Show the begining of the string")
 9 
10 needFilter = "abcdefghijk"
11 
12 print(needFilter)
13 
14 filter = _Filter.new()
15 
16 print("Add the filter word".."abc")
17 
18 filter:addWord("abc")
19 
20 
21 print("Add the filter file \"filter.txt\"")
22 
23 filter:addFile("filter.txt")
24 
25 print("Change the replacer string")
26 
27 filter.replacer = "****"
28 
29 print("Set the property divided equals true")
30 
31 --filter.divided = true
32 
33 print("Start filter")
34 
35 over = filter:filter(needFilter)
36 
37 print(over)
View Code

 3、fog

fog = _Fog.new()

fog.near  起雾的距离     fog.far  达到最大浓度的距离     这里的距离是指以要渲染的mesh为中心周围的距离,当camera.eye大于fog.far的距离时,那么一直都是有最大浓度雾的效果。

_rd:useFog(fog)

teapot:drawMesh()

_rd:popFog()

 1 _sys:addPath('res')
 2 _dofile('cameracontrol.lua')
 3 fog = _Fog.new()
 4 fog.near, fog.far, fog.color = 20, 70, _Color.Red
 5 --_rd.bgColor = fog.color
 6 _rd.camera.eye = _Vector3.new(50, 50, 50)
 7 _rd:useLight(_AmbientLight.new())
 8 _rd:useLight(_SkyLight.new())
 9 
10 pp = _ParticlePlayer.new()
11 pp:play('pfx_env_huoba.pfx', _Matrix3D.new():setTranslation(20, 0, 0))
12 pp:play('pfx_env_huoba.pfx', _Matrix3D.new():setTranslation(-20, 0, 0))
13 pp:play('pfx_env_huoba.pfx', _Matrix3D.new():setTranslation(0, 20, 0))
14 pp:play('pfx_env_huoba.pfx', _Matrix3D.new():setTranslation(0, -20, 0))
15 
16 teapot = _mf:createTeapot()
17 teapot.transform:setScaling(5, 5, 5)
18 mats = {}
19 mats[1] = _Matrix3D.new():setTranslation(20, 20, 0)
20 mats[2] = _Matrix3D.new():setTranslation(-20, 20, 0)
21 mats[3] = _Matrix3D.new():setTranslation(20, -20, 0)
22 mats[4] = _Matrix3D.new():setTranslation(-20, -20, 0)
23 
24 font = _Font.new('Arial', 10)
25 font.textColor = _Color.Yellow
26 
27 distance = fog.far - fog.near
28 
29 function newfog()
30     fog.far = distance + fog.near
31     _rd:popFog()
32     _rd:useFog(fog)
33 end
34 
35 _app:onKeyDown(function(key)
36     if key == _System.KeyW then
37         if fog.near < 100 then
38             fog.near = fog.near + 2
39             newfog()
40         end
41     elseif key == _System.KeyS then
42         if fog.near > 0 then
43             fog.near = fog.near - 2
44             newfog()
45         end
46     elseif key == _System.KeyE then
47         if distance < 100 then
48             distance = distance + 2
49             newfog()
50         end
51     elseif key == _System.KeyD then
52         if distance > 0 then
53             distance = distance - 2
54             newfog()
55         end
56     end
57 end)
58 
59 _rd:useFog(fog)
60 
61 _app:onIdle(function(e)
62     font:drawText(0, 0, 'Press WS to adjust fog near')
63     font:drawText(0, 14, 'Press ED to adjust fog far')
64     for i = 1, 4 do
65         _rd:pushMatrix3D(mats[i])
66             teapot:drawMesh()
67         _rd:popMatrix3D()
68     end
69     _rd:drawAxis(100)
70 end)
View Code

 4、font-align

fontText:drawText(x1, y1, x2, y2, string, align) 这个对其方式align是相对于x1,y1,x2,y2这个矩形的

这个align = {} 的数据结构值得借鉴,而且定义了一个key叫做value,它的值是一个function,返回水平+垂直的对其方式,根据它的和可以唯一确定对其方式。

align = {}
align.hor = {_Font.hLeft, _Font.hCenter ,_Font.hRight}
align.ver = {_Font.vTop, _Font.vCenter, _Font.vBottom}
align.v, align.h = 2, 2
align.value = function() return align.hor[align.h] + align.ver[align.v] end

_app:onKeyDown(function(key)
    if key == _System.KeyW then
        if (align.v > 1) then
            align.v = align.v - 1
        end
    elseif key == _System.KeyS then
        if (align.v < #align.ver) then
            align.v = align.v + 1
        end
    elseif key == _System.KeyA then
        if (align.h > 1) then
            align.h = align.h - 1
        end
    elseif key == _System.KeyD then
        if (align.h < #align.hor) then
            align.h = align.h + 1
        end
    end
end)

fontText = _Font.new('宋体', 19)
English = 'hello fancyguo'
Chinese = '你好'
Korean = '안녕하세요'
Japanese = 'こんにちは'

font = _Font.new('Arial', 10)
font.textColor = _Color.Yellow

_app:onIdle(function(e)
    font:drawText(0, 0, 'Press WSAD to adjust text pos')

    local x, y = _rd.w / 2 - 150, _rd.h / 2 - 250
    _rd:drawRect(x, y, 400 + x, 100 + y, 0xffffff00)
    _rd:drawRect(x, 100 + y, 400 + x, 200 + y, 0xffffff00)
    _rd:drawRect(x, 200 + y, 400 + x, 300 + y, 0xffffff00)
    _rd:drawRect(x, 300 + y, 400 + x, 400 + y, 0xffffff00)
    fontText:drawText(x, y, 400 + x, 100 + y, English, align.value())
    fontText:drawText(x, 100 + y, 400 + x, 200 + y, Chinese, align.value())
    fontText:drawText(x, 200 + y, 400 + x, 300 + y, Korean, align.value())
    fontText:drawText(x, 300 + y, 400 + x, 400 + y, Japanese, align.value())
    print(align.value(), align.hor[align.h], align.ver[align.v])
end)
View Code

 5、font-cutline

通过一些按键对文本进行排版。

strings = font:cutLines(str, textLen, 40)   

str被分割的字符串,textLen指定的行宽实现自动换行,40是供排版使用的像素

返回值是被分割后的每行的字符串(结尾没有换行)所以要这样导出strings

for i, v in ipairs(strings) do text = text .. v .. '\n' end

 1 strChi = '青果灵动是国内第一家自主研发3D网页游戏的技术力量型公司,我们执着于做好玩的游戏,我们是3D网页游戏领域中的先行者。'
 2 strEng = 'Fancyguo is the first independent researching and developing 3D technology-based webpage game company, we are dedicated to developing the best game.'
 3 str = strEng .. strChi
 4 
 5 bold = false
 6 italic = false
 7 underline = false
 8 arial = false
 9 size = 19
10 width = 400
11 spacing = 10
12 
13 function str2text()
14     text = ''
15     stringLen = font:stringWidth(str)
16     textLen = math.min(stringLen, width)
17     strings = font:cutLines(str, textLen, 40)
18     for i, v in ipairs(strings) do text = text .. v .. '\n' end
19 end
20 
21 function newfont()
22     font = _Font.new(arial and 'arial' or '宋体', size, 0, 0, bold, italic, underline)
23     font.lineSpace = spacing
24     str2text()
25 end
26 newfont()
27 str2text()
28 
29 function SwitchTorF(flag)
30     if flag then
31         flag = false
32     else
33         flag = true
34     end
35     return flag
36 end
37 
38 _app:onKeyDown(function(key)
39     if key == _System.KeyQ then
40         if(spacing < 100) then spacing = spacing + 1 newfont() end
41     elseif key == _System.KeyA then
42         if(spacing > 0) then spacing = spacing - 1 newfont() end
43     elseif key == _System.KeyW then
44         if(width < 1000) then width = width + 5 str2text() end
45     elseif key == _System.KeyS then
46         if(width > 0) then width = width - 5  str2text() end
47     elseif key == _System.KeyE then
48         if(size < 50) then size = size + 1 newfont() end
49     elseif key == _System.KeyD then
50         if(size > 2) then size = size - 1 newfont() end
51     elseif key == _System.Key1 then
52         arial = SwitchTorF(arial)
53         newfont()
54     elseif key == _System.Key2 then
55         bold = SwitchTorF(bold)
56         newfont()
57     elseif key == _System.Key3 then
58         italic = SwitchTorF(italic)
59         newfont()
60     elseif key == _System.Key4 then
61         underline = SwitchTorF(underline)
62         newfont()
63     end
64 end)
65 
66 noticefont = _Font.new('Arial', 10)
67 noticefont.textColor = _Color.Yellow
68 
69 _app:onIdle(function(e)
70     noticefont:drawText(0, 0, 'Press 1 to switch typeface')
71     noticefont:drawText(0, 14, 'Press 2 to switch bold')
72     noticefont:drawText(0, 28, 'Press 3 to switch italic')
73     noticefont:drawText(0, 42, 'Press 4 to switch underline')
74     noticefont:drawText(0, 56, 'Press QA to adjust spacing')
75     noticefont:drawText(0, 70, 'Press WS to adjust width')
76     noticefont:drawText(0, 84, 'Press ED to switch size')
77 
78     local x, y = _rd.w / 2 - 150, _rd.h / 2 - 150
79     local r, b = font:stringWidth(text) + x, font:stringHeight(text) + y
80     font:drawText(x, y, r, b, text, _Font.hLeft + _Font.vTop, spacing)
81     _rd:drawRect(x, y, r, b, 0xffffff00)
82 end)
View Code

 6、graphicsdata

主要包括_Graphicsdata类的一些操作,该类是与_Mesh相关的数据,maker、摄像机、光、雾、轨迹等,在屏幕上打印结果。

Maker应该就是一些矩阵的变换

addmat = _Matrix3D.new()

addmat.name = 'mk'

sen.graData:addMarker( addmat )  -- 添加一个maker

sen.graData:setMarkers( markerArraymat ) -- 添加一组marker

sen.graData:getMarkerCount()       -- marker数量

sen.graData:getMarkers()             -- 返回一组marker

sen.graData:getMarkerIndex('mk')   -- 返回mk的索引值,一般第一个添加的索引值是1,以此类推

其他方法和maker用法大同小异。

  1 _sys:addPath("res")
  2 dofile('cameraControl.lua')
  3 local sen = _Scene.new('test.sen')
  4 
  5 _app:onIdle(function(e)
  6     _rd:drawAxis(30)
  7     sen:render();
  8 end)
  9 
 10 --marker
 11 _info("Now,number of Marker:  " .. sen.graData:getMarkerCount() );
 12 
 13 local addmat = _Matrix3D.new();
 14 addmat.name ="mk"
 15 
 16 sen.graData:addMarker( addmat );
 17 _info("After add a marker,number of Marker:  " .. sen.graData:getMarkerCount() );
 18 
 19 _info("Index of mk: " .. sen.graData:getMarkerIndex('mk'))
 20 
 21 sen.graData:clearMarkers();
 22 _info("After clear all marker,number of Marker:  " .. sen.graData:getMarkerCount() );
 23 
 24 local markerArraymat = {};
 25 local markermat1 = _Matrix3D.new();
 26 markermat1.name = 'markermat1';
 27 markermat1:setTranslation(100,100,100);
 28 table.insert(markerArraymat,markermat1);
 29 local markermat2 = _Matrix3D.new();
 30 markermat2.name = 'markermat2';
 31 markermat2:setTranslation(200,200,200);
 32 table.insert(markerArraymat,markermat2);
 33 sen.graData:setMarkers( markerArraymat );
 34 _info("After set markers,number of Marker:  " .. sen.graData:getMarkerCount() );
 35 
 36 local markerarr = sen.graData:getMarkers();
 37 for i ,v in pairs(markerarr) do
 38     local p = v:getTranslation()
 39     print(i,p.x,p.y,p.z)
 40 end
 41 
 42 sen.graData:delMarker( 2 );
 43 _info("After del the second marker,number of Marker:  " .. sen.graData:getMarkerCount() );
 44 
 45 --Light
 46 local al = sen.graData:getLight('ambient');
 47 local sl = sen.graData:getLight('skylight');
 48 _info("Now,number of Light:  " .. sen.graData:getLightCount() );
 49 
 50 _info("Index of skylight: " .. sen.graData:getLightIndex('skylight'))
 51 
 52 sen.graData:addLight( sen.graData:getLight( 1 ) );
 53 _info("After add a Light,number of Light:  " .. sen.graData:getLightCount() );
 54 
 55 sen.graData:delLight( 1 );
 56 _info("After del a light,number of light:  " .. sen.graData:getLightCount() );
 57 
 58 sen.graData:clearLights(  );
 59 _info("After clear all lights,number of light:  " .. sen.graData:getLightCount() );
 60 
 61 local lightarr = {sl,al};
 62 sen.graData:setLights(lightarr)
 63 _info("After set lights,number of light:  " .. sen.graData:getLightCount() );
 64 
 65 --Orbit
 66 local p = { { pos = _Vector3.new(0,0,0)}, { pos = _Vector3.new(100,0,0) } };
 67 
 68 local wall = _Orbit.new( );
 69 wall:create( p );
 70 wall.name = "wall";
 71 sen.graData:addOrbit( wall );
 72 
 73 _info("Now,number of Orbit:  " .. sen.graData:getOrbitCount() );
 74 
 75 local tos = sen.graData:getOrbits();
 76 for i,v in pairs(tos) do
 77     local p1 = v.points[1]
 78     local p2 = v.points[2]
 79     print(p1.x,p1.y,p1.z)
 80     print(p2.x,p2.y,p2.z)
 81 end
 82 
 83 sen.graData:addOrbit( sen.graData:getOrbit( 1 ) );
 84 _info("After add a orbit,number of orbit:  " .. sen.graData:getOrbitCount() );
 85 
 86 sen.graData:delOrbit( 1 );
 87 _info("After del a orbit,number of orbit:  " .. sen.graData:getOrbitCount() );
 88 
 89 sen.graData:clearOrbits(  );
 90 _info("After clear all orbits,number of orbit:  " .. sen.graData:getOrbitCount() );
 91 
 92 local o = _Orbit.new()
 93 
 94 local pt = {};
 95 pt[1]={time = 0,pos = _Vector3.new(0,0,0)}
 96 pt[2]={time = 1000,pos = _Vector3.new(100,0,0)}
 97 o:create(pt)
 98 
 99 local orbitarr = { o ,wall};
100 sen.graData:setOrbits(orbitarr)
101 _info("After set orbits,number of orbit:  " .. sen.graData:getOrbitCount() );
View Code

 7、grass

通过方向键控制茶壶的移动,同时摄像机也跟着茶壶进行视角的移动,移动的过程中草丛产生摇摆的特效

scene.terrain:acrossGrass(pos, region, power) 

pos是圆心,这里每次都取茶壶的位移量;region是摆动半径;power摆动力度;

PS:左移摄像机视角的时候,要eye-look向量差乘camera.up向量,因为是左手坐标系,所以得到左方向的向量。

 1 _sys:addPath( 'res' )
 2 _dofile( "cameraControl.lua" );
 3 
 4 scene = _Scene.new( "grass.sen" );
 5 
 6 teapot = _mf:createTeapot( );
 7 teapot.transform:setTranslation( 0, 0, 5 );
 8 
 9 _rd.camera:moveRadius( -120 );
10 
11 scene:onRender( 
12     function ( node )
13         
14         if node.terrain then
15             node.terrain:draw( );
16             return;
17         end
18         
19         if pickedNode == node then
20             _rd:useBlender( blender );
21         end
22         
23         _rd:push3DMatrix( node.transform );
24         node.mesh:drawMesh( );
25         node.mesh:drawBoundBox( );
26         _rd:pop3DMatrix( );
27         
28         if pickedNode == node then
29             _rd:popBlender( );
30         end
31     end
32 )
33 
34 function render( e )
35     
36     _rd:drawAxis( 100 );
37     scene:render( );
38     teapot:drawMesh( );
39     
40     _gc( );
41 end
42 _app:onIdle( render )
43 
44 function keyDown( keycode )
45     local forward = _Vector3.sub( _rd.camera.look, _rd.camera.eye );
46     forward.z = 0;
47     forward:normalize( );
48     local left = _Vector3.cross( forward, _rd.camera.up );
49 
50     if keycode == _System.KeyUp then
51         teapot.transform:mulTranslationRight( forward );
52         _rd.camera.look.x = _rd.camera.look.x + forward.x;
53         _rd.camera.look.y = _rd.camera.look.y + forward.y;
54         _rd.camera.eye.x = _rd.camera.eye.x + forward.x;
55         _rd.camera.eye.y = _rd.camera.eye.y + forward.y;
56         scene.terrain:acrossGrass( teapot.transform:getTranslation( ), 10.0, 3.01 );
57     end
58     
59     if keycode == _System.KeyDown then
60         teapot.transform:mulTranslationRight( -forward.x, -forward.y, 0 );
61         _rd.camera.look.x = _rd.camera.look.x - forward.x;
62         _rd.camera.look.y = _rd.camera.look.y - forward.y;
63         _rd.camera.eye.x = _rd.camera.eye.x - forward.x;
64         _rd.camera.eye.y = _rd.camera.eye.y - forward.y;
65         scene.terrain:acrossGrass( teapot.transform:getTranslation( ), 10.0, 3.01 );
66     end
67 
68     if keycode == _System.KeyLeft then
69         teapot.transform:mulTranslationRight( left.x, left.y, 0 );
70         _rd.camera.look.x = _rd.camera.look.x + left.x;
71         _rd.camera.look.y = _rd.camera.look.y + left.y;
72         _rd.camera.eye.x = _rd.camera.eye.x + left.x;
73         _rd.camera.eye.y = _rd.camera.eye.y + left.y;
74         scene.terrain:acrossGrass( teapot.transform:getTranslation( ), 10.0, 3.01 );
75     end
76 
77     if keycode == _System.KeyRight then
78         teapot.transform:mulTranslationRight( -left.x, -left.y, 0 );
79         _rd.camera.look.x = _rd.camera.look.x - left.x;
80         _rd.camera.look.y = _rd.camera.look.y - left.y;
81         _rd.camera.eye.x = _rd.camera.eye.x - left.x;
82         _rd.camera.eye.y = _rd.camera.eye.y - left.y;
83 
84         scene.terrain:acrossGrass( teapot.transform:getTranslation( ), 10.0, 3.01 );
85     end
86 end    
87 _app:onKeyDown( keyDown )
View Code

 8、image-processhsl & processrgb

通过按键更改图片的参数

image:processHSL(hue, saturation, lightness)  控制色调、饱和度、亮度

image:processRGB(modulate, additive, subtract) 控制乘色、加色、减色

9、indicator

_Indicator 是一个场景指示器,绑定在一个transform上,通过鼠标拖动来改变transform.

indicator = _Indicator.new()

indicator.state = number -- number为0、1、2 分别代表移动、旋转、缩放

indicator:operStart(x, y)   -- 接收鼠标按下的操作 一般写在_app:onMouseDown()的回调里

indicator:operMove(x, y)   -- 接收鼠标移动的操作 一般写在_app:onMouseMove()的回调里

indicator:operEnd(x, y)     -- 接收鼠标抬起的操作 一般写在_app:onMouseUp()的回调里

推荐阅读