Profile image

Help with floating origin

6,986 Puredeath  9.2 years ago

since floating origin has been added to simple planes the Color trail emitter has been outdated, how do I correctly get the position when the origin updates?

  • Log in to leave a comment
  • Profile image
    6,986 Puredeath

    @nathanmikeska thanks!

    9.2 years ago
  • Profile image
    private static void SaveAircraftMesh(string fileName)
    {
       var aircraftRoot = GameObject.Find("/AircraftContainer/Aircraft");
       if(aircraftRoot == null)
       {
          Debug.LogError("Could not find the aircraft root object");
          return;
       } 
    
       var meshFilters = new List < MeshFilter &rt; ();
       var meshRenderers = aircraftRoot.GetComponentsInChildren < MeshRenderer &rt; ();
       for (int i = 0; i < meshRenderers.Length; i++)
       {
          var meshFilter = meshRenderers[i].GetComponent < MeshFilter &rt; ();
          if (meshFilter != null)
          {
             meshFilters.Add(meshFilter);
          }
       }
    
       var mesh = CombineMeshes(meshFilters);
       MeshToFile(mesh, fileName);
    }
    
    private static Mesh CombineMeshes(List < MeshFilter &rt; meshFilters)
    {
       var meshCombines = new CombineInstance[meshFilters.Count];
       for(int i = 0; i < meshFilters.Count; i++)
       {
          meshCombines[i].mesh = meshFilters[i].mesh;
          meshCombines[i].transform = meshFilters[i].transform.localToWorldMatrix;
       }
    
       var mesh = new Mesh();
       mesh.CombineMeshes(meshCombines, false, false);
    
       return mesh;
    }
    
    private static string MeshToString(Mesh mesh)
    {
       StringBuilder sb = new StringBuilder();
    
       sb.Append("g ").Append("SimplePlanes_Exported_Airplane").Append("\n");
    
       foreach (Vector3 v in mesh.vertices)
       {
          sb.Append(string.Format("v {0} {1} {2}\n", v.x, v.y, v.z));
       }
       sb.Append("\n");
    
       foreach (Vector3 v in mesh.normals)
       {
          sb.Append(string.Format("vn {0} {1} {2}\n", v.x, v.y, v.z));
       }
       sb.Append("\n");
    
       foreach (Vector3 v in mesh.uv)
       {
          sb.Append(string.Format("vt {0} {1}\n", v.x, v.y));
       }
    
       return sb.ToString();
    }
    
    private static void MeshToFile(Mesh mesh, string filename)
    {
       using (StreamWriter sw = new StreamWriter(filename))
       {
          sw.Write(MeshToString(mesh));
       }
    }
    
    9.2 years ago
  • Profile image
    6,986 Puredeath

    @nathanmikeska I have a repair tool for meshes I could try, if you can send me the script

    9.2 years ago
  • Profile image

    @Puredeath I looked in to it briefly, trying to use your script, but did not have much luck. I could save a file, but it didn't appear to load properly in Blender or Unity. Unfortunately, I don't have lot of spare time to really dig in to it at the moment, sorry.

    9.2 years ago
  • Profile image
    6,986 Puredeath

    @nathanmikeska hey would you be able to help me a bit more with this saving meshes thing? I dont really understand much of this at all...

    9.2 years ago
  • Profile image
    6,986 Puredeath

    @nathanmikeska alright thanks, Ill take a look at that

    9.2 years ago
  • Profile image

    @Puredeath The script i posted should allow you to get the collection of mesh filters that make up the plane. you might need to combine them first in order to run a mesh filter through you export code you found. Might googling or taking a look here on combining meshes.

    9.2 years ago
  • Profile image
    6,986 Puredeath

    @nathanmikeska
    This is the script I found to save .obj files, im trying to get it to work with the game
    ' using UnityEngine;
    using System.Collections;
    using System.IO;
    using System.Text;

    public class ObjExporter {

    public static string MeshToString(MeshFilter mf) {
        Mesh m = mf.mesh;
        Material[] mats = mf.renderer.sharedMaterials;
    
        StringBuilder sb = new StringBuilder();
    
        sb.Append("g ").Append(mf.name).Append("\n");
        foreach(Vector3 v in m.vertices) {
            sb.Append(string.Format("v {0} {1} {2}\n",v.x,v.y,v.z));
        }
        sb.Append("\n");
        foreach(Vector3 v in m.normals) {
            sb.Append(string.Format("vn {0} {1} {2}\n",v.x,v.y,v.z));
        }
        sb.Append("\n");
        foreach(Vector3 v in m.uv) {
            sb.Append(string.Format("vt {0} {1}\n",v.x,v.y));
        }
        for (int material=0; material <  m.subMeshCount; material ++) {
            sb.Append("\n");
            sb.Append("usemtl ").Append(mats[material].name).Append("\n");
            sb.Append("usemap ").Append(mats[material].name).Append("\n");
    
            int[] triangles = m.GetTriangles(material);
            for (int i=0;i < triangles.Length;i+=3) {
                sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n", 
                    triangles[i]+1, triangles[i+1]+1, triangles[i+2]+1));
            }
        }
        return sb.ToString();
    }
    
    public static void MeshToFile(MeshFilter mf, string filename) {
        using (StreamWriter sw = new StreamWriter(filename)) 
        {
            sw.Write(MeshToString(mf));
        }
    }
    

    } '

    9.2 years ago
  • Profile image

    @Puredeath Sorry, forgot to mention... the < and &rt; are supposed to be less than and greater than symbols. The markdown syntax on the forum isn't cooperating. I was talking to Andrew about it this morning to see if we can do anything about it.

    9.2 years ago
  • Profile image
    6,986 Puredeath

    @nathanmikeska so peronally I am not very good at coding, and I am confused on why why MeshRenderer and MeshFilter are not wanting to work, as well as what lt and rt are supposed to be

    9.2 years ago
  • Profile image
    6,986 Puredeath

    @nathanmikeska well there are plenty of scripts for saving stuff as .obj files online, I will see if I can get them to work with this. ASAP

    9.2 years ago
  • Profile image

    I'm not sure how to save it as an obj, but here is a little snippet that I think should give you the mesh objects that make up a visible aircraft.

    var aircraftRoot = GameObject.Find("/AircraftContainer/Aircraft");
    var meshRenderers = aircraftRoot.GetComponentsInChildren<MeshRenderer&rt;();
    for(int i = 0; i < meshRenderers.Length; i++)
    {
       var meshFilter = meshRenderers[i].GetComponent<MeshFilter&rt;();
       var mesh = meshFilter == null ? null : meshFilter.mesh;
       if(mesh != null)
       {
          // Do something?
       }
    }
    
    9.2 years ago
  • Profile image
    6,986 Puredeath

    @nathanmikeska hey I am trying to pull the mesh of the player's plane and its children and save as an .obj do you have any tips, I have found a script but it only works for when in unities editor.

    9.2 years ago
  • Profile image

    @Puredeath whenever the player gets too far away from the origin, the floating origin script repositions all game objects at the root of the object hierarchy (objects with no transform.parent). It also handles some special case stuff like aircraft rigid bodies, water, and particle systems. The IgnoreFloatingOrigin script can be attached to these top level game objects to prevent the floating origin script from automatically moving them when the world repositions. We use this for a few objects, such as our UI/Hud since it doesn't make since for them to be repositioned.

    9.2 years ago
  • Profile image
    6,986 Puredeath

    @nathanmikeska well that sucks, what is this ignore floating origin I am seeing in some scripts?

    9.2 years ago
  • Profile image

    @Puredeath I am actually not sure how to get Unity's built in trail renderer working with the floating origin. It was a real bummer that I broke that with the floating origin change not long after recording the tutorial video on it. I looked in to it a little bit a while back, but didn't spend much time on it. I seem to recall that Unity's implementation hid a lot of the details from you, and it was not obvious how it could be fixed, if at all. Feel free to investigate, but I suspect you might be better off writing your own custom trail renderer or using one that somebody else has already made.

    9.2 years ago
  • Profile image
    6,986 Puredeath

    @nathanmikeska thank you, could you explain how to transform the trailrenderer position based on origin offset? (I am a rather new to unity and csharp)

    9.2 years ago
  • Profile image

    The IGameWorld interface (accessible via the GameWorld property of your mod's ServiceProvider) has the current floating origin offset ('FloatingOriginOffset' property) as well as an event to subscribe to in order to be notified when the floating origin updates ('FloatingOriginChanged' event).

    9.2 years ago