Get simple rectangle shaped mesh face direction

i have a tube that contains several separated rectangle shaped mesh objects. My goal is to spawn an object in the center of each rectangle and face perpendicular to the rectangle surface

On each separated rectangle i have this code:

Mesh mesh = GetComponent<MeshFilter>().mesh;
        int[] triangles = mesh.triangles;

        Vector3 n0=Vector2.zero,n1=Vector2.zero,n2=Vector2.zero;
        for (int i = 0; i < mesh.triangles.Length; i += 3)
        {
            n0 = mesh.vertices[mesh.triangles[i + 0]];
            n1 = mesh.vertices[mesh.triangles[i + 1]];
            n2 = mesh.vertices[mesh.triangles[i + 2]];
        }

        Vector3 a = Vector3.Cross (n1 - n2, n2 - n0).normalized;

        Instantiate(prefab, transform.renderer.bounds.center,Quaternion.LookRotation(a));

Result is this:

My goal is this

any ideas ?? :slight_smile:

Can this works? (Eventually try changing the 180 angle)

Instantiate (prefab, transform.renderer.bounds.center, transform.rotation * Quaternion.Euler (180,0,0 ));

I suspect the transform is that of the tube and you have no transform for the rects. Right? If so I am wrong… sorry.

If you have no transform for the rectangles, try this:

Instantiate (prefab, transform.renderer.bounds.center, Quaternion.LookRotation (transform.position, transform.up) * Quaternion.Euler (180, 0, 0 ));

You can omit the Euler to invert the rotation and you can try changing the transform.up to transform.forward or right (depending on the tube orientation).

This is my last bet… :slight_smile:

Instantiate (prefab, transform.renderer.bounds.center, Quaternion.FromToRotation (Vector3.up, a));

You can try .forward or .right depending on the prefab orientation.

Thank you for helping but all of the individual rectangles game objects have the same rotation so i cant use the standard functions like this

I think the solution will be to get the perpendicular to the mesh rectangle front facing side
What do you think?

Individual rectangles game objects have the same rotation

I think they have the same localRotation (the same you are looking in the editor) but their transform.up is that green line you are seeing above.

The first solution I am almost sure could works. I just wonder if you tryed it.

yes i tried the code but the didnt solve it unfortunately . Also the transform.up points to the same direction for all objects (not perpendicular) , i think it cannot be seeing clearly from pics above … take a look at this new pic with the first code you suggested

Any thoughts ?? :slight_smile:

just a quick update… i found that (in my original code) if dont use “transform.renderer.bounds.center” location in the instantiate function but use “mesh.bounds.center” the rotation is correct but position is way off

Instantiate(prefab, mesh.bounds.center,Quaternion.LookRotation(a));

any thoughts/ideas on that? :slight_smile:

If you can send me a sample scene I try to figure out… I’m having an hard time just in understanding your setup :slight_smile:

You can try:

  • Transform t = Instantiate (… mesh.bounds.center
  • t.position = transform.renderer.bounds.center;

If your asset is facing the z axis when imported, doing “obj.transform.foward = direction to face” should suffice.
To illustrate the exemple i made you a sample project with a scrolling inifinte tube that you can download here:
https://www.sugarsync.com/pf/D2307301_98582733_600566

for the following result:

3 Likes

Thanks! I have not played yet with procedural meshes, so I think I can learn something from that…
Yutube… nice name! :slight_smile:

1 Like

Mesh vertices use local positions, need to convert it to world position to calculate center rectangle.

I used a Quad as rectangle in this example. Spawned object must look at to forward axis.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter))]
public class temp : MonoBehaviour {

    public GameObject prefab;

    GameObject instancePrefab;
    Mesh meshQuad;

    void Start(){

        meshQuad = GetComponent<MeshFilter>().mesh;
        instancePrefab = (GameObject)Instantiate(prefab);

    }

    void prefabLookAt(){

        Matrix4x4 localToWorld = transform.localToWorldMatrix;

        Vector3 v0 = localToWorld.MultiplyPoint3x4(meshQuad.vertices[meshQuad.triangles[0]]);
        Vector3 v1 = localToWorld.MultiplyPoint3x4(meshQuad.vertices[meshQuad.triangles[1]]);
        Vector3 v2 = localToWorld.MultiplyPoint3x4(meshQuad.vertices[meshQuad.triangles[2]]);

        Vector3 normal = Vector3.Cross(v1-v0,v2-v0);
      
        Vector3 center = Vector3.zero;
      
        int i;
        for(i=0;i<meshQuad.vertices.Length;i++)
            center += localToWorld.MultiplyPoint3x4(meshQuad.vertices[i]);
      
        center /= (float)i;

        instancePrefab.transform.position = center;
        instancePrefab.transform.rotation = Quaternion.LookRotation(normal);

    }

    void Update(){

        prefabLookAt();

    }

}
3 Likes

Sorry for the late reply , i had some free time today and tested isGreen code and it solved my issue :slight_smile:
everything works as it should be , thank you for helping me out.

I will also test the Yseron source with procedural mesh as it looks interesting.

thanks again for all the help :slight_smile:

1 Like