JSFiddle - React, Tailwind, and code Playground
HTML
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three/build/three.module.js"
}
}
</script>
JavaScript
import * as THREE from 'three';
const width = window.innerWidth, height = window.innerHeight;
// init
const camera = new THREE.PerspectiveCamera( 70, width / height, 0.01, 10 );
camera.position.z = 1;
// scene
const scene = new THREE.Scene();
// rotation group
const group = new THREE.Group();
scene.add( group );
// base geometry
const geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 ).toNonIndexed();
const material = new THREE.MeshBasicMaterial( { color: 0x77bbff } );
const mesh = new THREE.Mesh( geometry, material );
group.add( mesh );
// line segments
const lineGeometry = new THREE.EdgesGeometry( geometry );
const foregroundLines = new THREE.LineSegments( lineGeometry, new THREE.LineBasicMaterial( {
color: 0,
} ) );
foregroundLines.renderOrder = 2;
const backgroundLines = new THREE.LineSegments( lineGeometry, new THREE.LineBasicMaterial( {
color: 0xffffff,
depthFunc: THREE.GreaterDepth,
depthWrite: false,
} ) );
backgroundLines.renderOrder = 1;
group.add( foregroundLines, backgroundLines );
// renderer
const renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( width, height );
renderer.setAnimationLoop( animate );
document.body.appendChild( renderer.domElement );
// animation
function animate( time ) {
group.rotation.x = time / 2000;
group.rotation.y = time / 1000;
renderer.render( scene, camera );
}