Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alexander11 · Aug 28, 2016 at 02:39 AM · c#unity 53djs to c#extrusion

Hello i am having some trouble converting a JS to a C# script.

Hello i am having some trouble converting a JS script to a C# script, the script i am trying to convert as you can see below.

I am getting alot of errors which i dont know how to fix such as CS1061, CS1955, CS0103, etc.

Can someone please help me convert the js to c#?



-the JavaScript i am trying to put into C#.(The results of my script implementation is below)


 // Generates an extrusion trail from the attached mesh
 // Uses the MeshExtrusion algorithm in MeshExtrusion.cs to generate and preprocess the mesh.
 var time = 2.0;
 var autoCalculateOrientation = true;
 var minDistance = 0.1;
 var invertFaces = false;
 private var srcMesh : Mesh;
 private var precomputedEdges : MeshExtrusion.Edge[];
 
 class ExtrudedTrailSection
 {
     var point : Vector3;
     var matrix : Matrix4x4;
     var time : float;
 }
 
 function Start ()
 {
     srcMesh = GetComponent(MeshFilter).sharedMesh;
     precomputedEdges = MeshExtrusion.BuildManifoldEdges(srcMesh);
 }
 
 private var sections = new Array();
 
 function LateUpdate () {
     var position = transform.position;
     var now = Time.time;
     
     // Remove old sections
     while (sections.length > 0 && now > sections[sections.length - 1].time + time) {
         sections.Pop();
     }
 
     // Add a new trail section to beginning of array
     if (sections.length == 0 || (sections[0].point - position).sqrMagnitude > minDistance * minDistance)
     {
         var section = ExtrudedTrailSection ();
         section.point = position;
         section.matrix = transform.localToWorldMatrix;
         section.time = now;
         sections.Unshift(section);
     }
     
     // We need at least 2 sections to create the line
     if (sections.length < 2)
         return;
 
     var worldToLocal = transform.worldToLocalMatrix;
     var finalSections = new Matrix4x4[sections.length];
     var previousRotation : Quaternion;
     
     for (var i=0;i<sections.length;i++)
     {
         if (autoCalculateOrientation)
         {
             if (i == 0)
             {
                 var direction = sections[0].point - sections[1].point;
                 var rotation = Quaternion.LookRotation(direction, Vector3.up);
                 previousRotation = rotation;
                 finalSections[i] = worldToLocal * Matrix4x4.TRS(position, rotation, Vector3.one);    
             }
             // all elements get the direction by looking up the next section
             else if (i != sections.length - 1)
             {    
                 direction = sections[i].point - sections[i+1].point;
                 rotation = Quaternion.LookRotation(direction, Vector3.up);
                 
                 // When the angle of the rotation compared to the last segment is too high
                 // smooth the rotation a little bit. Optimally we would smooth the entire sections array.
                 if (Quaternion.Angle (previousRotation, rotation) > 20)
                     rotation = Quaternion.Slerp(previousRotation, rotation, 0.5);
                     
                 previousRotation = rotation;
                 finalSections[i] = worldToLocal * Matrix4x4.TRS(sections[i].point, rotation, Vector3.one);
             }
             // except the last one, which just copies the previous one
             else
             {
                 finalSections[i] = finalSections[i-1];
             }
         }
         else
         {
             if (i == 0)
             {
                 finalSections[i] = Matrix4x4.identity;
             }
             else
             {
                 finalSections[i] = worldToLocal * sections[i].matrix;
             }
         }
     }
     
     // Rebuild the extrusion mesh    
     MeshExtrusion.ExtrudeMesh (srcMesh, GetComponent(MeshFilter).mesh, finalSections, precomputedEdges, invertFaces);
 }
 
 @script RequireComponent (MeshFilter)
 


-Trying to convert JavaScript to CSharp(My results).

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [RequireComponent(typeof(MeshExtrusion))]
 public class Extrusion : MonoBehaviour
 {
     bool autoCalculateOrientation = true;
     bool invertFaces = false;
     Mesh srcMesh;
     MeshExtrusion.Edge[] precomputedEdges;
 
     class ExtrudedTrailSection
     {
         Vector3 point;
         Matrix4x4 matrix;
     }
     void Start()
     {
         srcMesh = GetComponent<MeshFilter>().sharedMesh;
         precomputedEdges = MeshExtrusion.BuildManifoldEdges(srcMesh);
     }
     ArrayList sections = new ArrayList();
     void LateUpdate()
     {
         var position = transform.position;
 
         if (sections.Length == 0 || (sections[0].point - position).sqrMagnitude)
         {
             var section = ExtrudedTrailSection();
             section.point = position;
             section.matrix = transform.localToWorldMatrix;
             section.time = now;
             sections.Unshift(section);
         }
 
         var worldToLocal = transform.worldToLocalMatrix;
         var finalSections = new Matrix4x4[sections.Length];
         if (sections.length < 2)
             return;
 
         worldToLocal = transform.worldToLocalMatrix;
         finalSections = new Matrix4x4[sections.length];
         Quaternion previousRotation;
 
         for (var i = 0; i < sections.length; i++)
         {
             if (autoCalculateOrientation)
             {
                 if (i == 0)
                 {
                     var direction = sections[0].point - sections[1].point;
                     var rotation = Quaternion.LookRotation(direction, Vector3.up);
                     previousRotation = rotation;
                     finalSections[i] = worldToLocal * Matrix4x4.TRS(position, rotation, Vector3.one);
                 }
                 else if (i != sections.length - 1)
                 {
                     direction = sections[i].point - sections[i + 1].point;
                     rotation = Quaternion.LookRotation(direction, Vector3.up);
 
                     if (Quaternion.Angle(previousRotation, rotation) > 20)
                         rotation = Quaternion.Slerp(previousRotation, rotation, 0.5);
 
                     previousRotation = rotation;
                     finalSections[i] = worldToLocal * Matrix4x4.TRS(sections[i].point, rotation, Vector3.one);
                 }
                 else
                 {
                     finalSections[i] = finalSections[i - 1];
                 }
             }
             else
             {
                 if (i == 0)
                 {
                     finalSections[i] = Matrix4x4.identity;
                 }
                 else
                 {
                     finalSections[i] = worldToLocal * sections[i].matrix;
                 }
             }
         }
 
         MeshExtrusion.ExtrudeMesh(srcMesh, GetComponent<MeshFilter>().mesh, finalSections, precomputedEdges, invertFaces);
     }
 }


