You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自研Unity软件渲染器:3D相机环绕时物体旋转映射问题求助

软件渲染器物体旋转显示异常问题求助

我使用Unity的Texture2D.SetPixel实现了一款软件渲染器,目前渲染器可正常运行,且已通过将物体从全局空间转换为局部空间的方式,实现了相机环绕物体飞行的视觉效果(模拟相机环绕而非物体移动)。但当前遇到物体旋转显示异常的问题:

  • 最初尝试将物体旋转减去相机旋转,无效果
  • 改为相加后,仅正面视角显示正常,背面视角完全失效

演示场景中,当箭头局部旋转为(0,0,0)时,若不处理旋转逻辑,箭头仅会相对于屏幕向上显示。

演示视频:

  • 未尝试修复的示例:Example with no fix attempt
  • 两次修复尝试示例:Fix attempts one and two

说明:旋转基于x、y、z轴,其中z轴为向前方向,y轴为向上方向。以下是绘制物体的函数代码:

public void Draw(RenderContext context, RCamera _Cam, Matrix4f ViewProjection, Matrix4f proj, Vector3 pos, Vector3 rot, Texture2D texture)
{
    /* Vector3 nPos = pos - _Cam.Pos;
     Vector3 rotatedPos = new Vector3();*/

    // -( (Mathf.Sin(_Cam.Rot.x)) * (Mathf.Cos(_Cam.Rot.y)) )

    float x = _Cam.Rot.x;
    float y = _Cam.Rot.y;
    float z = _Cam.Rot.z;

    //the problem child line
    Vector3 nRot = rot + new Vector3(x,y,z);

    //Mathf.Sin(_Cam.Rot.x) * Mathf.Cos(_Cam.Rot.y)
    Vector3 camRot = new Vector3(x, y, z );

    //  Vector3 nRot = rot - camRot*_Cam.Fov*Mathf.Deg2Rad;

    //Vector3 nRot = new Vector3( Mathf.Sin( rot.x)+ Mathf.Cos( y),Mathf.Sin( rot.y)+Mathf.Cos( y), Mathf.Sin( rot.z)+ Mathf.Cos( z));

    Debug.Log(rot.x * (x * _Cam.Fov * Mathf.Deg2Rad));

    RotateMatrixX(_Cam, pos.x - _Cam.Pos.x, pos.z - _Cam.Pos.z, 0 - _Cam.Rot.y);
    RotateMatrixY(_Cam, pos.y - _Cam.Pos.y, _Cam.RotatedZ, 0 - _Cam.Rot.x);

    Vector3 rotPos = new Vector3(_Cam.RotatedX , _Cam.RotatedY, _Cam.RotatedZ);

    Matrix4f translation = new Matrix4f().InitTranslation(rotPos.x, rotPos.y, rotPos.z);
    Matrix4f rotation = new Matrix4f().InitRotation(nRot.x, nRot.y, nRot.z);
    Matrix4f scale = new Matrix4f().InitScale(0.001f, 0.001f, 0.001f);
    Matrix4f transform = proj.Mul(translation.Mul(rotation));

    if (rotPos.z > 0)
    {
        DrawMesh(context, ViewProjection, transform, texture);
    }
}

我保留了部分尝试的注释,但所有方案均未彻底解决问题,已困扰多日,希望能找到遗漏的关键点,感谢帮助。

内容的提问来源于stack exchange,提问作者Indie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 03:08:24