Last active
August 29, 2015 14:01
-
-
Save sugi-cho/4e269af09e1047c1c213 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; | |
using System.Collections.Generic; | |
public class SpawnFromTriangle : MonoBehaviour | |
{ | |
public Transform[] hanas; | |
public Triangle[] triangles; | |
public int numSpawn = 10; | |
public float emission = 10f; | |
public Transform notSpawnArea; | |
int count = 0; | |
// Use this for initialization | |
void Start () | |
{ | |
triangles = Triangle.GetTriangles (GetComponent<MeshFilter> ().mesh); | |
notSpawnArea.parent = transform; | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
for (int i = 0; i < numSpawn; i++) { | |
Triangle tr = triangles [Random.Range (0, triangles.Length)]; | |
if (emission * tr.area < Random.value) | |
continue; | |
Vector3 pos = tr.RandomPoint (); | |
if ((notSpawnArea.localPosition - pos).magnitude < notSpawnArea.localScale.x) | |
continue; | |
Quaternion rot = Quaternion.FromToRotation (Vector3.forward, tr.normal); | |
Transform t = (Transform)Instantiate (hanas [count = count % hanas.Length], pos, rot); | |
t.parent = transform; | |
t.localPosition = pos; | |
t.localRotation = rot; | |
t.localScale *= Random.Range (0.7f, 1f); | |
count ++; | |
} | |
} | |
} |
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; | |
using System.Collections.Generic; | |
public class Triangle { | |
public static Triangle[] GetTriangles(Mesh m){ | |
Vector3[] vertices = m.vertices; | |
int[] triangles = m.triangles; | |
List<Triangle> tList = new List<Triangle>(); | |
for (int i = 0; i < triangles.Length/3; i++) | |
tList.Add(new Triangle(vertices[triangles[i*3]], vertices[triangles[i*3+1]], vertices[triangles[i*3+2]])); | |
return tList.ToArray(); | |
} | |
Vector3 point1, point2, point3; | |
public float area; | |
public Vector3 center,normal; | |
public Triangle (Vector3 p1, Vector3 p2, Vector3 p3){ | |
point1 = p1; point2 = p2; point3 = p3; | |
Vector3 cross = Vector3.Cross(point2-point1, point3-point1); | |
area = cross.magnitude/2f; | |
center = (point1+point2+point3)/3f; | |
normal = cross.normalized; | |
} | |
public Vector3 RandomPoint(){ | |
Vector3 | |
p1 = Vector3.Lerp (point1, point2, Mathf.Sqrt(Random.value)), | |
p2 = Vector3.Lerp (point1, point3, Mathf.Sqrt(Random.value)); | |
return Vector3.Lerp (p1, p2, Random.value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment