简体   繁体   English

如何使用GL功能在Unity3D上显示纹理

[英]How to Show Texture on Unity3D using GL function

I tried to show an image to a GL.QUADS. 我试图将图像显示给GL.QUADS。 But nothing but a black rect show on the screen. 但是,除了屏幕上的黑色矩形显示之外,什么也没有。 How can I fix it. 我该如何解决。

Here is my script code and I add the component to MainCamera. 这是我的脚本代码,并将组件添加到MainCamera。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Material mat;
    void OnPostRender() {
        if (!mat) {
            Debug.LogError("Please Assign a material on the inspector");
            return;
        }
        GL.PushMatrix();
        mat.SetPass(1);
        GL.LoadOrtho();
        GL.Begin(GL.QUADS);
        GL.TexCoord2(0, 0);
        GL.Vertex3(0.25F, 0.25F, 0);
        GL.TexCoord2(0, 1);
        GL.Vertex3(0.25F, 0.75F, 0);
        GL.TexCoord2(1, 1);
        GL.Vertex3(0.75F, 0.75F, 0);
        GL.TexCoord2(1, 0);
        GL.Vertex3(0.75F, 0.25F, 0);
        GL.End();
        GL.PopMatrix();
    }
}

I created a custom material named 'CrossHairMaterial' using 'standard' shader: 我使用“标准”着色器创建了一个名为“ CrossHairMaterial”的自定义材质: 在此处输入图片说明

在此处输入图片说明

Finally it showed a black rectangle. 最后,它显示了一个黑色矩形。 在此处输入图片说明

Unity's GL API requires vertex shader to work. Unity的GL API需要顶点着色器才能工作。 The default standard shader that is used when new material is created will not work because it's not a vertex shader. 创建新材质时使用的默认标准着色器不起作用,因为它不是顶点着色器。

Any vertex shader in Unity should also work. Unity中的任何顶点着色器也应该起作用。

First of all, change mat.SetPass(1); 首先,更改mat.SetPass(1); to mat.SetPass(0); mat.SetPass(0); .

These are the supported Shaders: 这些是受支持的着色器:

1 .Particles --> VertexLit Blended. 1 .Particles-> VertexLit混合。

2 .Unlit-->Color 2。不亮->颜色

3 .Unlit-->Texture 3。不亮->纹理

4 .Unlit-->Transparent 4。不亮->透明

Lagacy Shaders that should work: 应该起作用的Lagacy Shaders:

5 .Legacy Shaders-->Transparent-->VertexLit. 5。旧版着色器->透明-> VertexLit。

6 .Legacy Shaders-->VertexLit. 6。旧版着色器-> VertexLit。

I recommend you use #2 , #3 or #4 depending on if you need Texture or Transparency in the material. 我建议您使用#2#3#4,这取决于您是否需要材质中的纹理或透明度。

Test with Unlit-->Texture: 测试不亮->纹理:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM