-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.js
126 lines (107 loc) · 4.25 KB
/
scene.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const THREE = require('three');
const OrbitControls = require('three-orbitcontrols');
const Physijs = require('physijs-webpack/browserify');
class Scene {
constructor(param = {}) {
//===============
//---Parameter---
//===============
var defaultParam = {
camera: {
depth: 64,
near: -50,
far: 1000,
position: new THREE.Vector3(2, 2, 2),
lookAt: [0, 0, 0],
},
numPlayer: 1,
maxPlayer: 1
};
this.params = Object.assign(defaultParam, param);
//===============
//-----Scene-----
//===============
// this.scene = new THREE.Scene();
// Physijs scene
this.scene = new Physijs.Scene();
this.scene.setGravity(new THREE.Vector3(0, -45, 0));
addEventListener(
'update',
function(){scene.simulate();}
);
//==================
//-----Renderer-----
//==================
var canvas = document.querySelector('#c');
canvas.style.position = 'absolute';
canvas.style.left = `${ (window.innerWidth / this.params.maxPlayer) * (this.params.numPlayer - 1)}px`;
this.renderer = new THREE.WebGLRenderer({canvas, antialias: true, alpha:true});
this.renderer.setSize(window.innerWidth / this.params.maxPlayer, window.innerHeight);
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.shadowMap.enabled = true;
//================
//-----Camera-----
//================
const cameraConfig = this.params.camera;
const depth = cameraConfig.depth;
const frustumSize = 64;
// this.camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );
// this.camera.position.y = 1;
// this.camera.position.z = 5;
this.camera = new THREE.OrthographicCamera(depth / -2, depth / 2, depth / 2, depth / -2, cameraConfig.near, cameraConfig.far);
this.camera.position.copy(cameraConfig.position);
this.camera.lookAt(new THREE.Vector3().fromArray(cameraConfig.lookAt));
this.camera.left = window.innerWidth / (- frustumSize * this.params.maxPlayer);
this.camera.right = window.innerWidth / (frustumSize * this.params.maxPlayer);
this.camera.top = window.innerHeight / frustumSize;
this.camera.bottom = window.innerHeight / - frustumSize;
this.camera.updateProjectionMatrix();
// var cameraHelper = new THREE.CameraHelper(this.camera);
// this.add(cameraHelper);
// this.controls = new OrbitControls(this.camera, this.renderer.domElement);
//===============
//-----Light-----
//===============
var spotLight = new THREE.SpotLight( 0xffffff );
spotLight.position.set( 8, 10, 10 );
this.scene.add( spotLight );
var spotLight1 = new THREE.SpotLight( 0x444444 );
spotLight1.position.set( -8, -10, -10 );
this.scene.add( spotLight1 );
// var spotLightHelper = new THREE.SpotLightHelper( spotLight );
// this.scene.add( spotLightHelper );
addEventListener('resize', () => this.onWindowResize());
}
/**
*
* @param {THREE.Object3D} object
*/
add(object) {
this.scene.add(object);
}
/**
*
* @param {String} object Name dari THREE.Object3D
*/
remove(objectName) {
var selectedObject = this.scene.getObjectByName(objectName);
this.scene.remove(selectedObject);
}
render() {
this.renderer.render(this.scene, this.camera);
}
simulate() {
this.scene.simulate();
}
onWindowResize() {
const frustumSize = 64;
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.setPixelRatio(window.devicePixelRatio);
this.camera.left = window.innerWidth / - frustumSize;
this.camera.right = window.innerWidth / frustumSize;
this.camera.top = window.innerHeight / frustumSize;
this.camera.bottom = window.innerHeight / - frustumSize;
this.camera.updateProjectionMatrix();
}
}
module.exports = Scene;