-
Notifications
You must be signed in to change notification settings - Fork 0
/
GPT_Scene.js
148 lines (122 loc) · 5.01 KB
/
GPT_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* @module libgptjs Graphical Programming with ThreeJS (GPT)
* @class GPT_Scene
* @summary
* Groups all the functions needed to put the objects (geometry, lights, cameras, etc) in the scene.
*/
/**
* Importing object THREE from our costumized global script
*/
import THREE from '../external-libs/three-global'
/**
* Creates our GPT_Scene object containing initially an empty map of GPT_Model, an empty map of GPT_Lights and a THREE.Scene.
* We will use th arrays to update the objects using widgets (we need to keep the references to them).
*
* Using a map it preserves the original insertion order, has merging functionalities and better performance
* when adding/removing operations are performed frequently
*/
function GPT_Scene() {
// maps can have any object type as key and can be iterated in order of insertion
this.gpt_models = new Map();
this.gpt_lights = new Map();
this.scene = new THREE.Scene();
}
/**
* Override this method for creating models with their corresponding meshes, and their initial positions in the scene.
* Then add each model in the models array
*/
GPT_Scene.prototype.createObjects = function () {
console.error("GPT_Scene.createObjects: Not implemented");
}
/**
* Override this method for transforming (translate, rotate) the objects in the scene
* @param {Number} ms time in milliseconds passed since last frame
*/
GPT_Scene.prototype.updateObjects = function (ms) {
console.error("GPT_Scene.updateObjects: Not implemented");
}
/**
* Override this method for creating lights with their corresponding source type (point, directional, etc) and their correspoding initial positions
*/
GPT_Scene.prototype.createLights = function () {
console.error("GPT_Scene.createLights: Not implemented");
}
/**
* Override this method for transforming (translate, rotate) the lights in the scene
* @param {Number} ms time in milliseconds passed since last frame
*/
GPT_Scene.prototype.updateLights = function (ms) {
console.error("GPT_Scene.updateLights: Not implemented");
}
/**
* Invokes the methods for creating the scene, adds a THREE.Scene and all the Object3D/Mesh from the models array (same with the lights array)
* THREE.Scene can add objects of type THREE.Mesh or THREE.Object3D (grouping object. Ex: robot = base + arm)
* The same objects will be updated (animated) in renderer.update() method
*/
GPT_Scene.prototype.setupScene = function () {
this.createObjects();
for (let [key, value] of this.gpt_models) {
// IMPORTANT: give a name so later it can be removed from THREE.Scene
value.name = key;
this.scene.add(value);
console.debug("GPT_Scene: added " + key);
}
console.debug("GPT_Scene: total models: " + this.gpt_models.size);
this.createLights();
for (let [key, value] of this.gpt_lights) {
// IMPORTANT: give a name so later it can be removed from THREE.Scene
value.name = key;
this.scene.add(value);
console.debug("GPT_Scene: added " + key);
}
console.debug("GTP_Scene: total lights: " + this.gpt_lights.size);
}
GPT_Scene.prototype.updateScene = function (ms) {
this.updateObjects(ms);
this.updateLights(ms);
}
/**
* Removes object Model from THREE.Scene at runtime
* Also removes object from 'gpt_models'
* @param {String} object_name_
*/
GPT_Scene.prototype.removeModelFromScene = function (object_name_) {
// remove from THREE.Scene
const selectedObject = this.scene.getObjectByName(object_name_);
if (selectedObject === undefined) {
console.error("GPT_Scene.removeModelFromScene: could not remove '" + object_name_ + "'. Object undefined in THREE.Scene");
return;
}
this.scene.remove(selectedObject);
if (this.scene.getObjectByName(object_name_) !== undefined) {
console.error("GPT_Scene.removeModelFromScene: could not remove '" + object_name_ + "'. Object still in THREE.Scene");
return;
}
// remove from gpt_models
this.gpt_models.delete(object_name_);
if (this.gpt_models.get(object_name_) !== undefined) {
console.error("GPT_Scene.removeModelFromScene: could not remove '" + object_name_ + "'. Object stil in gpt_models");
return;
}
console.debug("GPT_Scene: removed '" + object_name_ + "'. Total models: " + this.gpt_models.size);
}
/**
* Adds objec Model to THREE.Scene at runtime
* Also adds object to 'gpt_models'
* @param {String} obj_name_ model name
* @param {THREE.Mesh} obj_mesh_ mesh
*/
GPT_Scene.prototype.AddModelToScene = function (obj_name_, obj_mesh_) {
this.gpt_models.set(obj_name_, obj_mesh_);
// set name and id to be able to delete from scene later
let _o = this.gpt_models.get(obj_name_);
_o.name = obj_name_;
// add to THREE.Scene
this.scene.add(_o);
if (this.gpt_models.get(obj_name_) === undefined) {
console.error("GPT_Scene.AddModelToScene: could not add '" + obj_name_ + "'");
return;
}
console.debug("GPT_Scene: added '" + obj_name_ + "'. Total models: " + this.gpt_models.size);
}
export default GPT_Scene;