Version: 2020.2
Shader variants and keywords
Shading language used in Unity

GLSL Shader programs

In addition to using Cg/HSL shader programs, OpenGL Shading Language (GLSL) Shaders can be written directly.

However, use of raw GLSL is only recommended for testing, or when you know you are only targeting Mac OS X, OpenGL ES mobile devices, or Linux. In all normal cases, Unity will cross-compile Cg/HLSL into optimized GLSL when needed.

GLSL snippets

Los snippets del programa GLSL están escritos entre las palabras claves GLSLPROGRAM y ENDGLSL.

En GLSL, todos los puntos de entrada de la función shader tienen que ser llamados main(). Cuando Unity carga el shader GLSL, carga la fuente una vez para el Vertex Program, con el define del preprocesador VERTEX, y una vez más para el fragment programa, con el define del preprocesador FRAGMENT. Así que la forma de separar las partes del Vertex Program y Fragment program en el fragmento de GLSL es rodearlas con #ifdef VERTEX .. #endif y #ifdef FRAGMENT .. #endif.Cada fragmento GLSL debe contener tanto un vertex program como un fragment program.

Los archivos include estándar coinciden con los proporcionados para los shaders Cg/HLSL; Sólo tienen una extensión .glslinc:

UnityCG.glslinc

Las entradas de Vertex Shader provienen de variables GLSL predefinidas (gl_Vertex, gl_MultiTexCoord0, …) o son atributos definidos por el usuario. Por lo general, sólo el vector tangente necesita un atributo definido por el usuario:

attribute vec4 Tangent;

Los datos del vertex program al fragment program se pasan a través de variables Varying, por ejemplo:

varying vec3 lightDir; // vertex shader computes this, fragment shader uses this

External OES textures

Unity does some preprocessing during Shader compilation; for example, texture2D/texture2DProj functions may be replaced to texture/textureProj, based on graphics API (GlES3, GLCore). Some extensions don’t support new convention, most notably GL_OES_EGL_image_external.

If you want to sample external textures in GLSL shaders, use textureExternal/textureProjExternal calls instead of texture2D/texture2DProj.

Ejemplo:

gl_FragData[0] = textureExternal(_MainTex, uv);
Shader variants and keywords
Shading language used in Unity
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961