Skip to content

Instantly share code, notes, and snippets.

@warrickct
Last active January 25, 2018 01:35
Show Gist options
  • Save warrickct/b2755f43607f695a213c6c5e8923f8d4 to your computer and use it in GitHub Desktop.
Save warrickct/b2755f43607f695a213c6c5e8923f8d4 to your computer and use it in GitHub Desktop.
Using a gameobject, creates a network transmittable ModelWireData object containing vertices, uvs, triangles and transform data.
using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class ModelWireData
{
float xPos, yPos, zPos, xRot, yRot, zRot, xSca, ySca, zSca;
List<Vertex> verticesWiredata = new List<Vertex>();
List<UvCoord> uvsWireData = new List<UvCoord>();
List<int> trianglesWireData;
//extracts and converts model data to wiredata.
public ModelWireData(GameObject model)
{
//position
this.xPos = model.transform.position.x;
this.yPos = model.transform.position.y;
this.zPos = model.transform.position.z;
//rotation
this.xRot = model.transform.rotation.x;
this.yRot = model.transform.rotation.y;
this.zRot = model.transform.rotation.z;
//scale
this.ySca = model.transform.lossyScale.y;
this.zSca = model.transform.lossyScale.z;
this.xSca = model.transform.lossyScale.x;
Mesh modelSharedMesh = model.GetComponent<MeshFilter>().sharedMesh;
//mesh: loops the v3 vert array, Converts to triple float class Vertex, Adds to list of triple float objects.
foreach (Vector3 modelVertex in modelSharedMesh.vertices)
{
Vertex modelVertexWiredata = new Vertex(modelVertex);
verticesWiredata.Add(modelVertexWiredata);
}
//uv's: iterates uvs, same as mesh but double float data.
foreach(Vector2 modelUv in modelSharedMesh.uv)
{
UvCoord modelUvWireData = new UvCoord(modelUv);
uvsWireData.Add(modelUvWireData);
}
//triangles: single int array, no custom class needed.
foreach(int triangle in modelSharedMesh.triangles)
{
trianglesWireData.Add(triangle);
}
}
}
//serializable sub class for list construction
[Serializable]
public class Vertex
{
float xPos, yPos, zPos;
public Vertex(Vector3 vertex)
{
this.xPos = vertex.x;
this.yPos = vertex.y;
this.zPos = vertex.z;
}
}
//serializable sub class for list construction
[Serializable]
public class UvCoord
{
float uPos, vPos;
public UvCoord (Vector2 uv)
{
this.uPos = uv.x;
this.vPos = uv.y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment