public void Test_Should_Add_Vertices_To_List() { // Set List <Vector3> vertices = new List <Vector3>(); Vector3[] triangleVertices = new Vector3[] { vertex1, vertex2, vertex3 }; Color[] triangleColors = new Color[] { color1, color2, color3 }; int vertexIndex = 3; RenderTriangle renderTriangle = new RenderTriangle(vertexIndex, triangleVertices, triangleColors); // Act List <Vector3> verticesResult = renderTriangle.AddVertices(vertices); Assert.That( new Vector3(0f, 0f, 0f), Is.EqualTo(verticesResult.ToArray()[0]) ); Assert.That( new Vector3(0f, 0f, 2f), Is.EqualTo(verticesResult.ToArray()[1]) ); Assert.That( new Vector3(2f, 0f, 0f), Is.EqualTo(verticesResult.ToArray()[2]) ); }
void Start() { triangulationManager = FindObjectOfType <TriangulationManager>(); renderTriangle = FindObjectOfType <RenderTriangle>(); colorTecniqueDD.onValueChanged.AddListener(delegate { OnColorTecnique(colorTecniqueDD.value); }); triangulationAlgDD.onValueChanged.AddListener(delegate { OnTriangulationAlg(triangulationAlgDD.value); }); amountSL.onValueChanged.AddListener(delegate { OnChangeAmount(amountSL.value); }); redSL.onValueChanged.AddListener(delegate { OnChangeColor(redSL.value, greenSL.value, blueSL.value, alphaSL.value); }); greenSL.onValueChanged.AddListener(delegate { OnChangeColor(redSL.value, greenSL.value, blueSL.value, alphaSL.value); }); blueSL.onValueChanged.AddListener(delegate { OnChangeColor(redSL.value, greenSL.value, blueSL.value, alphaSL.value); }); alphaSL.onValueChanged.AddListener(delegate { OnChangeColor(redSL.value, greenSL.value, blueSL.value, alphaSL.value); }); BGredSL.onValueChanged.AddListener(delegate { OnChangeBackgroundColor(BGredSL.value, BGgreenSL.value, BGblueSL.value); }); BGgreenSL.onValueChanged.AddListener(delegate { OnChangeBackgroundColor(BGredSL.value, BGgreenSL.value, BGblueSL.value); }); BGblueSL.onValueChanged.AddListener(delegate { OnChangeBackgroundColor(BGredSL.value, BGgreenSL.value, BGblueSL.value); }); calculateAlphaT.onValueChanged.AddListener(delegate { OnChangeCalculatedAlpha(calculateAlphaT.isOn); }); invertColorsT.onValueChanged.AddListener(delegate { OnChangeInvertColors(invertColorsT.isOn); }); }
public void Test_Should_Add_Colors_To_List() { // Set List <Color> colors = new List <Color>(); Vector3[] triangleVertices = new Vector3[] { vertex1, vertex2, vertex3 }; Color[] triangleColors = new Color[] { color1, color2, color3 }; int vertexIndex = 1; RenderTriangle renderTriangle = new RenderTriangle(vertexIndex, triangleVertices, triangleColors); // Act List <Color> colorsResult = renderTriangle.AddColors(colors); // Assert Assert.That(Color.black, Is.EqualTo(colorsResult[0])); Assert.That(Color.black, Is.EqualTo(colorsResult[1])); Assert.That(Color.black, Is.EqualTo(colorsResult[2])); }
public void Test_Should_Add_Triangles_To_List() { // Set List <int> triangles = new List <int>(); Vector3[] triangleVertices = new Vector3[] { vertex1, vertex2, vertex3 }; Color[] triangleColors = new Color[] { color1, color2, color3 }; int vertexIndex = 1; RenderTriangle renderTriangle = new RenderTriangle(vertexIndex, triangleVertices, triangleColors); // Act List <int> trianglesResult = renderTriangle.AddTriangles(triangles); // Assert Assert.That(1, Is.EqualTo(trianglesResult[0])); Assert.That(2, Is.EqualTo(trianglesResult[1])); Assert.That(3, Is.EqualTo(trianglesResult[2])); }
public Blocks.RenderTriangle[] ToRenderTriangles() { RenderTriangle[] res = new RenderTriangle[vertices.Length / 3]; for (int i = 0; i < res.Length; i++) { //Debug.Log(vertices[i * 3].x + " " + vertices[i * 3].y + " " + vertices[i * 3].z); //Debug.Log(vertices[i * 3+1].x + " " + vertices[i * 3+1].y + " " + vertices[i * 3+1].z); //Debug.Log(vertices[i * 3+2].x + " " + vertices[i * 3+2].y + " " + vertices[i * 3+2].z); int pos = i * 3; RenderTriangle cur = new RenderTriangle(); cur.vertex1 = Util.MakeFloat4(vertices[pos].x, vertices[pos].y, vertices[pos].z, 0); cur.vertex2 = Util.MakeFloat4(vertices[pos + 1].x, vertices[pos + 1].y, vertices[pos + 1].z, 0); cur.vertex3 = Util.MakeFloat4(vertices[pos + 2].x, vertices[pos + 2].y, vertices[pos + 2].z, 0); cur.uv1 = Util.MakeFloat2(uvs[pos].x, uvs[pos].y); cur.uv2 = Util.MakeFloat2(uvs[pos + 1].x, uvs[pos + 1].y); cur.uv3 = Util.MakeFloat2(uvs[pos + 2].x, uvs[pos + 2].y); res[i] = cur; } return(res); }
public void GenerateUnitQuad() { Vector3[] vertices = new Vector3[] { new Vector3( 1, 0, 1), new Vector3( 1, 0, -1), new Vector3(-1, 0, 1), new Vector3(-1, 0, -1), }; Vector2[] uv = new Vector2[] { new Vector2(1, 1), new Vector2(1, 0), new Vector2(0, 1), new Vector2(0, 0), }; int[] triangles = new int[] { 0, 1, 2, 2, 1, 3, }; RenderTriangle tri1 = new RenderTriangle(); tri1.SetPoints(vertices[triangles[0]], vertices[triangles[1]], vertices[triangles[2]]); tri1.SetUV(uv[triangles[0]], uv[triangles[1]], uv[triangles[2]]); RenderTriangle tri2 = new RenderTriangle(); tri2.SetPoints(vertices[triangles[3]], vertices[triangles[4]], vertices[triangles[5]]); tri2.SetUV(uv[triangles[3]], uv[triangles[4]], uv[triangles[5]]); AddTriangle(tri1); AddTriangle(tri2); }
public void GenerateUnitQuad() { Vector3[] vertices = new Vector3[] { new Vector3(1, 0, 1), new Vector3(1, 0, -1), new Vector3(-1, 0, 1), new Vector3(-1, 0, -1), }; Vector2[] uv = new Vector2[] { new Vector2(1, 1), new Vector2(1, 0), new Vector2(0, 1), new Vector2(0, 0), }; int[] triangles = new int[] { 0, 1, 2, 2, 1, 3, }; RenderTriangle tri1 = new RenderTriangle(); tri1.SetPoints(vertices[triangles[0]], vertices[triangles[1]], vertices[triangles[2]]); tri1.SetUV(uv[triangles[0]], uv[triangles[1]], uv[triangles[2]]); RenderTriangle tri2 = new RenderTriangle(); tri2.SetPoints(vertices[triangles[3]], vertices[triangles[4]], vertices[triangles[5]]); tri2.SetUV(uv[triangles[3]], uv[triangles[4]], uv[triangles[5]]); AddTriangle(tri1); AddTriangle(tri2); }
public void AddTriangle(RenderTriangle tri) { this.tris.Add(tri); }
private void Split() { NDPlane plane = new NDPlane(); plane.ComputePlane((mpta + mptb + mptc) / 3, RandomVector3()); List <Vector3> upper = new List <Vector3>(); List <Vector3> lower = new List <Vector3>(); List <Vector3> intersection = new List <Vector3>(); plane.IntersectTriangleHull(mpta, mptb, mptc, upper, lower, intersection); upper.AddRange(intersection); lower.AddRange(intersection); List <int> indices = new List <int>(); // UPPER HULL Triangulator.TriangulateNDSlice(upper, indices); for (int i = 0; i < indices.Count; i += 3) { RenderTriangle tri = new RenderTriangle(); Vector3 pta = upper[indices[i]]; Vector3 ptb = upper[indices[i + 1]]; Vector3 ptc = upper[indices[i + 2]]; Vector2 uva = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref pta); Vector2 uvb = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref ptb); Vector2 uvc = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref ptc); tri.SetPoints(pta, ptb, ptc); tri.SetUV(uva, uvb, uvc); splits.Add(tri); } indices.Clear(); // LOWER HULL Triangulator.TriangulateNDSlice(lower, indices); for (int i = 0; i < indices.Count; i += 3) { RenderTriangle tri = new RenderTriangle(); Vector3 pta = lower[indices[i]]; Vector3 ptb = lower[indices[i + 1]]; Vector3 ptc = lower[indices[i + 2]]; Vector2 uva = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref pta); Vector2 uvb = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref ptb); Vector2 uvc = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref ptc); tri.SetPoints(pta, ptb, ptc); tri.SetUV(uva, uvb, uvc); splits.Add(tri); } }
private void Split() { NDPlane plane = new NDPlane(); plane.ComputePlane((mpta + mptb + mptc) / 3, RandomVector3()); List<Vector3> upper = new List<Vector3>(); List<Vector3> lower = new List<Vector3>(); List<Vector3> intersection = new List<Vector3>(); plane.IntersectTriangleHull(mpta, mptb, mptc, upper, lower, intersection); upper.AddRange(intersection); lower.AddRange(intersection); List<int> indices = new List<int>(); // UPPER HULL Triangulator.TriangulateNDSlice(upper, indices); for (int i = 0; i < indices.Count; i += 3) { RenderTriangle tri = new RenderTriangle(); Vector3 pta = upper[indices[i]]; Vector3 ptb = upper[indices[i+1]]; Vector3 ptc = upper[indices[i+2]]; Vector2 uva = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref pta); Vector2 uvb = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref ptb); Vector2 uvc = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref ptc); tri.SetPoints(pta, ptb, ptc); tri.SetUV(uva, uvb, uvc); splits.Add(tri); } indices.Clear(); // LOWER HULL Triangulator.TriangulateNDSlice(lower, indices); for (int i = 0; i < indices.Count; i += 3) { RenderTriangle tri = new RenderTriangle(); Vector3 pta = lower[indices[i]]; Vector3 ptb = lower[indices[i + 1]]; Vector3 ptc = lower[indices[i + 2]]; Vector2 uva = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref pta); Vector2 uvb = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref ptb); Vector2 uvc = Triangulator.GenerateUVCoords(ref mpta, ref mptb, ref mptc, ref muva, ref muvb, ref muvc, ref ptc); tri.SetPoints(pta, ptb, ptc); tri.SetUV(uva, uvb, uvc); splits.Add(tri); } }
public void AddTriangle(RenderTriangle tri) { this.tris.Add(tri); }
int Triangle_ClipAgainstPlane(vec3 plane_p, vec3 plane_n, RenderTriangle in_tri, ref RenderTriangle out_tri1, ref RenderTriangle out_tri2) { // Make sure plane normal is indeed normal plane_n = RenderMath.Vector_Normalize(plane_n); // Return signed shortest distance from point to plane, plane normal must be normalised // Create two temporary storage arrays to classify points either side of plane // If distance sign is positive, point lies on "inside" of plane vec3[] inside_points = new vec3[3]; int nInsidePointCount = 0; vec3[] outside_points = new vec3[3]; int nOutsidePointCount = 0; // Get signed distance of each point in triangle to plane float d0 = dist(in_tri.points[0], plane_p, plane_n); float d1 = dist(in_tri.points[1], plane_p, plane_n); float d2 = dist(in_tri.points[2], plane_p, plane_n); if (d0 >= 0) { inside_points[nInsidePointCount++] = in_tri.points[0]; } else { outside_points[nOutsidePointCount++] = in_tri.points[0]; } if (d1 >= 0) { inside_points[nInsidePointCount++] = in_tri.points[1]; } else { outside_points[nOutsidePointCount++] = in_tri.points[1]; } if (d2 >= 0) { inside_points[nInsidePointCount++] = in_tri.points[2]; } else { outside_points[nOutsidePointCount++] = in_tri.points[2]; } // Now classify triangle points, and break the input triangle into // smaller output triangles if required. There are four possible // outcomes... if (nInsidePointCount == 0) { // All points lie on the outside of plane, so clip whole triangle // It ceases to exist return(0); // No returned triangles are valid } if (nInsidePointCount == 3) { // All points lie on the inside of plane, so do nothing // and allow the triangle to simply pass through out_tri1 = in_tri; return(1); // Just the one returned original triangle is valid } if (nInsidePointCount == 1 && nOutsidePointCount == 2) { // Triangle should be clipped. As two points lie outside // the plane, the triangle simply becomes a smaller triangle // Copy appearance info to new triangle out_tri1.lightValue = in_tri.lightValue; out_tri1.faceColor = in_tri.faceColor; // The inside point is valid, so keep that... out_tri1.points[0] = inside_points[0]; // but the two new points are at the locations where the // original sides of the triangle (lines) intersect with the plane out_tri1.points[1] = Vector_IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0]); out_tri1.points[2] = Vector_IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[1]); return(1); // Return the newly formed single triangle } if (nInsidePointCount == 2 && nOutsidePointCount == 1) { // Triangle should be clipped. As two points lie inside the plane, // the clipped triangle becomes a "quad". Fortunately, we can // represent a quad with two new triangles // Copy appearance info to new triangles out_tri1.lightValue = in_tri.lightValue; out_tri1.faceColor = in_tri.faceColor; out_tri2.lightValue = in_tri.lightValue; out_tri2.faceColor = in_tri.faceColor; // The first triangle consists of the two inside points and a new // point determined by the location where one side of the triangle // intersects with the plane out_tri1.points[0] = inside_points[0]; out_tri1.points[1] = inside_points[1]; out_tri1.points[2] = Vector_IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0]); // The second triangle is composed of one of he inside points, a // new point determined by the intersection of the other side of the // triangle and the plane, and the newly created point above out_tri2.points[0] = inside_points[1]; out_tri2.points[1] = out_tri1.points[2]; out_tri2.points[2] = Vector_IntersectPlane(plane_p, plane_n, inside_points[1], outside_points[0]); return(2); // Return two newly formed triangles which form a quad } return(0); }
void RenderCustomMesh(CustomMesh renderMesh) { vec3 rotation = renderMesh.rotation * Mathf.Deg2Rad; rotMat = RenderMath.Matrix_MakeRotation(new vec3(rotation.x, 0, rotation.z)); RenderTriangle triProjected, triTransformed, triViewed; // float outputColor = 0f; Matrix4x4 transMat = RenderMath.Matrix_MakeTranslation(renderMesh.position); Matrix4x4 worldMat = RenderMath.Matrix_MakeIdentity(); worldMat = rotMat; worldMat = RenderMath.Matrix_MultiplyMatrix(worldMat, transMat); vec3 vUp = new vec3(0, 1, 0); vec3 vTarget = new vec3(0, 0, 1); Matrix4x4 cameraRotMat = RenderMath.Matrix_MakeRotationY(cameraRotation.y); vLookDir = RenderMath.Matrix_MultiplyVector(vTarget, cameraRotMat); vTarget = vCamera + vLookDir; Matrix4x4 cameraMat = RenderMath.Matrix_PointAt(vCamera, vTarget, vUp); Matrix4x4 viewMat = RenderMath.Matrix_QuickInverse(cameraMat); for (int i = 0; i < renderMesh.tris.Count; i += 3) { triProjected = new RenderTriangle(); triTransformed = new RenderTriangle(); triViewed = new RenderTriangle(); triTransformed.points[0] = RenderMath.Matrix_MultiplyVector(renderMesh.verts[renderMesh.tris[i]], worldMat); triTransformed.points[1] = RenderMath.Matrix_MultiplyVector(renderMesh.verts[renderMesh.tris[i + 1]], worldMat); triTransformed.points[2] = RenderMath.Matrix_MultiplyVector(renderMesh.verts[renderMesh.tris[i + 2]], worldMat); // triTransformed.Log(); vec3 normal, line1, line2; line1 = triTransformed.points[1] - triTransformed.points[0]; line2 = triTransformed.points[2] - triTransformed.points[0]; normal = RenderMath.Vector_CrossProduct(line1, line2); normal = RenderMath.Vector_Normalize(normal); //Debug.DrawRay(new Vector3(triTransformed.averagePos.x,triTransformed.averagePos.y,triTransformed.averagePos.z), new Vector3(normal.x,normal.y,normal.z)); //show mesh normals in scene view; if (RenderMath.Vector_DotProduct(normal, triTransformed.points[0] - vCamera) < 0f) { vec3 light_direction = new vec3(0, 1, -1); light_direction = RenderMath.Vector_Normalize(light_direction); triViewed.lightValue = Mathf.Max(0.1f, RenderMath.Vector_DotProduct(light_direction, normal)); triViewed.faceColor = (renderMesh.triColors.Count * 3 == renderMesh.tris.Count ? renderMesh.triColors[i / 3] : Color.white); triViewed.points[0] = RenderMath.Matrix_MultiplyVector(triTransformed.points[0], viewMat); triViewed.points[1] = RenderMath.Matrix_MultiplyVector(triTransformed.points[1], viewMat); triViewed.points[2] = RenderMath.Matrix_MultiplyVector(triTransformed.points[2], viewMat); int nClippedTriangles = 0; RenderTriangle[] clipped = new RenderTriangle[2]; clipped[0] = new RenderTriangle(); clipped[1] = new RenderTriangle(); nClippedTriangles = Triangle_ClipAgainstPlane(new vec3(0.0f, 0.0f, 0.1f), new vec3(0.0f, 0.0f, 1.0f), triViewed, ref clipped[0], ref clipped[1]); for (int n = 0; n < nClippedTriangles; n++) { triProjected.lightValue = clipped[n].lightValue; triProjected.faceColor = clipped[n].faceColor; triProjected.points[0] = RenderMath.Matrix_MultiplyVector(triViewed.points[0], projMat); triProjected.points[1] = RenderMath.Matrix_MultiplyVector(triViewed.points[1], projMat); triProjected.points[2] = RenderMath.Matrix_MultiplyVector(triViewed.points[2], projMat); triProjected.points[0] = RenderMath.Vector_Normalize(triProjected.points[0]); triProjected.points[1] = RenderMath.Vector_Normalize(triProjected.points[1]); triProjected.points[2] = RenderMath.Vector_Normalize(triProjected.points[2]); /* * triProjected.points[0].x *= -1.0f; * triProjected.points[1].x *= -1.0f; * triProjected.points[2].x *= -1.0f; * triProjected.points[0].y *= -1.0f; * triProjected.points[1].y *= -1.0f; * triProjected.points[2].y *= -1.0f; */ vec3 vOffsetView = new vec3(1, 1, 0); triProjected.points[0] += vOffsetView; triProjected.points[1] += vOffsetView; triProjected.points[2] += vOffsetView; triProjected.points[0].x *= 0.5f * (float)width; triProjected.points[0].y *= 0.5f * (float)height; triProjected.points[1].x *= 0.5f * (float)width; triProjected.points[1].y *= 0.5f * (float)height; triProjected.points[2].x *= 0.5f * (float)width; triProjected.points[2].y *= 0.5f * (float)height; vecTrianglesToRaster.Add(triProjected); } } } }