Mapping image onto a sphere in Three.js

前端 未结 2 1679
小蘑菇
小蘑菇 2021-01-31 05:51

I\'m currently using Three.js to try and create something. I\'ve got a sphere, and I\'m trying to map the eyeball image here onto it.

The problem I have is that the res

相关标签:
2条回答
  • 2021-01-31 06:11

    The texture that you have represents only the visible part of the eye which is not the entire sphere. You need to add white space around your existing texture to allow for the part of the sphere that is not visible.

    0 讨论(0)
  • 2021-01-31 06:22

    The key to the answer to your question is the image you linked to.

    That image looks like a MatCap image used in spherical environment mapping. You can see an example of spherical environment mapping here.

    The appropriate shader for such an environment map is one that uses (a function of) the sphere's normal as the UV index into the texture map.

    vec2 uv = normalize( vNormal ).xy * 0.5 + 0.5;
    
    vec3 color = texture2D( texture, uv ).rgb;
    

    It's as easy as that. :-)

    Fiddle: http://jsfiddle.net/zD2rH/184/


    EDIT: If you want to use MeshPhongMaterial, then you can achieve the same effect by modifying the sphere UVs like so:

    faceVertexUvs[ face ][ j ].x = face.vertexNormals[ j ].x * 0.5 + 0.5;
    faceVertexUvs[ face ][ j ].y = face.vertexNormals[ j ].y * 0.5 + 0.5;
    

    Same idea.

    2nd Fiddle: http://jsfiddle.net/zD2rH/183/

    EDIT: updated to three.js r.74

    Eye Rendered on Sphere from MatCap Texture

    0 讨论(0)
提交回复
热议问题