Cg/HSL シェーダープログラム の他に、OpenGL シェーディング言語 (GLSL, OpenGL Shading Language) シェーダーも直接作成することが可能です。
ただし、生 GLSL の使用はテストの場合、または、Mac OS X、 OpenGL ES モバイルデバイス、 Linux だけを対象にするとわかっている場合にのみ推奨されます。すべての通常の場合で、Unity は必要な場合に、Cg/HLSL を最適化された GLSL にクロスコンパイルします。
GLSL プログラムスニペットは GLSLPROGRAM
と ENDGLSL
キーワードの間に記述します。
GLSL では、すべてのシェーダー関数のエントリーポイントは main()
でなければなりません。Unity が GLSL シェーダーをロードするとき、 1 回は頂点プログラムのために VERTEX
プリプロセッサー定義でソースを読み込み、フラグメントプログラムのために FRAGMENT
プリプロセッサー定義でもう 1 回読み込みます。頂点プログラムとフラグメントプログラムを GLSL スニペットの中で分けるためには、それらを #ifdef VERTEX .. #endif
と #ifdef FRAGMENT .. #endif
で囲む必要があります。各 GLSL スニペットには頂点プログラムとフラグメントプログラム両方が含まれていなければなりません。
標準的なインクルードファイルは、Cg/HLSL シェーダーのために提供されたものに一致し、.glslinc
拡張子がついています。
UnityCG.glslinc
頂点シェーダーの入力は定義済み GLSL 変数 (gl _Vertex
、gl_ MultiTexCoord0
など) か、ユーザー定義の属性です。通常は、接線ベクトルのみがユーザー定義の属性を必要とします。
attribute vec4 Tangent;
頂点プログラムからフラグメントプログラムへのデータは varying 変数を通して渡されます。
varying vec3 lightDir; // 頂点シェーダーがこれを計算し、フラグメントシェーダーが使用します。
Unity はシェーダーコンパイルの間に部分的に前処理のを行います。例えば、graphics API (GlES3, GLCore) に基づいてtexture2D/texture2DProj
関数は texture/textureProj
に置き変えられます。拡張のなかには新しい規約をサポートしないものがあります。特に注目すべきは GL_OES_EGL_image_external
です。
GLSL シェーダーで外部テクスチャをサンプリングしたい場合は、texture2D/texture2DProj
の代わりに textureExternal/textureProjExternal
呼び出しを使用します。
例
gl_FragData[0] = textureExternal(_MainTex, uv);
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.