-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
071641c
commit 10ac5c0
Showing
4 changed files
with
1,094 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import * as THREE from 'https://unpkg.com/[email protected]/build/three.module.js'; | ||
import { OrbitControls } from 'https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js'; | ||
|
||
// (function(){var script=document.createElement('script');script.onload=function(){var stats=new Stats();document.body.appendChild(stats.dom);requestAnimationFrame(function loop(){stats.update();requestAnimationFrame(loop)});};script.src='//mrdoob.github.io/stats.js/build/stats.min.js';document.head.appendChild(script);})() | ||
const socket = io(); | ||
|
||
|
@@ -12,7 +13,6 @@ socket.on("newCube", username => { | |
}); | ||
|
||
socket.on("positions",data=>{ | ||
console.log(data); | ||
Object.keys(data).forEach(function(key){ | ||
cubes[data[key].username] = new Cube(data[key].x,data[key].y,data[key].z); | ||
}); | ||
|
@@ -26,49 +26,12 @@ socket.on("username",data=>{ | |
username = data; | ||
}); | ||
|
||
var scene,renderer,camera,controls; | ||
var scene,renderer,camera,controls,thirdPersonCamera,userCube; | ||
var keyboard = {}; | ||
var speed = 0.3; | ||
var cubeMaterial = new THREE.MeshBasicMaterial({wireframe:true}); | ||
var cubeGeometry = new THREE.BoxGeometry(10,10,10,3,3,3); | ||
|
||
|
||
function init(){ | ||
var WIDTH = window.innerWidth,HEIGHT = window.innerHeight; | ||
|
||
scene = new THREE.Scene(); | ||
|
||
renderer = new THREE.WebGLRenderer({antialias:true}); | ||
renderer.setSize(WIDTH,HEIGHT); | ||
renderer.setClearColor(0x333F47,1); | ||
document.body.appendChild(renderer.domElement); | ||
|
||
camera = new THREE.PerspectiveCamera(45,WIDTH/HEIGHT,1,10000); | ||
camera.position.set(0,20,100); | ||
camera.lookAt(0,0,0); | ||
scene.add(camera); | ||
|
||
var material = new THREE.MeshBasicMaterial({wireframe:true,color:"black"}); | ||
|
||
var floorGeometry = new THREE.PlaneGeometry(200,200,20,20); | ||
var floor = new THREE.Mesh(floorGeometry,material); | ||
floor.rotation.x = Math.PI/2; | ||
scene.add(floor); | ||
|
||
controls = new OrbitControls(camera,renderer.domElement); | ||
controls.minDistance = 50; | ||
controls.maxDistance = 275; | ||
animate(); | ||
} | ||
|
||
function animate(){ | ||
requestAnimationFrame(animate); | ||
controls.update(); | ||
renderer.render(scene,camera); | ||
} | ||
|
||
init(); | ||
|
||
class Cube{ | ||
constructor(positionX,positionY,positionZ){ | ||
this.positionX = positionX; | ||
|
@@ -95,20 +58,13 @@ class Cube{ | |
this.cube.position.x += speed; | ||
break; | ||
} | ||
|
||
// for (var vertexIndex = 0; vertexIndex < this.cube.geometry.vertices.length; vertexIndex++){ | ||
// var localVertex = this.cube.geometry.vertices[vertexIndex].clone(); | ||
// var globalVertex = this.cube.matrix.multiplyVector3(localVertex); | ||
// var directionVector = globalVertex.subSelf(this.cube.Player.position); | ||
// var ray = new THREE.Ray(this.cube.Player.position,directionVector.clone().normalize()); | ||
// var collisionResults = ray.intersectObjects( collidableMeshList ); | ||
// if ( collisionResults.length > 0 && collisionResults[0].distance < directionVector.length()){ | ||
// console.log("collision!!"); | ||
// } | ||
// } | ||
} | ||
|
||
setPosition(positionX,positionY,positionZ){ | ||
this.idealPosition = new THREE.Vector3(positionX,positionY,positionZ); | ||
this.currentPosition = new THREE.Vector3(); | ||
this.currentPosition.lerp(this.idealPosition,0.2); | ||
this.cube.position.copy(this.currentPosition); | ||
this.cube.position.x = positionX; | ||
this.cube.position.y = positionY; | ||
this.cube.position.z = positionZ; | ||
|
@@ -121,9 +77,87 @@ class Cube{ | |
remove(){ | ||
scene.remove(this.cube); | ||
} | ||
|
||
get Position() { | ||
return this.cube.position; | ||
} | ||
|
||
get Rotation() { | ||
return this.cube.quaternion; | ||
} | ||
} | ||
|
||
// class ThirdPersonCamera { | ||
// constructor(params) { | ||
// this._params = params; | ||
// this._camera = params.camera; | ||
|
||
// this._currentPosition = new THREE.Vector3(); | ||
// this._currentLookat = new THREE.Vector3(); | ||
// } | ||
|
||
// _CalculateIdealOffset() { | ||
// const idealOffset = new THREE.Vector3(0, 15,40); | ||
// idealOffset.applyQuaternion(this._params.target.Rotation); | ||
// idealOffset.add(this._params.target.Position); | ||
// return idealOffset; | ||
// } | ||
|
||
// _CalculateIdealLookat() { | ||
// const idealLookat = new THREE.Vector3(0, 0, 0); | ||
// idealLookat.applyQuaternion(this._params.target.Rotation); | ||
// idealLookat.add(this._params.target.Position); | ||
// return idealLookat; | ||
// } | ||
|
||
// Update(timeElapsed) { | ||
// const idealOffset = this._CalculateIdealOffset(); | ||
// const idealLookat = this._CalculateIdealLookat(); | ||
// const t = 1.0 - Math.pow(0.001, timeElapsed); | ||
|
||
// this._currentPosition.lerp(idealOffset, t); | ||
// this._currentLookat.lerp(idealLookat, t); | ||
|
||
// this._camera.position.copy(this._currentPosition); | ||
// this._camera.lookAt(this._currentLookat); | ||
// } | ||
// } | ||
|
||
|
||
function init(){ | ||
var WIDTH = window.innerWidth,HEIGHT = window.innerHeight; | ||
|
||
scene = new THREE.Scene(); | ||
|
||
renderer = new THREE.WebGLRenderer({antialias:true}); | ||
renderer.setSize(WIDTH,HEIGHT); | ||
renderer.setClearColor(0x333F47,1); | ||
document.body.appendChild(renderer.domElement); | ||
|
||
camera = new THREE.PerspectiveCamera(45,WIDTH/HEIGHT,1,10000); | ||
camera.position.set(0,15,60); | ||
camera.lookAt(0,0,0); | ||
scene.add(camera); | ||
|
||
var material = new THREE.MeshBasicMaterial({wireframe:true,color:"black"}); | ||
|
||
var floorGeometry = new THREE.PlaneGeometry(200,200,20,20); | ||
var floor = new THREE.Mesh(floorGeometry,material); | ||
floor.rotation.x = Math.PI/2; | ||
scene.add(floor); | ||
|
||
controls = new OrbitControls(camera,renderer.domElement); | ||
controls.minDistance = 10; | ||
controls.maxDistance = 275; | ||
|
||
userCube = new Cube(0,5.5,0); | ||
// thirdPersonCamera = new ThirdPersonCamera({camera:camera,target:userCube}); | ||
|
||
|
||
animate(); | ||
} | ||
|
||
var userCube = new Cube(0,5.5,0); | ||
init(); | ||
|
||
var dirty = true; | ||
|
||
|
@@ -173,6 +207,19 @@ socket.on("disconnected",data=>{ | |
cubes[data].remove(); | ||
}); | ||
|
||
var previousT; | ||
|
||
function animate(){ | ||
controls.update(); | ||
if (previousT === null) { | ||
previousT = t; | ||
} | ||
// thirdPersonCamera.Update(0.2); | ||
renderer.render(scene,camera); | ||
requestAnimationFrame(animate); | ||
} | ||
|
||
|
||
//chat | ||
document.querySelector(".chatbox input").addEventListener("focus",function(eve){ | ||
window.removeEventListener('keydown',keyDown); | ||
|
@@ -212,11 +259,8 @@ socket.on("message",data=>{ | |
document.querySelector(".messages").scrollTop = document.querySelector(".messages").scrollHeight; | ||
}); | ||
|
||
|
||
|
||
|
||
document.addEventListener('visibilitychange', function(){ | ||
if(document.visibilityState=="hidden"){ | ||
keyboard = {}; | ||
} | ||
}); | ||
}); |