Skip to content

Commit

Permalink
new examples, new Begin/End poly feature
Browse files Browse the repository at this point in the history
  • Loading branch information
obviousjim committed Jan 26, 2012
1 parent 2e5a384 commit c2f530f
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ static function HelloWorld () {
* Test functions for all the geometry rendering
*
*/
@MenuItem ("Assignment1/Test Geometry")
static function TestAllGeometry() {

@MenuItem ("Assignment1/Build Primitives")
static function BuildPrimitives(){
//create little rows of geometric primitives
var i : int;
var g : GameObject;
Expand All @@ -36,9 +35,24 @@ static function TestAllGeometry() {

//light them
for(i = 0; i < 10; i++){
GeometryHelper.CreateSpotLight(Vector3(i*30,70,0), -Vector3.up, 100.0, 40.0, .03, Color(Random.Range(0,255),Random.Range(0,255),Random.Range(0,255)));
GeometryHelper.CreateSpotLight(Vector3(i*30,70,0), -Vector3.up, 100.0, 40.0, 5.6, Color(Random.Range(0,1.0),Random.Range(0,1.0),Random.Range(0,1.0)));
}

}

@MenuItem ("Assignment1/Build Cylinder Ring")
static function BuildCylinderRing(){
var cylinder : GameObject;
for(var i = 0; i < 50; i++){
var degrees : float = 360 * i / 50.0;
var distance : float = 50;
var pos : Vector3 = Vector3(Mathf.Cos(degrees * Mathf.Deg2Rad), 0, Mathf.Sin(degrees * Mathf.Deg2Rad) ) * distance;
cylinder = GeometryHelper.CreateCylinder(pos, Vector3(1,1,1));
cylinder.transform.rotation.eulerAngles = Vector3(90,-degrees + 90,0);
}
}

@MenuItem ("Assignment1/Build Colored Planes")
static function BuildColoredPlanes(){
var redPlane = GeometryHelper.CreateVerticlePlaneAlongX(Vector3(-75, 0,0), 10, 10);
var greenPlane = GeometryHelper.CreateHorizontalPlane(Vector3(-75, 0,0), 4, 10);
var bluePlane = GeometryHelper.CreateVerticlePlaneAlongZ(Vector3(-75, 0,0), 4, 10);
Expand All @@ -47,7 +61,11 @@ static function TestAllGeometry() {
GeometryHelper.ApplyColor(redPlane, Color.red);
GeometryHelper.ApplyColor(greenPlane, Color.green);
GeometryHelper.ApplyColor(bluePlane, Color.blue);

}

@MenuItem ("Assignment1/Build Triangle Cluster")
static function BuildTriangleCluster(){

//test random geometry
//this creates random triangles
var triangleCenter = Vector3(-175,0,0);
Expand All @@ -60,19 +78,43 @@ static function TestAllGeometry() {
}

//light the triangles with random spot lights

for(i = 0; i < 10; i++){
var position : Vector3 = triangleCenter + Random.onUnitSphere * 75;
//point back to the center
var direction = (triangleCenter - position ).normalized;
//args to creat a point light are: position : Vector3, direction : Vector3, angle : float, intensity : float, color : Color
GeometryHelper.CreateSpotLight(position, direction, 100.0, 40.0, 1, Color.white);
}

}

@MenuItem ("Assignment1/Cube Explosion")
static function CubeExplosion(){
for(var i = 0; i < 500; i++){
//create a position
var pos : Vector3 = Random.insideUnitSphere * 100;
var size : float = Mathf.Pow((100 - pos.magnitude), 2) / 200.0;
var cube : GameObject = GeometryHelper.CreateCube(pos, size);
cube.transform.rotation = Random.rotation;
}
}

@MenuItem ("Assignment1/Glassy Wave")
static function GlassyWave(){
var tri : GameObject;
GeometryHelper.BeginPolys();
for(var z = 0; z < 25; z++){
for(var x = 0; x < 25; x++){
GeometryHelper.AddPoly(Vector3(x,0,z), // the bottom two points are in rows along x
Vector3(x+.5, Mathf.Abs(Mathf.Sin(x/2.0)*4), Mathf.Abs(z+Mathf.Sin(z/2.0)*2)), //the top waves
Vector3(x-1,0,z) ); //one back so the bottom vertices touch
}
}
GeometryHelper.EndPolys();
}

@MenuItem ("Assignment1/Build My Memory")
static function BuildMemory() {
//Fill this out

}
}

11 changes: 11 additions & 0 deletions Assets/Editor/Project1Template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* "On Being Memorious" == Recall an abstract space from your memory and recreate it using only programmed primitives, colors, and lights.
* Using the GeometryHelper class and example project, create a menu script that generates a static 3d scene without any hand editing/
* Due Tuesday, January 31st. Done Individually.
*
*/
@MenuItem ("Assignment1/Build My Memory")
static function BuildMemory() {
//Fill this out

}
105 changes: 73 additions & 32 deletions Assets/Plugins/GeometryHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
public class GeometryHelper {
private static var materials : Array = [];
private static var geometryParent : GameObject;
private static var mesh : Mesh = new Mesh();
private static var mesh : Mesh;
private static var vertices : Array;
private static var indices : Array;
private static var uvs : Array;
private static var colors : Array;

//SPHERES
static function CreateSphere(pos : Vector3, radius : float) : GameObject {
return CreatePrimitive(pos, radius, PrimitiveType.Sphere);
Expand Down Expand Up @@ -99,42 +104,66 @@ public class GeometryHelper {

//POLYGONS
static function CreatePoly(a : Vector3, b : Vector3, c : Vector3) : GameObject {
var verts : Vector3[] = new Vector3[3];
var tris : int[] = new int[3];
var uvs : Vector2[] = new Vector2[3];
var meshContainer : GameObject = GameObject("Tri");
BeginPolys();
AddPoly(a,b,c);
EndPolys();

}

static function BeginPolys(){
if(mesh != null){
Debug.LogWarning("Forcing end polys. check that begin/end is matched");
EndPolys();
}

var mesh : Mesh = new Mesh();
verts[0] = a;
verts[1] = b;
verts[2] = c;
tris[0] = 0;
tris[1] = 1;
tris[2] = 2;
uvs[0] = Vector2(0,0);
uvs[1] = Vector2(1,0);
uvs[2] = Vector2(0,1);
mesh = new Mesh();
vertices = [];
indices = [];
uvs = [];
colors = [];
}

static function AddPoly(a : Vector3, b : Vector3, c : Vector3){
if(mesh == null){
Debug.LogWarning("Forcing begin polys. Check that begin/end is matched");
BeginPolys();
}

mesh.vertices = verts;
mesh.triangles = tris;
vertices.Push(a);
vertices.Push(b);
vertices.Push(c);
indices.Push(vertices.length-3);
indices.Push(vertices.length-2);
indices.Push(vertices.length-1);
uvs.Push(Vector2(0,0));
uvs.Push(Vector2(1,0));
uvs.Push(Vector2(0,1));

// colors.Push(Color(Random.value,Random.value,Random.value));
// colors.Push(Color(Random.value,Random.value,Random.value));
// colors.Push(Color(Random.value,Random.value,Random.value));
}

static function EndPolys() : GameObject {
mesh.vertices = vertices;
mesh.triangles = indices;
mesh.uv = uvs;
mesh.colors = colors;
mesh.RecalculateNormals();
mesh.RecalculateBounds();

var meshContainer : GameObject = GameObject("TriMesh");
meshContainer.AddComponent(MeshFilter);
meshContainer.GetComponent(MeshFilter).mesh = mesh;
meshContainer.AddComponent(MeshRenderer);

var mat : Material = new Material(Shader.Find("Diffuse"));
mat.SetColor("_Color", Color.gray);
materials.Push(mat);

meshContainer.renderer.sharedMaterial = mat;
meshContainer.renderer.sharedMaterial = GetMaterialWithColor(Color.gray);
meshContainer.transform.parent = GetGeometryParent();

return meshContainer;
}

mesh = null;
return meshContainer;
}

//LIGHTS
static function CreatePointLight(position : Vector3, range : float, intensity : float, color : Color) : GameObject {
var lightContainer = CreateLight(position, intensity, color, LightType.Point);
Expand Down Expand Up @@ -172,24 +201,36 @@ public class GeometryHelper {

//TODO: applying colors
static function ApplyColor(object : GameObject, c : Color){
var mat : Material = new Material(Shader.Find("Diffuse"));
mat.SetColor("_Color", c);
//TODO: look for re-usable colors
materials.Push(mat);
object.renderer.sharedMaterial = mat;
object.renderer.sharedMaterial = GetMaterialWithColor(c);
}

//TODO: applying rotations
static function Flip(object : GameObject, axis : Vector3){
object.transform.Rotate(axis, 180);
}

private static function GetMaterialWithColor( c : Color) : Material{
for(var i = 0; i < materials.length; i++){
if(materials[i].color == c){
mat = materials[i];
break;
}
}

if(mat == null){
mat = new Material(Shader.Find("Diffuse"));
mat.SetColor("_Color", c);
materials.Push(mat);
}
return mat;

}

private static function GetGeometryParent() : Transform{
if(geometryParent == null){
geometryParent = GameObject("GeneratedGeometry");
}
return geometryParent.transform;
}
}


}
Binary file modified Assets/Project1Example.unity
Binary file not shown.
Binary file removed Library/InspectorExpandedItems.asset
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Library/assetDatabase3
Binary file not shown.
Binary file removed Library/cache/77/7714d76630b584449a289f3a112d253c
Binary file not shown.
Binary file removed Library/cache/95/957dc15530e6e4a838d8521202873ee6
Binary file not shown.
Binary file modified Library/expandedItems
Binary file not shown.
Binary file modified Library/guidmapper
Binary file not shown.
Binary file removed Library/metadata/77/7714d76630b584449a289f3a112d253c
Binary file not shown.
Binary file removed Library/metadata/f6/f60949fc16235490aa7d51bae440b83f
Binary file not shown.

0 comments on commit c2f530f

Please sign in to comment.