teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

2022/02/01 03:46

投稿

unity_beginner_
unity_beginner_

スコア14

title CHANGED
@@ -1,1 +1,1 @@
1
- unlit/color の自作シェーダで色をインタウィンドウから定したい
1
+ スクリプトから色を指できるシェーダーを自作したい
body CHANGED
@@ -1,78 +1,72 @@
1
- ### 前提・実現したいこと
2
-
3
- unlit/colorった自作シェーダを適用させたマテリアルのインスペクタウィンドウでマテリアルの色を設定
4
- 現在Main Colorで色設定はできますがそれがマテリアルに反映されません。
5
-
6
-
7
-
8
-
9
- ### 該当のソースコード
10
-
11
- ```ここに言語名を入力
12
- Shader "Unlit/karui_sheder"
13
- {
14
-
15
-
16
- Properties
17
- {
18
- _Color("Color", Color) = (1,1,1,1)
19
- _MainTex("Albedo (RGB)", 2D) = "white" {}
20
- }
21
- SubShader
22
- {
23
- Tags { "RenderType"="Opaque" }
24
- LOD 100
25
- Cull off
26
-
27
- Pass
28
- {
29
- CGPROGRAM
30
- #pragma vertex vert
31
- #pragma fragment frag
32
- // make fog work
33
- #pragma multi_compile_fog
34
-
35
- #include "UnityCG.cginc"
36
-
37
- struct appdata
38
- {
39
- float4 vertex : POSITION;
40
- float2 uv : TEXCOORD0;
41
- };
42
-
43
- struct v2f
44
- {
45
- float2 uv : TEXCOORD0;
46
- UNITY_FOG_COORDS(1)
47
- float4 vertex : SV_POSITION;
48
- };
49
-
50
- sampler2D _MainTex;
51
- float4 _MainTex_ST;
52
-
53
- v2f vert (appdata v)
54
- {
55
- v2f o;
56
- o.vertex = UnityObjectToClipPos(v.vertex);
57
- o.uv = TRANSFORM_TEX(v.uv, _MainTex);
58
- UNITY_TRANSFER_FOG(o,o.vertex);
59
- return o;
60
- }
61
-
62
- fixed4 frag (v2f i) : SV_Target
63
- {
64
- // sample the texture
65
- fixed4 col = tex2D(_MainTex, i.uv);
66
- // apply fog
67
- UNITY_APPLY_FOG(i.fogCoord, col);
68
- return col;
69
- }
70
- ENDCG
71
- }
72
- }
73
- }
74
- ```
75
-
76
- ### 試したこと
77
-
78
- MainTexの設定のほうが優先されてColorの設定が反映されていないのかと考え、_MainTexの行を消してみましたが灰色になるだけでした。
1
+ ### 前提・実現したいこと
2
+
3
+ Unity3D以下の条件を持ったシェーダ作ろうと
4
+ ・影描写をしない(unlit)
5
+ ・表裏両方描く(Cull off)
6
+ ・スクリプトでオブジェクトの色を取得できる
7
+ ・スクリプトからオブジェクトの色を変更できる
8
+ ・サブメッシュがある
9
+
10
+ 似た質問を探してみましたが見当たりませんでした、知っている方よろしくお願いします。
11
+
12
+
13
+
14
+ ### 該当のソースコード
15
+
16
+ ```ここに言語名を入力
17
+ Shader "Custom/ryoumen_sheder"
18
+ {
19
+ Properties
20
+ {
21
+ _Color ("Color", Color) = (1,1,1,1)
22
+ _MainTex ("Albedo (RGB)", 2D) = "red" {}
23
+ _Glossiness ("Smoothness", Range(0,1)) = 0.5
24
+ _Metallic ("Metallic", Range(0,1)) = 0.0
25
+ }
26
+ SubShader
27
+ {
28
+ Tags { "RenderType"="Opaque" }
29
+ LOD 200
30
+ Cull off
31
+
32
+ CGPROGRAM
33
+ // Physically based Standard lighting model, and enable shadows on all light types
34
+ #pragma surface surf Standard fullforwardshadows
35
+
36
+ // Use shader model 3.0 target, to get nicer looking lighting
37
+ #pragma target 3.0
38
+
39
+ sampler2D _MainTex;
40
+
41
+ struct Input
42
+ {
43
+ float2 uv_MainTex;
44
+ };
45
+
46
+ half _Glossiness;
47
+ half _Metallic;
48
+ fixed4 _Color;
49
+
50
+ // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
51
+ // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
52
+ // #pragma instancing_options assumeuniformscaling
53
+ UNITY_INSTANCING_BUFFER_START(Props)
54
+ // put more per-instance properties here
55
+ UNITY_INSTANCING_BUFFER_END(Props)
56
+
57
+ void surf (Input IN, inout SurfaceOutputStandard o)
58
+ {
59
+ // Albedo comes from a texture tinted by color
60
+ fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
61
+ o.Albedo = c.rgb;
62
+ // Metallic and smoothness come from slider variables
63
+ o.Metallic = _Metallic;
64
+ o.Smoothness = _Glossiness;
65
+ o.Alpha = c.a;
66
+ }
67
+ ENDCG
68
+ }
69
+ FallBack "Diffuse"
70
+ }
71
+
72
+ ```