JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="canvas-element" width="640" height="360"></canvas>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
}
}
</script>
JavaScript
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const canvasElement = document.getElementById('canvas-element');
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, canvasElement.width/canvasElement.height);
camera.position.set(2, 2, 2);
const renderer = new THREE.WebGLRenderer({
antialias: true,
canvas: canvasElement,
});
renderer.shadowMap.enabled = true;
const controls = new OrbitControls(camera, canvasElement);
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
const light = new THREE.DirectionalLight();
light.position.set(1, 1, 1);
light.castShadow = true;
light.shadow.bias = -0.0001;
scene.add(light);
const boxGeo = new THREE.BoxGeometry(1, 1, 1);
const boxMat = new THREE.MeshLambertMaterial({
color: 0x00ff00,
});
const box = new THREE.Mesh(boxGeo, boxMat);
box.castShadow = true;
box.receiveShadow = true;
scene.add(box);
const planeGeo = new THREE.PlaneGeometry(10, 10);
const planeMat = new THREE.MeshLambertMaterial({
color: 0xffffff,
side: THREE.DoubleSide,
});
const plane = new THREE.Mesh(planeGeo, planeMat);
plane.position.set(0, 0, -.5);
plane.castShadow = true;
plane.receiveShadow = true;
scene.add(plane);
animate();