Unity 绘制人物屏幕线框(根据动画变化)

思路:在人物的每一个骨骼上面,添加一个Box,然后每一帧遍历Box,计算出box的8个顶点,然后转换为屏幕坐标。比较所有屏幕坐标,找出最大和最小的x,y。然后就可以根据屏幕坐标绘制gl线框了。

public static float[] MathSreenPoint(Transform[] cube , Camera _camera)
    {
        List<Vector2> screenpos = new List<Vector2>();
        for (int i = 0; i < cube.Length; i++)
        {
            screenpos.AddRange(WorldCubePoint2Screen(cube[i], _camera));
        }

        float left = -1;
        float right = -1;
        float up = -1;
        float down = -1;
        for (int i = 0; i < screenpos.Count; i++)
        {
            if (screenpos[i].x > right || right == -1)
            {
                right = screenpos[i].x;
            }
            if (screenpos[i].x < left || left == -1)
            {
                left = screenpos[i].x;
            }
            if (screenpos[i].y > up || up == -1)
            {
                up = screenpos[i].y;
            }
            if (screenpos[i].y < down || down == -1)
            {
                down = screenpos[i].y;
            }
        }
        return new float[] { up, down, left, right };
    }



private static Vector2[] WorldCubePoint2Screen(Transform cube , Camera camera)
    {
        Transform parent = cube.parent;
        cube.parent = null;

        Vector3 scale = cube.localScale;
        Vector3 cubepoint1 = cube.position + cube.right * scale.x / 2 + cube.up * scale.y / 2 + cube.forward * scale.z / 2;
      
        Vector3 cubepoint2 = cube.position - cube.right * scale.x / 2 + cube.up * scale.y / 2 + cube.forward * scale.z / 2;
      

        Vector3 cubepoint3 = cube.position + cube.right * scale.x / 2 - cube.up * scale.y / 2 + cube.forward * scale.z / 2;
     

        Vector3 cubepoint4 = cube.position + cube.right * scale.x / 2 + cube.up * scale.y / 2 - cube.forward * scale.z / 2;
    
        Vector3 cubepoint5 = cube.position - cube.right * scale.x / 2 - cube.up * scale.y / 2 + cube.forward * scale.z / 2;
      
        Vector3 cubepoint6 = cube.position + cube.right * scale.x / 2 - cube.up * scale.y / 2 - cube.forward * scale.z / 2;
      
        Vector3 cubepoint7 = cube.position - cube.right * scale.x / 2 + cube.up * scale.y / 2 - cube.forward * scale.z / 2;
   
        Vector3 cubepoint8 = cube.position - cube.right * scale.x / 2 - cube.up * scale.y / 2 - cube.forward * scale.z / 2;

        cube.parent = parent;
        Vector2[] screenpos = new Vector2[8];

        
        screenpos[0] = camera.WorldToScreenPoint(cubepoint1);
        screenpos[1] = camera.WorldToScreenPoint(cubepoint2);
        screenpos[2] = camera.WorldToScreenPoint(cubepoint3);
        screenpos[3] = camera.WorldToScreenPoint(cubepoint4);
        screenpos[4] = camera.WorldToScreenPoint(cubepoint5);
        screenpos[5] = camera.WorldToScreenPoint(cubepoint6);
        screenpos[6] = camera.WorldToScreenPoint(cubepoint7);
        screenpos[7] = camera.WorldToScreenPoint(cubepoint8);
        return screenpos;
    }

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值