Projext/Assets/Dungeon_Environment/Scripts/Utilities.cs

1 line
20 KiB
C#
Raw Permalink Normal View History

2026-02-22 13:37:34 +00:00
#if UNITY_EDITORusing UnityEngine;using System.Collections;using UnityEditor;using System.IO;using System.Collections.Generic;using System;using Random = UnityEngine.Random;using System.Globalization;public class UTUUtilities : AssetPostprocessor{ class Section { public int Offset; public int NumIndices; public int Materialindex; public string MaterialName; public Material Mat; } class MeshBinding { public Mesh mesh; public MeshRenderer renderer; } void GetFloatsFromLine( string Line, ref List<float> Floats, GameObject gameobject ) { Floats.Clear(); string[] Tokens = Line.Split(new char[]{ ' ' } ); for( int u = 1; u < Tokens.Length; u++ ) { try { CultureInfo ci = (CultureInfo)CultureInfo.InvariantCulture.Clone(); ci.NumberFormat.CurrencyDecimalSeparator = "."; float Val = float.Parse( Tokens[u], ci.NumberFormat); Floats.Add( Val ); } catch( FormatException e ) { Debug.LogError( "float.Parse failed in " + gameobject.name + " input=" + Tokens[u] + " exception=" + e.Message ); } } } void GetIntsFromLine( string Line, ref List<int> Ints, GameObject gameobject ) { Ints.Clear(); string[] Tokens = Line.Split(new char[]{ ' ', '/' } ); for( int u = 1; u < Tokens.Length; u++ ) { try { int Val = int.Parse( Tokens[u] ); Ints.Add( Val ); } catch( FormatException e ) { Debug.LogError( "int.Parse failed in " + gameobject.name + " input=" + Tokens[u] + " exception=" + e.Message ); } } } private void OnPostprocessModel( GameObject gameobject ) { UnityEditor.ModelImporter importer = this.assetImporter as UnityEditor.ModelImporter; string Path = importer.assetPath; if( !Path.Contains( ".obj" ) ) return; string OBJFile = File.ReadAllText( Path ); List<float> Floats = new List<float>(); List<int> Ints = new List<int>(); List<Vector3> Vertices = new List<Vector3>(); List<Color> Colors = new List<Color>(); List<Vector2> Texcoords0 = new List<Vector2>(); List<Vector2> Texcoords1 = new List<Vector2>(); List<Vector3> Normals = new List<Vector3>(); List<Vector4> Tangents = new List<Vector4>(); bool ReplaceNormals = true; List<int> Indices = new List<int>(); List<Section> Sections = new List<Section>(); string[] Lines = OBJFile.Split( new char[]{ '\n' } ); for( int i = 0; i < Lines.Length; i++ ) { string Line = Lines[i]; if( Line.StartsWith( "v " ) ) { string[] Tokens = Line.Split(new char[]{ ' ' } ); Floats.Clear(); for( int u = 1; u < Tokens.Length; u++ )//0 should be "v" { try { CultureInfo ci = (CultureInfo)CultureInfo.InvariantCulture.Clone(); ci.NumberFormat.CurrencyDecimalSeparator = "."; float Val = float.Parse( Tokens[u], ci.NumberFormat); Floats.Add( Val ); } catch( FormatException e ) { Debug.LogError( "float.Parse failed for verts in " + gameobject.name + " input=" + Tokens[u] + " exception=" + e.Message ); } } if( Floats.Count >= 3 ) Vertices.Add( new Vector3( Floats[0], Floats[1], Floats[2] ) ); else Vertices.Add( new Vector3( 0, 0, 0 ) ); if( Floats.Count == 7 ) { Colors.Add( new Color( Floats[3], Floats[4], Floats[5], Floats[6] ) ); } } else if( Line.StartsWith( "vt " ) ) { GetFloatsFromLine( Line, ref Floats, gameobject ); if( Floats.Count == 2 ) { Texcoords0.Add( new Vector2( Floats[0], Floats[1] ) ); } else Texcoords0.Add( new Vector2( 0, 0 ) ); } else if( Line.StartsWith( "vt1 " ) ) { GetFloatsFromLine( Line, ref Floats, gameobject ); if( Floats.Count == 2 ) { Texcoords1.Add( new Vector2( Floats[0], Floats[1] ) ); } else Texcoords1.Add( new Vector2( 0, 0 ) ); } else i