Sign in to follow this  

Unity Scene Sorting

Recommended Posts

In my current system, I have a collection of Entity objects (planets, ships, missiles, etc.). Each Entity contains one or more Geode objects. A Geode is a batch of geometry with a single texture and material. Whenever an Entity moves, I update its world transformation based on its local transformation and the transformation of its "parent" Entity (if any). Each rendered Geode uses its Entity's world transform. For rendering, I maintain a list of all visible geodes, sorted by transparency/depth, then texture, then material. Each frame, I iterate through the list of geodes and set the geode's transform and state and draw the geometry to the screen. This works great, except my Python code runs extremely slow with only about 1000 entities on the screen. The bottleneck seems to be my Matrix code. Beyond trying to use C++ to speed up that particular code, this has made me think about a fundamental design question: Is it better to sort my objects by state and perform extra matrix calculations so each geode has an absolute world transform, or should I render from a transform heirarchy (i.e. scene graph) and sort the nodes as best as I can, with the matrix heirarchy taking precedence over reducing texture/material state changes? - Mike

Share this post

Link to post
Share on other sites
Generally it is better to only depth sort transparent objects. Other objects should be sorted to reduce state change in the system. The kinds of state changes you should be avoiding are these (not in any particular order, I don't know where the bottle necks are on PC, prob best to experiment so see where the bottleneck is on your system). You could also experiement with hardware instancing (though would only work on more advanced GPUs).

- SetStreamSource (I beleive this is a big hit on PC) - Render all the instances of the same geometry at once (or combine static terrain into the same vertex buffer).
- FX technique/pass begin/end - Sort by material type so only called once for each shader type.
- Set constant and render state - Sort by material so only gets called once per material.

It seems pretty unusual to have matrix multiply be the bottleneck, though you are going to taking a big hit by using an interpreted language. Generally it is a good idea to have "sync" operation that goes through your scene and sync's all the heirachies before rendering (so when you get to the rendering each object has its own correctly transformed world matrix), this would avoid having to do unnessacary matrix multiplies.

Share this post

Link to post
Share on other sites
Original post by griffin2000
Generally it is a good idea to have "sync" operation that goes through your scene and sync's all the heirachies before rendering (so when you get to the rendering each object has its own correctly transformed world matrix), this would avoid having to do unnessacary matrix multiplies.

That's what I'm doing. Each frame (prior to the rendering loop), I iterate through all Entity objects and call an Update() method. If an entities' transformation changes, its world transform and the world transform of each "child" entity is automatically updated.

The problem is that in my current setup, I have 256 star systems, each with five planets. Each planet rotates around its axis AND orbits a sun, so for each planet I'm constantly calculating two new rotation matrices and a translation matrix and multiplying them all together. I can probably improve this by multiplying the current "local" transform by a static quaternion rotation matrix, rather than re-allocating three or four Matrix objects per planet per frame. I will give that a try when I get home from work, but I'm not sure how much that will help.

Original post by griffin2000
BTW I thought this was the directX forum :-) Those were the directX commands I listed, there will be equivalents in OGL.

No problem. I spent six or seven years learning unmanaged C++ and DirectX before switching to Python and OpenGL, so I know about the functions you're talking about.

Share this post

Link to post
Share on other sites
Yeah sounds like each object should have have a position vector and quaterion vector, and an LTM. During sync you just fill in the LTM from the quaternion and the position (quat->mtx equation is here, then multiply it by its parents LTM.

The other thing would be to have an LOD system so you ignore planets for suns that are far from the camera (don't update them or render them).

Still I think moving C++ for your numerically intensive operations will be a huge speed up.

Share this post

Link to post
Share on other sites
I still have lingering doubts about my system, so here are a few more questions:

1) If it is more important to sort by texture/shader/material, why does OpenGL and D3D even provide a matrix stack? The only purpose of this stack seems to be iterating through a tree of parent-child entity transforms and rendering each entity's geometry batches with little or no sorting by texture/shader/material.

I could iterate through an entity tree, call the OpenGL transform functions, and use glGetFloatv() to retrieve the generated matrix for each entity. I could then use this matrix to set the current transform when I iterate through a sorted list of entities. However, a previous thread recommended against using the stack in this way. The main point was that custom matrix functions would often be faster than the OpenGL functions. I find this a little hard to believe, since I have not been able to match their performance with a Python matrix class, a C++ matrix class compiled as a Python extension, or a Pyrex matrix class.

2) The OpenGL Redbook recommends using the built-in functions rather using custom matrix logic:

Page 464:
Use specific matrix calls such as glRotate*(), glTranslate*(), and glScale*() rather than
composing your own rotation, translation, or scale matrices and calling glMultMatrix().

I want to maintain an entity transform heirarchy (i.e. scene graph), so that I can attach wheels to a car, a torch to a hand, etc. In order to accomplish this and still use the OpenGL functions, I would need to have each function traverse the tree backwards to its "topmost" parent, and then traverse back through the tree and call each entity's "UpdateTransform()" method. Would there be a benefit to this approach?

- Mike

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
    • Total Posts
  • Similar Content

    • By CpawsMusic
      Hey there! My name is Cpaws and I compose music, sound effects and overall sound design for video games and film. I'm looking to work on a horror game mainly for fun and as an addition to my portfolio. I've used Unity & Wwise before for audio implementation.
      If you're interested in working together, don't hesitate to contact me at
      Here's a few demo reels of my past projects: 
      Here's some snippets of some quick game music I've composed:
      Here's my portfolio/website:
    • By muhamad rabee
      My first mobile game made by unity



      I appreciate every suggestion
    • By Simplepg
    • By Simplepg
    • By ForgedInteractive

      Who We Are
      We are Forged Interactive, a small team of like-minded game developers with the sole purpose of making games we love! We're a team of artists, animators, programmers, level designers, writers, composers, producers, and other creative minds. We want to make games that you, the modern gamer want to play! We hope to build a community that enjoys our games as much as we love creating them. With your feedback and support we will be able to achieve that.

      About the Game
      GAME NAME is a fun, action-packed army builder with unique characters, challenges and engaging levels. Set forth on an adventure to protect friends, family and countrymen from new adversaries. Once defeated your enemies turn coat and join you in your adventures. Players can enjoy a range of troops and abilities based on their gameplay style which become more important as maps introduce more challenging terrain, enemies and bosses. Strong orc knights, dangerous shamans, and even a dragon are out on the prowl. Knowing when to fight and when to run, and how to manage your army is essential. Your actions alone decide the fate of this world.

      Previous Work by Team
      Although we are working towards our first game as a team, our team members themselves have past experience in the industry.
      This includes members who have worked on titles including:
      Final Fantasy Kingsglaive, FIFA, Xcom 2 and Civilization.

      Who are we looking for? 3D Modellers Concept Artists Marketing Specialists Level Designer

      What do we expect? Reference work or portfolio. Examples what have you already done and what projects you have worked on academic or otherwise. The ability to commit to the project on a regular basis. If you are going on a two-week trip, we don't mind, but it would be good if you could commit 10+ hours to the project each week. Willingness to work with a royalty based compensation model, you will be paid when the game launches. Openness to learning new tools and techniques
      What can we offer? Continuous support and availability from our side. You have the ability to give design input, and creative say in the development of the game. Shown in credits on websites, in-game and more. Insight and contacts from within the Industry.
      If you are interested in knowing more or joining. Please email or PM us on Skype. Myself or Colin will reply to you within 48 hours.

      Skype: ForgedInteractive

      David and Colin

      Follow us on:

      Twitter: @ForgedInteract
  • Popular Now