Files
2026-06-04 11:42:34 +02:00

137 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;
namespace Parabox.STL
{
// Token: 0x02000003 RID: 3
public static class pb_Stl
{
// Token: 0x06000001 RID: 1 RVA: 0x00002050 File Offset: 0x00000250
public static bool WriteFile(string path, Mesh mesh, FileType type = FileType.Ascii, bool convertToRightHandedCoordinates = true)
{
return pb_Stl.WriteFile(path, new Mesh[] { mesh }, type, convertToRightHandedCoordinates);
}
// Token: 0x06000002 RID: 2 RVA: 0x00002064 File Offset: 0x00000264
public static bool WriteFile(string path, IList<Mesh> meshes, FileType type = FileType.Ascii, bool convertToRightHandedCoordinates = true)
{
try
{
if (type == FileType.Binary)
{
using (BinaryWriter binaryWriter = new BinaryWriter(File.Open(path, FileMode.Create), new ASCIIEncoding()))
{
binaryWriter.Write(new byte[80]);
uint num = (uint)(meshes.Sum((Mesh x) => x.triangles.Length) / 3);
binaryWriter.Write(num);
foreach (Mesh mesh in meshes)
{
Vector3[] array = (convertToRightHandedCoordinates ? pb_Stl.Left2Right(mesh.vertices) : mesh.vertices);
Vector3[] array2 = (convertToRightHandedCoordinates ? pb_Stl.Left2Right(mesh.normals) : mesh.normals);
int[] triangles = mesh.triangles;
int num2 = triangles.Length;
if (convertToRightHandedCoordinates)
{
Array.Reverse(triangles);
}
for (int i = 0; i < num2; i += 3)
{
int num3 = triangles[i];
int num4 = triangles[i + 1];
int num5 = triangles[i + 2];
Vector3 vector = pb_Stl.AvgNrm(array2[num3], array2[num4], array2[num5]);
binaryWriter.Write(vector.x);
binaryWriter.Write(vector.y);
binaryWriter.Write(vector.z);
binaryWriter.Write(array[num3].x);
binaryWriter.Write(array[num3].y);
binaryWriter.Write(array[num3].z);
binaryWriter.Write(array[num4].x);
binaryWriter.Write(array[num4].y);
binaryWriter.Write(array[num4].z);
binaryWriter.Write(array[num5].x);
binaryWriter.Write(array[num5].y);
binaryWriter.Write(array[num5].z);
binaryWriter.Write(0);
}
}
goto IL_21C;
}
}
string text = pb_Stl.WriteString(meshes, true);
File.WriteAllText(path, text);
IL_21C:;
}
catch (Exception ex)
{
Debug.LogError(ex.ToString());
return false;
}
return true;
}
// Token: 0x06000003 RID: 3 RVA: 0x000022F0 File Offset: 0x000004F0
public static string WriteString(Mesh mesh, bool convertToRightHandedCoordinates = true)
{
return pb_Stl.WriteString(new Mesh[] { mesh }, convertToRightHandedCoordinates);
}
// Token: 0x06000004 RID: 4 RVA: 0x00002304 File Offset: 0x00000504
public static string WriteString(IList<Mesh> meshes, bool convertToRightHandedCoordinates = true)
{
StringBuilder stringBuilder = new StringBuilder();
string text = ((meshes.Count == 1) ? meshes[0].name : "Composite Mesh");
stringBuilder.AppendLine(string.Format("solid {0}", text));
foreach (Mesh mesh in meshes)
{
Vector3[] array = (convertToRightHandedCoordinates ? pb_Stl.Left2Right(mesh.vertices) : mesh.vertices);
Vector3[] array2 = (convertToRightHandedCoordinates ? pb_Stl.Left2Right(mesh.normals) : mesh.normals);
int[] triangles = mesh.triangles;
if (convertToRightHandedCoordinates)
{
Array.Reverse(triangles);
}
int num = triangles.Length;
for (int i = 0; i < num; i += 3)
{
int num2 = triangles[i];
int num3 = triangles[i + 1];
int num4 = triangles[i + 2];
Vector3 vector = pb_Stl.AvgNrm(array2[num2], array2[num3], array2[num4]);
stringBuilder.AppendLine(string.Format("facet normal {0} {1} {2}", vector.x, vector.y, vector.z));
stringBuilder.AppendLine("outer loop");
stringBuilder.AppendLine(string.Format("\tvertex {0} {1} {2}", array[num2].x, array[num2].y, array[num2].z));
stringBuilder.AppendLine(string.Format("\tvertex {0} {1} {2}", array[num3].x, array[num3].y, array[num3].z));
stringBuilder.AppendLine(string.Format("\tvertex {0} {1} {2}", array[num4].x, array[num4].y, array[num4].z));
stringBuilder.AppendLine("endloop");
stringBuilder.AppendLine("endfacet");
}
}
stringBuilder.AppendLine(string.Format("endsolid {0}", text));
return stringBuilder.ToString();
}
// Token: 0x06000005 RID: 5 RVA: 0x00002578 File Offset: 0x00000778
private static Vector3[] Left2Right(Vector3[] v)
{
Matrix4x4 matrix4x = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(1f, 1f, -1f));
Vector3[] array = new Vector3[v.Length];
for (int i = 0; i < v.Length; i++)
{
array[i] = matrix4x.MultiplyPoint3x4(v[i]);
}
return array;
}
// Token: 0x06000006 RID: 6 RVA: 0x000025D8 File Offset: 0x000007D8
private static Vector3 AvgNrm(Vector3 a, Vector3 b, Vector3 c)
{
return new Vector3((a.x + b.x + c.x) / 3f, (a.y + b.y + c.y) / 3f, (a.z + b.z + c.z) / 3f);
}
}
}