JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.jsdelivr.net/npm/three@0.125.2/build/three.js"></script>
JavaScript
var camera, scene, renderer;
var geometry, material, mesh;
init();
animate();
function init() {
THREE.Object3D.DefaultUp = new THREE.Vector3(0, 0, 1);
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
camera.position.z = 5;
scene = new THREE.Scene();
let L = 2;
let W = 1;
let H = 2;
geometry = new THREE.BoxBufferGeometry( L,W,H ).toNonIndexed();
material = new THREE.MeshBasicMaterial();
let textureData = createBoxTexture(L,W,H);
material.map = textureData.texture;
setCargoTextureUV(geometry, textureData);
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
}
// Create a texture with three drawings - one for the top and bottom, one for first side-pair, and one for the second side-pair
function createBoxTexture(l, w, h) {
var cargoCanvasTexture = document.createElement("canvas");
var ctx = cargoCanvasTexture.getContext("2d");
let pixelPerMeter = 32;
const canvasWidth = 128;
const canvasHeight = 512;
const itemLength = l * pixelPerMeter;
const itemWidth = w * pixelPerMeter;
const itemHeight = h * pixelPerMeter;
cargoCanvasTexture.width = canvasWidth;
cargoCanvasTexture.height = canvasHeight;
ctx.fillStyle = "red";
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
const complementColor = "white";
// Borders
ctx.strokeStyle = "white";
ctx.lineWidth = 4;
ctx.strokeRect(0, 0, itemLength, itemWidth);
ctx.strokeRect(0, itemWidth, itemLength, itemHeight);
ctx.strokeRect(0, itemWidth + itemHeight, itemWidth, itemHeight);
ctx.textAlign = "center";
ctx.fillStyle = complementColor;
ctx.lineWidth = 2;
ctx.textBaseline = "middle";
const fontSize = itemWidth * 0.5;
ctx.font = "bold " + fontSize + "px roboto";
//Top...