Sorry I can’t make my project public due to safety concerns. I hope this app.js will give enough information:
import $ from 'jquery'; //jQuery
import 'webpack-jquery-ui';
import * as THREE from 'three'; //Three.js
import Controls from './controls'
import vertexShader from '../shaders/vertexShader'
import fragmentShader from '../shaders/fragmentShader'
import skyboxVertexShader from '../shaders/skyboxVertexShader'
import skyboxFragmentShader from '../shaders/skyboxFragmentShader'
import Gyroscope from './lib/Gyroscope'
import '../scss/main.scss'
import { networkManager } from './networkManager'
import metadrone1 from '../res/images/metadrone_screen_1.png'
import metadrone2 from '../res/images/metadrone_screen_2.png'
import metadrone3 from '../res/images/metadrone_screen_3.png'
import metadrone4 from '../res/images/metadrone_screen_4.png'
import maximova1 from '../res/images/maximova-1.png'
import maximova2 from '../res/images/maximova-2.png'
import maximova3 from '../res/images/maximova-3.png'
import proalerts1 from '../res/images/proalerts_1.png'
import metext from '../res/textures/me.png'
var scene, camera, controls, renderer, gyroscope, time = 0;
//start it up
initialiseScene();
setupScene();
//connect To the Server
networkManager.connectToServer();
//once connected setup player (camera) and start the loop
networkManager.localUserConnectedDisplayCallback = function(user){
initialiseLocalUser(user)
update();
}
//remote user connection callback
networkManager.userConnectedDisplayCallback = function(user){
//setup remote user locally
let metexture = new THREE.TextureLoader().load(metext)
let userMaterial = new THREE.MeshBasicMaterial({map: metexture, side: THREE.DoubleSide})
user.cube.material = userMaterial
user.eyes.material = userMaterial
scene.add(user.mesh)
$('#canvas2D').append("<h1 id="+ user.userID +" class='playerName'>[" + user.userName + "]</h1>")
}
//chat input listener
$('#console-input').on('keyup', function(e){
if (e.keyCode == 13) {
if (networkManager.this_user) {
var message = $('#console-input').val();
networkManager.addChatMessage(message);
$('#console-input').val("");
}
}
})
//network chat listener
networkManager.updatedChatMessagesDisplayCallback = function(message){
$('#console-messages').append("<br>" + message);
$('#console-messages')[0].scrollTop = $('#console-messages')[0].scrollHeight;
var html = $('#chat-inner-scroll').html()
html = html + "<br>" + message
$('#chat-inner-scroll').html(html)
$('#chat-inner-scroll')[0].scrollTop = $('#chat-inner-scroll')[0].scrollHeight;
}
//SETUP --- 3D SCENE
function initialiseCamera(ratio) {
camera = new THREE.PerspectiveCamera(70, ratio, 0.1, 4000 );
}
function initialiseScene() {
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xffffff );
let ratio = $('#canvas3D').innerWidth()/$('#canvas3D').innerHeight()
initialiseCamera(ratio)
renderer = new THREE.WebGLRenderer({canvas: document.getElementById('canvas3D'), antialias: true, alpha: false});
renderer.debug.checkShaderErrors = true;
resize();
}
function initialiseLocalUser(localUser) {
localUser.mesh.add(camera)
controls = new Controls(localUser.mesh)
controls.movementSpeed = 300;
localUser.controls = controls
scene.add(controls.getObject());
controls.yaw.translateX( (Math.random() > 0.5 ? -1 : 1) * (50 + (Math.random() * 50)));
controls.yaw.translateZ( (Math.random() > 0.5 ? -1 : 1) * (50 + (Math.random() * 50)));
controls.invertedYrot = controls.yaw.position.z < 0
controls.yaw.lookAt(new THREE.Vector3());
controls.yaw.rotateY(Math.PI)
setupGyroscope(localUser.mesh)
}
function setupGyroscope(mesh) {
gyroscope = new Gyroscope(mesh)
gyroscope.onOrientationChange()
$(window).on('deviceorientation', function(e) {
if(gyroscope){
gyroscope.onDeviceOrientation(e)
}
} );
$(window).on('orientationchange', function(e) {
if(gyroscope){
gyroscope.onOrientationChange(e)
}
});
if(window.screen.mozLockOrientation ) {
window.screen.mozLockOrientation( 'portrait-primary' );
window.screen.onorientationchange = function() {
window.screen.mozUnlockOrientation();
window.screen.mozLockOrientation( 'portrait-primary' );
};
document.addEventListener( 'visibilityChange', function(e) {
if( !document.hidden ) {
window.screen.mozUnlockOrientation();
window.screen.mozLockOrientation( 'portrait-primary' );
}
});
}
}
function setupSkyBox() {
var light = new THREE.DirectionalLight( 0xdef1fc, 1.0 );
light.position.x = 300;
light.position.y = 250;
light.position.z = - 500;
scene.add( light );
var skyGeo = new THREE.SphereBufferGeometry( 1000, 32, 15 );
var skyMat = new THREE.ShaderMaterial( {
uniforms: {
topColor: { value: light.color }, //0xfff4d9
bottomColor: { value: new THREE.Color( 0xffffff ) }, //0xfffBf0
offset: { value: 400 },
exponent: { value: 0.6 }
},
vertexShader: skyboxVertexShader,
fragmentShader: skyboxFragmentShader,
side: THREE.BackSide
});
var sky = new THREE.Mesh( skyGeo, skyMat );
scene.add( sky );
}
function addObstacle() {
let metexture = new THREE.TextureLoader().load(metext)
let centerGeom = new THREE.BoxGeometry( 50, 50, 50);
let centerMaterial = new THREE.MeshBasicMaterial({map: metexture, side: THREE.DoubleSide})
let center = new THREE.Mesh( centerGeom, centerMaterial );
center.translateX(0)
center.translateZ(0)
scene.add(center)
}
function setupScene() {
//setupSkyBox()
addObstacle()
var light = new THREE.AmbientLight( 0x66747c, 1.0 );
let color = new THREE.Color().setRGB(0.903,0.912,0.965)
var directionalLight = new THREE.DirectionalLight(color, 1.0 );
directionalLight.position.set(-10, 10, 80);
scene.add( directionalLight );
scene.add( light );
}
//UPDATE
let canvas = document.getElementById('canvas3D')
let clock = new THREE.Clock();
var previous = new THREE.Vector3();
function update() {
let delta = clock.getDelta()
time += delta
networkManager.time = time
//networkManager.updateUserNetworkOrientation()
// if(gyroscope) {
// gyroscope.update()
// }
// var moved = !previous.equals(controls.yaw.position)
// console.log(controls.didmove);
// //console.log(controls.yaw.position)
// $.each(networkManager.users, function(index, user){
// user.lerpOrientation(delta, camera, canvas, controls.didmove)
// if($("#"+user.userID).length) {
// if(user.nameVector !== null) {
// $("#"+user.userID).css({'left':user.nameVector.x + 'px',
// 'top':user.nameVector.y + 'px'})
// }
// }
// })
//if( !$('#console-input').is(':focus')){
controls.update(delta)
//}
// previous.copy(controls.yaw.position);
renderer.render(scene, camera)
requestAnimationFrame( update )
}
// ---- JQUERY STUFF ----
$('#console').resizable({ ghost: true, handles: 's'});
function resize() {
let width = window.innerWidth;
let height = window.innerHeight;
camera.aspect = width/height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
$("body, html").css({width:width, height:height})
calculateScrollArrows()
}
function calculateScrollArrows() {
var maxHeight = $("#menu-box-outer").innerHeight() - $("#menu-scroll").innerHeight()
var offset = $("#menu-scroll").offset().top - $("#menu-box-outer").offset().top
let isIos = navigator.userAgent.match("/(iPod|iPhone|iPad)/")
if($("#menu-box-outer").innerHeight() > $("#menu-scroll").innerHeight()){
if(offset == 0){
$('#menu-scroll-arrow-down').css("visibility", isIos ? "hidden" : "visible")
$('#menu-scroll-arrow-up').css("visibility", isIos ? "visible" : "hidden")
} else if(offset == maxHeight){
$('#menu-scroll-arrow-down').css("visibility", isIos ? "visible" : "hidden")
$('#menu-scroll-arrow-up').css("visibility", isIos ? "hidden" : "visible")
} else {
$('#menu-scroll-arrow-down').css("visibility","visible")
$('#menu-scroll-arrow-up').css("visibility","visible")
}
} else {
$('#menu-scroll-arrow-down').css("visibility","hidden")
$('#menu-scroll-arrow-up').css("visibility","hidden")
}
}
$("#menu-scroll").scroll( function(){
calculateScrollArrows()
})
$(window).on('resize', function() {
resize();
})
$(window).on('unload', function() {
networkManager.removeUser()
scene.remove(networkManager.this_user.mesh)
})
$(window).on('beforeunload', function() {
networkManager.removeUser()
scene.remove(networkManager.this_user.mesh)
})
$(window).on('pagehide', function() {
networkManager.removeUser()
scene.remove(networkManager.this_user.mesh)
})
$(window).on('load', function() {
resize();
$('#metadrone-1').attr('src', metadrone1)
$('#metadrone-2').attr('src', metadrone2)
$('#metadrone-3').attr('src', metadrone3)
$('#metadrone-4').attr('src', metadrone4)
$('#maximova-1').attr('src', maximova1)
$('#maximova-2').attr('src', maximova2)
$('#maximova-3').attr('src', maximova3)
$('#pro-alerts-1').attr('src', proalerts1)
})
$('#grid').on('click', function(){
$('#content-container').css({"visibility":"hidden"})
})
$('.menu-item h1').on('click', function(e){
let idx = $(this).parent().index()
scrollToContentItem(idx)
e.stopPropagation();
})
$('#about-button').on('click', function(e){
let idx = $('#content').children().length - 1
scrollToContentItem(idx)
e.stopPropagation();
})
function scrollToContentItem(idx) {
let offset = ($('.content-item').eq(idx).offset().top - $('#content').offset().top) + 1
$('#content-container').css({"visibility":"visible"})
$('#content-scroll').animate({
scrollTop: offset
}, 10, function() {
$('#content-container').css({"visibility":"visible"})
});
}
$('#close-button').on('click', function(){
console.log("yoooo clicked")
$('#content-container').css({"visibility":"hidden"})
})
$('#cheats').on('click', function(){
alert("type '#chname ...' to change your name\ntype '#rain' to make it rain");
})