JSFiddle - React, Tailwind, and code Playground
HTML
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js"
}
}
</script>
JavaScript
import * as THREE from 'three';
// init
const camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 1;
const scene = new THREE.Scene();
const geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
const material = new THREE.MeshBasicMaterial( {
opacity: 0.5,
transparent: true,
} );
const mesh1 = new THREE.Mesh( geometry, material );
scene.add( mesh1 );
const customBlendMaterial = new THREE.MeshBasicMaterial( {
opacity: 0.5,
blending: THREE.CustomBlending,
} );
const mesh2 = new THREE.Mesh( geometry, customBlendMaterial );
scene.add( mesh2 );
mesh1.position.x = - 0.2;
mesh2.position.x = 0.2;
const renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animation );
document.body.appendChild( renderer.domElement );
// animation
function animation( time ) {
mesh1.rotation.x = time / 2000;
mesh1.rotation.y = time / 1000;
mesh2.rotation.x = time / 2000;
mesh2.rotation.y = time / 1000;
renderer.render( scene, camera );
}