Last active
May 16, 2020 02:54
-
-
Save kitasenjudesign/0c5b54646a5d7bacbee96f0216080c70 to your computer and use it in GitHub Desktop.
DrawMeshInstanced雛形
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Rendering; | |
public class PieceData { | |
public Vector3 pos = new Vector3( | |
5f * ( Random.value-0.5f ), | |
5f * ( Random.value-0.5f ), | |
5f * ( Random.value-0.5f ) | |
); | |
public Quaternion rot =Quaternion.Euler(0,0,0); | |
public Vector3 scale = new Vector3(1f,1f,1f); | |
public Vector4 uv = new Vector4(); | |
} |
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Rendering; | |
public class Pieces : MonoBehaviour { | |
[SerializeField] protected Mesh _mesh; | |
[SerializeField] protected Material _mat; | |
protected Matrix4x4[] _matrices; | |
protected Vector4[] _colors; | |
protected MaterialPropertyBlock _propertyBlock; | |
protected int _count = 400; | |
private PieceData[] _data; | |
public const int MAX = 1023; | |
void Start(){ | |
_propertyBlock = new MaterialPropertyBlock(); | |
_matrices = new Matrix4x4[MAX]; | |
_data = new PieceData[MAX]; | |
_colors = new Vector4[MAX]; | |
//_uvs = new Vector4[MAX]; | |
_count=0; | |
for(int i=0;i<MAX;i++){ | |
_matrices[_count] = Matrix4x4.identity; | |
_data[_count] = new PieceData(); | |
_data[_count].pos.x = 2f * ( Random.value-0.5f ); | |
_data[_count].pos.y = 2f * ( Random.value-0.5f ); | |
_data[_count].pos.z = 2f * ( Random.value-0.5f ); | |
_data[_count].rot = Quaternion.Euler( | |
Random.value * 360f, | |
Random.value * 360f, | |
Random.value * 360f | |
); | |
//_uvs[_count] = SpriteUV.GetUv(Mathf.FloorToInt(Random.value*6),4,4); | |
_colors[_count] = new Vector4( | |
Random.value, | |
Random.value, | |
Random.value, | |
1f | |
); | |
_count++; | |
} | |
} | |
void Update(){ | |
for (int i = 0; i < _count; i++) | |
{ | |
_matrices[i].SetTRS( | |
_data[i].pos, | |
_data[i].rot, | |
_data[i].scale | |
); | |
_matrices[i] = transform.localToWorldMatrix * _matrices[i]; | |
} | |
_propertyBlock.SetVectorArray("_Color", _colors); | |
//_propertyBlock.SetVectorArray("_Uv", _uvs); | |
Graphics.DrawMeshInstanced( | |
_mesh, | |
0, | |
_mat, | |
_matrices, | |
_count, | |
_propertyBlock, | |
ShadowCastingMode.On, | |
false, | |
gameObject.layer | |
); | |
} | |
} |
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
// Upgrade NOTE: upgraded instancing buffer 'Props' to new syntax. | |
Shader "pieces/Pieces" | |
{ | |
Properties | |
{ | |
_Color ("Color", Vector) = (1.0, 0.0, 0.0, 1.0) | |
_MainTex ("_MainTex", 2D) = "white" {} | |
_Th ("_Th", Range(0,1)) = 0.5 | |
} | |
SubShader | |
{ | |
//Tags { "RenderType"="Opaque" } | |
Tags { "RenderType"="Transparent" "Queue"="Transparent" } | |
Blend SrcAlpha OneMinusSrcAlpha | |
LOD 100 | |
cull off | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
// make fog work | |
#pragma multi_compile_fog | |
#pragma multi_compile_instancing | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
struct v2f | |
{ | |
float2 uv : TEXCOORD0; | |
UNITY_FOG_COORDS(1) | |
float4 vertex : SV_POSITION; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
float _Th; | |
UNITY_INSTANCING_BUFFER_START(Props) | |
UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color) // Make _Color an instanced property (i.e. an array) | |
#define _Color_arr Props | |
UNITY_DEFINE_INSTANCED_PROP(fixed4, _Uv) // Make _Color an instanced property (i.e. an array) | |
#define _Uv_arr Props | |
UNITY_INSTANCING_BUFFER_END(Props) | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
//インスタンス ID がシェーダー関数にアクセス可能になります。頂点シェーダーの最初に使用する必要があります | |
UNITY_SETUP_INSTANCE_ID (v); | |
//頂点シェーダーで入力構造体から出力構造体へインスタンス ID をコピーします。 | |
//フラグメントシェーダーでは、インスタンスごとのデータにアクセスするときのみ必要です | |
UNITY_TRANSFER_INSTANCE_ID (v, 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 | |
{ | |
UNITY_SETUP_INSTANCE_ID (i); | |
// 変数にアクセス | |
fixed4 col = UNITY_ACCESS_INSTANCED_PROP(_Color_arr, _Color); | |
//fixed4 uvv = UNITY_ACCESS_INSTANCED_PROP(_Uv_arr, _Uv); | |
//fixed4 tex = tex2D(_MainTex, uvv.xy + i.uv*uvv.zw); | |
//clip(tex.a - _Th); | |
// apply fog | |
//UNITY_APPLY_FOG(i.fogCoord, col); | |
return col;//tex; | |
} | |
ENDCG | |
} | |
} | |
} |
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
// Upgrade NOTE: upgraded instancing buffer 'Props' to new syntax. | |
Shader "pieces/PiecesSurf" { | |
Properties { | |
_Color ("Color", Color) = (1,1,1,1) | |
_MainTex ("Albedo (RGB)", 2D) = "white" {} | |
_Glossiness ("Smoothness", Range(0,1)) = 0.5 | |
_Metallic ("Metallic", Range(0,1)) = 0.0 | |
_Amount ("_Amount", Range(0,5)) = 0.0 | |
} | |
SubShader { | |
Tags { "RenderType"="Opaque" } | |
LOD 200 | |
CGPROGRAM | |
// Physically based Standard lighting model, and enable shadows on all light types | |
//#pragma surface surf Standard fullforwardshadows | |
#pragma surface surf Standard addshadow vertex:vert | |
// Use shader model 3.0 target, to get nicer looking lighting | |
#pragma target 3.0 | |
sampler2D _MainTex; | |
struct Input { | |
float2 uv_MainTex; | |
}; | |
half _Glossiness; | |
half _Metallic; | |
//fixed4 _Color; | |
float _Amount; | |
UNITY_INSTANCING_BUFFER_START(Props) | |
UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color) // Make _Color an instanced property (i.e. an array) | |
#define _Color_arr Props | |
UNITY_INSTANCING_BUFFER_END(Props) | |
void vert(inout appdata_full v, out Input o ) | |
{ | |
UNITY_INITIALIZE_OUTPUT(Input, o); | |
//インスタンス ID がシェーダー関数にアクセス可能になります。頂点シェーダーの最初に使用する必要があります | |
UNITY_SETUP_INSTANCE_ID (v); | |
//頂点シェーダーで入力構造体から出力構造体へインスタンス ID をコピーします。 | |
//フラグメントシェーダーでは、インスタンスごとのデータにアクセスするときのみ必要です | |
//UNITY_TRANSFER_INSTANCE_ID (v, o); | |
//頂点をてきとうに、ごちゃごちゃする、ここではノーマル方向に値を足してる | |
//v.vertex.xyz += v.normal.xyz * _Amount; | |
} | |
void surf (Input IN, inout SurfaceOutputStandard o) { | |
//UNITY_SETUP_INSTANCE_ID (IN); | |
fixed4 col = UNITY_ACCESS_INSTANCED_PROP(_Color_arr, _Color); | |
// Albedo comes from a texture tinted by color | |
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * col; | |
o.Albedo = c.rgb*0.5; | |
o.Emission = c.rgb*0.5; | |
// Metallic and smoothness come from slider variables | |
o.Metallic = _Metallic; | |
o.Smoothness = _Glossiness; | |
o.Alpha = c.a; | |
} | |
ENDCG | |
} | |
FallBack "Diffuse" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment