Skip to content

Instantly share code, notes, and snippets.

@kirillrybin
Last active August 29, 2015 13:58
Show Gist options
  • Save kirillrybin/10246864 to your computer and use it in GitHub Desktop.
Save kirillrybin/10246864 to your computer and use it in GitHub Desktop.
Unity Useful Snippets
private static List<int[]> GetHistogram(Texture2D tex)
{
var pixels = tex.GetPixels32();
var histogram = new List<int[]>(4);
for (var i = 0; i < 4; i++)
histogram.Add(new int[256]);
for (var y = tex.height - 1; y >= 0; y--)
{
for (var x = 0; x < tex.width; x++)
{
var pos = x + y * tex.width;
var pixel = pixels[pos];
for (var i = 0; i < 256; i++)
{
if ((int)(pixel.r) == i) histogram[0][i]++;
if ((int)(pixel.g) == i) histogram[1][i]++;
if ((int)(pixel.b) == i) histogram[2][i]++;
if ((int)(pixel.a) == i) histogram[3][i]++;
}
}
}
return histogram;
}
using UnityEngine;
/**
* "Процедурная генерация трёхмерных моделей"
* http://habrahabr.ru/post/194620/
*/
public class MeshUtil {
public static Mesh Triangle(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2)
{
var normal = Vector3.Cross((vertex1 - vertex0), (vertex2 - vertex0)).normalized;
var mesh = new Mesh
{
vertices = new[] { vertex0, vertex1, vertex2 },
normals = new[] { normal, normal, normal },
uv = new[] { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1) },
triangles = new[] { 0, 1, 2 }
};
return mesh;
}
public static Mesh Quad(Vector3 origin, Vector3 width, Vector3 length)
{
var normal = Vector3.Cross(length, width).normalized;
var mesh = new Mesh
{
vertices = new[] { origin, origin + length, origin + length + width, origin + width },
normals = new[] { normal, normal, normal, normal },
uv = new[] { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) },
triangles = new[] { 0, 1, 2, 0, 2, 3 }
};
return mesh;
}
public static Mesh Plane(Vector3 origin, Vector3 width, Vector3 length, int widthCount, int lengthCount)
{
var combine = new CombineInstance[widthCount * lengthCount];
var i = 0;
for (var x = 0; x < widthCount; x++)
{
for (var y = 0; y < lengthCount; y++)
{
combine[i].mesh = Quad(origin + width * x + length * y, width, length);
i++;
}
}
var mesh = new Mesh();
mesh.CombineMeshes(combine, true, false);
return mesh;
}
public static Mesh Cube(Vector3 width, Vector3 length, Vector3 height)
{
var corner0 = -width / 2 - length / 2 - height / 2;
var corner1 = width / 2 + length / 2 + height / 2;
var combine = new CombineInstance[6];
combine[0].mesh = Quad(corner0, length, width);
combine[1].mesh = Quad(corner0, width, height);
combine[2].mesh = Quad(corner0, height, length);
combine[3].mesh = Quad(corner1, -width, -length);
combine[4].mesh = Quad(corner1, -height, -width);
combine[5].mesh = Quad(corner1, -length, -height);
var mesh = new Mesh();
mesh.CombineMeshes(combine, true, false);
return mesh;
}
public static Mesh Octahedron(float radius)
{
var v = new Vector3[6];
v[0] = new Vector3(0, -radius, 0);
v[1] = new Vector3(-radius, 0, 0);
v[2] = new Vector3(0, 0, -radius);
v[3] = new Vector3(+radius, 0, 0);
v[4] = new Vector3(0, 0, +radius);
v[5] = new Vector3(0, radius, 0);
var mesh = new Mesh
{
vertices = v,
triangles = new[] { 0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 4, 1,
5, 2, 1,
5, 3, 2,
5, 4, 3,
5, 1, 4}
};
mesh.RecalculateNormals();
return mesh;
}
public static Mesh Tetrahedron(float radius)
{
var v0 = new Vector3(0, radius, 0);
var v1 = new Vector3(0, -radius * 0.333f, radius * 0.943f);
var v2 = new Vector3(radius * 0.816f, -radius * 0.333f, -radius * 0.471f);
var v3 = new Vector3(-radius * 0.816f, -radius * 0.333f, -radius * 0.471f);
var combine = new CombineInstance[4];
combine[0].mesh = Triangle(v0, v1, v2);
combine[1].mesh = Triangle(v1, v3, v2);
combine[2].mesh = Triangle(v0, v2, v3);
combine[3].mesh = Triangle(v0, v3, v1);
var mesh = new Mesh();
mesh.CombineMeshes(combine, true, false);
return mesh;
}
public static Mesh Icosahedron(float radius)
{
var magicAngle = Mathf.PI * 26.565f / 180;
var segmentAngle = Mathf.PI * 72 / 180;
var currentAngle = 0f;
var v = new Vector3[12];
v[0] = new Vector3(0, radius, 0);
v[11] = new Vector3(0, -radius, 0);
for (var i = 1; i < 6; i++)
{
v[i] = new Vector3(radius * Mathf.Sin(currentAngle) * Mathf.Cos(magicAngle),
radius * Mathf.Sin(magicAngle),
radius * Mathf.Cos(currentAngle) * Mathf.Cos(magicAngle));
currentAngle += segmentAngle;
}
currentAngle = Mathf.PI * 36 / 180;
for (var i = 6; i < 11; i++)
{
v[i] = new Vector3(radius * Mathf.Sin(currentAngle) * Mathf.Cos(-magicAngle),
radius * Mathf.Sin(-magicAngle),
radius * Mathf.Cos(currentAngle) * Mathf.Cos(-magicAngle));
currentAngle += segmentAngle;
}
var combine = new CombineInstance[20];
combine[0].mesh = Triangle(v[0], v[1], v[2]);
combine[1].mesh = Triangle(v[0], v[2], v[3]);
combine[2].mesh = Triangle(v[0], v[3], v[4]);
combine[3].mesh = Triangle(v[0], v[4], v[5]);
combine[4].mesh = Triangle(v[0], v[5], v[1]);
combine[5].mesh = Triangle(v[11], v[7], v[6]);
combine[6].mesh = Triangle(v[11], v[8], v[7]);
combine[7].mesh = Triangle(v[11], v[9], v[8]);
combine[8].mesh = Triangle(v[11], v[10], v[9]);
combine[9].mesh = Triangle(v[11], v[6], v[10]);
combine[10].mesh = Triangle(v[2], v[1], v[6]);
combine[11].mesh = Triangle(v[3], v[2], v[7]);
combine[12].mesh = Triangle(v[4], v[3], v[8]);
combine[13].mesh = Triangle(v[5], v[4], v[9]);
combine[14].mesh = Triangle(v[1], v[5], v[10]);
combine[15].mesh = Triangle(v[6], v[7], v[2]);
combine[16].mesh = Triangle(v[7], v[8], v[3]);
combine[17].mesh = Triangle(v[8], v[9], v[4]);
combine[18].mesh = Triangle(v[9], v[10], v[5]);
combine[19].mesh = Triangle(v[10], v[6], v[1]);
var mesh = new Mesh();
mesh.CombineMeshes(combine, true, false);
return mesh;
}
}
using UnityEngine;
using UnityEditor;
using System.Collections;
/*
* Unity Transform Editor Script
* By Chevy Ray Johnston ([email protected])
*
* Note: This is an editor class. To use it you have to place your script in Assets/Editor inside your project folder.
* Custom editors run automatically, so once you've placed the script in your project, your transforms should look different.
*/
[CustomEditor(typeof(Transform))]
public class TransformEditor : Editor
{
static bool mode2D;
public override void OnInspectorGUI()
{
//Grab target & modifiable values
var t = (Transform)target;
var p = t.localPosition;
var r = t.localEulerAngles;
var s = t.localScale;
//Toggle 2D mode
mode2D = GUILayout.Toggle(mode2D, "2D Mode");
if (mode2D)
{
//Modify 2D transform
var p2 = EditorGUILayout.Vector2Field("Position", new Vector2(p.x, p.y));
var s2 = EditorGUILayout.Vector2Field("Scale", new Vector2(s.x, s.y));
var r2 = EditorGUILayout.Slider("Rotation", r.z, 0, 359);
p.Set(p2.x, p2.y, p.z);
s.Set(s2.x, s2.y, s.z);
r.Set(r.x, r.y, r2);
}
else
{
//Modify 3D transform
p = EditorGUILayout.Vector3Field("Position", t.localPosition);
r = EditorGUILayout.Vector3Field("Rotation", t.localEulerAngles);
s = EditorGUILayout.Vector3Field("Scale", t.localScale);
}
//Reset buttons
GUILayout.Label("Reset");
GUILayout.BeginHorizontal();
if (GUILayout.Button("Position"))
p.Set(0, 0, 0);
if (GUILayout.Button("Rotation"))
r.Set(0, 0, 0);
if (GUILayout.Button("Scale"))
s.Set(1, 1, 1);
if (GUILayout.Button("All"))
{
p.Set(0, 0, 0);
r.Set(0, 0, 0);
s.Set(1, 1, 1);
}
GUILayout.EndHorizontal();
//Apply changes
if (GUI.changed)
{
Undo.RegisterUndo(t, "Transform Change");
t.localPosition = Validate(p);
t.localEulerAngles = Validate(r);
t.localScale = Validate(s);
}
}
private Vector3 Validate(Vector3 v)
{
if (float.IsNaN(v.x))
v.x = 0;
if (float.IsNaN(v.y))
v.y = 0;
if (float.IsNaN(v.z))
v.z = 0;
return v;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment