Skip to content

Commit f5128c9

Browse files
committed
[Toolkit.Graphics] Add support for Model.Draw with an effect that doesn't inherit from IEffectMatrices
1 parent 1fb8ee9 commit f5128c9

File tree

1 file changed

+53
-6
lines changed
  • Source/Toolkit/SharpDX.Toolkit.Graphics

1 file changed

+53
-6
lines changed

Source/Toolkit/SharpDX.Toolkit.Graphics/Model.cs

+53-6
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ public unsafe void Draw(GraphicsDevice context, Matrix world, Matrix view, Matri
166166
Matrix* localSharedDrawBoneMatrices = stackalloc Matrix[boneCount];
167167

168168
CopyAbsoluteBoneTransformsTo(new IntPtr(localSharedDrawBoneMatrices));
169+
170+
var viewProjection = Matrix.Identity;
171+
bool viewProjectionUsed = false;
172+
var worldViewProjection = Matrix.Identity;
173+
bool worldViewProjectionUsed = false;
174+
169175
for (int i = 0; i < count; i++)
170176
{
171177
var mesh = Meshes[i];
@@ -179,16 +185,57 @@ public unsafe void Draw(GraphicsDevice context, Matrix world, Matrix view, Matri
179185
throw new InvalidOperationException("Mesh has no effect");
180186
}
181187

188+
Matrix result;
189+
Matrix.Multiply(ref localSharedDrawBoneMatrices[index], ref world, out result);
190+
182191
var matrices = effect as IEffectMatrices;
183192
if (matrices == null)
184193
{
185-
throw new InvalidOperationException("Effect has no IEffectMatrices");
194+
var worldParameter = effect.Parameters["World"];
195+
if (worldParameter != null)
196+
worldParameter.SetValue(ref result);
197+
198+
var viewdParameter = effect.Parameters["View"];
199+
if (viewdParameter != null)
200+
viewdParameter.SetValue(ref view);
201+
202+
var projectiondParameter = effect.Parameters["Projection"];
203+
if (projectiondParameter != null)
204+
projectiondParameter.SetValue(ref projection);
205+
206+
var viewProjectiondParameter = effect.Parameters["ViewProjection"];
207+
208+
if (viewProjectiondParameter != null)
209+
{
210+
if (!viewProjectionUsed)
211+
{
212+
Matrix.Multiply(ref view, ref projection, out viewProjection);
213+
viewProjectionUsed = true;
214+
}
215+
viewProjectiondParameter.SetValue(ref viewProjection);
216+
}
217+
218+
var worldViewProjectiondParameter = effect.Parameters["WorldViewProj"];
219+
220+
if (worldViewProjectiondParameter != null)
221+
{
222+
if (!viewProjectionUsed)
223+
{
224+
Matrix.Multiply(ref view, ref projection, out viewProjection);
225+
viewProjectionUsed = true;
226+
}
227+
228+
Matrix.Multiply(ref result, ref viewProjection, out worldViewProjection);
229+
230+
worldViewProjectiondParameter.SetValue(ref worldViewProjection);
231+
}
232+
}
233+
else
234+
{
235+
matrices.World = result;
236+
matrices.View = view;
237+
matrices.Projection = projection;
186238
}
187-
Matrix result;
188-
Matrix.Multiply(ref localSharedDrawBoneMatrices[index], ref world, out result);
189-
matrices.World = result;
190-
matrices.View = view;
191-
matrices.Projection = projection;
192239
}
193240

194241
mesh.Draw(context);

0 commit comments

Comments
 (0)