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

回答編集履歴

3

Unlit版を追記

2021/01/20 20:47

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -11,92 +11,92 @@
11
11
 
12
12
  public class PlaneToHemisphereWizard : ScriptableWizard
13
13
  {
14
- [SerializeField][Min(0.0f)] private float planeWidth = 4.0f;
14
+ [SerializeField][Min(0.0f)] private float planeWidth = 4.0f;
15
- [SerializeField][Min(0.0f)] private float planeHeight = 3.0f;
15
+ [SerializeField][Min(0.0f)] private float planeHeight = 3.0f;
16
- [SerializeField][Min(0.0f)] private float hemisphereRadius = 1.0f;
16
+ [SerializeField][Min(0.0f)] private float hemisphereRadius = 1.0f;
17
- [SerializeField][Min(1)] private int divisions = 8;
17
+ [SerializeField][Min(1)] private int divisions = 8;
18
18
 
19
- private void OnWizardCreate()
20
- {
21
- var bottomLeft = new Vector3(-this.planeWidth * 0.5f, -this.planeHeight * 0.5f, 0.0f);
22
- var texelWidth = 0.5f / this.divisions;
23
- var texelHeight = 0.5f / this.divisions;
24
- var deltaX = this.planeWidth * texelWidth;
25
- var deltaY = this.planeHeight * texelHeight;
26
- var vertexCountX = (this.divisions * 2) + 1;
27
- var vertexCountY = (this.divisions * 2) + 1;
28
- var planeVertices = Enumerable.Range(0, vertexCountY).SelectMany(
29
- j => Enumerable.Range(0, vertexCountX).Select(
30
- i => bottomLeft + new Vector3(i * deltaX, j * deltaY, 0.0f))).ToArray();
31
- var planeNormals = planeVertices.Select(_ => Vector3.back).ToArray();
32
- var planeTangents = planeVertices.Select(_ => new Vector4(1.0f, 0.0f, 0.0f, -1.0f)).ToArray();
33
- var planeUvs = Enumerable.Range(0, vertexCountY).SelectMany(
34
- j => Enumerable.Range(0, vertexCountX).Select(
35
- i => new Vector2(i * texelWidth, j * texelHeight))).ToArray();
36
- var quadTriangles = new[]
37
- {
38
- 0, vertexCountX, 1, vertexCountX + 1, 1, vertexCountX
39
- };
40
- var planeTriangles = Enumerable.Range(0, vertexCountY - 1).SelectMany(
41
- j => Enumerable.Range(0, vertexCountX - 1).SelectMany(
42
- i => quadTriangles.Select(k => i + (j * vertexCountX) + k))).ToArray();
43
- var hemisphereNormals = planeUvs.Select(
44
- uv =>
45
- {
46
- var angleH = (uv.x + 0.5f) * Mathf.PI;
47
- var angleV = (uv.y - 0.5f) * Mathf.PI;
48
- var sinH = Mathf.Sin(angleH);
49
- var cosH = Mathf.Cos(angleH);
50
- var sinV = Mathf.Sin(angleV);
51
- var cosV = Mathf.Cos(angleV);
52
- return new Vector3(-sinH * cosV, sinV, cosH * cosV);
53
- }).ToArray();
54
- var hemisphereTangents = planeUvs.Select(
55
- uv =>
56
- {
57
- var angleH = (uv.x + 0.5f) * Mathf.PI;
58
- var sinH = Mathf.Sin(angleH);
59
- var cosH = Mathf.Cos(angleH);
60
- return new Vector4(-cosH, 0.0f, -sinH, -1.0f);
61
- }).ToArray();
62
- var hemisphereVertices = hemisphereNormals.Select(normal => normal * this.hemisphereRadius).ToArray();
63
- var mesh = new Mesh
64
- {
65
- name = "PlaneToHemisphere",
66
- vertices = planeVertices,
67
- normals = planeNormals,
68
- tangents = planeTangents,
69
- uv = planeUvs,
70
- triangles = planeTriangles
71
- };
72
- mesh.AddBlendShapeFrame(
73
- "Deformation",
74
- 100.0f,
75
- planeVertices.Zip(
76
- hemisphereVertices,
77
- (planeVertex, hemisphereVertex) => hemisphereVertex - planeVertex).ToArray(),
78
- planeNormals.Zip(
79
- hemisphereNormals,
80
- (planeNormal, hemisphereNormal) => hemisphereNormal - planeNormal).ToArray(),
81
- planeTangents.Zip(
82
- hemisphereTangents,
83
- (planeTangent, hemisphereTangent) => (Vector3)hemisphereTangent - (Vector3)planeTangent).ToArray());
84
- mesh.RecalculateNormals();
85
- mesh.RecalculateTangents();
86
- var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
87
- var newObject = new GameObject(mesh.name);
88
- var skinnedMeshRenderer = newObject.AddComponent<SkinnedMeshRenderer>();
89
- skinnedMeshRenderer.sharedMesh = mesh;
90
- skinnedMeshRenderer.sharedMaterial = cube.GetComponent<Renderer>().sharedMaterial;
91
- DestroyImmediate(cube);
92
- ProjectWindowUtil.CreateAsset(mesh, $"{mesh.name}.asset");
93
- }
19
+ private void OnWizardCreate()
20
+ {
21
+ var bottomLeft = new Vector3(-this.planeWidth * 0.5f, -this.planeHeight * 0.5f, 0.0f);
22
+ var texelWidth = 0.5f / this.divisions;
23
+ var texelHeight = 0.5f / this.divisions;
24
+ var deltaX = this.planeWidth * texelWidth;
25
+ var deltaY = this.planeHeight * texelHeight;
26
+ var vertexCountX = (this.divisions * 2) + 1;
27
+ var vertexCountY = (this.divisions * 2) + 1;
28
+ var planeVertices = Enumerable.Range(0, vertexCountY).SelectMany(
29
+ j => Enumerable.Range(0, vertexCountX).Select(
30
+ i => bottomLeft + new Vector3(i * deltaX, j * deltaY, 0.0f))).ToArray();
31
+ var planeNormals = planeVertices.Select(_ => Vector3.back).ToArray();
32
+ var planeTangents = planeVertices.Select(_ => new Vector4(1.0f, 0.0f, 0.0f, -1.0f)).ToArray();
33
+ var planeUvs = Enumerable.Range(0, vertexCountY).SelectMany(
34
+ j => Enumerable.Range(0, vertexCountX).Select(
35
+ i => new Vector2(i * texelWidth, j * texelHeight))).ToArray();
36
+ var quadTriangles = new[]
37
+ {
38
+ 0, vertexCountX, 1, vertexCountX + 1, 1, vertexCountX
39
+ };
40
+ var planeTriangles = Enumerable.Range(0, vertexCountY - 1).SelectMany(
41
+ j => Enumerable.Range(0, vertexCountX - 1).SelectMany(
42
+ i => quadTriangles.Select(k => i + (j * vertexCountX) + k))).ToArray();
43
+ var hemisphereNormals = planeUvs.Select(
44
+ uv =>
45
+ {
46
+ var angleH = (uv.x + 0.5f) * Mathf.PI;
47
+ var angleV = (uv.y - 0.5f) * Mathf.PI;
48
+ var sinH = Mathf.Sin(angleH);
49
+ var cosH = Mathf.Cos(angleH);
50
+ var sinV = Mathf.Sin(angleV);
51
+ var cosV = Mathf.Cos(angleV);
52
+ return new Vector3(-sinH * cosV, sinV, cosH * cosV);
53
+ }).ToArray();
54
+ var hemisphereTangents = planeUvs.Select(
55
+ uv =>
56
+ {
57
+ var angleH = (uv.x + 0.5f) * Mathf.PI;
58
+ var sinH = Mathf.Sin(angleH);
59
+ var cosH = Mathf.Cos(angleH);
60
+ return new Vector4(-cosH, 0.0f, -sinH, -1.0f);
61
+ }).ToArray();
62
+ var hemisphereVertices = hemisphereNormals.Select(normal => normal * this.hemisphereRadius).ToArray();
63
+ var mesh = new Mesh
64
+ {
65
+ name = "PlaneToHemisphere",
66
+ vertices = planeVertices,
67
+ normals = planeNormals,
68
+ tangents = planeTangents,
69
+ uv = planeUvs,
70
+ triangles = planeTriangles
71
+ };
72
+ mesh.AddBlendShapeFrame(
73
+ "Deformation",
74
+ 100.0f,
75
+ planeVertices.Zip(
76
+ hemisphereVertices,
77
+ (planeVertex, hemisphereVertex) => hemisphereVertex - planeVertex).ToArray(),
78
+ planeNormals.Zip(
79
+ hemisphereNormals,
80
+ (planeNormal, hemisphereNormal) => hemisphereNormal - planeNormal).ToArray(),
81
+ planeTangents.Zip(
82
+ hemisphereTangents,
83
+ (planeTangent, hemisphereTangent) => (Vector3)hemisphereTangent - (Vector3)planeTangent).ToArray());
84
+ mesh.RecalculateNormals();
85
+ mesh.RecalculateTangents();
86
+ var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
87
+ var newObject = new GameObject(mesh.name);
88
+ var skinnedMeshRenderer = newObject.AddComponent<SkinnedMeshRenderer>();
89
+ skinnedMeshRenderer.sharedMesh = mesh;
90
+ skinnedMeshRenderer.sharedMaterial = cube.GetComponent<Renderer>().sharedMaterial;
91
+ DestroyImmediate(cube);
92
+ ProjectWindowUtil.CreateAsset(mesh, $"{mesh.name}.asset");
93
+ }
94
94
 
95
- [MenuItem("Assets/Create/Plane to Hemisphere")]
95
+ [MenuItem("Assets/Create/Plane to Hemisphere")]
96
- private static void OpenWizard()
96
+ private static void OpenWizard()
97
- {
97
+ {
98
- DisplayWizard<PlaneToHemisphereWizard>("Plane to Hemisphere");
98
+ DisplayWizard<PlaneToHemisphereWizard>("Plane to Hemisphere");
99
- }
99
+ }
100
100
  }
