将3dsMax中制作的摄像机运动轨迹导出至Unity使用(二)

时间:2019-04-26 21:09:10   收藏:0   阅读:1008

(二)开发3dsmax插件导出摄像机运动轨迹数据

基本思路:

  a) 查找场景中的摄像机节点

  b) 从动画中逐帧获取摄像机位置、方向

  c) 进行3dsmax->Unity坐标变换并输出

 

查找场景中的摄像机节点的方法

  a) 遍历场景中节点,寻找类型为摄像机的

//获得场景对象并遍历场景中的节点
//ExpInterface* ei
ei
->theScene->EnumTree(...);
//判断节点类型是否是摄像机
//INode *node
ObjectState os = node->EvalWorldState(0);
if (os.obj && os.obj->SuperClassID()==CAMERA_CLASS_ID) {...}

   b)导出时手动选中摄像机节点,通过选中节点得到摄像机 

//Interface* ip;
ip->GetSelNodeCount();
INode * node = ip->GetSelNode(i);

逐帧获取摄像机位置、方向 

    //Interface * ip  INode * node
    TimeValue start =ip->GetAnimRange().Start();
    TimeValue end = ip->GetAnimRange().End();
   int tickPerFrame = GetTicksPerFrame(); Point3 initLookAtDir(
0, 0, -1); //相机默认lookat方向(在相机的local坐标系中) Point3 initUpDir(0, 1, 0); //相机默认up方向(在相机的local坐标系中) Matrix3 tm; for (TimeValue t = start; t <= end; t += tickPerFrame) { tm = node->GetNodeTM(t); CameraPostion pos; pos.position = tm.GetRow(3); pos.lookAtDir = VectorTransform(initLookAtDir, tm); pos.upDir = VectorTransform(initUpDir, tm); posList.append(pos); }

3dsmax->Unity坐标变换

   a) 坐标单位换算    

    int type;
    float scale;
    GetMasterUnitInfo(&type, &scale);
    if (type == UNITS_CENTIMETERS)
        POS_SCALE = 0.01f;
    else if (type == UNITS_METERS)
        POS_SCALE = 1;

  b)坐标轴变换

//模型从3dmax到unity导入过程: x轴坐标取反 然后绕x旋转-90, 变换矩阵为(行向量)
// [-1 0 0]
// [0 0 -1]
// [0 1 0]    //x‘ = -x   y‘=z   z‘=-y

    Matrix3 mat;
    mat.SetRow(0, Point3(-1, 0, 0));
    mat.SetRow(1, Point3(0, 0, -1));
    mat.SetRow(2, Point3(0, 1, 0));
    mat.SetRow(3, Point3(0, 0, 0));
    for (size_t i = 0; i < m_cameraPosList.length(); i++)
    {
        CameraPostion & pos = m_cameraPosList[i];
        pos.outputPos = pos.outputPos * mat;
        pos.outputLookAtDir = VectorTransform(pos.outputLookAtDir, mat);
        pos.outputUpDir = VectorTransform(pos.outputUpDir, mat);
    }

 

原文:https://www.cnblogs.com/lgc2003/p/10776490.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!