playMaker

Author Topic: How can I get a material index from an object that has multi materials?  (Read 1699 times)

Silicon Power

  • Full Member
  • ***
  • Posts: 186
Please read second post
« Last Edit: August 13, 2020, 07:30:13 AM by Silicon Power »

Silicon Power

  • Full Member
  • ***
  • Posts: 186
Re: How can I get a material index from an object that has multi materials?
« Reply #1 on: August 13, 2020, 07:31:21 AM »
Ok I find a C# material that does this but can some please convert it to a playmaker action?

Code: [Select]
using UnityEngine;

public class FindHitSubMesh : MonoBehaviour
{

    private int[] subMeshesFaceTotals;
    private int totalSubMeshes;

    void Awake()
    {
        MeshFilter mf = (MeshFilter)gameObject.GetComponent(typeof(MeshFilter));
        Mesh mesh = mf.mesh;

        totalSubMeshes = mesh.subMeshCount;
        subMeshesFaceTotals = new int[totalSubMeshes];

        for (int i = 0; i < totalSubMeshes; i++)
        {
            subMeshesFaceTotals[i] = mesh.GetTriangles(i).Length / 3;
        }
    }


    void OnMouseDown()
    {

        RaycastHit hit;

        if (!Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
            return;

        int hitSubMeshNumber = 0;
        int maxVal = 0;

        for (int i = 0; i < totalSubMeshes; i++)
        {
            maxVal += subMeshesFaceTotals[i];

            if (hit.triangleIndex <= maxVal - 1)
            {
                hitSubMeshNumber = i + 1;
                break;
            }

        }

        Debug.Log("We hit sub mesh number: " + hitSubMeshNumber);
    }
}

Silicon Power

  • Full Member
  • ***
  • Posts: 186
Re: How can I get a material index from an object that has multi materials?
« Reply #2 on: August 15, 2020, 02:01:46 AM »
Up

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: How can I get a material index from an object that has multi materials?
« Reply #3 on: August 15, 2020, 05:31:40 AM »
Hi.
Can you try this, i have not tested.

You should do a raycast 1st then (when Hit) do the action in the attachment below.

Silicon Power

  • Full Member
  • ***
  • Posts: 186
Re: How can I get a material index from an object that has multi materials?
« Reply #4 on: August 15, 2020, 07:25:28 AM »
Hi.
Can you try this, i have not tested.

You should do a raycast 1st then (when Hit) do the action in the attachment below.

When I play game it changes "Hit Sub Mesh Number" variable to None!  :'(
« Last Edit: August 15, 2020, 07:44:41 AM by Silicon Power »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: How can I get a material index from an object that has multi materials?
« Reply #5 on: August 15, 2020, 10:05:22 AM »
Hi.
Try this version :)

Silicon Power

  • Full Member
  • ***
  • Posts: 186
Re: How can I get a material index from an object that has multi materials?
« Reply #6 on: August 15, 2020, 04:47:02 PM »
Hi.
Try this version :)

Thank you a lot. But something isn't right it always show number 1 doesn't matter where I click
« Last Edit: August 15, 2020, 04:58:07 PM by Silicon Power »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: How can I get a material index from an object that has multi materials?
« Reply #7 on: August 16, 2020, 04:09:42 AM »
Hi.
How i can test this?

Can you make a simple scene so i can test.