101
101
  ```
102
102
 
@@ -112,86 +112,177 @@
112
112
  ```ShaderLab
113
113
  Shader "Custom/PlaneToSphere"
114
114
  {
115
- Properties
115
+ Properties
116
- {
116
+ {
117
- _Color ("Color", Color) = (1, 1, 1, 1)
117
+ _Color ("Color", Color) = (1, 1, 1, 1)
118
- _MainTex ("Albedo (RGB)", 2D) = "white" {}
118
+ _MainTex ("Albedo (RGB)", 2D) = "white" {}
119
- _Glossiness ("Smoothness", Range(0, 1)) = 0.5
119
+ _Glossiness ("Smoothness", Range(0, 1)) = 0.5
120
- _Metallic ("Metallic", Range(0, 1)) = 0.0
120
+ _Metallic ("Metallic", Range(0, 1)) = 0.0
121
- _SphereRadius ("Sphere Radius", Range(0, 10)) = 1.0
121
+ _SphereRadius ("Sphere Radius", Range(0, 10)) = 1.0
122
- _Deformation ("Deformation", Range(0, 1)) = 0.0
122
+ _Deformation ("Deformation", Range(0, 1)) = 0.0
123
- }
123
+ }
124
- SubShader
124
+ SubShader
125
- {
125
+ {
126
- Tags { "RenderType"="Opaque" }
126
+ Tags { "RenderType"="Opaque" }
127
127
 
128
- Cull Off
128
+ Cull Off
129
129
 
130
- CGPROGRAM
130
+ CGPROGRAM
131
- #pragma surface surf Standard fullforwardshadows vertex:vert
131
+ #pragma surface surf Standard fullforwardshadows vertex:vert
132
- #pragma target 3.0
132
+ #pragma target 3.0
133
133
 
134
- sampler2D _MainTex;
134
+ sampler2D _MainTex;
135
- half _Glossiness;
135
+ half _Glossiness;
136
- half _Metallic;
136
+ half _Metallic;
137
- fixed4 _Color;
137
+ fixed4 _Color;
138
- float _SphereRadius;
138
+ float _SphereRadius;
139
- float _Deformation;
139
+ float _Deformation;
140
140
 
141
- struct Input
141
+ struct Input
142
- {
142
+ {
143
- float2 uv_MainTex;
143
+ float2 uv_MainTex;
144
- };
144
+ };
145
145
 
146
- #define EPSILON 0.000001
146
+ #define EPSILON 0.000001
147
147
 
148
- void vert(inout appdata_full v)
148
+ void vert(inout appdata_full v)
149
- {
149
+ {
150
- if (_Deformation < EPSILON)
150
+ if (_Deformation < EPSILON)
151
- {
151
+ {
152
- return;
152
+ return;
153
- }
153
+ }
154
154
 
155
- // 法線、接線はブレンドシェイプ版と同じ線形モーフィングでも
155
+ // 法線、接線はブレンドシェイプ版と同じ線形モーフィングでも
156
- // 粗が目立たなかったためブレンドシェイプ版と同様に補間した
156
+ // 粗が目立たなかったためブレンドシェイプ版と同様に補間した
157
- float sinH;
157
+ float sinH;
158
- float cosH;
158
+ float cosH;
159
- float sinV;
159
+ float sinV;
160
- float cosV;
160
+ float cosV;
161
- sincos(v.texcoord.x * 2.0 * UNITY_PI, sinH, cosH);
161
+ sincos(v.texcoord.x * 2.0 * UNITY_PI, sinH, cosH);
162
- sincos((v.texcoord.y - 0.5) * UNITY_PI, sinV, cosV);
162
+ sincos((v.texcoord.y - 0.5) * UNITY_PI, sinV, cosV);
163
- float3 sphereNormal = float3(-sinH * cosV, sinV, cosH * cosV);
163
+ float3 sphereNormal = float3(-sinH * cosV, sinV, cosH * cosV);
164
- float3 sphereTangent = float3(cosH, 0.0, sinH) * v.tangent.w;
164
+ float3 sphereTangent = float3(cosH, 0.0, sinH) * v.tangent.w;
165
- v.normal = lerp(v.normal, sphereNormal, _Deformation);
165
+ v.normal = lerp(v.normal, sphereNormal, _Deformation);
166
- v.tangent.xyz = lerp(v.tangent.xyz, sphereTangent, _Deformation);
166
+ v.tangent.xyz = lerp(v.tangent.xyz, sphereTangent, _Deformation);
167
167
 
168
- // 頂点座標の水平成分を円弧に沿った補間とすることで見苦しさを軽減する
168
+ // 頂点座標の水平成分を円弧に沿った補間とすることで見苦しさを軽減する
169
- // 垂直成分は線形補間でもさほど違和感はなさそうだった
169
+ // 垂直成分は線形補間でもさほど違和感はなさそうだった
170
- float sinTheta;
170
+ float sinTheta;
171
- float cosTheta;
171
+ float cosTheta;
172
- float geographicCoord = (2.0 * v.texcoord.x - 1.0) * UNITY_PI;
172
+ float geographicCoord = (2.0 * v.texcoord.x - 1.0) * UNITY_PI;
173
- sincos(_Deformation * geographicCoord, sinTheta, cosTheta);
173
+ sincos(_Deformation * geographicCoord, sinTheta, cosTheta);
174
- float3 arcPosition = float3(sinTheta, 0.0, 1.0 - cosTheta) * _SphereRadius / _Deformation;
174
+ float3 arcPosition = float3(sinTheta, 0.0, 1.0 - cosTheta) * _SphereRadius / _Deformation;
175
- arcPosition.yz += float2(lerp(v.vertex.y, sphereNormal.y * _SphereRadius, _Deformation), lerp(0.0, -_SphereRadius, _Deformation));
175
+ arcPosition.yz += float2(lerp(v.vertex.y, sphereNormal.y * _SphereRadius, _Deformation), lerp(0.0, -_SphereRadius, _Deformation));
176
- arcPosition.x *= lerp(max(abs(v.vertex.x), EPSILON) / max(abs(_SphereRadius * geographicCoord), EPSILON), 1.0, _Deformation);
176
+ arcPosition.x *= lerp(max(abs(v.vertex.x), EPSILON) / max(abs(_SphereRadius * geographicCoord), EPSILON), 1.0, _Deformation);
177
- arcPosition.xz *= lerp(1.0, cosV, _Deformation);
177
+ arcPosition.xz *= lerp(1.0, cosV, _Deformation);
178
- v.vertex.xyz = arcPosition;
178
+ v.vertex.xyz = arcPosition;
179
- }
179
+ }
180
180
 
181
- void surf(Input IN, inout SurfaceOutputStandard o)
181
+ void surf(Input IN, inout SurfaceOutputStandard o)
182
- {
182
+ {
183
- fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
183
+ fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
184
- o.Albedo = c.rgb;
184
+ o.Albedo = c.rgb;
185
- o.Metallic = _Metallic;
185
+ o.Metallic = _Metallic;
186
- o.Smoothness = _Glossiness;
186
+ o.Smoothness = _Glossiness;
187
- o.Alpha = c.a;
187
+ o.Alpha = c.a;
188
- }
188
+ }
189
- ENDCG
189
+ ENDCG
190
- }
190
+ }
191
- FallBack "Diffuse"
191
+ FallBack "Diffuse"
192
192
  }
