我的轴不会出现

时间:2016-11-01 10:00:23

标签: c# opengl unity3d

我正在尝试实现一个绘制3D指标的类,例如:enter image description here

通过在每一帧绘制每个轴来指示摄像机的当前旋转,就像它是3D一样。

然而,当我的代码运行时,我看不到任何被绘制的东西。当我尝试在Update()中绘制它时,屏幕上没有出现任何内容,但我仍然可以在控制台中看到“已在更新中绘制轴”。我尝试在OnPostRender()中执行此操作,但它似乎没有被触发,“轴已被绘制”未显示在控制台中。

我可能做错了什么?

using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.UI;

public class AxesManager : MonoBehaviour {

   private static readonly int DEFAULT_AXIS_LENGTH = 100;

   private Material _xMaterial;
   private Material _yMaterial;
   private Material _zMaterial;

   private Vector3 _origin;
   private int _length;


   // Use this for initialization
   void Start () {
       UpdateMaterials();
       _origin = GetAxesOrigin();
       _length = DEFAULT_AXIS_LENGTH;
   }

   // Update is called once per frame
   void Update () {
      //ClearAxes();
      //DrawAxes();
      //Debug.Log("The axes have been drawn in Update");
   }

   void OnPostRender()
   {
       DrawAxes();
       Debug.Log("The axes have been drawn");
   }

   private void DrawAxes()
   {
       //draw x axis
       GL.PushMatrix();
       GL.MultMatrix(transform.localToWorldMatrix);
       _xMaterial.SetPass(0);
       GL.LoadOrtho();
       GL.Begin(GL.LINES);
       GL.Color(Color.green);
       GL.Vertex(_origin);
       GL.Vertex(new Vector3(_length, 0, 0));
       GL.End();
       GL.PopMatrix();

       //draw y axis
       GL.PushMatrix();
       GL.MultMatrix(transform.localToWorldMatrix);
       _yMaterial.SetPass(0);
       GL.LoadOrtho();
       GL.Begin(GL.LINES);
       GL.Color(Color.red);
       GL.Vertex(_origin);
       GL.Vertex(new Vector3(0, _length, 0));
       GL.End();
       GL.PopMatrix();

       //draw z axis
       GL.PushMatrix();
       GL.MultMatrix(transform.localToWorldMatrix);
       _zMaterial.SetPass(0);
       GL.LoadOrtho();
       GL.Begin(GL.LINES);
       GL.Color(Color.blue);
       GL.Vertex(_origin);
       GL.Vertex(new Vector3(0, 0, _length));
       GL.End();
       GL.PopMatrix();

   }

   private void ClearAxes()
   {
       //clear the axes here
   }

   private Vector3 GetAxesOrigin()
   {
       return new Vector3(100,100,0);
   }

   private void UpdateMaterials()
   {
       _xMaterial = Resources.Load("Materials" + Path.DirectorySeparatorChar + "XAxisMaterial", typeof(Material)) as Material;
       _yMaterial = Resources.Load("Materials" + Path.DirectorySeparatorChar + "YAxisMaterial", typeof(Material)) as Material;
       _zMaterial = Resources.Load("Materials" + Path.DirectorySeparatorChar + "ZAxisMaterial", typeof(Material)) as Material;
   }
}

编辑:将脚本附加到相机(渲染顶层)后,我现在可以看到OnPostRender()被触发,但没有任何显示,也没有错误。它可能是什么?

0 个答案:

没有答案