Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Creating my own particles in a particlesystem for displaying a point cloud in webgl

Discussion in 'General Graphics' started by McSwan, Mar 22, 2022.

  1. McSwan

    McSwan

    Joined:
    Nov 21, 2013
    Posts:
    107
    Hi, I want to display a point cloud in webgl. I thought I could use the particle system, but I need to load the particles positions in and then have the particle system paused.

    Here's my attemp so far but I can't see any of the particles.

    Can I manually create particles like this ? Is thee a proper way to create particles from a file?

    You could also create some interesting effects if you could start your particle system in a preloaded format.

    Code (CSharp):
    1.  
    2. public class ParticlePoints : MonoBehaviour
    3. {
    4.  
    5.     ParticleSystem m_System;
    6.     ParticleSystem.Particle[] m_Particles;
    7.  
    8.     Vector3[] points;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         if (m_System == null)
    14.         {
    15.             m_System = GetComponent<ParticleSystem>();
    16.         }
    17.  
    18.         PointCloudData data = new PointCloudData();
    19.         data.Vertices = new[] { new Vector3(0f, 0f, 0f), new Vector3(1f, 1f, 1f),
    20.             new Vector3(10f, 2f, 1f), new Vector3(1f, 1f, 2f),
    21.             new Vector3(1f, 2f, 1f), new Vector3(1f, 1f, 2f),
    22.             new Vector3(20f, 2f, 1f), new Vector3(2f, 1f, 2f)};
    23.  
    24.         points = data.Vertices;
    25.         CreatePoints();
    26.     }
    27.  
    28.     public void CreatePoints(/*Vector3[] points*/)
    29.     {
    30.  
    31.         //m_Particles = new ParticleSystem.Particle[m_System.main.maxParticles];
    32.         m_Particles = new ParticleSystem.Particle[points.Length];
    33.         for (int i = 0; i < points.Length; i++)
    34.         {
    35.             m_Particles[i].position = points[i];
    36.         }
    37.         m_System.SetParticles(m_Particles, points.Length);
    38.         //m_System.Play();
    39.         m_System.Pause();
    40.  
    41.     }
    42.  
    43. }
    44.  
     
  2. McSwan

    McSwan

    Joined:
    Nov 21, 2013
    Posts:
    107
    Hi Michael,

    It looks like you need to setparticles every frame in update. Here is a solution.

    Owing to the fact that we are uploading point every frame, it means we need to keep the cloud in memory and upload in every frame to the gpu which is somewhat slow. Also, in webgl it uses a lot of memory. It'd be nice if we could upload the point cloud to the gpu and delete the cloud from local memory.

    Code (CSharp):
    1.  
    2.  
    3. public class ParticlePoints : MonoBehaviour
    4. {
    5.  
    6.     public int maxStars = 1000;
    7.     public int universeSize = 10;
    8.     public ParticleSystem theParticleSystem;
    9.     private ParticleSystem.Particle[] points;
    10.  
    11.     private void Create()
    12.     {
    13.         points = new ParticleSystem.Particle[maxStars];
    14.  
    15.         for (int i = 0; i < maxStars; i++)
    16.         {
    17.             points[i].position = Random.insideUnitSphere * universeSize;
    18.             points[i].startSize = Random.Range(0.05f, 0.05f);
    19.             points[i].startColor = new Color(1, 1, 1, 1);
    20.         }
    21.  
    22.     }
    23.  
    24.     void Start()
    25.     {
    26.         Create();
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         if (points != null)
    33.         {
    34.             theParticleSystem.SetParticles(points, points.Length);
    35.         }
    36.     }
    37. }