# making a 2d shape with 3d planes in script (similar to cube effect in linux)

I want to make a 2d triangle (viewed from above) with 3d planes as sides , i have generated the planes according to the sides, my problem is which position to locate them, i dont know if you guys have somewhere i could look for or an idea how to do this.

where to put the planes (x,y,z) if it has 3 sides, or if it has 8 sides, the sides are all grouped up in an empty GO which is at the center

any ideas? thanks in advance =)

I ended up using the Law of Sines to calculate the distance from the center to the sides. You know the Law of Sines sin(A)/a = sin(B)/b = sin(C)/c

After that i focused on a 2d Angle and made good use of polar coordinates to locate the X,Y coordinates withing the circle made from all the centers of the sides.

Problem solved. =D

BTW, heres my code, just add this to an empty object and change the properties, and you can see the results there.

public var sides:int;
public var sideSize:float = 1;
private  var shapeSide:GameObject;

function Awake(){

var side : GameObject;

if(sides < 3){
sides = 3;
}

var currentDegInterval : int = 360 / sides;
var currentAngle : int = 0;
var distanceToCenter = SideDistanceToCenter(sides, sideSize);

Debug.Log("intervalo: " + currentDegInterval);
Debug.Log("distancia: " + distanceToCenter);

for (var i = 0; i < sides; i++){
Debug.Log("lado: " + i);
Debug.Log("Angulo actual: " + currentAngle);

side = GameObject.CreatePrimitive(PrimitiveType.Plane);

var sideTransform = side.transform;
sideTransform.parent = transform;

var sidePosition : Vector2 = PolarCoordinates(currentAngle, distanceToCenter);
var sideDisplacement : Vector3 = Vector3(sidePosition.x * 10, 0 , sidePosition.y * 10);
sideTransform.position += sideDisplacement;

Debug.Log("Desplazamiento: " + sideDisplacement);

sideTransform.localScale = Vector3(sideSize,1,sideSize);
sideTransform.Rotate(90,90,currentAngle);

currentAngle = currentAngle + currentDegInterval;
}

}

function PolarCoordinates(angleDegrees:int, radius:float) : Vector2{

var coordinates : Vector2;
var angleRadians : float = angleDegrees * Mathf.PI / 180;
var xCoord : float = radius * Mathf.Cos(angleRadians);
var yCoord : float = radius * Mathf.Sin(angleRadians);

coordinates = Vector2(xCoord, yCoord);

return coordinates;

}

function SideDistanceToCenter(numberSides:int, sideSize:float) : float{

var distanceToCenter : float;
var angleRadComplimenting : float;
var angleRadNonCompli : float;

angleRadComplimenting = DegreesComplimentingSideAngle(numberSides) * Mathf.PI / 180;
angleRadNonCompli = (180 - 90 - DegreesComplimentingSideAngle(numberSides)) * Mathf.PI / 180;

distanceToCenter = (sideSize/2) * (Mathf.Sin(angleRadComplimenting)) /(Mathf.Sin(angleRadNonCompli));

return distanceToCenter;

}

function DegreesComplimentingSideAngle(numberSides:int) : int{

var resultingAngle : int;

resultingAngle = (180 * (numberSides - 2)) / (numberSides * 2);

return resultingAngle;

}

Do you have to do this in code? Because the easiest way to get that result (a triangular prism as I understand it) is to just create it in a modelling program such as Blender.

Hi, I am adding here the script which will take The value N >> No of Faces in Prism
And Generate the prism Runtime !!
for using This Script !!

// Create A Empty GameObject !!
// Attach This Script To it and Also Add The Mesh Renderer And Mesh Filter Components to That Object.

