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/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",
"three/addons/": "https://unpkg.com/three/examples/jsm/"
}
}
</script>
JavaScript
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
const options = {
ortho: true,
}
const scene = new THREE.Scene();
const tex = new THREE.TextureLoader;
let camera;
const texLoad = ( url, callback ) => {
return tex.load( url, texture => {
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
if ( callback ) callback( texture );
})
}
if (options.ortho) {
const cameraSize = 360;
camera = new THREE.OrthographicCamera(
-cameraSize / 2, cameraSize / 2,
cameraSize / 2, -cameraSize / 2,
1, 1000
);
camera.position.set(0, 500, 0);
camera.lookAt(0, 0, 0);
}
else {
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 10000);
camera.position.set(0, 0, 300)
camera.lookAt(0, 0, 0)
}
const renderer = new THREE.WebGLRenderer({ });
if (options.ortho) renderer.setSize(256,256);
else renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio( window.devicePixelRatio );
let container = document.createElement('div');
document.body.appendChild(container);
scene.background = new THREE.Color(0x87ceeb);
const ambientLight = new THREE.AmbientLight(0xffffff, 1);
scene.add(ambientLight);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
window.addEventListener('resize', onWindowResize, false)
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
renderer.setSize(window.innerWidth, window.innerHeight)
}
if (!options.ortho) scene.add(new THREE.AxesHelper(10));
const onLoadComplete = (txt) => {
const planeGeometry = new THREE.PlaneGeometry(360, 360);
const planeMaterial = new THREE.MeshBasicMaterial({ map: txt });
const plane = new THREE.Mesh(planeGeometry, planeMaterial);
if (options.ortho) plane.rotateX(-Math.PI / 2);
scene.add(plane);
const size = 16;
const textureJoined = getTexture2DRandom(size * 3,...