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

[WIP][Rendering] Blendshapes (on behalf of Noa7/Noah7071) #2136

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
10 changes: 1 addition & 9 deletions sources/engine/Stride.Engine/Rendering/ModelRenderProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Stride.Graphics;
using Stride.Rendering.Materials;
using Stride.Rendering.Materials.ComputeColors;
//using Valve.VR;

namespace Stride.Rendering
{
Expand Down Expand Up @@ -128,14 +127,7 @@ private void UpdateRenderModel(ModelComponent modelComponent, RenderModel render
renderMesh.MATBSHAPE = mesh.MATBSHAPE;
renderMesh.BasisKeyWeight = mesh.BasisKeyWeight;
renderMesh.BlendShapesCount=mesh.Shapes.Count;
renderMesh.VerticesCount = mesh.Draw.VertexMapping.Length;
//renderMesh.BlendShapesCount = mesh.GetBlendShapesCount();
//int verticesCount = mesh.GetBlendShapesCount() * mesh.Draw.VertexMapping.Length;
//renderMesh.VerticesCount = verticesCount;
//renderMesh.BlendShapeWeights = mesh.BlendShapeWeights;
//renderMesh.BlendShapeVertices = mesh.BlendShapeVertices;
//renderMesh.MATBSHAPE = mesh.MATBSHAPE;

renderMesh.VerticesCount = mesh.Draw.VertexMapping.Length;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,30 @@ shader TransformationBlendShape : TransformationBase, PositionStream4, Transform
{

cbuffer PerDraw
{
//stage float2 morphWeights[MAX_MORPH_TARGETS];
//stage float3 morphTargetVertices[MAX_MORPH_TARGETS*MAX_VERTICES];
//stage float4x4 BlendMatrixArray[SkinningMaxBones];
{
stage float4x4 BSHAPEDATA[MAX_MORPH_TARGETS*MAX_VERTICES];
stage float BasisKeyWeight;
stage float BasisKeyWeight;
}


stage stream uint VertexID : SV_VertexID;

float4 ApplyBlendshapes(int vID, float4 pos)
{
float4x4 mat=BSHAPEDATA[vID];
float4 blendPos=pos*mat[1][3];

for(int i=0;i<MAX_MORPH_TARGETS;i++)
{
float4 morphedShape=BSHAPEDATA[i* MAX_VERTICES+ vID][0];
blendPos=+float4(morphedShape[0]+BasisKeyWeight,morphedShape[1], morphedShape[2] , pos[3])*morphedShape[3];
// blendPos=float4(BSHAPEDATA[0][[2],0,-1 , pos[3]);
}


//blendPos=float4(mat[vID][0], mat[vID][1],mat[vID][2],pos[3]);
float4x4 mat=BSHAPEDATA[vID];
float4 blendPos=pos*mat[1][3];
for(int i=0;i<MAX_MORPH_TARGETS;i++)
{
float4 morphedShape=BSHAPEDATA[i* MAX_VERTICES+ vID][0];
blendPos=+float4(morphedShape[0]+BasisKeyWeight,morphedShape[1], morphedShape[2] , pos[3])*morphedShape[3];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to write blendPos+=float4... instead of blendPos=+float4... ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, error on my part, fixed to standard a= a+b format in latest

}
return blendPos;


}
}


override stage void PreTransformPosition()
{


//if(streams.VertexID==0)
//{
base.PreTransformPosition();

streams.PositionWS=ApplyBlendshapes(streams.VertexID, streams.PositionWS);
//}

//uint tyh= streams.VertexID;
float xnew=streams.PositionWS.x;
float ynew=streams.PositionWS.y;

float znew=streams.PositionWS.z;
float4 blendPos = float4(xnew,ynew,znew,streams.PositionWS.w);
//float4 blendPos=ApplyBlendShape(streams.VertexID, streams.PositionWS);
streams.PositionWS = blendPos;
base.PreTransformPosition();
streams.PositionWS=ApplyBlendshapes(streams.VertexID, streams.PositionWS);
}


};
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,9 @@ namespace Stride.Rendering
{
public static partial class TransformationBlendShape
{
//public static readonly ValueParameterKey<Matrix> BlendWeightArrayoxx = ParameterKeys.NewValue<Matrix>();

public static readonly ValueParameterKey<Matrix> BSHAPEDATA = ParameterKeys.NewValue<Matrix>();

public static readonly ValueParameterKey<float> BasisKeyWeight=ParameterKeys.NewValue<float>();

// public static readonly ValueParameterKey<Vector2> morphWeights = ParameterKeys.NewValue<Vector2>();

// public static readonly ValueParameterKey<Vector3> morphTargetVertices = ParameterKeys.NewValue<Vector3>();

// public static readonly ValueParameterKey<float> morphTargetVertexIndices = ParameterKeys.NewValue<float>();

// public static readonly ValueParameterKey<int> BlendIndices = ParameterKeys.NewValue<int>();

public static readonly ValueParameterKey<float> BasisKeyWeight=ParameterKeys.NewValue<float>();

}
}
12 changes: 2 additions & 10 deletions sources/engine/Stride.Rendering/Rendering/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,13 @@ public void AddBlendShapes(Shape shape, float weight)
public float[] GetBlendWeights(out float cummulativeWeight)
{
float[] shapeWeights = new float[Shapes.Count];
cummulativeWeight = 0f;

cummulativeWeight = 0f;
for (int i = 0; i < Shapes.Count; i++)
{
var shapeKV = Shapes.ElementAt(i);
var shape = shapeKV.Key;
var shapeWeight = Math.Clamp(shapeKV.Value, 0f, 1f);
float adjustedWeight = shapeWeight;
// if (cummulativeWeight >= 1f) { adjustedWeight = 0f; }
// else if (cummulativeWeight + shapeWeight > 1) { adjustedWeight = 1 - cummulativeWeight; }
//else
//{
// adjustedWeight = shapeWeight;
//}
cummulativeWeight += adjustedWeight;
shapeWeights[i] = adjustedWeight;
}
Expand All @@ -216,15 +209,14 @@ public List<Vector3> AdjustPositionToDrawInstance()
{
if (!mappings.ContainsKey(tup_id_vec.Item2))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the new TryAdd of the dictionary to avoid double lookup in the dictionary

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

{
mappings.Add(tup_id_vec.Item2, tup_id_vec.Item3);
mappings.TryAdd(tup_id_vec.Item2, tup_id_vec.Item3);
}
}
var positions = Draw.VCPOLYIN.Select(c => c.Item3).ToArray();
List<int> updatedVertexMapping = Draw.VertexMapping.ToList();


Vector3[] NewVectices = new Vector3[updatedVertexMapping.Count];

for (var i = 0; i < updatedVertexMapping.Count; i++)
{
var v = posBlend[originalVerticesIDS[updatedVertexMapping[i]]];
Expand Down
1 change: 0 additions & 1 deletion sources/tools/Stride.Importer.FBX/MeshConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,6 @@ public ref class MeshConverter
auto buffer = buildMesh->buffer;
auto vertexBufferBinding = VertexBufferBinding(GraphicsSerializerExtensions::ToSerializableVersion(gcnew BufferData(BufferFlags::VertexBuffer, buffer)), gcnew VertexDeclaration(vertexElements->ToArray()), buildMesh->polygonCount * 3, 0, 0);

//auto drawData = gcnew MeshDraw();
auto vbb = gcnew List<VertexBufferBinding>();
vbb->Add(vertexBufferBinding);
drawData->VertexBuffers = vbb->ToArray();
Expand Down