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

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.

public class ParticlePoints : MonoBehaviour
{

    ParticleSystem m_System;
    ParticleSystem.Particle[] m_Particles;

    Vector3[] points;

    // Start is called before the first frame update
    void Start()
    {
        if (m_System == null)
        {
            m_System = GetComponent<ParticleSystem>();
        }

        PointCloudData data = new PointCloudData();
        data.Vertices = new[] { new Vector3(0f, 0f, 0f), new Vector3(1f, 1f, 1f),
            new Vector3(10f, 2f, 1f), new Vector3(1f, 1f, 2f),
            new Vector3(1f, 2f, 1f), new Vector3(1f, 1f, 2f),
            new Vector3(20f, 2f, 1f), new Vector3(2f, 1f, 2f)};

        points = data.Vertices;
        CreatePoints();
    }

    public void CreatePoints(/*Vector3[] points*/)
    {

        //m_Particles = new ParticleSystem.Particle[m_System.main.maxParticles];
        m_Particles = new ParticleSystem.Particle[points.Length];
        for (int i = 0; i < points.Length; i++)
        {
            m_Particles[i].position = points[i];
        }
        m_System.SetParticles(m_Particles, points.Length);
        //m_System.Play();
        m_System.Pause();

    }

}

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.

public class ParticlePoints : MonoBehaviour
{

    public int maxStars = 1000;
    public int universeSize = 10;
    public ParticleSystem theParticleSystem;
    private ParticleSystem.Particle[] points;

    private void Create()
    {
        points = new ParticleSystem.Particle[maxStars];

        for (int i = 0; i < maxStars; i++)
        {
            points[i].position = Random.insideUnitSphere * universeSize;
            points[i].startSize = Random.Range(0.05f, 0.05f);
            points[i].startColor = new Color(1, 1, 1, 1);
        }

    }

    void Start()
    {
        Create();
    }

    // Update is called once per frame
    void Update()
    {
        if (points != null)
        {
            theParticleSystem.SetParticles(points, points.Length);
        }
    }
}