Flip normals in three.js on sphere

10,402

Solution 1

You can flip the normals in your geometry by reversing the winding order of your faces. You then have to fix UVs.

for ( var i = 0; i < geometry.faces.length; i ++ ) {

    var face = geometry.faces[ i ];
    var temp = face.a;
    face.a = face.c;
    face.c = temp;

}

geometry.computeFaceNormals();
geometry.computeVertexNormals();

var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
for ( var i = 0; i < faceVertexUvs.length; i ++ ) {

    var temp = faceVertexUvs[ i ][ 0 ];
    faceVertexUvs[ i ][ 0 ] = faceVertexUvs[ i ][ 2 ];
    faceVertexUvs[ i ][ 2 ] = temp;

}

However, you can get the same effect by simply setting Material.side = THREE.BackSide, or Material.side = THREE.DoubleSide.

In either case, your texture will be rendered flipped. You can either flip your texture before-hand, or build a model outside of three.js and import it.

three.js r.65

Solution 2

When you are creating material for your sphere, specify {side:THREE.DoubleSide}. This will make faces visible from both sides.

You can also change it anytime after your material is created.

Solution 3

It is fixed !!

The flip of an object with a negative scale object.scale.x = -1 also reverse the normals since three.js r89 (see: Support reflection matrices. #12787).

(But I have to upgrade to r91 to solve my normal issue.)

Share:
10,402
Inx
Author by

Inx

Updated on June 15, 2022

Comments

  • Inx
    Inx about 2 years

    I have been searching around and haven't found any really good answer to my question yet.. The thing is that I have this sphere.. just a basic sphere, and I want to flip the normals so the sphere gets the sort of "hollow/carved effect" and then plater on apply my textures to the "inside" of the sphere. any ideas of how to flip the normals?

    Also.. if its not possible to do this in three.js.. would it be possible to import a model where the normals are already flipped and get the effect I'm looking for?

  • WestLangley
    WestLangley about 9 years
    __dirtyVertices was removed from the library 3 years ago. You are using an old version of the library.
  • aboutqx
    aboutqx over 8 years
    Well,it confuse me,using code above in a skybox,I have to set side:THREE.FrontSide,but the texture is still rendered flipped.With fliping the texture ,I get alias.So i have to build another BoxGeometry?Why BoxGeometry built from vertex will render it without flipping texture?
  • WestLangley
    WestLangley over 8 years
    @aboutqx If you have a question, you need to ask it in a new post.
  • arpo
    arpo over 7 years
    This solution is the way to get the ray caster to work. Ray caster only sees on side, doubleSide doesn't work.
  • WestLangley
    WestLangley over 7 years
    @arpo Raycaster supports THREE.DoubleSide for meshes.
  • arpo
    arpo over 7 years
    @westlangley Strange, I posted this a while ago, and I haven't got it working, have to try again. stackoverflow.com/questions/40864144/…
  • user2145184
    user2145184 about 6 years
    This did not work for me in r91 using shadermaterial. Setting side: THREE.BackSide worked though.
  • atom
    atom over 3 years
    @WestLangley for the hundredth time your help save my life! Thank you!
  • Raphael Setin
    Raphael Setin almost 3 years
    It's not worth it to make your mesh/CAD exporter to export with the "right" normals. Instead use the Material.side = THREE.DoubleSide as @WestLangley pointed out. This saved me a lot of trouble!!