Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: Silicon Power on August 12, 2020, 03:57:18 PM

Title: How can I get a material index from an object that has multi materials?
Post by: Silicon Power on August 12, 2020, 03:57:18 PM
Please read second post
Title: Re: How can I get a material index from an object that has multi materials?
Post by: Silicon Power 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);
    }
}
Title: Re: How can I get a material index from an object that has multi materials?
Post by: Silicon Power on August 15, 2020, 02:01:46 AM
Up
Title: Re: How can I get a material index from an object that has multi materials?
Post by: djaydino 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.
Title: Re: How can I get a material index from an object that has multi materials?
Post by: Silicon Power 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!  :'(
Title: Re: How can I get a material index from an object that has multi materials?
Post by: djaydino on August 15, 2020, 10:05:22 AM
Hi.
Try this version :)
Title: Re: How can I get a material index from an object that has multi materials?
Post by: Silicon Power 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
Title: Re: How can I get a material index from an object that has multi materials?
Post by: djaydino on August 16, 2020, 04:09:42 AM
Hi.
How i can test this?

Can you make a simple scene so i can test.