Last active
July 5, 2017 01:05
-
-
Save baobao/85f6995f4fd8712361d3585e0b7ea791 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
/// <summary> | |
/// 頂点カラーとテクスチャ画像でで三角形を描画します | |
/// </summary> | |
[RequireComponent (typeof(MeshRenderer))] | |
[RequireComponent (typeof(MeshFilter))] | |
public class DynamicCreateMesh : MonoBehaviour | |
{ | |
[SerializeField] | |
private Material _mat; | |
private void Start () | |
{ | |
var mesh = new Mesh (); | |
mesh.vertices = new Vector3[] { | |
new Vector3 (0, 1f), | |
new Vector3 (1f, -1f), | |
new Vector3 (-1f, -1f), | |
}; | |
mesh.triangles = new int[] { | |
0, 1, 2 | |
}; | |
// 変更箇所 : UV座標を再設定 | |
mesh.uv = new Vector2[] { | |
new Vector2 (0.5f, 1f), | |
new Vector2 (1f, 0f), | |
new Vector2 (0, 0), | |
}; | |
mesh.colors = new Color[] { | |
Color.white, | |
Color.red, | |
Color.green | |
}; | |
mesh.RecalculateNormals (); | |
var filter = GetComponent<MeshFilter> (); | |
filter.sharedMesh = mesh; | |
var renderer = GetComponent<MeshRenderer> (); | |
renderer.material = _mat; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Baobao/Unlit/SimpleVertexColorShader" | |
{ | |
Properties{ | |
// インスペクタからテクスチャをセットできるようにする | |
_MainTex("Texture", 2D) = "white"{} | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" } | |
LOD 100 | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
// プロパティでセットされた画像が格納される(同じ変数名にする必要あり) | |
sampler2D _MainTex; | |
// どこからも参照していないように見えてUnityCG.cgincで使われています | |
float4 _MainTex_ST; | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
fixed3 color : COLOR0; | |
// テクスチャのUV座標(複数テクスチャにより連番。例:TEXCOORD1,2) | |
float2 uv:TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
fixed3 color : COLOR0; | |
// テクスチャのUV座標(複数テクスチャにより連番。例:TEXCOORD1,2) | |
float2 uv:TEXCOORD0; | |
}; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.uv = TRANSFORM_TEX(v.uv, _MainTex); | |
o.color = v.color; | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
fixed4 texCol = tex2D(_MainTex, i.uv); | |
// 頂点カラーに対してテクスチャのカラーを乗算 | |
fixed4 o = fixed4(i.color, 1) * texCol; | |
return o; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment