JSFiddle - React, Tailwind, and code Playground
by Gwyn Milcote
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/89/three.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/123879/OrbitControls.js"></script>
JavaScript
/**
* @file
* The main scene.
*/
/**
* Set our global variables.
*/
var camera, // We need a camera.
scene, // The camera has to see something.
renderer, // Render our graphics.
controls, // Our Orbit Controller for camera magic.
container, // Our HTML container for the program.
rotationPoint; // The point in which our camera will rotate around.
var characterSize = 30;
//var outlineSize = characterSize * 0.05;
// Track all objects and collisions.
var objects = [];
var collisions = [];
// Set mouse and raycaster.
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
// Store movements.
var movements = [];
var playerSpeed = 5;
// Watch for double clicks.
var clickTimer = null;
// The movement destination indicator.
var indicatorTop;
var indicatorBottom;
/**
* Run initial setup function and loop through rendering.
*/
init();
animate();
/**
* Initializer function.
*/
function init() {
// Build the container
container = document.createElement( 'div' );
document.body.appendChild( container );
// Create the scene.
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xccddff );
scene.fog = new THREE.Fog( 0xccddff, 500, 2000 );
// Ambient lights.
var ambient = new THREE.AmbientLight( 0xffffff );
scene.add( ambient );
// Add hemisphere lighting.
var hemisphereLight = new THREE.HemisphereLight( 0xdddddd, 0x000000, 0.5 );
scene.add( hemisphereLight );
// Create a rotation point.
rotationPoint = new THREE.Object3D();
rotationPoint.position.set( 0, 0, 0 );
scene.add( rotationPoint );
createCharacter();
createFloor();
createTree(300, 300);
createTree(800, -300);
createTree(-300, 800);
createTree(-800, -800);
// Create the camera.
camera = new THREE.PerspectiveCamera(
50, // Angle
window.innerWidth / window.innerHeight, // Aspect Ratio.
1, // Near view.
20000 // Far view.
);
camera.position.z = -300;
camera.position.y =...