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 /
  • Help Room /
avatar image
0
Question by wmaass88 · Feb 27, 2018 at 01:19 AM · matrix4x4.trs

Populating SpeeTree instances with a SWA file.

Hello,

I am trying to parse a SWA file and place a mass amount of SpeedTree trees. See SWA format below:

https://docs.speedtree.com/doku.php?id=swa_files

I can parse the file just fine but I am stuck on how to deal with the up/right vectors and scale values to properly position, rotate and scale the tree. My efforts so far produce lots of anomalies. The trees appear to move or shrink/grow as I move the camera, some trees are not rotated correctly, and more ugliness. I took a shot at using Matrix4x4 and SetTRS but this stuff is above my pay grade. My rough parser/populate function is below, any advice is greatly appreciated.

 public void getFoliage(){
         
         string line = "";
         int sectionCount = 0;
         string species = "none";
         string subSpecies = "none";
         string file = "none";
 
         string path = Application.dataPath + "/test.swa";
         StreamReader sr = new StreamReader (path);
 
         if (sr != null) {
 
             while (!sr.EndOfStream) {
 
 
                 line = sr.ReadLine ();
 
                 //see if we have encountered a new section of foliage in the swa file
                 if (line.Contains ("/")) {
 
                     //get the species info
                     string[] speciesInfo = line.Split("/"[0]);
                     species = speciesInfo [0];
                     subSpecies = speciesInfo [1];
                     file = speciesInfo [2].Remove(speciesInfo [2].Length - 4);
 
                     //get the count of this section
                     sectionCount = int.Parse (sr.ReadLine());
 
                 
 
                     for (int i = 0; i <= sectionCount -1; i++)
                     {
                         //read and splt the line in
                         string[] temp = sr.ReadLine ().Split (" " [0]);
 
                         //create the position, up vector, right vector and scale
                         //note ST uses Z as up so we swap the values when assinging them
                         Vector3 position = new Vector3(float.Parse(temp[0]),float.Parse(temp[2]),float.Parse(temp[1]));
                         Vector3 upVector = new Vector3 (float.Parse (temp [3]), float.Parse (temp [5]), float.Parse (temp [4]));
                         Vector3 rightVector = new Vector3 (float.Parse (temp [6]), float.Parse (temp [8]), float.Parse (temp [7]));
                         float scale = float.Parse (temp [9]);
 
                         //create a vector3 for scaling in Unity
                         Vector3 scalerVal = new Vector3 (scale, scale, scale);
 
                         //create the rotation vector?    
                         Vector3 v3 = Vector3.Cross (upVector, rightVector).normalized * Mathf.Rad2Deg;
                         Quaternion rotation = Quaternion.Euler(v3.x, v3.y, v3.z);
 
 
 
                         //instantiate the object
                         GameObject tempSt = (Instantiate(Resources.Load(file), position,  Quaternion.identity) as GameObject);
 
                         //create an array of mesh filters to hold the tree and its LOD meshes                            
                         Component[] mf;
                         mf = tempSt.transform.GetComponentsInChildren<MeshFilter>();
                                                 
                         int filter = 0;
                         foreach (MeshFilter m in mf) {
                                                             
                             Vector3[] origVerts;
                             Vector3[] newVerts;
                             
                             origVerts = m.mesh.vertices;
                             newVerts = new Vector3[origVerts.Length];
                             
                             Matrix4x4 mt = Matrix4x4.identity;
                             mt.SetTRS(position, rotation, scalerVal);
                             
                                                         
                             int t = 0;
                             while (t < origVerts.Length) {
                                                                 
                                 newVerts[t] = mt.MultiplyPoint3x4(origVerts[t]);
                                 t++;
                             }
                             
                             m.mesh.vertices = newVerts;
                             
                         
                         }
 
 
                         }
 
                 }
     
             }
 
 
             sr.Close();
         }
 
     }
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

126 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

Related Questions

Rotation and translation between objects with TRS matrix 0 Answers

Changing a Transform matrix from right handed to left handed 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