Minequack/src/app/render.js
2021-11-01 12:08:11 +02:00

304 lines
11 KiB
JavaScript

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
const gameStats = {
fps: 0,
playerName: 'TheRed'
}
function existCheck(id, metadata) {
let check = require('fs').existsSync(__dirname+'/app/resources/'+id+'/textures.json');
if(check) {
file = require(__dirname+'/app/resources/'+id+'/textures.json');
console.log(file);
if(file[metadata] == undefined) {
return false;
} else {
if(!require('fs').existsSync(__dirname+'/app/resources/'+id+'/'+file[metadata].f)) {
console.log('1')
return false;
} else if(!require('fs').existsSync(__dirname+'/app/resources/'+id+'/'+file[metadata].b)) {
console.log('2')
return false;
} else if(!require('fs').existsSync(__dirname+'/app/resources/'+id+'/'+file[metadata].l)) {
console.log('3')
return false;
} else if(!require('fs').existsSync(__dirname+'/app/resources/'+id+'/'+file[metadata].r)) {
console.log('4')
return false;
} else if(!require('fs').existsSync(__dirname+'/app/resources/'+id+'/'+file[metadata].u)) {
console.log('5')
return false;
} else if(!require('fs').existsSync(__dirname+'/app/resources/'+id+'/'+file[metadata].d)) {
console.log('6')
return false;
} else {
console.log('7')
return true;
}
}
} else {
return false;
};
};
/**
* @description AssembledCube creates a cube using 6 planes, which makes it easier to optimize (also applies a texture and cube size, position)
* @example new AssembledCube('stone', theTexture, 1, [0,0,0]);
*/
class AssembledCube {
constructor(id, texture, size, position) {
let x = size;
let y = size;
this.id = id;
this.texture = texture;
this.position = position;
let geometry1 = new THREE.PlaneGeometry( x, y );
let texture1 = new THREE.TextureLoader().load(__dirname+'/app/resources/'+this.id+'/'+this.texture.f);
texture1.magFilter = THREE.NearestFilter; // Makes so the texture isnt blurry, instead it becomes pixely (kinda like minecraft)
let material1 = new THREE.MeshBasicMaterial( {map: texture1, side: THREE.BackSide} );
let plane1 = new THREE.Mesh( geometry1, material1 );
let geometry2 = new THREE.PlaneGeometry( x, y );
let texture2 = new THREE.TextureLoader().load(__dirname+'/app/resources/'+this.id+'/'+this.texture.b);
texture2.magFilter = THREE.NearestFilter; // Makes so the texture isnt blurry, instead it becomes pixely (kinda like minecraft)
let material2 = new THREE.MeshBasicMaterial( {map: texture2, side: THREE.BackSide} );
let plane2 = new THREE.Mesh( geometry2, material2 );
let geometry3 = new THREE.PlaneGeometry( x, y );
let texture3 = new THREE.TextureLoader().load(__dirname+'/app/resources/'+this.id+'/'+this.texture.l);
texture3.magFilter = THREE.NearestFilter; // Makes so the texture isnt blurry, instead it becomes pixely (kinda like minecraft)
let material3 = new THREE.MeshBasicMaterial( {map: texture3, side: THREE.BackSide} );
let plane3 = new THREE.Mesh( geometry3, material3 );
let geometry4 = new THREE.PlaneGeometry( x, y );
let texture4 = new THREE.TextureLoader().load(__dirname+'/app/resources/'+this.id+'/'+this.texture.r);
texture4.magFilter = THREE.NearestFilter; // Makes so the texture isnt blurry, instead it becomes pixely (kinda like minecraft)
let material4 = new THREE.MeshBasicMaterial( {map: texture4, side: THREE.BackSide} );
let plane4 = new THREE.Mesh( geometry4, material4 );
let geometry5 = new THREE.PlaneGeometry( x, y );
let texture5 = new THREE.TextureLoader().load(__dirname+'/app/resources/'+this.id+'/'+this.texture.u);
texture5.magFilter = THREE.NearestFilter; // Makes so the texture isnt blurry, instead it becomes pixely (kinda like minecraft)
let material5 = new THREE.MeshBasicMaterial( {map: texture5, side: THREE.BackSide} );
let plane5 = new THREE.Mesh( geometry5, material5 );
let geometry6 = new THREE.PlaneGeometry( x, y );
let texture6 = new THREE.TextureLoader().load(__dirname+'/app/resources/'+this.id+'/'+this.texture.d);
texture6.magFilter = THREE.NearestFilter; // Makes so the texture isnt blurry, instead it becomes pixely (kinda like minecraft)
let material6 = new THREE.MeshBasicMaterial( {map: texture6, side: THREE.BackSide} );
let plane6 = new THREE.Mesh( geometry6, material6 );
plane1.position.x = this.position[0];
plane1.position.y = this.position[1];
plane1.position.z = this.position[2]+x/2;
plane2.position.x = this.position[0];
plane2.position.y = this.position[1];
plane2.position.z = this.position[2]-x/2;
plane3.position.x = this.position[0]+x/2;
plane3.position.y = this.position[1];
plane3.position.z = this.position[2];
plane4.position.x = this.position[0]-x/2;
plane4.position.y = this.position[1];
plane4.position.z = this.position[2];
plane5.position.x = this.position[0];
plane5.position.y = this.position[1]+x/2;
plane5.position.z = this.position[2];
plane6.position.x = this.position[0];
plane6.position.y = this.position[1]-x/2;
plane6.position.z = this.position[2];
plane1.rotation.y = 0*(Math.PI/180)
plane2.rotation.y = 180*(Math.PI/180)
plane3.rotation.y = 90*(Math.PI/180)
plane4.rotation.y = -90*(Math.PI/180)
plane5.rotation.x = -90*(Math.PI/180)
plane6.rotation.x = 90*(Math.PI/180)
this.plane1 = plane1;
this.plane2 = plane2;
this.plane3 = plane3;
this.plane4 = plane4;
this.plane5 = plane5;
this.plane6 = plane6;
}
return() {
return [this.plane1, this.plane2, this.plane3, this.plane4, this.plane5, this.plane6];
}
update(positiona) {
this.plane1.position.x = positiona[0];
this.plane1.position.y = positiona[1];
this.plane1.position.z = positiona[2]+this.size/2;
this.plane2.position.x = positiona[0];
this.plane2.position.y = positiona[1];
this.plane2.position.z = positiona[2]-this.size/2;
this.plane3.position.x = positiona[0]+this.size/2;
this.plane3.position.y = positiona[1];
this.plane3.position.z = positiona[2];
this.plane4.position.x = positiona[0]-this.size/2;
this.plane4.position.y = positiona[1];
this.plane4.position.z = positiona[2];
this.plane5.position.x = positiona[0];
this.plane5.position.y = positiona[1]+this.size/2;
this.plane5.position.z = positiona[2];
this.plane6.position.x = positiona[0];
this.plane6.position.y = positiona[1]-this.size/2;
this.plane6.position.z = positiona[2];
}
}
/**
* @description This class is used for creating players
* @example new Player("TheBlueBurger", [0, 0, 0], [0, 0])
*/
class Player {
constructor(playername, position, rotation) {
this.name = playername;
this.position = position;
this.rotation = rotation;
}
update(positiona, rotationa) {
this.position[0] = positiona[0];
this.position[1] = positiona[1];
this.position[2] = positiona[2];
this.rotation[2] = 0;
this.rotation[0] = rotationa[1];
this.rotation[1] = rotationa[0];
}
}
class World {
constructor() {
this.constants = {
g: 9.81, // meters per second squared
bs: 1 // meters
}
}
}
class Block {
constructor(position, id, metadata) {
this.position = position;
this.id = id;
this.metadata = metadata;
this.texture = getTexture(this.id, this.metadata)
}
return() {
}
}
class Skybox {
constructor(x) {
this.position = [0, 0, -x/2]; // sets the default skybox position
this.id = 'stone'; // texture id
this.metadata = 0; // metadata
this.size = x; // the size of skybox
this.texture = getTexture(this.id, this.metadata) // get the texture using id and metadata
this.cube = new AssembledCube(this.texture.id, this.texture, this.size, this.position); // create a cube (6 seperate planes instead of a whole cube so easier to optimize)
} // line 50
update(positiona) {
this.cube.update(positiona)
}
return() {
console.log(this.texture);
return this.cube; // returns our cube
}
}
let player = new Player('TheRedPotato', [0, 1, 0], [0, 0])
let i = 0;
let cube;
// setInterval(() => {i++},1000)
// setTimeout(() => {
// window.open("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
// }, 50000)
function getTexture(id, metadata) {
let check = !existCheck(id, metadata);
console.log(check);
if(check) {
let texture = require(__dirname+'/app/resources/undefined/textures.json')[0];
texture.id = 'undefined';
return texture;
} else {
let texture = require(__dirname+'/app/resources/'+id+'/textures.json')[metadata];
texture.id = id;
return texture;
}
};
function genBlockArray(x, z, position) {
for(i = 0;i<z;i++) { // leave it alone, aint used for now but its just something i keep so that i can have a refference point later when i do generation
for(i2 = 0; i2<x; i2++) {
pos = [position.x+i2, position.y, position.z+i];
scene.add(new Block(pos, 'stone', 0).return());
}
}
}
function render() {
requestAnimationFrame( render );
// gameStats.fps = performance.now();
// console.log(gameStats.fps/i);
cube.rotation.y += 0.01;
cube.rotation.x += 0.0101;
// player.update(player.position, [player.rotation[0], player.rotation[1]])
camera.position.x = player.position[0]
camera.position.y = player.position[1]
camera.position.z = player.position[2]
// camera.rotation.x = player.rotation[2]
camera.rotation.y = player.rotation[1]
camera.rotation.z = player.rotation[0]
skybox.update(player.position);
renderer.render(scene, camera)
}
window.addEventListener( 'resize', onWindowResize, false );
function onWindowResize(){
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function setup() {
document.body.appendChild( renderer.domElement );
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
const material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
cube = new THREE.Mesh( geometry, material );
scene.add( cube );
cube.position.z = -10;
scene.add(skyboxobj.plane1)
scene.add(skyboxobj.plane2)
scene.add(skyboxobj.plane3)
scene.add(skyboxobj.plane4)
scene.add(skyboxobj.plane5)
scene.add(skyboxobj.plane6)
render();
}
let skybox = new Skybox(300);
let skyboxobj = new Skybox(300).return();
setup();
// can i test something? sure