// Then Change The Values And Have Fun !!

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshFilter))]
public class PrismScript : MonoBehaviour
{
public int NumberOfTriangles;
public Vector3 CenterPoint = Vector3.zero;
public float DistanceFromCenter = 5;

int t_OldNumberOfTriangles;

// Use this for initialization
void Awake()
{
if(NumberOfTriangles < 3)
NumberOfTriangles = 3;

if(DistanceFromCenter < 3)
DistanceFromCenter = 3;

t_OldNumberOfTriangles = NumberOfTriangles;
GetComponent().mesh = CreatePrismMesh_N_Triangles();

InvokeRepeating("CheckData",5,1);
}

void CheckData()
{
if(t_OldNumberOfTriangles != NumberOfTriangles)
{
if(NumberOfTriangles < 3)
NumberOfTriangles = 3;

if(DistanceFromCenter < 3)
DistanceFromCenter = 3;

t_OldNumberOfTriangles = NumberOfTriangles;
GetComponent().mesh = CreatePrismMesh_N_Triangles();
}
}

Mesh CreatePrismMesh_N_Triangles()
{
// Created A Mesh !!
Mesh prismMesh = new Mesh();

// Generating Vertex Points List !!
Vector3[] vertexList = new Vector3[NumberOfTriangles + 1]; // +1 because One Point Will be Act As a Center Always !!

// Generating The Normals And UVs And Triangles List To Store The Vertex Order !!
Vector3[] normals	 = new Vector3[NumberOfTriangles + 1];
Vector2[] uv 		 = new Vector2[NumberOfTriangles + 1];
int[] 	  triangles  = new int[NumberOfTriangles * 3];			// Preserve the Vertex Order !!
Color[]	  Vertexcolors = new Color[NumberOfTriangles + 1];

vertexList[0] = CenterPoint;  // Center Point Of The Prism !!
vertexList[0].y += 5;
// Consider the points in XZ plane take a base as Y=0
Vector3 t_PointCordinates		= CenterPoint;
float   t_ReferenceAngle 		= Mathf.Atan2(CenterPoint.z, CenterPoint.x);  // Since here i am considering the XZ plane !!
float   t_AngleInDeg_OnCenter	= 360.0f / NumberOfTriangles;
float	t_AngleValue			= 0.0f;
//Debug.Log("Ref. Angle " + t_ReferenceAngle * Mathf.Rad2Deg);

// Here We Are Calculating All Points Based On N >> Number OF Triangles !!
for(int t_Temp = 0; t_Temp < NumberOfTriangles; t_Temp++)
{
t_PointCordinates = CenterPoint;
if(t_Temp == 0)
{
t_PointCordinates.z = t_PointCordinates.z + DistanceFromCenter;
t_AngleValue = 90 + t_AngleInDeg_OnCenter;
//Debug.Log(" Angle = " + t_AngleValue);
}
else
{
//Debug.Log(" Angle = " + t_AngleValue);
t_PointCordinates.x = t_PointCordinates.x + DistanceFromCenter * Mathf.Cos(t_ReferenceAngle + Mathf.Deg2Rad * t_AngleValue);
t_PointCordinates.z = t_PointCordinates.z + DistanceFromCenter * Mathf.Sin(t_ReferenceAngle + Mathf.Deg2Rad * t_AngleValue);

t_AngleValue += t_AngleInDeg_OnCenter;
}

// Chnage The Y here For Making The Depth !!
vertexList[t_Temp + 1] = t_PointCordinates;
//Debug.Log("Point = " + t_PointCordinates);
Color t_Color = new Color(Random.Range(0.0f,1.0f) +  0.5f,Random.Range(0.0f,1.0f) +  0.5f,Random.Range(0.0f,1.0f) + 0.5f);
Debug.DrawLine(CenterPoint,t_PointCordinates,t_Color,100);
}

// For Normals !!
for(int t_Temp = 0; t_Temp < vertexList.Length; t_Temp++)
normals[t_Temp] = -Vector3.up;

// For Triangles !!
for(int t_Temp = 0, t_TriangleCount = 0; t_Temp < triangles.Length; t_Temp = t_Temp + 3,t_TriangleCount++)
{
int t_VOrder = (t_TriangleCount + 2);

triangles[t_Temp] = t_VOrder;
if(t_VOrder % vertexList.Length == 0)
triangles[t_Temp] = t_VOrder % vertexList.Length + 1;

triangles[t_Temp + 1] = t_VOrder -1;
triangles[t_Temp + 2] = 0;  // To Make Last To be A Center Always !!
}

// For Setting The UV Maps !!
for(int t_Temp = 0; t_Temp < NumberOfTriangles + 1; t_Temp++)
{
// OLD TRIAL >>> FAILED :( :( !!

// float t_Ratio = (t_Temp/(NumberOfTriangles + 1));
// uv[t_Temp].x = t_Ratio;
// uv[t_Temp].y = 1 - t_Ratio;

// NEW TRIAL !! >> :) :)
uv[t_Temp].x	= (vertexList[t_Temp].x)/(NumberOfTriangles + 1);
uv[t_Temp].y	= (vertexList[t_Temp].z)/(NumberOfTriangles + 1);
}

// For Setting The Vertex Colors !!
for(int t_Temp = 0; t_Temp < vertexList.Length; t_Temp++)
{
float t_Red 	= 1 * t_Temp / (vertexList.Length*1.0f);
float t_Green 	= 0.5f - 1 * t_Red;
float t_Blue	= 0.25f + 1 * t_Green;

Color t_Color = new Color(t_Red,t_Green,t_Blue,1.0f);
Vertexcolors[t_Temp] = t_Color;
}

prismMesh.vertices = vertexList;
prismMesh.normals = normals;
prismMesh.uv = uv;
prismMesh.colors = Vertexcolors;
prismMesh.triangles = triangles;

// Returning The Final Mesh !!
return prismMesh;
}

}

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshFilter))]
public class PrismScript : MonoBehaviour
{
public int NumberOfTriangles;
public Vector3 CenterPoint = Vector3.zero;
public float DistanceFromCenter = 5;

int t_OldNumberOfTriangles;

// Use this for initialization
void Awake()
{
if(NumberOfTriangles < 3)
NumberOfTriangles = 3;

if(DistanceFromCenter < 3)
DistanceFromCenter = 3;

t_OldNumberOfTriangles = NumberOfTriangles;
GetComponent().mesh = CreatePrismMesh_N_Triangles();

InvokeRepeating("CheckData",5,1);
}

void CheckData()
{
if(t_OldNumberOfTriangles != NumberOfTriangles)
{
if(NumberOfTriangles < 3)
NumberOfTriangles = 3;

if(DistanceFromCenter < 3)
DistanceFromCenter = 3;

t_OldNumberOfTriangles = NumberOfTriangles;
GetComponent().mesh = CreatePrismMesh_N_Triangles();
}
}

Mesh CreatePrismMesh_N_Triangles()
{
// Created A Mesh !!
Mesh prismMesh = new Mesh();

// Generating Vertex Points List !!
Vector3[] vertexList = new Vector3[NumberOfTriangles + 1]; // +1 because One Point Will be Act As a Center Always !!

// Generating The Normals And UVs And Triangles List To Store The Vertex Order !!
Vector3[] normals	 = new Vector3[NumberOfTriangles + 1];
Vector2[] uv 		 = new Vector2[NumberOfTriangles + 1];
int[] 	  triangles  = new int[NumberOfTriangles * 3];			// Preserve the Vertex Order !!
Color[]	  Vertexcolors = new Color[NumberOfTriangles + 1];

vertexList[0] = CenterPoint;  // Center Point Of The Prism !!
vertexList[0].y += 5;
// Consider the points in XZ plane take a base as Y=0
Vector3 t_PointCordinates		= CenterPoint;
float   t_ReferenceAngle 		= Mathf.Atan2(CenterPoint.z, CenterPoint.x);  // Since here i am considering the XZ plane !!
float   t_AngleInDeg_OnCenter	= 360.0f / NumberOfTriangles;
float	t_AngleValue			= 0.0f;
//Debug.Log("Ref. Angle " + t_ReferenceAngle * Mathf.Rad2Deg);

// Here We Are Calculating All Points Based On N >> Number OF Triangles !!
for(int t_Temp = 0; t_Temp < NumberOfTriangles; t_Temp++)
{
t_PointCordinates = CenterPoint;
if(t_Temp == 0)
{
t_PointCordinates.z = t_PointCordinates.z + DistanceFromCenter;
t_AngleValue = 90 + t_AngleInDeg_OnCenter;
//Debug.Log(" Angle = " + t_AngleValue);
}
else
{
//Debug.Log(" Angle = " + t_AngleValue);
t_PointCordinates.x = t_PointCordinates.x + DistanceFromCenter * Mathf.Cos(t_ReferenceAngle + Mathf.Deg2Rad * t_AngleValue);
t_PointCordinates.z = t_PointCordinates.z + DistanceFromCenter * Mathf.Sin(t_ReferenceAngle + Mathf.Deg2Rad * t_AngleValue);

t_AngleValue += t_AngleInDeg_OnCenter;
}

// Chnage The Y here For Making The Depth !!
vertexList[t_Temp + 1] = t_PointCordinates;
//Debug.Log("Point = " + t_PointCordinates);
Color t_Color = new Color(Random.Range(0.0f,1.0f) +  0.5f,Random.Range(0.0f,1.0f) +  0.5f,Random.Range(0.0f,1.0f) + 0.5f);
Debug.DrawLine(CenterPoint,t_PointCordinates,t_Color,100);
}

// For Normals !!
for(int t_Temp = 0; t_Temp < vertexList.Length; t_Temp++)
normals[t_Temp] = -Vector3.up;

// For Triangles !!
for(int t_Temp = 0, t_TriangleCount = 0; t_Temp < triangles.Length; t_Temp = t_Temp + 3,t_TriangleCount++)
{
int t_VOrder = (t_TriangleCount + 2);

triangles[t_Temp] = t_VOrder;
if(t_VOrder % vertexList.Length == 0)
triangles[t_Temp] = t_VOrder % vertexList.Length + 1;

triangles[t_Temp + 1] = t_VOrder -1;
triangles[t_Temp + 2] = 0;  // To Make Last To be A Center Always !!
}

// For Setting The UV Maps !!
for(int t_Temp = 0; t_Temp < NumberOfTriangles + 1; t_Temp++)
{
// OLD TRIAL >>> FAILED :( :( !!

// float t_Ratio = (t_Temp/(NumberOfTriangles + 1));
// uv[t_Temp].x = t_Ratio;
// uv[t_Temp].y = 1 - t_Ratio;

// NEW TRIAL !! >> :) :)
uv[t_Temp].x	= (vertexList[t_Temp].x)/(NumberOfTriangles + 1);
uv[t_Temp].y	= (vertexList[t_Temp].z)/(NumberOfTriangles + 1);
}

// For Setting The Vertex Colors !!
for(int t_Temp = 0; t_Temp < vertexList.Length; t_Temp++)
{
float t_Red 	= 1 * t_Temp / (vertexList.Length*1.0f);
float t_Green 	= 0.5f - 1 * t_Red;
float t_Blue	= 0.25f + 1 * t_Green;

Color t_Color = new Color(t_Red,t_Green,t_Blue,1.0f);
Vertexcolors[t_Temp] = t_Color;
}

prismMesh.vertices = vertexList;
prismMesh.normals = normals;
prismMesh.uv = uv;
prismMesh.colors = Vertexcolors;
prismMesh.triangles = triangles;

// Returning The Final Mesh !!
return prismMesh;
}

}