Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skinned meshes #30

Merged
merged 4 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions FerramAerospaceResearch/FARPartGeometry/GeometryMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class GeometryMesh
public Part part;
private GeometryPartModule module;
public bool valid;
public bool isSkinned;

public int invertXYZ;

Expand All @@ -74,10 +75,13 @@ public GeometryMesh(MeshData meshData, Transform meshTransform, Matrix4x4 worldT
{
this.meshLocalVerts = meshData.vertices;
this.triangles = meshData.triangles;
this.isSkinned = meshData.isSkinned;
Bounds meshBounds = meshData.bounds;

vertices = new Vector3[meshLocalVerts.Length];
this.thisToVesselMatrix = worldToVesselMatrix * meshTransform.localToWorldMatrix;
this.meshTransform = meshTransform;
UpdateLocalToWorldMatrix();
this.thisToVesselMatrix = worldToVesselMatrix * meshLocalToWorld;

for (int i = 0; i < vertices.Length; i++)
{
Expand All @@ -94,13 +98,12 @@ public GeometryMesh(MeshData meshData, Transform meshTransform, Matrix4x4 worldT
vertices[i] = vert;
}

this.meshTransform = meshTransform;
this.gameObjectActiveInHierarchy = meshTransform.gameObject.activeInHierarchy;

bounds = TransformBounds(meshBounds, thisToVesselMatrix);

float tmpTestBounds = bounds.center.x + bounds.center.y + bounds.center.z +
bounds.extents.x + bounds.extents.y + bounds.extents.z;
float tmpTestBounds = bounds.center.x + bounds.center.y + bounds.center.z
+ bounds.extents.x + bounds.extents.y + bounds.extents.z;
if (float.IsNaN(tmpTestBounds) || float.IsInfinity(tmpTestBounds))
{
ThreadSafeDebugLogger.Instance.RegisterMessage("Bounds error in " + module.part.partInfo.title);
Expand All @@ -123,10 +126,22 @@ public bool TrySetThisToVesselMatrixForTransform()
if (meshTransform == null)
return false;
lock(this)
meshLocalToWorld = meshTransform.localToWorldMatrix;
UpdateLocalToWorldMatrix();
return true;
}

private void UpdateLocalToWorldMatrix()
{
if (!isSkinned)
{
meshLocalToWorld = meshTransform.localToWorldMatrix;
}
else
{
meshLocalToWorld = Matrix4x4.TRS(meshTransform.position, meshTransform.rotation, Vector3.one);
}
}

public void TransformBasis(Matrix4x4 newThisToVesselMatrix)
{
try
Expand All @@ -145,7 +160,7 @@ public void TransformBasis(Matrix4x4 newThisToVesselMatrix)

for (int i = 0; i < vertices.Length; i++)
{
Vector3 vert = tempMatrix.MultiplyPoint3x4(meshLocalVerts[i]);// = Vector3.zero;
Vector3 vert = tempMatrix.MultiplyPoint3x4(meshLocalVerts[i]); // = Vector3.zero;

float tmpTestVert = vert.x + vert.y + vert.z;
if (float.IsNaN(tmpTestVert) || float.IsInfinity(tmpTestVert))
Expand Down Expand Up @@ -175,17 +190,17 @@ public void TransformBasis(Matrix4x4 newThisToVesselMatrix)

public void MultithreadTransformBasis(object newThisToVesselMatrixObj)
{
lock (this)
lock(this)
{
this.TransformBasis((Matrix4x4)newThisToVesselMatrixObj);
this.TransformBasis((Matrix4x4) newThisToVesselMatrixObj);
}
}

private Bounds TransformBounds(Bounds oldBounds, Matrix4x4 matrix)
{
Vector3 center, extents;
center = oldBounds.center;//matrix.MultiplyPoint3x4(m.bounds.center);
extents = oldBounds.extents;//matrix.MultiplyVector(m.bounds.size);
center = oldBounds.center; //matrix.MultiplyPoint3x4(m.bounds.center);
extents = oldBounds.extents; //matrix.MultiplyVector(m.bounds.size);

Vector3 lower = Vector3.one * float.PositiveInfinity;
Vector3 upper = Vector3.one * float.NegativeInfinity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ private MeshData GetVisibleMeshData(Transform t, bool onlyMeshes)
{
m = new Mesh();
smr.BakeMesh(m);
MeshData md = new MeshData(m.vertices, m.triangles, m.bounds);
MeshData md = new MeshData(m.vertices, m.triangles, m.bounds, true);

UnityEngine.Object.Destroy(m); //ensure that no memory is left over
return md;
Expand Down
4 changes: 3 additions & 1 deletion FerramAerospaceResearch/FARPartGeometry/MeshData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ public class MeshData
public Vector3[] vertices;
public int[] triangles;
public Bounds bounds;
public bool isSkinned;

MeshData() { }

public MeshData(Vector3[] vertices, int[] tris, Bounds bounds)
public MeshData(Vector3[] vertices, int[] tris, Bounds bounds, bool isSkinned = false)
{
this.vertices = vertices;
this.triangles = tris;
this.bounds = bounds;
this.isSkinned = isSkinned;
}
}
}
Binary file not shown.
Binary file modified GameData/FerramAerospaceResearch/Plugins/ferramGraph.dll
Binary file not shown.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ CHANGELOG
=======================================================
-------------------master branch-------------------

Fix skinned meshes having scale applied twice in voxelization ([#30](https://github.com/dkavolis/Ferram-Aerospace-Research/pull/30))
Fix new stock parts ([#24](https://github.com/dkavolis/Ferram-Aerospace-Research/pull/24), thanks [@HSJasperism](https://github.com/HSJasperism))
Greatly improved debug visual voxels framerate ([#18](https://github.com/dkavolis/Ferram-Aerospace-Research/pull/18), [#29](https://github.com/dkavolis/Ferram-Aerospace-Research/pull/29))
Enlarged debug voxel texture for higher fidelity ([#18](https://github.com/dkavolis/Ferram-Aerospace-Research/pull/18))
Expand Down