-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.js
122 lines (104 loc) · 4.72 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
/* Scena, nacita a zobrazi svet a robota
robot -
world -
startFunction - funkcia, ktora sa zavola po nacitani vsetkeho */
function Scene(robot, world, startFunction) {
this.robot = robot;
this.world = world;
this.startFunction = startFunction;
this.scene = new THREE.Scene();
this.skyBoxScene = new THREE.Scene();
//Kamera
this.camera = new THREE.PerspectiveCamera( 45, $('#world-container').width() / $('#world-container').height(), 0.1, 1000 );
this.camera.position.set(0,5,5);
this.camera.lookAt(new THREE.Vector3(0,0,0));
this.scene.add(this.camera);
//svetla
var ambientLight = new THREE.AmbientLight(0x404040); // soft white light
this.scene.add(ambientLight);
this.topLight = new THREE.PointLight(0xFFFFFF);
this.topLight.position.set((world.x+1)/2, 100, 20, -(world.y+1)/2);
this.scene.add(this.topLight);
var loader = new THREE.JSONLoader();
this.brick={loaded:false};
this.ground={loaded:false};
this.robot.loaded=false;
loader.load('models/brick.json', function ( geometry, materials ) {
this.brick.geometry = geometry;
this.brick.materials = materials;
this.brick.loaded=true;
this.onFinishLoading();
}.bind(this));
loader.load('models/ground.json', function ( geometry, materials ) {
this.ground.geometry = geometry;
this.ground.materials = materials;
this.ground.loaded=true;
this.onFinishLoading();
}.bind(this));
loader.load('models/robot.json', function ( geometry, materials ) {
this.robot.geometry = geometry;
this.robot.materials = materials;
this.robot.createMesh(this.scene);
this.robot.loaded=true;
this.onFinishLoading();
}.bind(this));
//nacita skybox, teraz viacmenej zbytocne, ale funguje pekne pri otacani kamery
var urls = [ 'skybox/arrakisday_ft.jpg', 'skybox/arrakisday_bk.jpg',
'skybox/arrakisday_up.jpg', 'skybox/arrakisday_dn.jpg',
'skybox/arrakisday_rt.jpg', 'skybox/arrakisday_lf.jpg'
];
var textureCube = new THREE.CubeTextureLoader().load( urls, function(texture) {
shader = THREE.ShaderLib[ "cube" ];
shader.uniforms[ "tCube" ].value = texture;
var shaderMaterial = new THREE.ShaderMaterial( {
fragmentShader: shader.fragmentShader,
vertexShader: shader.vertexShader,
uniforms: shader.uniforms,
depthWrite: false,
side: THREE.BackSide
});
this.skyBox = new THREE.Mesh( new THREE.BoxGeometry( 50, 50, 50 ), shaderMaterial );
this.skyBoxScene.add(this.skyBox);
this.camera.add(this.skyBox);
this.skyBox.position.y=-8;
}.bind(this));
}
/* Zavola sa, ked sa nieco nacitalo. Overi, ci je vsetko nacitane a ak ano, spravi potrebne veci */
Scene.prototype.onFinishLoading = function() {
if (this.brick.loaded && this.ground.loaded && this.robot.loaded) {
this.container = document.getElementById('world-container');
this.stats = new Stats();
this.container.appendChild(this.stats.domElement);
this.renderer = new THREE.WebGLRenderer({ antialias: true });
this.renderer.setClearColor( 0xffffff );
this.renderer.autoClear = false;
this.renderer.shadowMap.enabled=true;
this.renderer.setSize($('#world-container').width(), $('#world-container').height());
this.container.appendChild(this.renderer.domElement);
window.addEventListener( 'resize', this.onWindowResize.bind(this), false );
this.world.setScene(this);
this.updateCamera();
this.startFunction();
}
}
/* Nastala zmena velkosti okna */
Scene.prototype.onWindowResize = function() {
this.camera.aspect = $('#world-container').width() / $('#world-container').height();
this.camera.updateProjectionMatrix();
this.renderer.setSize($('#world-container').width(), $('#world-container').height());
}
/* Update kamery, nieco sa pohlo (mozno pouzit follow ako pri skyboxe?) */
Scene.prototype.updateCamera = function() {
if (this.robot.loaded) {
this.camera.position.set(robot.mesh.position.x,robot.mesh.position.y+3, robot.mesh.position.z+5);
this.camera.lookAt(new THREE.Vector3(robot.mesh.position.x,robot.mesh.position.y+1, robot.mesh.position.z));
}
}
/* Hlavny render */
Scene.prototype.render = function() {
this.renderer.clear();
this.renderer.render( this.skyBoxScene, this.camera );
this.renderer.clearDepth(); //vykreslujeme vsetko pred skyboxom
this.renderer.render( this.scene, this.camera );
this.stats.update();
}