前提・実現したいこと
シェーダー内で頂点 UVを構築し、一つのメッシュにテクスチャを並べたい
該当のソースコード
ShaderLab
1// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 2 3Shader "Unlit/Terrain hard2" 4{ 5 Properties 6 { 7 _MainTex ("Main texture", 2D) = "white" {} 8 } 9 10 11 SubShader 12 { 13 Tags 14 { 15 "RenderType"="Opaque" 16 } 17 18 ZWrite Off 19 20 Pass 21 { 22 CGPROGRAM 23 24 #pragma vertex vert 25 #pragma fragment frag 26 27 sampler2D _MainTex; 28 29 30 31 struct appdata 32 { 33 float4 vertex : POSITION; 34 }; 35 36 struct v2f 37 { 38 float4 pos : SV_POSITION; 39 float2 uv : TEXCOORD0; 40 }; 41 42 43 44 45 v2f vert (appdata v) 46 { 47 v2f o; 48 49 o.pos = UnityObjectToClipPos( v.vertex ); 50 51 //We create texture coordinates from worldspace coordinates 52 o.uv = v.vertex.xy / 16; 53 54 return o; 55 } 56 57 half4 frag (v2f i) : COLOR 58 { 59 half4 texcol = tex2D (_MainTex, i.uv.xy); 60 return texcol; 61 } 62 63 64 ENDCG 65 } 66 } 67} 68
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class WorldPlactice : MonoBehaviour 6{ 7 public int width = 1; 8 public int height = 1; 9 10 public TilePractice[,] tiles; 11 12 public Material material; 13 14 Mesh mesh; 15 16 // Use this for initialization 17 void Start() 18 { 19 CreateTiles(); 20 GenerateMesh(); 21 } 22 23 // Update is called once per frame 24 void Update() 25 { 26 Graphics.DrawMesh(mesh, Vector3.zero, Quaternion.identity, material, 0); 27 } 28 29 void CreateTiles() 30 { 31 tiles = new TilePractice[width, height]; 32 33 for (int i = 0; i < width; i++) 34 { 35 for (int j = 0; j < height; j++) 36 { 37 tiles[i, j] = new TilePractice(TilePractice.Type.Grass); 38 } 39 } 40 } 41 42 void GenerateMesh() 43 { 44 MeshDataPractice data = new MeshDataPractice(tiles); 45 mesh = new Mesh(); 46 mesh.vertices = data.vertices.ToArray(); 47 mesh.triangles = data.triangles.ToArray(); 48 mesh.RecalculateNormals(); 49 mesh.RecalculateBounds(); 50 } 51} 52
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class TilePractice 6{ 7 public enum Type { Grass } 8 public Type type; 9 10 public TilePractice(Type type) 11 { 12 this.type = type; 13 } 14} 15
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class MeshDataPractice 6{ 7 public List<Vector3> vertices; 8 public List<int> triangles; 9 10 public MeshDataPractice(TilePractice[,] data) 11 { 12 vertices = new List<Vector3>(); 13 triangles = new List<int>(); 14 15 for (int i = 0; i < data.GetLength(0); i++) 16 { 17 for (int j = 0; j < data.GetLength(1); j++) 18 { 19 CreateSquare(data[i, j], i, j); 20 } 21 } 22 } 23 24 void CreateSquare(TilePractice tile, int x, int y) 25 { 26 vertices.Add(new Vector3(x + 0, y + 0)); 27 vertices.Add(new Vector3(x + 250, y + 0)); 28 vertices.Add(new Vector3(x + 0, y + 250)); 29 vertices.Add(new Vector3(x + 250, y + 250)); 30 31 triangles.Add(vertices.Count - 1); 32 triangles.Add(vertices.Count - 3); 33 triangles.Add(vertices.Count - 4); 34 35 triangles.Add(vertices.Count - 2); 36 triangles.Add(vertices.Count - 1); 37 triangles.Add(vertices.Count - 4); 38 } 39} 40
試したこと
シェーダ内で頂点UVを構築し、テクスチャを一つの大きなメッシュに並べようとしましたが、出来ませんでした。
本当はテクスチャを二枚以上に増やし、ノイズを使って地形のように並べるつもりなのですが、シェーダーに関して詳しくなく、どうやればテクスチャを並べるように投影出来るのか分からなかったため、質問しました。
回答よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。