_ModelFactory

模型的工厂类,功能包括几何模型的创建、模型相关数据的读取与保存、粒子的读取与保存等相关操作。该类是一个单例,在程序开始已经创建的了名称为_mf的对象。 全部展开

单例展开

  • + _mf : _ModelFactory

    程序开始时自动创建。脚本中应该使用此对象。

    示例

        meshs.Cube = _mf:createCube()

只读属性展开

  • + ImageBmp : uint

    BMP图像。

  • + ImageDds : uint

    DDS图像。

  • + ImageGif : uint

    GIF图像。

  • + ImageJpg : uint

    JPG图像。

  • + ImagePng : uint

    PNG图像。

  • + ImageTga : uint

    TGA图像。

  • + ImageTiff : uint

    TIFF图像。

构造方法展开

  • + function _ModelFactory() : _ModelFactory

    示例

        mf = _ModeFactory.new()

公共方法展开

  • + function addImageHeader(resname: string, width: int, height: int)

    在图片文件附加一个长宽值,当作图片的原始尺寸。

    • 参数
    • resname: 图片文件名。
    • width: 宽。
    • height:长。
  • + function buildAnimaTexture(srcname:string, desname:string, interval:uint, row:uint, column:uint, num:uint)

    合成序列图。

    • 参数
    • srcname : 原文件名。
    • desname: 序列图文件名。
    • interval: 每帧的间隔。
    • row: 分割成的行数。
    • column: 分割成的列数。
    • num: 一共多少帧动画。

    示例

        _mf:buildAnimaTexture( 'asadasdas.png', 'asadasdas.gff', 80, 4, 4, 16 )
  • + function buildAnimaTexture(srcnames:string[], desname:string, interval:uint)

    合成序列图。

    • 参数
    • srcnames: 原文件名数组。
    • desname: 序列图文件名。
    • interval: 每帧的间隔。

    示例

        local texs = {}
    table.insert(texs, 'asadasdas01.png')
    table.insert(texs, 'asadasdas02.png')
    table.insert(texs, 'asadasdas03.png')
    table.insert(texs, 'asadasdas04.png')
    _mf:buildAnimaTexture( texs, 'asadasdas.gff', 50 )
  • + function buildSkeletonBoundSphere(sklname: string, sknname: string)

    用于给骨骼增加Bound Sphere,应用到骨骼碰撞的项目可以在出版的时候调用该接口在骨骼文件中直接增加碰撞信息,之后就不需要用到碰撞蒙皮文件了。

    • 参数
    • sklname: skl文件名。
    • sknname: skn文件名。
      • 示例
    •  sample/modelfactory-buildSkeletonBoundSphere.lua
  • + function changeImageFormat(srcname : string, desname : string, format : uint, quality : uint)

    转换图片格式。

    • 参数
    • srcname : 原文件名。
    • desname : 转换后的文件名。
    • format : 贴图格式。
    • quality : 转换的质量参数。
      • 示例
    •  _mf:changeImageFormat('a.bmp', 'a.jpg', _ModelFactory.ImageJpg, 10)
  • + function changeImagesFormat(srcnames : string [], format : uint, quality : uint)

    转换图片格式。

    • 参数
    • srcnames : 原文件名。
    • format : 贴图格式。
    • quality : 转换的质量参数。
      • 示例
    •  local files = {'a.bmp', 'b.bmp'}
      _mf:changeImagesFormat(files, _ModelFactory.ImageJpg, 10)
  • + function checkAnimation(filename : string)

    将tag文件的信息合并到动画文件中。

    • 参数
    • filename : 动画文件名。
      • 示例
    •  msh: checkAnimation('idle.san')
  • + function checkModel(filename : string)

    压缩旧版的mesh文件。

    • 参数
    • filename : mesh文件名。
      • 示例
    •  msh:checkModel('sword.msh')
  • + function checkTerrain(filename : string, layerfile : bool)

    将旧版的ten文件或者sen文件转为新版。

    • 参数
    • filename : ten文件名或sen文件名。
    • layerfile : 是否将地形的透明通道分离出来,单独存为同文件名的.lay文件。
      • 示例
    •  msh:checkTerrain('skill.sen', false)
  • + function createCone() : _Mesh

    创建圆锥体模型。

    • 返回
    • 创建的_Mesh对象。
      • 示例
    •  _rd.camera.eye = _Vector3.new(5, 5, 5)
      meshs = _mf:createCone()
      _app:onIdle(function(e)
      meshs:drawMesh()
      end)
  • + function createCube() : _Mesh

    创建立方体模型。

    • 返回
    • 创建的_Mesh对象。
      • 示例
    •  _rd.camera.eye = _Vector3.new(5, 5, 5)
      meshs = _mf:createCube()
      _app:onIdle(function(e)
      meshs:drawMesh()
      end)
  • + function createCylinder() : _Mesh

    创建圆柱体模型。

    • 返回
    • 创建的_Mesh对象。
      • 示例
    •  _rd.camera.eye = _Vector3.new(5, 5, 5)
      meshs = _mf:createCylinder()
      _app:onIdle(function(e)
      meshs:drawMesh()
      end)
  • + function createHemiSphere() : _Mesh

    创建半球模型。

    • 返回
    • 创建的_Mesh对象。
      • 示例
    •  _rd.camera.eye = _Vector3.new(5, 5, 5)
      meshs = _mf:createHemiSphere()
      _app:onIdle(function(e)
      meshs:drawMesh()
      end)
  • + function createPlane() : _Mesh

    创建平面模型。

    • 返回
    • 创建的_Mesh对象。
      • 示例
    •  _rd.camera.eye = _Vector3.new(5, 5, 5)
      meshs = _mf:createPlane()
      _app:onIdle(function(e)
      meshs:drawMesh()
      end)
  • + function createPyramid() : _Mesh

    创建角锥模型。

    • 返回
    • 创建的_Mesh对象。
      • 示例
    •  _rd.camera.eye = _Vector3.new(5, 5, 5)
      meshs = _mf:createPyramid()
      _app:onIdle(function(e)
      meshs:drawMesh()
      end)
  • + function createSphere() : _Mesh

    创建球体模型。

    • 返回
    • 创建的_Mesh对象。
      • 示例
    •  _rd.camera.eye = _Vector3.new(5, 5, 5)
      meshs = _mf:createSphere()
      _app:onIdle(function(e)
      meshs:drawMesh()
      end)
  • + function createTeapot() : _Mesh

    创建茶壶模型。

    • 返回
    • 创建的_Mesh对象。
      • 示例
    •  _rd.camera.eye = _Vector3.new(5, 5, 5)
      meshs = _mf:createTeapot()
      _app:onIdle(function(e)
      meshs:drawMesh()
      end)
  • + function cutImage(srcname : string, desname : string, x : uint, y : uint, w : uint, h : uint)

    分割图片。

    • 参数
    • srcname : 原文件名。
    • desname : 分割后的文件名。
    • x, y : 起始位置。
    • w, h : 长宽。
      • 示例
    •  _mf:cutImage('a.bmp', 'b.bmp', 0, 0, 32, 32)
  • + function isAlphaImage(resname : string) : bool

    判断一个图片文件是否带alpha通道。

    • 参数
    • resname: 文件名。
  • + function isSolidColorImage( resname : string ) : bool

    判断图片是否是纯色图片。

    • 参数
    • name: 文件名。
  • + function loadSpeed(resname : string) : number

    从文件读取模型的speed数据。

    • 参数
    • resname : 资源文件名。
    • 返回
    • speed值。
      • 示例
    •  _mf:loadSpeed('res/a.bmp')
  • + function optimizeAlphaImage(srcname: string, desname: string)

    优化alpha通道全为1的32位图片。

    • 参数
    • srcname: 源文件名。
    • desname: 目标文件名,未传入时覆盖原文件。
  • + function optimizeMesh(srcname : string, desname : string ) : bool

    用于减少模型顶点色,提高渲染效率,压缩文件大小。

    • 参数
    • srcname : 原文件名。
    • desname : 转换后的文件名,为nil时覆盖源文件。

    • 示例

    • Sample/modelfactory-optimizeMesh.lua

  • + function optimizeSkeletonAnimation(srcname : string, desname : string ) : bool

    用于减少骨骼动画冗余关键帧,压缩文件大小。

    • 参数
    • srcname : 原文件名。
    • desname : 转换后的文件名,为nil时覆盖源文件。

    • 示例

    • Sample/modelfactory-optimizeSkeletonAnimation.lua

  • + function paintDiffuse( msh : _Mesh, color : _uint )

    设定模型的漫反射颜色。

    • 参数
    • msh : 要设置颜色的模型。
    • color : 漫反射的颜色。

    示例

        _rd.camera.eye = _Vector3.new(0, 5, 0)
    meshs = _mf:createCube()
    _mf:paintDiffuse( meshs, _Color.Red )
    _app:onIdle(function(e)
    meshs:drawMesh()
    end)
  • + function paintSpecular( msh : _Mesh, color : _uint )

    设定模型的镜面反射颜色。

    • 参数
    • msh : 设置颜色的模型。
    • color : 镜面反射颜色。

    示例

        _rd.camera.eye = _Vector3.new(0, 5, 0)
    meshs = _mf:createCube()
    _mf:paintSpecular( meshs, _Color.Red )
    _app:onIdle(function(e)
    meshs:drawMesh()
    end)
  • + function resizeImage(srcname : string, desname : string, wratio: float, hratio: float)

    转换图片格式。

    • 参数
    • srcname : 原文件名。
    • desname : 转换后的文件名。
    • wratio: 横向缩放比例。
    • hratio: 纵向缩放比例。
      • 示例
    •  _mf:resizeImage('a.bmp', 'b.bmp', 0.5, 1.5)
  • + function resizeImagePowerOfTwo(srcname: string, desname: string[, square : bool])

    把图片拉伸到最近的2的幂。

    • 参数
    • srcname: 原始文件名。
    • dstname: 新文件名。
    • square: 拉伸为正方形(默认为false)。
  • + function resizeImages(srcname : string [], wratio: float, hratio: float)

    转换图片格式。

    • 参数
    • srcname : 原文件名数组。
    • wratio: 横向缩放比例。
    • hratio: 纵向缩放比例。
      • 示例
    • local a = {'a.bmp', 'b.bmp'}
      _mf:resizeImages(a, 0.5, 1.5)
  • + function saveSpeed(resname : string, s : number)

    保存模型的speed数据到文件。

    • 参数
    • resname : 资源文件名。
    • s : speed值。
      • 示例
    •  _mf:saveSpeed('res/a.san', 0.1)
  • + function transformShape( msh : _Mesh, mat : _Matrix3D )

    对模型进行变换。

    • 参数
    • msh : 要进行变换的模型。
    • mat : 变换矩阵。

    示例

        _rd.camera.eye = _Vector3.new(0, 0, 0)
    _rd.camera.look = _Vector3.new(0, 5, 0)
    mat = _Matrix3D.new()
    mat:setTranslation( 0, 5, 0, 0 )
    meshs = _mf:createCube()
    _mf:transformShape( meshs, mat )
    _app:onIdle(function(e)
    meshs:drawMesh()
    end)
  • + function transformTexcoord( msh : _Mesh, mat : _Matrix2D )

    对模型使用的纹理进行变换。

    • 参数
    • msh : 要进行纹理变换的模型。
    • mat : 变换矩阵。

    • 示例

       mat = _Matrix2D.new()
      meshs = _mf:createCube()
      _mf:transformTexcoord( meshs, mat )
      _app:onIdle(function(e)
      meshs:drawMesh()
      end)

