How to get correct rotation value from pickinfo.getnormal()

im placing a object on where pickpoint. positios perfect but im unable to get proper rotation anybody has solution?

decal is not solution

why somuch complicated method to get a rotation from normal

https://www.babylonjs-playground.com/#14VWOV#14

please checkit when i click on side wall plane rotation is weird.

i need only y rotation value to hang a paint on a wall

1 Like

@Vijayxd totally agree: too many complications going on here. Initially I assumed the geometry is bad so I scolded the 3D artist, well, actually I checked the vertex normals in Inspector. Geometry is ok.

So I checked the src code of getNormal and found the normal is negated in some cases, like this:

        // Flip the normal if the picking ray is in the same direction.
        if (this.ray && Vector3.Dot(result, this.ray.direction) > 0) {
            result.negateInPlace();
        }

I guess this is why the doc for getNormal says “Note that the returned normal will always point towards the picking ray.”. I fail to see the reason for this.

Anyway, the wrong normal can be flipped back like this:

      let norm = pickinfo.getNormal(true);
      if(pickinfo.ray) {
        let dd = BABYLON.Vector3.Dot(norm, pickinfo.ray.direction)
        if(dd > 0) norm.negateInPlace();
      }

Now all normals will be as expected.

Adding @bghgary who worked on this recently :slight_smile:

See this thread for the context on why we added this.

@Jo_Mantelers Can you explain your scenario in more detail? Maybe also provide a PG if possible. Are you trying to pick a mesh from the back face?

No, I don’t want to pick the backface. Some meshes just fail to produce correct normals, even though the face/vertex normals are correct. As stated, it depends on the dot product of normal and pickinfo.ray, i.e. camera . Here a PG: when you move the mouse and stick is yellow, the normal is valid, when red normal is flipped/invalid (inside the chimney, sides of stairs, small box). I could not detect anything wrong with the geometries, so I will do a normal flip in my app.

1 Like

Thanks for the repro, this PR will fix the problem:

1 Like

Ahh, yes, my change was comparing two vector in different spaces. Sorry for the troubles. Thanks @Evgeni_Popov for fixing. :slight_smile: