Skip to content

Commit

Permalink
Fixed Mesh Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsakharov committed Apr 3, 2024
1 parent d3d05da commit 7e8d714
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
8 changes: 4 additions & 4 deletions Prowl.Editor/Assets/Importers/ModelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,24 +391,24 @@ private static void LoadMeshes(SerializedAsset ctx, FileInfo assetPath, Assimp.S
if (m.HasBones)
{
mesh.boneNames = new string[m.Bones.Count];
mesh.bindPoses = new Prowl.Runtime.Matrix4x4[m.Bones.Count];
mesh.BoneIndices = new Color32[vertexCount];
mesh.bindPoses = new System.Numerics.Matrix4x4[m.Bones.Count];
mesh.BoneIndices = new System.Numerics.Vector4[vertexCount];
mesh.BoneWeights = new System.Numerics.Vector4[vertexCount];
for (var i = 0; i < m.Bones.Count; i++)
{
var bone = m.Bones[i];
mesh.boneNames[i] = bone.Name;

var offsetMatrix = bone.OffsetMatrix;
Prowl.Runtime.Matrix4x4 bindPose = new Prowl.Runtime.Matrix4x4(
System.Numerics.Matrix4x4 bindPose = new System.Numerics.Matrix4x4(
offsetMatrix.A1, offsetMatrix.B1, offsetMatrix.C1, offsetMatrix.D1,
offsetMatrix.A2, offsetMatrix.B2, offsetMatrix.C2, offsetMatrix.D2,
offsetMatrix.A3, offsetMatrix.B3, offsetMatrix.C3, offsetMatrix.D3,
offsetMatrix.A4, offsetMatrix.B4, offsetMatrix.C4, offsetMatrix.D4
);

// Adjust translation by scale
bindPose.Translation *= scale;
bindPose.Translation *= (float)scale;

mesh.bindPoses[i] = bindPose;

Expand Down
29 changes: 21 additions & 8 deletions Prowl.Editor/EmbeddedResources/DefaultAssets/Standard.shader
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ Pass 0

#ifdef SKINNED
#ifdef HAS_BONEINDICES
layout (location = 6) in ivec4 vertexBoneIndices;
layout (location = 6) in vec4 vertexBoneIndices;
#else
ivec4 vertexBoneIndices = ivec4(0, 0, 0, 0);
vec4 vertexBoneIndices = vec4(0, 0, 0, 0);
#endif

#ifdef HAS_BONEWEIGHTS
Expand Down Expand Up @@ -107,7 +107,7 @@ Pass 0

for (int i = 0; i < MAX_BONE_INFLUENCE; i++)
{
int index = vertexBoneIndices[i] - 1;
int index = int(vertexBoneIndices[i]) - 1;
if (index < 0)
continue;

Expand Down Expand Up @@ -248,10 +248,23 @@ ShadowPass 0
Vertex
{
layout (location = 0) in vec3 vertexPosition;
layout (location = 1) in vec2 vertexTexCoord;
#ifdef HAS_UV
layout (location = 1) in vec2 vertexTexCoord0;
#else
vec2 vertexTexCoord0 = vec2(0.0, 0.0);
#endif
#ifdef SKINNED
layout (location = 5) in ivec4 vertexBoneIndices;
layout (location = 6) in vec4 vertexBoneWeights;
#ifdef HAS_BONEINDICES
layout (location = 6) in vec4 vertexBoneIndices;
#else
vec4 vertexBoneIndices = vec4(0, 0, 0, 0);
#endif

#ifdef HAS_BONEWEIGHTS
layout (location = 7) in vec4 vertexBoneWeights;
#else
vec4 vertexBoneWeights = vec4(0.0, 0.0, 0.0, 0.0);
#endif

const int MAX_BONE_INFLUENCE = 4;
const int MAX_BONES = 100;
Expand All @@ -271,7 +284,7 @@ ShadowPass 0

for (int i = 0; i < MAX_BONE_INFLUENCE; i++)
{
int index = vertexBoneIndices[i] - 1;
int index = int(vertexBoneIndices[i]) - 1;
if (index < 0)
continue;

Expand All @@ -284,7 +297,7 @@ ShadowPass 0
boneVertexPosition = totalPosition.xyz;
#endif

TexCoords = vertexTexCoord;
TexCoords = vertexTexCoord0;

gl_Position = mvp * vec4(boneVertexPosition, 1.0);
}
Expand Down
7 changes: 1 addition & 6 deletions Prowl.Runtime/Components/SkinnedMeshRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,17 @@ public class SkinnedMeshRenderer : MonoBehaviour, ISerializable
void GetBoneMatrices()
{
boneTransforms = new System.Numerics.Matrix4x4[Mesh.Res.boneNames.Length];
bindPoses = new System.Numerics.Matrix4x4[Mesh.Res.bindPoses.Length];
bindPoses = Mesh.Res.bindPoses;
for (int i = 0; i < Mesh.Res.boneNames.Length; i++)
{
var t = Root.transform.DeepFind(Mesh.Res.boneNames[i]);
if (t == null)
{
boneTransforms[i] = System.Numerics.Matrix4x4.Identity;
bindPoses[i] = Mesh.Res.bindPoses[i].ToFloat();
}
else
{
//var pose = Mesh.Res.bindPoses[i];
//boneTransforms[i] = (pose * t.localToWorldMatrix).ToFloat();
boneTransforms[i] = (t.localToWorldMatrix * this.GameObject.transform.worldToLocalMatrix).ToFloat();
bindPoses[i] = Mesh.Res.bindPoses[i].ToFloat();
//bindPoses[i].Translation *= 0.01f;
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions Prowl.Runtime/Resources/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public System.Numerics.Vector4[] BoneWeights {
public bool HasBoneWeights => (boneWeights?.Length ?? 0) > 0;

public string[]? boneNames;
public Matrix4x4[]? bindPoses;
public System.Numerics.Matrix4x4[]? bindPoses;

bool changed = true;
System.Numerics.Vector3[]? vertices;
Expand Down Expand Up @@ -481,7 +481,7 @@ internal static VertexFormat GetVertexLayout(Mesh mesh)
elements.Add(new Element(VertexSemantic.Tangent, VertexType.Float, 3, 0, true));

if (mesh.HasBoneIndices)
elements.Add(new Element(VertexSemantic.BoneIndex, VertexType.UnsignedByte, 4));
elements.Add(new Element(VertexSemantic.BoneIndex, VertexType.Float, 4));

if (mesh.HasBoneWeights)
elements.Add(new Element(VertexSemantic.BoneWeight, VertexType.Float, 4));
Expand All @@ -506,7 +506,8 @@ void Copy(byte[] source, ref int index)
index += source.Length;
}

for (int i = 0, index = 0; i < vertices.Length; i++)
int index = 0;
for (int i = 0; i < vertices.Length; i++)
{
if (index % layout.Size != 0)
throw new InvalidOperationException("[Mesh] Exceeded expected byte count while generating vertex data blob");
Expand Down Expand Up @@ -840,10 +841,10 @@ public void Deserialize(SerializedProperty value, Serializer.SerializationContex
var bindPosesCount = reader.ReadInt32();
if (bindPosesCount > 0)
{
bindPoses = new Matrix4x4[bindPosesCount];
bindPoses = new System.Numerics.Matrix4x4[bindPosesCount];
for (int i = 0; i < bindPosesCount; i++)
{
bindPoses[i] = new Matrix4x4() {
bindPoses[i] = new System.Numerics.Matrix4x4() {
M11 = reader.ReadSingle(),
M12 = reader.ReadSingle(),
M13 = reader.ReadSingle(),
Expand Down

0 comments on commit 7e8d714

Please sign in to comment.