Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping image onto a sphere in Three.js

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 result looks like this:

Enter image description here

How can I get it to map properly without looking stretched?

My code for creating the sphere and mapping the texture is below:

var geometry = new THREE.SphereGeometry(0.5,100,100);
var material = new THREE.MeshPhongMaterial( { map: THREE.ImageUtils.loadTexture('eyeballmap.jpg',THREE.SphericalRefractionMapping) } );
var eyeball = new THREE.Mesh( geometry, material );
eyeball.overdraw = true;
eyeball.castShadow = true;
scene.add( eyeball );
like image 245
Mat Richardson Avatar asked Feb 09 '14 19:02

Mat Richardson


2 Answers

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.

like image 37
gaitat Avatar answered Oct 20 '22 22:10

gaitat


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( tex, uv ).rgb;

Fiddle: https://jsfiddle.net/31o0zn2c/

three.js r.134

Eye Rendered on Sphere from MatCap Texture

like image 189
WestLangley Avatar answered Oct 20 '22 22:10

WestLangley