「Unity/Shader」の版間の差分
提供: 初心者エンジニアの簡易メモ
| 行71: | 行71: | ||
参考:https://qiita.com/nekoco/items/592ac7109106aef97522 | 参考:https://qiita.com/nekoco/items/592ac7109106aef97522 | ||
| − | == | + | ==MeshRendererにmaterialを動的に設定== |
<pre> | <pre> | ||
public class SampleScene : MonoBehaviour | public class SampleScene : MonoBehaviour | ||
{ | { | ||
| − | + | [SerializeField] Material material; // UnlitShaderなどをドラッグ | |
| − | + | [SerializeField] MeshRenderer targetMeshRenderer; // Cubeなど3DObjectをドラッグ | |
| − | + | ||
void Start() | void Start() | ||
{ | { | ||
| − | // | + | InitMaterial(); |
| + | } | ||
| + | void InitMaterial() | ||
| + | { | ||
| + | // 複数設定したり | ||
Material[] mats = targetMeshRenderer.materials; | Material[] mats = targetMeshRenderer.materials; | ||
mats[0] = material; | mats[0] = material; | ||
targetMeshRenderer.materials = mats; | targetMeshRenderer.materials = mats; | ||
| − | // | + | |
| + | // 1つだけ設定したり | ||
targetMeshRenderer.material = material; | targetMeshRenderer.material = material; | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | ==Textureを動的に設定== | ||
| + | ==MeshRendererにmaterialを設定== | ||
| + | <pre> | ||
| + | public class SampleScene : MonoBehaviour | ||
| + | { | ||
| + | [SerializeField] Texture texture; // png画像ファイルをドラッグ | ||
| + | void Start() | ||
| + | { | ||
| + | InitTexture(); | ||
| + | } | ||
| + | InitTexture() | ||
| + | { | ||
| + | // 画像ファイルを直接張ったり | ||
| + | targetMeshRenderer.material.mainTexture = texture; | ||
} | } | ||
} | } | ||
2022年2月2日 (水) 21:07時点における版
目次
作成
- 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
MeshRendererにmaterialを動的に設定
public class SampleScene : MonoBehaviour
{
[SerializeField] Material material; // UnlitShaderなどをドラッグ
[SerializeField] MeshRenderer targetMeshRenderer; // Cubeなど3DObjectをドラッグ
void Start()
{
InitMaterial();
}
void InitMaterial()
{
// 複数設定したり
Material[] mats = targetMeshRenderer.materials;
mats[0] = material;
targetMeshRenderer.materials = mats;
// 1つだけ設定したり
targetMeshRenderer.material = material;
}
}
Textureを動的に設定
MeshRendererにmaterialを設定
public class SampleScene : MonoBehaviour
{
[SerializeField] Texture texture; // png画像ファイルをドラッグ
void Start()
{
InitTexture();
}
InitTexture()
{
// 画像ファイルを直接張ったり
targetMeshRenderer.material.mainTexture = texture;
}
}
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
