Skip to content

Instantly share code, notes, and snippets.

@sugi-cho
Last active December 29, 2015 23:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugi-cho/7744818 to your computer and use it in GitHub Desktop.
Save sugi-cho/7744818 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class CreateMesh {
//16250枚Quadが入ったMeshを作る。
[MenuItem("Custom/Create/16250meshes")]
public static void CreateParticleMesh(){
Mesh mesh = new Mesh();
int vertexCount = 65000;
Vector3[] vertices = new Vector3[vertexCount];
Vector3[] normals = new Vector3[vertexCount];
Vector2[] uv1 = new Vector2[vertexCount];
Vector2[] uv2 = new Vector2[vertexCount];
int[] indices = new int[vertexCount*6/4];
for(int i = 0; i < vertexCount/4; i++){
vertices[i*4 + 0] = Vector3.zero;
uv1[i*4 + 0] = new Vector2(0,0);
vertices[i*4 + 1] = Vector3.zero;
uv1[i*4 + 1] = new Vector2(1f,0);
vertices[i*4 + 2] = Vector3.zero;
uv1[i*4 + 2] = new Vector2(1f,1f);
vertices[i*4 + 3] = Vector3.zero;
uv1[i*4 + 3] = new Vector2(0,1f);
normals[i*4 + 0] = normals[i*4 + 1] = normals[i*4 + 2] = normals[i*4 + 3] = -Vector3.forward;
float x = (float)(i % 128) / 256f;
float y = (float)(i / 128) / 256f;
uv2[i*4 + 0] = uv2[i*4 + 1] = uv2[i*4 + 2] = uv2[i*4 + 3] = new Vector2(x,y);
indices[i*6 + 0] = i*4+0;
indices[i*6 + 1] = i*4+2;
indices[i*6 + 2] = i*4+1;
indices[i*6 + 3] = i*4+2;
indices[i*6 + 4] = i*4+0;
indices[i*6 + 5] = i*4+3;
}
mesh.vertices = vertices;
mesh.normals = normals;
mesh.uv = uv1;
mesh.uv2 = uv2;
mesh.SetIndices(indices, MeshTopology.Triangles, 0);
AssetDatabase.CreateAsset(mesh, "Assets/ParticleMesh.asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
//ここのCGINCがいくつか必要
//https://gist.github.com/sugi-cho/6076993
Shader "Custom/Snow" {
Properties {
_Color ("color", Color) = (0.5,0.5,0.5,0.5)
_MainTex ("Particle Texture", 2D) = "white" {}
_EmitPos ("emit pos", Vector) = (0,0,0,0)
_Range ("emit radius", Vector) = (100,10,100,1)
_Size ("particle size", Float) = 1
_Speed ("limit speed", Float) = 1
_Life ("lefe time", Float) = 30
_Floor("floor level", Float) = -10
}
CGINCLUDE
#include "UnityCG.cginc"
#include "Libs/Random.cginc"
#include "Libs/Noise.cginc"
#define PI 3.1415
sampler2D _MainTex;
fixed4 _Color;
half4 _EmitPos,_Range;
half _Size,_Speed,_Life,_Floor;
uniform float4x4 _MVP1, _MVP2, _MVP_I1, _MVP_I2;
uniform float4 _CamPos1;
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float4 texcoord1 : TEXCOORD1;
};
struct v2f {
float4 pos : SV_POSITION;
float2 tex1 : TEXCOORD0;
float2 tex2 : TEXCOORD1;
float4 color : COLOR;
};
float easeInOutQuad(float x) {
float t = x; float b = 0; float c = 1; float d = 1;
t /= d/2;
if (t < 1) return c/2*t*t + b;
return -c/2 * ((--t)*(t-2) - 1) + b;
}
v2f vert (appdata v, float2 delta)
{
float3 r = _Range.xyz*(_Range.w);
float3 emitPos = _EmitPos + (rand3(v.texcoord1.xy + delta)-0.5) * r;
emitPos.xz += (rand3(v.texcoord1.xy - delta).xz - 0.5)*r*0.1;
float
x = v.texcoord.x,
y = v.texcoord.y,
t = fmod(_Time.y - rand(emitPos.xy)*_Life, _Life),
speed = _Speed * (0.75 + rand(v.texcoord1.x + v.texcoord1.y)*0.5);
t = min(t*t, t*2-1);
float4 pos = v.vertex;
pos.xyz = emitPos;
pos.y -= t*speed;
pos.y = max(pos.y, _Floor);
pos.xyz += snoise3D(mul(_Object2World,pos).xyz*0.1 + rand3(v.texcoord1.xy + delta));
pos = mul(UNITY_MATRIX_MV, pos);
pos.xy += v.texcoord * _Size;
pos = mul(UNITY_MATRIX_P, pos);
v2f o;
o.pos = pos;
o.tex1 = v.texcoord;
o.tex2 = v.texcoord1;
return o;
}
v2f vert1 (appdata v){
return vert(v, float2(-1,.8));
}
v2f vert2 (appdata v){
return vert(v, float2(0.3,0.5));
}
v2f vert3 (appdata v){
return vert(v, float2(1,.4));
}
fixed4 frag (v2f i) : COLOR
{
return fixed4( 1,1,1, 1-distance(float2(0.5,0.5), i.tex1)*2);
}
ENDCG
SubShader {
Tags { "RenderType" = "Transparent" "Queue" = "Transparent"}
Blend SrcAlpha One
AlphaTest Greater .01
Cull Off Lighting Off ZTest Always
Pass {
CGPROGRAM
#pragma vertex vert1
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma target 3.0
#pragma glsl
ENDCG
}
Pass {
CGPROGRAM
#pragma vertex vert2
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma target 3.0
#pragma glsl
ENDCG
}
Pass {
CGPROGRAM
#pragma vertex vert3
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma target 3.0
#pragma glsl
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment