I have a 3D mesh with 3 sub meshes in it, for 3 different materials.

I would like particles to spawn from the mesh, but only from location where one of the material is assigned.

How can I make this happen?

  • \$\begingroup\$ What framework/engine/libraries are you using? What have you tried so far? \$\endgroup\$ – DMGregory Jun 25 '18 at 19:48
  • \$\begingroup\$ I'm Unity 2017. I'm not using any libraries. Asset has 2 materials set in Blender. \$\endgroup\$ – eon Jun 25 '18 at 20:21
  • \$\begingroup\$ can you elaborate your question ? \$\endgroup\$ – Faisal Imran Jun 26 '18 at 5:46

If I understand correctly, you have a mesh with multiple materials, each one applied to a certain part of the mesh, which Unity does using sub meshes. You then want to emit particles from one specific sub mesh.

If this is correct, then it should be fairly simple to achieve. You will need to extract the relevant sub mesh into its own distinct mesh, then create a particle system with its Shape module set up as type Mesh and the target mesh being the one you extracted. More information about the Shape module can be found here in Unity’s documentation. Note that your mesh must be marked as readable in its import settings.

A simple script that performs these operations (warning: untested):

public class SetupMeshEmitter {
    public int submeshIndex;

    private void Start() {
        var sourceMesh = GetComponent<MeshFilter>().sharedMesh;
        var mesh = new Mesh();
        mesh.vertices = sourceMesh.vertices;
        mesh.triangles = sourceMesh.GetTriangles(submeshIndex);

        var ps = GetComponent<ParticleSystem>();

        //this section taken from Unity docs
        var sh = ps.shape;
        sh.enabled = true;
        sh.shapeType = ParticleSystemShapeType.Mesh; //just in case you forgot to set it up in the inspector
        sh.mesh = mesh;
  • \$\begingroup\$ Ed Marty, thank you. But, just to clarifiy I have only one mesh. In edit mode in Blender, I have set 3 materials on it but as object it's only one. \$\endgroup\$ – eon Jun 26 '18 at 14:51
  • \$\begingroup\$ If you assign multiple materials to a mesh and they are applied to different parts of the mesh, then Unity does this by having different submeshes internally. The asset in the editor only presents as a single mesh, but you can access the submeshes programmatically as I showed above. This should work for your purpose. \$\endgroup\$ – Ed Marty Jun 27 '18 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.