代码示例

    _sys:addPath('res')
    _dofile('cameracontrol.lua')
    _rd.camera.eye = _Vector3.new(5, 5, 5)
    _rd:useLight(_AmbientLight.new())
    _rd:useLight(_SkyLight.new())

    meshs = {}
    meshs.Cube = _mf:createCube()
    meshs.Pyramid = _mf:createPyramid()
    meshs.Sphere = _mf:createSphere()
    meshs.Cylinder = _mf:createCylinder()
    meshs.Cone = _mf:createCone()
    meshs.Teapot = _mf:createTeapot()
    meshs.Plane = _mf:createPlane()

    curMesh = meshs.Cube

    textureFlag = true

    function newt()
    if textureFlag then
    curMesh:setTexture(_Image.new('stone_color.jpg'))
    else
    curMesh:setTexture()
    end
    end
    newt()

    _app:onKeyDown(function(key)
    if key == _System.Key1 then
    curMesh = meshs.Cube
    newt()
    elseif key == _System.Key2 then
    curMesh = meshs.Pyramid
    newt()
    elseif key == _System.Key3 then
    curMesh = meshs.Sphere
    newt()
    elseif key == _System.Key4 then
    curMesh = meshs.Cylinder
    newt()
    elseif key == _System.Key5 then
    curMesh = meshs.Cone
    newt()
    elseif key == _System.Key6 then
    curMesh = meshs.Teapot
    newt()
    elseif key == _System.Key7 then
    curMesh = meshs.Plane
    newt()
    elseif key == _System.KeySpace then
    if textureFlag then textureFlag = false else textureFlag = true end
    newt()
    end
    end)

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

    _app:onIdle(function(e)
    noticefont:drawText(0, 0, 'Press 1 to draw a Cube')
    noticefont:drawText(0, 14, 'Press 2 to draw a Pyramid')
    noticefont:drawText(0, 28, 'Press 3 to draw a Sphere')
    noticefont:drawText(0, 42, 'Press 4 to draw a Cylinder')
    noticefont:drawText(0, 56, 'Press 5 to draw a Cone')
    noticefont:drawText(0, 70, 'Press 6 to draw a Teapot')
    noticefont:drawText(0, 84, 'Press 7 to draw a Plane')
    noticefont:drawText(0, 98, 'Press space to draw texture')

    curMesh:drawMesh()
    _rd:drawAxis(2)
    end)
问题反馈(登录才可以发表哦!)