JSFiddle - React, Tailwind, and code Playground
by Travnik
HTML
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three/build/three.module.js"
}
}
</script>
CSS
body {
margin: 0;
}
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 texture = new THREE.DataTexture(new Uint8Array(2048*2048*4),2048,2048);
texture.image.data.fill(255);
texture.needsUpdate = true;
const geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
const material = new THREE.MeshBasicMaterial({map:texture});
const mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
const renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animation );
document.body.appendChild( renderer.domElement );
function randomizeTexture(){
const n = 2048*2048;
for(let i=0; i<n; i++){
const address = i*4;
texture.image.data[address] = Math.floor(Math.random()*255);
texture.image.data[address+1] = Math.floor(Math.random()*255);
texture.image.data[address+2] = Math.floor(Math.random()*255);
}
texture.needsUpdate = true;
console.log('Randomized Texture');
}
window.addEventListener('click',randomizeTexture);
// animation
function animation( time ) {
mesh.rotation.x = time / 2000;
mesh.rotation.y = time / 1000;
renderer.render( scene, camera );
}