【问题标题】:SetMatrix has no effect (WebCamTexture Unity)SetMatrix 没有效果(WebCamTexture Unity)
【发布时间】:2015-05-27 17:13:37
【问题描述】:

我正在使用 WebCamTexture 从相机(iOS 和 Android)获取输入。但是,由于这是原始输入,因此在渲染到纹理时旋转是错误的。我看了很多,发现了这个(看底部):WebCamTexture rotated and flipped on iPhone

他的代码(但带有测试值):

Quaternion rotation = Quaternion.Euler(45f, 30f, 90f);
Matrix4x4 rotationMatrix = Matrix4x4.TRS(Vector3.zero, rotation, new Vector3(1, 1, 1));
material.SetMatrix("_Rotation", rotationMatrix);

但无论我使用什么值,都不会发生任何事情(无论是在编辑器中还是在设备上)...

谢谢!

编辑 经过一些激烈的测试,我发现 material.SetMatrix、SetFloat、SetWhatever 没有任何效果(不设置值),除非它在“属性”块中声明。看看 unity:s 自己的例子,这不应该(也不能)为矩阵完成(不能在 Properties 内声明,只能在 CGProgram 内声明)。那么......那你如何设置矩阵呢?还是我做错了什么?

【问题讨论】:

  • 几件事。 1. 旋转不是与SetMatrix 累积的,因此您的 sn-p 只会在开始时旋转一次。 2. 您链接的着色器 旋转几何体,而不是纹理(也许您想要这个?)。官方文档有一个 working example 的纹理旋转。
  • 谢谢,我去看看!我应该提到我每 3 年接触一次着色器...... :)
  • @Jerdak Btw,想多解释一下该解决方案如何“旋转几何体,而不是纹理”?泰!
  • 该着色器中唯一使用_rotation 的地方是这一行:float4 newPosition = mul( UNITY_MATRIX_MVP, mul(_Rotation, v.vertex) );,它是顶点着色器的一部分,仅应用于o.pos = newPosition 处的顶点,因此它只旋转几何体。
  • @Jerdak 我真正想要的是计算texcoord ..?太晚了,我不知道着色器或其语法......是的,对不起:)

标签: ios matrix unity3d shader


【解决方案1】:

只需将相机沿 z 轴旋转 90 度(渲染webcamtexture 游戏对象的相机)。

【讨论】:

    【解决方案2】:

    您应该使用:WebCamTexture.videoRotationAngle

    它旨在解决这个问题,阅读更多关于这个here

    示例代码:

    using UnityEngine;
    using System.Collections;
    
    public class ExampleClass : MonoBehaviour {
        public WebCamTexture webcamTexture;
        public Quaternion baseRotation;
        void Start() {
            webcamTexture = new WebCamTexture();
    
            renderer.material.mainTexture = webcamTexture;
            baseRotation = transform.rotation;
            webcamTexture.Play();
       }
       void Update() {
           transform.rotation = baseRotation * Quaternion.AngleAxis(webcamTexture.videoRotationAngle, Vector3.up);
       }
    }
    

    【讨论】:

    • 感谢您的回答,但问题是我已经将它与 NGUI 集成(使用 UITexture),因此我无法旋转对象,因为它与约束挂钩。我需要在着色器中完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2012-10-30
    • 2018-05-21
    相关资源
    最近更新 更多