193
193
  ```
194
194
 
195
195
  下図のような変形の仕方になりました。マテリアルの`_Deformation`をスクリプトから操作すれば平面から球体へ変形できるかと思います。
196
196
 
197
- ![図2](8dcb1af3be096053addce20e4e12a6db.gif)
197
+ ![図2](8dcb1af3be096053addce20e4e12a6db.gif)
198
+
199
+ ##Unlit版
200
+
201
+ ```ShaderLab
202
+ Shader "Unlit/PlaneToSphere"
203
+ {
204
+ Properties
205
+ {
206
+ _MainTex ("Texture", 2D) = "white" {}
207
+ _SphereRadius ("Sphere Radius", Range(0, 10)) = 1.0
208
+ _Deformation ("Deformation", Range(0, 1)) = 0.0
209
+ }
210
+
211
+ SubShader
212
+ {
213
+ Tags { "RenderType"="Opaque" }
214
+
215
+ Cull Off
216
+
217
+ Pass
218
+ {
219
+ CGPROGRAM
220
+ #pragma vertex vert
221
+ #pragma fragment frag
222
+ #pragma multi_compile_fog
223
+
224
+ #include "UnityCG.cginc"
225
+
226
+ struct appdata
227
+ {
228
+ float4 vertex : POSITION;
229
+ float2 uv : TEXCOORD0;
230
+ };
231
+
232
+ struct v2f
233
+ {
234
+ float2 uv : TEXCOORD0;
235
+ UNITY_FOG_COORDS(1)
236
+ float4 vertex : SV_POSITION;
237
+ };
238
+
239
+ sampler2D _MainTex;
240
+ float4 _MainTex_ST;
241
+ float _SphereRadius;
242
+ float _Deformation;
243
+
244
+ #define EPSILON 0.000001
245
+
246
+ void deform(inout appdata v)
247
+ {
248
+ if (_Deformation < EPSILON)
249
+ {
250
+ return;
251
+ }
252
+
253
+ // Unlitなら法線、接線は不要なはずなので簡略化した
254
+ float sinV;
255
+ float cosV;
256
+ sincos((v.uv.y - 0.5) * UNITY_PI, sinV, cosV);
257
+ float sinTheta;
258
+ float cosTheta;
259
+ float geographicCoord = (2.0 * v.uv.x - 1.0) * UNITY_PI;
260
+ sincos(_Deformation * geographicCoord, sinTheta, cosTheta);
261
+ float3 arcPosition = float3(sinTheta, 0.0, 1.0 - cosTheta) * _SphereRadius / _Deformation;
262
+ arcPosition.yz += float2(lerp(v.vertex.y, sinV * _SphereRadius, _Deformation), lerp(0.0, -_SphereRadius, _Deformation));
263
+ arcPosition.x *= lerp(max(abs(v.vertex.x), EPSILON) / max(abs(_SphereRadius * geographicCoord), EPSILON), 1.0, _Deformation);
264
+ arcPosition.xz *= lerp(1.0, cosV, _Deformation);
265
+ v.vertex.xyz = arcPosition;
266
+ }
267
+
268
+ v2f vert(appdata v)
269
+ {
270
+ v2f o;
271
+ deform(v);
272
+ o.vertex = UnityObjectToClipPos(v.vertex);
273
+ o.uv = TRANSFORM_TEX(v.uv, _MainTex);
274
+ UNITY_TRANSFER_FOG(o,o.vertex);
275
+ return o;
276
+ }
277
+
278
+ fixed4 frag(v2f i) : SV_Target
279
+ {
280
+ fixed4 col = tex2D(_MainTex, i.uv);
281
+ UNITY_APPLY_FOG(i.fogCoord, col);
282
+ return col;
283
+ }
284
+ ENDCG
285
+ }
286
+ }
287
+ }
288
+ ```

2

シェーダー上で変形する案を追記

2021/01/20 20:47

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -103,4 +103,95 @@
103
103
  メニューの「Assets」→「Create」→「Plane To Hemisphere」でメッシュアセットを作成、併せてシーン上にそれをセットしたオブジェクトを生成させました。
104
104
  `SkinnedMeshRenderer`の「BlendShapes」→「Deformation」を操作すると、下図のように平面から半球に変形しました。
105
105
 
106
- ![図](7475e7e017b757ce310931dcb227e964.gif)
106
+ ![図1](7475e7e017b757ce310931dcb227e964.gif)
107
+
108
+ ##シェーダー上で変形する案
109
+
110
+ 下記のようなマテリアルを使用したところ...
111
+
112
+ ```ShaderLab
113
+ Shader "Custom/PlaneToSphere"
114
+ {
115
+ Properties
116
+ {
117
+ _Color ("Color", Color) = (1, 1, 1, 1)
118
+ _MainTex ("Albedo (RGB)", 2D) = "white" {}
119
+ _Glossiness ("Smoothness", Range(0, 1)) = 0.5
120
+ _Metallic ("Metallic", Range(0, 1)) = 0.0
121
+ _SphereRadius ("Sphere Radius", Range(0, 10)) = 1.0
122
+ _Deformation ("Deformation", Range(0, 1)) = 0.0
123
+ }
124
+ SubShader
125
+ {
126
+ Tags { "RenderType"="Opaque" }
127
+
128
+ Cull Off
129
+
130
+ CGPROGRAM
131
+ #pragma surface surf Standard fullforwardshadows vertex:vert
132
+ #pragma target 3.0
133
+
134
+ sampler2D _MainTex;
135
+ half _Glossiness;
136
+ half _Metallic;
137
+ fixed4 _Color;
138
+ float _SphereRadius;
139
+ float _Deformation;
140
+
141
+ struct Input
142
+ {
143
+ float2 uv_MainTex;
144
+ };
145
+
146
+ #define EPSILON 0.000001
147
+
148
+ void vert(inout appdata_full v)
149
+ {
150
+ if (_Deformation < EPSILON)
151
+ {
152
+ return;
153
+ }
154
+
155
+ // 法線、接線はブレンドシェイプ版と同じ線形モーフィングでも
156
+ // 粗が目立たなかったためブレンドシェイプ版と同様に補間した
157
+ float sinH;
158
+ float cosH;
159
+ float sinV;
160
+ float cosV;
161
+ sincos(v.texcoord.x * 2.0 * UNITY_PI, sinH, cosH);
162
+ sincos((v.texcoord.y - 0.5) * UNITY_PI, sinV, cosV);
163
+ float3 sphereNormal = float3(-sinH * cosV, sinV, cosH * cosV);
164
+ float3 sphereTangent = float3(cosH, 0.0, sinH) * v.tangent.w;
165
+ v.normal = lerp(v.normal, sphereNormal, _Deformation);
166
+ v.tangent.xyz = lerp(v.tangent.xyz, sphereTangent, _Deformation);
167
+
168
+ // 頂点座標の水平成分を円弧に沿った補間とすることで見苦しさを軽減する
169
+ // 垂直成分は線形補間でもさほど違和感はなさそうだった
170
+ float sinTheta;
171
+ float cosTheta;
172
+ float geographicCoord = (2.0 * v.texcoord.x - 1.0) * UNITY_PI;
173
+ sincos(_Deformation * geographicCoord, sinTheta, cosTheta);
174
+ float3 arcPosition = float3(sinTheta, 0.0, 1.0 - cosTheta) * _SphereRadius / _Deformation;
175
+ arcPosition.yz += float2(lerp(v.vertex.y, sphereNormal.y * _SphereRadius, _Deformation), lerp(0.0, -_SphereRadius, _Deformation));
176
+ arcPosition.x *= lerp(max(abs(v.vertex.x), EPSILON) / max(abs(_SphereRadius * geographicCoord), EPSILON), 1.0, _Deformation);
177
+ arcPosition.xz *= lerp(1.0, cosV, _Deformation);
178
+ v.vertex.xyz = arcPosition;
179
+ }
180
+
181
+ void surf(Input IN, inout SurfaceOutputStandard o)
182
+ {
183
+ fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
184
+ o.Albedo = c.rgb;
185
+ o.Metallic = _Metallic;
186
+ o.Smoothness = _Glossiness;
187
+ o.Alpha = c.a;
188
+ }
189
+ ENDCG
190
+ }
191
+ FallBack "Diffuse"
192
+ }
193
+ ```
194
+
195
+ 下図のような変形の仕方になりました。マテリアルの`_Deformation`をスクリプトから操作すれば平面から球体へ変形できるかと思います。
196
+
197
+ ![図2](8dcb1af3be096053addce20e4e12a6db.gif)

1

変数名を修正

2021/01/20 14:03

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -74,13 +74,13 @@
74
74
  100.0f,
75
75
  planeVertices.Zip(
76
76
  hemisphereVertices,
77
- (planeVertex, sphereVertex) => sphereVertex - planeVertex).ToArray(),
77
+ (planeVertex, hemisphereVertex) => hemisphereVertex - planeVertex).ToArray(),
78
78
  planeNormals.Zip(
79
79
  hemisphereNormals,
80
- (planeNormal, sphereNormal) => sphereNormal - planeNormal).ToArray(),
80
+ (planeNormal, hemisphereNormal) => hemisphereNormal - planeNormal).ToArray(),
81
81
  planeTangents.Zip(
82
82
  hemisphereTangents,
83
- (planeTangent, sphereTangent) => (Vector3)sphereTangent - (Vector3)planeTangent).ToArray());
83
+ (planeTangent, hemisphereTangent) => (Vector3)hemisphereTangent - (Vector3)planeTangent).ToArray());
84
84
  mesh.RecalculateNormals();
85
85
  mesh.RecalculateTangents();
86
86
  var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);