And the JavaScript access's the MeshExtrusion.cs so here it is.

 using UnityEngine;
 using System.Collections;
 
 /*
 * An algorithm to extrude an arbitrary mesh along a number of sections.
 
 The mesh extrusion has 2 steps:
 
 1. Extracting an edge representation from an arbitrary mesh
 - A general edge extraction algorithm is employed. (Same algorithm as traditionally used for stencil shadows) Once all unique edges are found, all edges that connect to only one triangle are extracted.
  Thus we end up with the outline of the mesh.
 
 2. extruding the mesh from the edge representation.
 We simply generate a segments joining the edges 
 
 
 
 
 */
 
 public class MeshExtrusion
 {
     public class Edge
     {
         // The indiex to each vertex
         public int[]  vertexIndex = new int[2];
         // The index into the face.
         // (faceindex[0] == faceindex[1] means the edge connects to only one triangle)
         public int[]  faceIndex = new int[2];
     }
     
     public static void ExtrudeMesh (Mesh srcMesh, Mesh extrudedMesh, Matrix4x4[] extrusion, bool invertFaces)
     {
         Edge[] edges = BuildManifoldEdges(srcMesh);
         ExtrudeMesh(srcMesh, extrudedMesh, extrusion, edges, invertFaces);
     }    
     
     public static void ExtrudeMesh (Mesh srcMesh, Mesh extrudedMesh, Matrix4x4[] extrusion, Edge[] edges, bool invertFaces)
     {
         int extrudedVertexCount = edges.Length * 2 * extrusion.Length;
         int triIndicesPerStep = edges.Length * 6;
         int extrudedTriIndexCount = triIndicesPerStep * (extrusion.Length -1);
         
         Vector3[] inputVertices = srcMesh.vertices;
         Vector2[] inputUV = srcMesh.uv;
         int[] inputTriangles = srcMesh.triangles;
 
         Vector3[] vertices = new Vector3[extrudedVertexCount + srcMesh.vertexCount * 2];
         Vector2[] uvs = new Vector2[vertices.Length];
         int[] triangles = new int[extrudedTriIndexCount + inputTriangles.Length * 2];
 
         // Build extruded vertices
         int v = 0;
         for (int i=0;i<extrusion.Length;i++)
         {
             Matrix4x4 matrix = extrusion[i];
             float vcoord = (float)i / (extrusion.Length -1);
             foreach (Edge e in edges)
             {
                 vertices[v+0] = matrix.MultiplyPoint(inputVertices[e.vertexIndex[0]]);
                 vertices[v+1] = matrix.MultiplyPoint(inputVertices[e.vertexIndex[1]]);
 
                 uvs[v+0] = new Vector2 (inputUV[e.vertexIndex[0]].x, vcoord);
                 uvs[v+1] = new Vector2 (inputUV[e.vertexIndex[1]].x, vcoord);
                 
                 v += 2;
             }
         }        
         
         // Build cap vertices
         // * The bottom mesh we scale along it's negative extrusion direction. This way extruding a half sphere results in a capsule.
         for (int c=0;c<2;c++)
         {
             Matrix4x4 matrix = extrusion[c == 0 ? 0 : extrusion.Length-1];
             int firstCapVertex = c == 0 ? extrudedVertexCount : extrudedVertexCount + inputVertices.Length;
             for (int i=0;i<inputVertices.Length;i++)
             {
                 vertices[firstCapVertex + i] = matrix.MultiplyPoint(inputVertices[i]);
                 uvs[firstCapVertex + i] = inputUV[i];
             }
         }
         
         // Build extruded triangles
         for (int i=0;i<extrusion.Length-1;i++)
         {
             int baseVertexIndex = (edges.Length * 2) * i;
             int nextVertexIndex = (edges.Length * 2) * (i+1);
             for (int e=0;e<edges.Length;e++)
             {
                 int triIndex = i * triIndicesPerStep + e * 6;
 
                 triangles[triIndex + 0] = baseVertexIndex + e * 2;
                 triangles[triIndex + 1] = nextVertexIndex  + e * 2;
                 triangles[triIndex + 2] = baseVertexIndex + e * 2 + 1;
                 triangles[triIndex + 3] = nextVertexIndex + e * 2;
                 triangles[triIndex + 4] = nextVertexIndex + e * 2 + 1;
                 triangles[triIndex + 5] = baseVertexIndex  + e * 2 + 1;
             }
         }
         
         // build cap triangles
         int triCount = inputTriangles.Length / 3;
         // Top
         {
             int firstCapVertex = extrudedVertexCount;
             int firstCapTriIndex = extrudedTriIndexCount;
             for (int i=0;i<triCount;i++)
             {
                 triangles[i*3 + firstCapTriIndex + 0] = inputTriangles[i * 3 + 1] + firstCapVertex;
                 triangles[i*3 + firstCapTriIndex + 1] = inputTriangles[i * 3 + 2] + firstCapVertex;
                 triangles[i*3 + firstCapTriIndex + 2] = inputTriangles[i * 3 + 0] + firstCapVertex;
             }
         }
         
         // Bottom
         {
             int firstCapVertex = extrudedVertexCount + inputVertices.Length;
             int firstCapTriIndex = extrudedTriIndexCount + inputTriangles.Length;
             for (int i=0;i<triCount;i++)
             {
                 triangles[i*3 + firstCapTriIndex + 0] = inputTriangles[i * 3 + 0] + firstCapVertex;
                 triangles[i*3 + firstCapTriIndex + 1] = inputTriangles[i * 3 + 2] + firstCapVertex;
                 triangles[i*3 + firstCapTriIndex + 2] = inputTriangles[i * 3 + 1] + firstCapVertex;
             }
         }
         
         if (invertFaces)
         {
             for (int i=0;i<triangles.Length/3;i++)
             {
                 int temp = triangles[i*3 + 0];
                 triangles[i*3 + 0] = triangles[i*3 + 1];
                 triangles[i*3 + 1] = temp;
             }
         }
         
         extrudedMesh.Clear();
         extrudedMesh.name= "extruded";
         extrudedMesh.vertices = vertices;
         extrudedMesh.uv = uvs;
         extrudedMesh.triangles = triangles;
         extrudedMesh.RecalculateNormals();
     }
 
     /// Builds an array of edges that connect to only one triangle.
     /// In other words, the outline of the mesh    
     public static Edge[] BuildManifoldEdges (Mesh mesh)
     {
         // Build a edge list for all unique edges in the mesh
         Edge[] edges = BuildEdges(mesh.vertexCount, mesh.triangles);
         
         // We only want edges that connect to a single triangle
         ArrayList culledEdges = new ArrayList();
         foreach (Edge edge in edges)
         {
             if (edge.faceIndex[0] == edge.faceIndex[1])
             {
                 culledEdges.Add(edge);
             }
         }
 
         return culledEdges.ToArray(typeof(Edge)) as Edge[];
     }
 
     /// Builds an array of unique edges
     /// This requires that your mesh has all vertices welded. However on import, Unity has to split
     /// vertices at uv seams and normal seams. Thus for a mesh with seams in your mesh you
     /// will get two edges adjoining one triangle.
     /// Often this is not a problem but you can fix it by welding vertices 
     /// and passing in the triangle array of the welded vertices.
     public static Edge[] BuildEdges(int vertexCount, int[] triangleArray)
     {
         int maxEdgeCount = triangleArray.Length;
         int[] firstEdge = new int[vertexCount + maxEdgeCount];
         int nextEdge = vertexCount;
         int triangleCount = triangleArray.Length / 3;
         
         for (int a = 0; a < vertexCount; a++)
             firstEdge[a] = -1;
             
         // First pass over all triangles. This finds all the edges satisfying the
         // condition that the first vertex index is less than the second vertex index
         // when the direction from the first vertex to the second vertex represents
         // a counterclockwise winding around the triangle to which the edge belongs.
         // For each edge found, the edge index is stored in a linked list of edges
         // belonging to the lower-numbered vertex index i. This allows us to quickly
         // find an edge in the second pass whose higher-numbered vertex index is i.
         Edge[] edgeArray = new Edge[maxEdgeCount];
         
         int edgeCount = 0;
         for (int a = 0; a < triangleCount; a++)
         {
             int i1 = triangleArray[a*3 + 2];
             for (int b = 0; b < 3; b++)
             {
                 int i2 = triangleArray[a*3 + b];
                 if (i1 < i2)
                 {
                     Edge newEdge = new Edge();
                     newEdge.vertexIndex[0] = i1;
                     newEdge.vertexIndex[1] = i2;
                     newEdge.faceIndex[0] = a;
                     newEdge.faceIndex[1] = a;
                     edgeArray[edgeCount] = newEdge;
                     
                     int edgeIndex = firstEdge[i1];
                     if (edgeIndex == -1)
                     {
                         firstEdge[i1] = edgeCount;
                     }
                     else
                     {
                         while (true)
                         {
                             int index = firstEdge[nextEdge + edgeIndex];
                             if (index == -1)
                             {
                                 firstEdge[nextEdge + edgeIndex] = edgeCount;
                                 break;
                             }
                         
                             edgeIndex = index;
                         }
                     }
             
                     firstEdge[nextEdge + edgeCount] = -1;
                     edgeCount++;
                 }
             
                 i1 = i2;
             }
         }
         
         // Second pass over all triangles. This finds all the edges satisfying the
         // condition that the first vertex index is greater than the second vertex index
         // when the direction from the first vertex to the second vertex represents
         // a counterclockwise winding around the triangle to which the edge belongs.
         // For each of these edges, the same edge should have already been found in
         // the first pass for a different triangle. Of course we might have edges with only one triangle
         // in that case we just add the edge here
         // So we search the list of edges
         // for the higher-numbered vertex index for the matching edge and fill in the
         // second triangle index. The maximum number of comparisons in this search for
         // any vertex is the number of edges having that vertex as an endpoint.
         
         for (int a = 0; a < triangleCount; a++)
         {
             int i1 = triangleArray[a*3+2];
             for (int b = 0; b < 3; b++)
             {
                 int i2 = triangleArray[a*3+b];
                 if (i1 > i2)
                 {
                     bool foundEdge = false;
                     for (int edgeIndex = firstEdge[i2]; edgeIndex != -1;edgeIndex = firstEdge[nextEdge + edgeIndex])
                     {
                         Edge edge = edgeArray[edgeIndex];
                         if ((edge.vertexIndex[1] == i1) && (edge.faceIndex[0] == edge.faceIndex[1]))
                         {
                             edgeArray[edgeIndex].faceIndex[1] = a;
                             foundEdge = true;
                             break;
                         }
                     }
                     
                     if (!foundEdge)
                     {
                         Edge newEdge = new Edge();
                         newEdge.vertexIndex[0] = i1;
                         newEdge.vertexIndex[1] = i2;
                         newEdge.faceIndex[0] = a;
                         newEdge.faceIndex[1] = a;
                         edgeArray[edgeCount] = newEdge;
                         edgeCount++;
                     }
                 }
                 
                 i1 = i2;
             }
         }
         
         Edge[] compactedEdges = new Edge[edgeCount];
         for (int e=0;e<edgeCount;e++)
             compactedEdges[e] = edgeArray[e];
         
         return compactedEdges;
     }
 }

Hey @NoseKills do you mind if i could have your help on this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jdean300 · Aug 28, 2016 at 03:51 AM

The only issue I can see is in line 28 - .sqrMagnitude is not a bool, you need to compare it to some value in order to put it into an if statement. (sections[0].point - position).sqrMagnitude > 0f For example

CSxxxx: no one knows what these are - post what the error is and what line it is coming from. Generally you should start with fixing the first error in the file because it often causes the others.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

231 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i access the MeshExtrusion.cs from Unity's PE? 1 Answer

Getting errors with extrusion script. 0 Answers

How do i Achieve mesh Extrusion? 2 Answers

How can I rotate my player with a Joystick? 0 Answers

Generate x,y,z coordinate for vehicles path 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges