Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by N1nja · Jun 29, 2010 at 04:57 AM · particlesdepthalgorithmsorting

Depth Sorting of Billboard Particles, how can I do it?

I have 3D Particles, I am trying to find a way to sort these particles by Depth, so that they're rendered in the correct order(from back to front);

But.. having 10,000 particles, tends to slow sorting down, naturally..

Anyone know of any fast Depth Sorting algorithms?

Or how about a way to use ZWrite, which appears to effectively sort them automatically, but has clipping issue, because of a particles texture transparency, and the order at which the particles were drawn.

Enabling ZWrite has sped things up ALOT, to bad for the clipping issues.. Using a sorting algorithm, I see no clipping issues, but then have no need for ZWrite, and so its still simply a problem of proccessing speed.. I've looked into GPU sorting, but I'm not sure if it can be done with the Indie version of Unity3D;

So.. I'm just stuck now, trying to figure this out.. and getting no where, anyone have any suggestions?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Daniel-Brauer · Aug 26, 2010 at 07:35 PM

Sorting: The best sorting algorithm for particles is often Bubble Sort. Yes, the worst naive approach to sorting actually has a practical application! Here's why:

  1. It monotonically approaches the correct order. Unlike Quicksort, Bubble Sort doesn't do any crazy shuffling. This means that you can limit its running time per frame and your array will still be in better order than when you started.
  2. It runs very well in the best case. For a sorted array, Bubble Sort has a O(n) running time. This is great, because usually your particles were already sorted in the previous frame. With pre-sorted arrays, Quicksort still runs in O(nlogn), or O(n^2) if it's a naive implementation. Up to a certain degree of "almost" sorted, Bubble Sort can still outperform Quicksort. Particles are usually mostly sorted, so you usually get best or almost best-case scenarios.
  3. It runs in-place. Quicksort can be written to do this too, but a lot of faster sorting algorithms need a bunch of extra memory to run.

I recommend setting up bubble sort with a limit of how many passes it can do per frame. Perhaps use Quicksort for the first frame so you have a good starting point. For 10k particles, let Bubble Sort do 5-10 passes per frame. You can trade robustness for performance really easily by tweaking the number of passes per frame. Even if your particles aren't perfectly sorted, they'll often look fine.

Z Buffering: For transparent particles, you don't want to use Z writing. Z writing is for when you want a rendered object to occlude things rendered later. Even if all your particles are on top of one another, you always want to draw all of them.

After all that: I should mention that your frame rate problems are probably not due to sorting or vertex processing, but fill rate. To test this theory, try generating an equivalent mesh just once and seeing how it performs with 10k, 20k, 30k particles. Just use a normal particle shader that doesn't do the billboarding itself, and uses the same blending coefficients. My guess is that your performance will drop immensely anyway.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image N1nja · Sep 04, 2010 at 06:30 PM 0
Share

actually it renders quite fast, quicksort is working "awwwight..", but I'll work on implementing a natural bubble sort of some kind.. currently it can handle about 10k particles being sorted at 30-40 fps, and without any sorting, a clean 60fps

avatar image
1

Answer by Noise crime · Jun 29, 2010 at 08:47 AM

Well sorting 10k particles is pretty much a folly and rarely ever needed. Normally the sheer number of particles and using blending gets around the issue. Are you sure you need sorting? What are the particles you are rendering?

Off hand the only alternative I can think of is to use the same system that vegetation/trees often use in games these days, which is to use alpha testing.

Alpha testing allows you to write into the depth buffer (thus giving you some sorting), but unfortunately its a boolean operation. The pixel is either drawn or not, there is no in-between, like alpha blending. This is often minimised by simply re-drawing the particles a second time, this time with zwrite off, alpha testing off and blending on, to give softer edges.

However it wont work for blended particles as you wont get the same levels of over-draw, which you normally need with particles to build up density as the depth buffer will prevent particles further away being rendered.

Its important to note that this method cannot guarantee perfect sort order appearance, but its usually good enough in the case of vegetation not be too noticeable, whilst at a greatly reduced cost compared to sorting polygons.

ANswer 2: Actually thinking about it, you could probably use a billboard system type shader, something that takes just a single vertex per particle to define its position in space. You can build the billboard from that and some other data (view direction etc). So then its just a case of sorting 10k vertices and how to pass that data into the shader.

Although it sounds a lot I'm guessing it shouldn't be too bad if you find the right sort algorithm and make use of temporal consistency, where by you should only need to sort every few frames as the view position to the particles doesn't change that greatly between single frames.

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image N1nja · Jun 29, 2010 at 02:32 PM 0
Share

actually, you're right on the spot with the shader that transforms a single vertex into a billboard.. I'm already doing that. :P How do you think I'm able to render 10k particles without lag anyways?

hehe.. It's just to bad that darker particles from that back appear in the front and crap. =[

avatar image Noise crime · Jun 29, 2010 at 06:33 PM 0
Share

so presumably then its just a question of finding a good sorting algorithm to provide those vertices in the correct order, though without writing to the zbuffer (could be done via alphatesting I guess) you still suffer the 'painters algorithm' of close overlapping billboards drawing rendering in the wrong order.

avatar image N1nja · Jun 30, 2010 at 01:27 AM 0
Share

Yup, if I do not write to the ZBuffer, I get ugly Transparency Artifacts from incorrect draw order.

If I do not write to the ZBuffer, I am forced to do Depth Sorting, since it will not be done automatically..

But either way it seems I have to do some depth sorting, or re-draw the particles twice.

I just wish there was a way to use the ZBuffer, without sorting, and without the ugly artifacts.

At the moment, I am able to get about 8,000 particles to render, with 10-20 FPS, although this really isn't good enough for me.

I am gonna need something faster.

avatar image N1nja · Jun 30, 2010 at 01:28 AM 0
Share

maybe I can only sort the particles in the view frustum for starters..

avatar image N1nja · Jun 30, 2010 at 01:31 AM 0
Share

without any depth sorting, I get an easy 50-60 FPS.. with 10k+ particles, it starts to drop around 20-30k.. so really the issue is in the sorting..

Show more comments
avatar image
0

Answer by Fixe · Oct 26, 2012 at 08:22 PM

The best algorithm for already sorted arrays is usually InsertionSort, It has lower overhead than bubblesort. Also Heapsort is really great and many particles engines uses only heapsort.

I'm my current implementation I'm using exactly both algorithms. The nice thing is that Particles usually don't spawn all at once, but are added gradually so I mostly using InsertionSort (I profiled and turned out to be the best).I Always keep less than 1k-2k particles, if Insertion sort perform worst case for 1k-2k the little freeze down will not be noticeable and the next frame the array is already sorted and the freeze will not occur again. In my tests heapsort was faster (also than quicksort) but ended up to be not necessary in my app.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Multiple camera depth sorting issues 0 Answers

Particle effect sorting around a 2d sprite 3 Answers

Particle Depth, possible? 1 Answer

Looking for a transparent shader 0 Answers

Can I change individual particles to different sorting layers? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges