JSFiddle - React, Tailwind, and code Playground
HTML
<!-- THREE Latest: -->
<script src="https://threejs.org/build/three.min.js"></script>
<!-- THREE r128: -->
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script> -->
<script src="https://threejs.org/examples/js/loaders/GLTFLoader.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<input type="file" name="file-input" id="file-input">
CSS
body {
background-color: #000;
margin: 0px;
overflow: hidden;
}
JavaScript
// Simple three.js example
var mesh, renderer, scene, camera, controls;
const input = document.getElementById('file-input');
input.onchange = function (event) {
const loadedFile = event.target.files[0];
const reader = new FileReader();
if (loadedFile) {
reader.readAsDataURL(loadedFile);
}
reader.onload = function (file) {
console.log('Loading...');
loadModel(reader.result);
};
};
init();
animate();
function init() {
// renderer
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0xCCCCCC, 1.0);
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// scene
scene = new THREE.Scene();
// camera
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 20, 20, 20 );
// controls
controls = new THREE.OrbitControls( camera, renderer.domElement );
// ambient
scene.add( new THREE.AmbientLight( 0xffffff ) );
// light
var light = new THREE.DirectionalLight( 0xffffff, 2 );
light.position.set( 20, 20, 0 );
scene.add( light );
}
function loadModel(file) {
const loader = new THREE.GLTFLoader();
loader.load(file, function (asset) {
console.log('Loaded!');
asset.scene.scale.set( 20, 20, 20 );
asset.scene.position.y = -20;
scene.add(asset.scene);
}, undefined, function (error) {
console.log(error);
});
}
function animate() {
requestAnimationFrame( animate );
//controls.update();
renderer.render( scene, camera );
}