「Unity/Shader」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→色プロパティにアクセス) |
|||
| 行6: | 行6: | ||
#作成したMaterialのinspector内のTextureに、用意した画像ファイルを入れる。 | #作成したMaterialのinspector内のTextureに、用意した画像ファイルを入れる。 | ||
#ヒエラルキーに3DObject/Cubeを作成して、MeshRendererのMaterialsに、Unlit_TestUnlitShaderを追加する | #ヒエラルキーに3DObject/Cubeを作成して、MeshRendererのMaterialsに、Unlit_TestUnlitShaderを追加する | ||
| + | |||
| + | 作成されたTestUnlitShader.shader | ||
| + | <pre> | ||
| + | Shader "Unlit/TestUnlitShader" | ||
| + | { | ||
| + | Properties | ||
| + | { | ||
| + | _MainTex ("Texture", 2D) = "white" {} | ||
| + | } | ||
| + | SubShader | ||
| + | { | ||
| + | Tags { "RenderType"="Opaque" } | ||
| + | LOD 100 | ||
| + | |||
| + | Pass | ||
| + | { | ||
| + | CGPROGRAM | ||
| + | #pragma vertex vert | ||
| + | #pragma fragment frag | ||
| + | // make fog work | ||
| + | #pragma multi_compile_fog | ||
| + | |||
| + | #include "UnityCG.cginc" | ||
| + | |||
| + | struct appdata | ||
| + | { | ||
| + | float4 vertex : POSITION; | ||
| + | float2 uv : TEXCOORD0; | ||
| + | }; | ||
| + | |||
| + | struct v2f | ||
| + | { | ||
| + | float2 uv : TEXCOORD0; | ||
| + | UNITY_FOG_COORDS(1) | ||
| + | float4 vertex : SV_POSITION; | ||
| + | }; | ||
| + | |||
| + | sampler2D _MainTex; | ||
| + | float4 _MainTex_ST; | ||
| + | |||
| + | v2f vert (appdata v) | ||
| + | { | ||
| + | v2f o; | ||
| + | o.vertex = UnityObjectToClipPos(v.vertex); | ||
| + | o.uv = TRANSFORM_TEX(v.uv, _MainTex); | ||
| + | UNITY_TRANSFER_FOG(o,o.vertex); | ||
| + | return o; | ||
| + | } | ||
| + | |||
| + | fixed4 frag (v2f i) : SV_Target | ||
| + | { | ||
| + | // sample the texture | ||
| + | fixed4 col = tex2D(_MainTex, i.uv); | ||
| + | // apply fog | ||
| + | UNITY_APPLY_FOG(i.fogCoord, col); | ||
| + | return col; | ||
| + | } | ||
| + | ENDCG | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
参考:https://qiita.com/nekoco/items/592ac7109106aef97522 | 参考:https://qiita.com/nekoco/items/592ac7109106aef97522 | ||
2022年2月2日 (水) 20:44時点における版
作成
- Assetsの下に、適当な画像ファイルを用意する
- Assetsの下で、Create/Shader/UnlitShaderを選択して、TestUnlitShader作成
- Assetsの下で、Create/Materialを選択して、作成
- 作成したMaterialを選択して、Shaderに、上で作った、TestUnlitShaderを入れる。
- 作成したMaterialのinspector内のTextureに、用意した画像ファイルを入れる。
- ヒエラルキーに3DObject/Cubeを作成して、MeshRendererのMaterialsに、Unlit_TestUnlitShaderを追加する
作成されたTestUnlitShader.shader
Shader "Unlit/TestUnlitShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
参考:https://qiita.com/nekoco/items/592ac7109106aef97522
Textureプロパティにアクセス
Material material; material.SetTexture(“_MainTex”, texture);
公式:https://docs.unity3d.com/ja/2019.4/ScriptReference/Material.SetTexture.html
色プロパティにアクセス
Material material;
material.SetColor(0, new Color(1f, 1f, 0f));
// カスタム値にアクセスしたい場合
material.SetColor("_HogeColor", new Color(1f, 1f, 0f));
公式:https://docs.unity3d.com/ja/2019.4/ScriptReference/Material.SetColor.html
