0
\$\begingroup\$

I have implemented a code to generate an Icosphere procedurally in Unity 5.3.4f, which works pretty well. However, what I need to do know it tweak the code to make the sphere to receive texture only in its inside triangle-faces, not outside. Searching trough the Unity forums, many have said that it's simply the case of flipping the normals of all triangles (i.e. multiplying them by -1). I tried it and it is not the case.

What is the correct way of changing the triangles (not trough shaders) so the Icosphere mesh can only be textured inside (e.g. in a skydome)?

EDIT: After Kromster's answer, I did some research and ended up implementing the following code:

Mesh InvertMeshFaces(Mesh _mesh)
{

    //Flip the triangles
    int[] triangles = _mesh.triangles;

    int numtriangles = triangles.Length / 3;
    for (int t = 0; t < numtriangles; t++)
    {
        int temptriangle = triangles[t * 3];
        triangles[t * 3] = triangles[(t * 3) + 2];
        triangles[(t * 3) + 2] = temptriangle;
    }

    //reseting the UV
    Vector2[] uvs = _mesh.uv;
    for (int uvnum = 0; uvnum < uvs.Length; uvnum++)
    {
        uvs[uvnum] = new Vector2(1 - uvs[uvnum].x, uvs[uvnum].y);
    }

    //reseting normals
    Vector3[] normals = _mesh.normals;
    for (int normalsnum = 0; normalsnum < norms.Length; normalsnum++)
    {
        norms[normalsnum] = -norms[normalsnum];
    }

    //setting the new values of triangles, normals and UVs
    _mesh.triangles = triangles;
    _mesh.uv = uvs;
    _mesh.normals = normals ;

    return _mesh;
}

So far, it works only partially. Applying that function to my Icosphere mesh, now it is possible to see the sphere rendered when camera is inside it. However, oddly enough, it's still also possible to see the sphere from outside. It means, both inside and outside are visible - the outside is not culled as expected.

\$\endgroup\$
  • 1
    \$\begingroup\$ Is the sphere invisible from the inside, or is it visible and just not textured? \$\endgroup\$ – Icy Defiance May 26 '16 at 4:23
  • \$\begingroup\$ @IcyDefiance It is currently invisible and being textured outside. I want to change both: make it only visible from the inside and only texturable from the inside. \$\endgroup\$ – AndraSol May 26 '16 at 5:54
1
\$\begingroup\$

To change polygons orientation you need to change indices order, so that faces are facing in different direction. From clockwise to counter-clickwise (or vice versa, it depends on your graphics engine setup). Change every triangle indices order from being 1-2-3 to 1-3-2.

\$\endgroup\$
  • \$\begingroup\$ Many thanks! It did work only partially: it made the sphere renderable from the inside and outside (not only inside). But no texture that I try gets applied. I will update the question with my current code. \$\endgroup\$ – AndraSol May 26 '16 at 8:20

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.