Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  How do I set a particle position at runtime ?
#8
(31-03-2020, 07:07 PM)slugGoddess Wrote: Thanks a lot Jose, you're the best !   Sonrisa  Loving ObiRope so much. Can't wait to try ObiCloth later.
Let me know if it makes it easier/quicker for you if I include the whole class definition instead of just snippets.

Took a look at the snippets, but it's confusing because there's bits missing that I'm not entirely sure how I'm supposed to fill in.

I think the overall intent of the code is to instantiate a rope at each vertex of a mesh, kinda like hair strands. Is this correct? In that case there's no need to fiddle with particles, simply setting the position/orientation of the rope should be enough.

1.- Create a rope prefab that has one end centered in the transform's origin, and is aligned with the Z axis, like this (the translation gizmo is the actual object's gizmo, not a control point). This is so that when we translate/rotate the prefab, we know that we are placing the root of the hair strand, and orienting it perpendicular to the mesh surface.
[Image: 8GXYXu9.png]
2.- Then, instantiate the prefab once per vertex using this code:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Obi;

public class Scalp : MonoBehaviour
{
   public Mesh scalp;
   public GameObject hair;

   private GameObject[] strandInstances;

   void Awake()
   {
       Vector3[] vertices = scalp.vertices;
       Vector3[] normals = scalp.normals;
       strandInstances = new GameObject[vertices.Length];

       for (int i = 0; i < vertices.Length; ++i)
       {
           // instantiate a strand at the vertex position/orientation:
           strandInstances[i] = Instantiate(hair, vertices[i], Quaternion.LookRotation(normals[i]));

           // parent it to the scalp (we assume the scalp has the solver component).
           strandInstances[i].transform.SetParent(transform, false);

           // get the first particle group (first control point):
           var group = strandInstances[i].GetComponent<ObiRope>().blueprint.groups[0];

           // attach it to the scalp:
           var attachment = strandInstances[i].AddComponent<ObiParticleAttachment>();
           attachment.particleGroup = group;
           attachment.target = this.transform;
       }
   }

   private void OnDestroy()
   {
       for (int i = 0; i < strandInstances.Length; ++i)
       {
           Destroy(strandInstances[i]);
       }
   }
}

Results when applied to a icosphere:
[Image: LMGcj5x.png]
[Image: ScFO5Ru.png]

It's quite slow though. Keep in mind that simulating and generating a mesh for each individual strand is way too costly. Even when done in the GPU instead of the CPU, guide hairs/interpolation is used. No hair system I've come across (realtime or not) simulates each individual hair.
Reply


Messages In This Thread
RE: How do I set a particle position at runtime ? - by josemendez - 31-03-2020, 09:04 PM