Eyeball_SkyMap
three.js shaders
HTML
<script src="http://threejs.org/build/three.min.js"></script>
<script src="http://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script id="vertex_shader" type="x-shader/x-vertex">
varying vec3 vNormal;
void main() {
vNormal = normal;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
</script>
<script id="fragment_shader" type="x-shader/x-fragment">
uniform sampler2D texture;
varying vec3 vNormal;
void main() {
vec2 uv = normalize( vNormal ).xy * 0.5 + 0.5;
vec3 color = texture2D( texture, uv ).rgb;
if ( vNormal.z < - 0.85 ) color = vec3( 0.777, 0.74, 0.74 ); // back of eye
gl_FragColor = vec4( color, 1.0 );
}
</script>
CSS
body {
background-color: #000;
margin: 0px;
overflow: hidden;
}
img {
width: 128px;
height: 128px;
position: absolute;
left: 0px;
top: 0px;
z-index: 1;
}
JavaScript
// MatCap-style image rendered on a sphere
//... after WestLangley's jsfiddle: http://jsfiddle.net/zD2rH/184/
//... from SO post here:http://stackoverflow.com/questions/21663923/mapping-image-onto-a-sphere-in-three-js/21742418
var camera, scene, renderer;
var image;
init();
animate();
function init() {
info = document.createElement( 'div' );
info.style.position = 'absolute';
info.style.top = '30px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.style.color = '#fff';
info.style.fontWeight = 'bold';
info.style.backgroundColor = 'transparent';
info.style.zIndex = '1';
info.style.fontFamily = 'Monospace';
info.innerHTML = 'Drag mouse to rotate camera; scroll to zoom';
document.body.appendChild( info );
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 0, 0, 10 );
controls = new THREE true; } );
var uniforms = {
"texture": { type: "t", value: texture }
};
// material
var material = new THREE.ShaderMaterial( {
uniforms : uniforms,
vertexShader : document.getElementById( 'vertex_shader' ).textContent,
fragmentShader : document.getElementById( 'fragment_shader' ).textContent,
side:THREE.DoubleSide
} );
var mySphere = new THREE.Mesh( new THREE.SphereGeometry( 30, 32, 24 ), material );
mySphere.rotation.set(0, Math.PI,0);
scene.add( mySphere );
}
function animate() {
requestAnimationFrame( animate );
//controls.update(); // not required here
render();
}
function render() {
renderer.render( scene, camera );
}
image.src =...