1
\$\begingroup\$

My question is fairly straightforward, I think. Using Monogame, when I load a model using contentManager.Load<Model>("Model"), the model's mesh has a BasicEffect attached. That means that if I load multiple models, I'll have multiple BasicEffect instances created, which seems wasteful. Based on other questions I've seen here (and based on instinct), I shouldn't need multiple effects for rendering simple models, but those effects are created by the content pipeline when the model is imported.

How do I resolve this problem? Is there a way to tell Monogame to not create effects on load, or do I have to override the effects created by the models manually? Is there something I'm missing in my logic?

\$\endgroup\$
  • \$\begingroup\$ I got around this in my XNA days by making a custom model class, its not much work. You could almost take the mono model class and remove all the effect stuff. That will get you 90% of the way and then you can setup your render pipeline to work how you like. That way you also decuple your mesh data from your shaders. \$\endgroup\$ – Justin William Stanley Bryant Apr 23 '17 at 20:43
  • \$\begingroup\$ I wonder if your answer lies in one of Shawn Hargreaves blogs about XNA. All these posts cover XNA: shawnhargreaves.com/blogindex.html Look at the "Shader" category for interesting posts about using or not using the BasicEffect. \$\endgroup\$ – Steve H Apr 27 '17 at 11:59
1
\$\begingroup\$

Because BasicEffects are classes (i.e. reference types) and they can be modified, so if all the Models shared the same BasicEffect, modifying one would modify all of them, which would be highly undesirable.

And if you're wondering why the loader provides the BasicEffect in the first place, it's so the user doesn't have to set up an Effect every time they want to load a Model if they don't want to do (i.e. it's a matter of convinience). Also it's good practise to make sure an object is fully initialised when creating/loading an object and then passing it to other code - passing around uninitialised objects is generally a bad idea.

\$\endgroup\$
  • \$\begingroup\$ But I shouldn't need an effect to load a model, right? I only need the effect for drawing. I'd like to load multiple models and draw them using the same BasicEffect. As is, loading models creates duplicate BasicEffect instances that I don't need. \$\endgroup\$ – Grimelios Apr 23 '17 at 21:23
  • \$\begingroup\$ @Grimelios It loads with an effect because a Model that can't be drawn is pretty useless, and returning without being properly initialised (i.e. with no Model) would be a source of bugs. As @Justin says, the only way to avoid it is to write your own Model class (and thus your own Model loader). \$\endgroup\$ – Pharap Apr 23 '17 at 21:39
  • \$\begingroup\$ A Model could be loaded perfectly well without its own dedicated effects. As described in my question, my goal is to separate out models from their drawing for efficiency reasons. I can't be the only person who has run into this issue. \$\endgroup\$ – Grimelios Apr 25 '17 at 17:12
  • \$\begingroup\$ @Grimelios It could be, but it couldn't be drawn immediately, the user would have to remember to load an effect for it. Like I said, the only way is to create your own Model class and your own Model loader. In most cases it isn't an issue because loading tends to be done all at once behind a loading screen. Some games load models on the fly, but they usually release other resources around the same time and that's always a juggling act anyway. The loading in that case tends to happen on another thread as well. Ultimately though it's best not to worry until it's a measurable issue. \$\endgroup\$ – Pharap Apr 25 '17 at 17:28
  • \$\begingroup\$ Right. I understand all that, but it seems so wasteful to me to store and apply a separate BasicEffect for every model, or even every mesh. As for the issue of not being drawable immediately, the user still needs to set up various matrices for drawing anyway. I was just surprised to see so little information on the topic. \$\endgroup\$ – Grimelios Apr 25 '17 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.