• 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 danielmxli · Jun 01, 2015 at 01:03 PM · particlesoptimizationloopjavascript-specificvisualize

Need tips for optimizing main func that loops through thousands of particles

Hi!

I'm trying to create fractal images with the following equation.

And it should look somet$$anonymous$$ng like t$$anonymous$$s but in 3D

I've got everyt$$anonymous$$ng right, and my script is below. But, I want to run 5 - 8 particle systems at once so I can create t$$anonymous$$s effect! I've ran the profiler and the CPU spikes when it's calling GenerateOrbit(); I know the main concern is the fact that I'm looping through every particle to change their positions, but I'm not really sure if there's an alternative. Perhaps there is a better way of creating 9 particle systems by using fabrication & instantiating?

 #pragma strict
  
  var particleCount : int = 4000;
   
  
  function Start ()
  {
     GenerateOrbit();            
  }
  
  function FixedUpdate(){
     
     GenerateOrbit();
     
  }
  
  function GenerateOrbit(){
  
        // Figure out w$$anonymous$$ch range is best for BIG fractals  
      var a : int = Random.Range(-1,5);
      var b : int = Random.Range(-11,2);
       var c : int = Random.Range(-10,20);
     
      var myParticleSystem : ParticleSystem; 
      var myParticles: ParticleSystem.Particle[];
      
      
      myParticleSystem = GetComponent(ParticleSystem);
      myParticleSystem.startColor = new Color32(Mathf.Round(Random.Range(0,255)),Mathf.Round(Random.Range(0,255)),Mathf.Round(Random.Range(0,255)),255);
      myParticleSystem.Emit(particleCount);
      
      myParticles = new ParticleSystem.Particle[particleCount + 1];
      myParticleSystem.GetParticles(myParticles);
      
      var x : float;
      var y : float;
      var prevPos : Vector3;
         
      for (var i = 0; i < particleCount; i++)
      {
          // Get previous particle position
          prevPos = (i < 1) ? new Vector3(Random.Range(-5,10), Random.Range(-5,10)) : myParticles[i - 1].position; // Perhaps Random.value isn't the best option here? 
          
          x = prevPos.x;
          y = prevPos.y;
          
          // For the sake of optimizing by not using Mathf.sqrt & Mathf.abs
          x_abs = (x > 0 ? x : -x);
          var d : float;
          d = (b * x + c) > 0 ? (b * x + c) : -(b * x + c);
          d = Mathf.Pow(d_abs,0.5); 
            
          var x1 = y - x / x_abs * d;
          var y1 = a - x;
          
          var newPosition = new Vector3(x1, y1, 0);
          
          myParticles[i].position = newPosition;
          myParticleSystem.SetParticles(myParticles, particleCount);
      }
      
     yield WaitForSeconds(5);
     myParticleSystem.Clear();
  }
  
 

Comment
Add comment · Show 2
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 Baste · Jun 01, 2015 at 04:59 PM 0
Share

Instead of doing this every fifth second, do parts of the calculation continually, and then show the results every fifth seconds. That'll spread the calculation over time, so you won't get any lag spikes.

I'm also pretty sure that you can't chear your way out of Mathf.Sqrt(value) by doing Mathf.Pow(value, 0.5f) instead - it's got pretty much the same performance cost.

Just fill the positions of (Time.deltaTime * (4000 / 5)) particles every frame, and you'll have filled all of the 4000 particles after five seconds has passed.

avatar image danielmxli · Jun 04, 2015 at 06:10 AM 0
Share

Thanks! That actually seems like a very viable idea. I was thinking of putting this into the Update() function, which calls every frame. I'm having some trouble thinking about how this would work out now that I have 7 particle systems... How would I be updating the same one in each consecutive Update() call? Is it possible?

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Custom particle system loop optimization 0 Answers

Is there a performance difference between Unity's Javascript and C#? 6 Answers

Particle System Overhead 6 Answers

Why aren't my particle systems batched? 1 Answer

Is shuriken well performing on iDevices? (iPhone, iPad) 1 Answer


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