JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://rawgithub.com/mrdoob/three.js/master/build/three.js"></script>
<script src="https://rawgithub.com/mrdoob/three.js/master/examples/js/libs/stats.min.js"></script>
<script type="text/x-shader" id="billboardVertex">
void main() {
vec3 look = normalize( cameraPosition - position );
if( length( look ) == 0.0 ) {
look.z = 1.0;
}
vec3 up = vec3( 0.0, 1.0, 0.0 );
vec3 right = normalize( cross( up, look ) );
up = normalize( cross( look, right ) );
mat4 transformMatrix;
transformMatrix[0][0] = right.x;
transformMatrix[0][1] = right.y;
transformMatrix[0][2] = right.z;
transformMatrix[1][0] = up.x;
transformMatrix[1][1] = up.y;
transformMatrix[1][2] = up.z;
transformMatrix[2][0] = look.x;
transformMatrix[2][1] = look.y;
transformMatrix[2][2] = look.z;
gl_Position = projectionMatrix * modelViewMatrix * transformMatrix * vec4( position, 1.0 );
}
</script>
<script type="text/x-shader" id="billboardFragment">
void main() {
gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 );
}
</script>
JavaScript
var windowWidth = window.innerWidth,
windowHeight = window.innerHeight,
scene = new THREE.Scene(),
camera = new THREE.PerspectiveCamera( 75, windowWidth / windowHeight, 1, 10000 ),
stats = new Stats(),
renderer = new THREE.WebGLRenderer( {
antialias: true
} );
function init() {
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0';
stats.domElement.style.left = '0';
stats.domElement.style.zIndex = '5';
renderer.setSize( windowWidth, windowHeight );
camera.position.z = 20;
camera.lookAt( scene.position );
document.body.appendChild( stats.domElement );
document.body.appendChild( renderer.domElement );
}
function makeBillboard() {
var material = new THREE.ShaderMaterial( {
vertexShader: document.getElementById( 'billboardVertex' ).textContent,
fragmentShader: document.getElementById( 'billboardFragment' ).textContent,
side: THREE.DoubleSide
} );
var geometry = new THREE.PlaneGeometry( 10, 10 );
var mesh = new THREE.Mesh( geometry, material );
mesh.position.z = 5;
scene.add( mesh );
}
function animate() {
requestAnimationFrame( animate );
stats.update();
renderer.render( scene, camera );
camera.position.x = Math.cos( Date.now() * 0.001 ) * 50;
camera.position.z = Math.sin( Date.now() * 0.001 ) * 50;
camera.lookAt( scene.position );
}
init();
makeBillboard();
animate();