aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/MeshManager.js
diff options
context:
space:
mode:
authorPierre Frisch2011-12-22 07:25:50 -0800
committerValerio Virgillito2012-01-27 11:18:17 -0800
commitb89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch)
tree0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/helper-classes/RDGE/src/core/script/MeshManager.js
parent2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff)
downloadninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/MeshManager.js')
-rw-r--r--js/helper-classes/RDGE/src/core/script/MeshManager.js242
1 files changed, 242 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/MeshManager.js b/js/helper-classes/RDGE/src/core/script/MeshManager.js
new file mode 100644
index 00000000..43813078
--- /dev/null
+++ b/js/helper-classes/RDGE/src/core/script/MeshManager.js
@@ -0,0 +1,242 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7function Model(name, mesh)
8{
9 this.name = name;
10 this.mesh = mesh;
11 this.camera = null;
12}
13
14/*
15 * Maintains a list of meshes to allow instancing of data
16 */
17function MeshManager() {
18 this.contentUrl = "assets_web/mesh/";
19 this.modelMap = {};
20 this.readyList = []; // meshes that have data ready
21 this.meshesLoading = true; // indicates that no meshes have loaded or that they are still loading
22 this.postMeshLoadCallbackList = [];
23 this.tempSphere = null;
24 this.requestCounter = 0;
25}
26
27/*
28 * Pass the scene meshNode stump, loads temp object while real mesh is downloading
29 */
30MeshManager.prototype.loadMesh = function (meshStump, tempMesh)
31{
32 // if it exists already, return the mesh requested
33 if ( this.modelMap[meshStump.name] !== undefined )
34 return this.modelMap[meshStump.name];
35
36 meshStump.ready = false;
37 meshStump.addr = this.contentUrl + meshStump.name + "_mesh.json";
38 meshStump.ctxID = g_Engine.getContext().renderer.id;
39
40 // sets a temp mesh up in place of the final mesh to load
41 if (!tempMesh)
42 {
43 if (this.tempSphere == null)
44 {
45 this.tempSphere = makeSphere(g_Engine.getContext().renderer.ctx, 25, 5, 5);
46 }
47
48 tempMesh = this.tempSphere;
49 }
50
51 // add the temp mesh to the map of loaded meshes
52 this.modelMap[meshStump.name] = tempMesh;
53
54 // update the request counter - we now have one more mesh to load
55 this.requestCounter++;
56
57 requestMesh(meshStump);
58
59 return null;
60};
61
62/*
63 * Deletes the passed mesh from the manager as well as all renderers
64 */
65MeshManager.prototype.deleteMesh = function (name)
66{
67 var model = this.modelMap[name];
68
69 if (model)
70 {
71 g_Engine.ctxMan.forEach(function(context)
72 {
73 context.renderer.deletePrimitive(model.primitive);
74 });
75
76 delete this.modelMap[name];
77 }
78};
79
80MeshManager.prototype.getModelByName = function (name)
81{
82 return this.modelMap[name];
83};
84
85MeshManager.prototype.getModelNames = function ()
86{
87 var names = [];
88 for (var index in this.modelMap) {
89 names.push(this.modelList[index].name);
90 }
91
92 return names;
93};
94
95
96MeshManager.prototype.processMeshData = function () {
97 var renderer = g_Engine.getContext().renderer;
98
99 // loop through meshes and load ready data
100 for (var index in this.readyList) {
101 // if item is ready load it
102 if (this.readyList[index] && this.readyList[index].ready && renderer.id === this.readyList[index].ctxID) {
103
104
105 // pop the item
106 var model = this.readyList[index];
107 this.readyList.splice(index, 1);
108
109 var primset = new rdgePrimitiveDefinition();
110
111 primset.vertexDefinition =
112 {
113 // this shows two ways to map this data to an attribute
114 "vert":{'type':rdgeConstants.VS_ELEMENT_POS, 'bufferIndex':0, 'bufferUsage': rdgeConstants.BUFFER_STATIC},
115 "a_pos":{'type':rdgeConstants.VS_ELEMENT_POS, 'bufferIndex':0, 'bufferUsage': rdgeConstants.BUFFER_STATIC},
116 "normal":{'type':rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': rdgeConstants.BUFFER_STATIC},
117 "a_norm":{'type':rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': rdgeConstants.BUFFER_STATIC},
118 "a_normal":{'type':rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex':1, 'bufferUsage': rdgeConstants.BUFFER_STATIC},
119 "texcoord":{'type':rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': rdgeConstants.BUFFER_STATIC},
120 "a_texcoord":{'type':rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': rdgeConstants.BUFFER_STATIC},
121 "a_texcoords":{'type':rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': rdgeConstants.BUFFER_STATIC},
122 "a_uv":{'type':rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex':2, 'bufferUsage': rdgeConstants.BUFFER_STATIC}
123 };
124
125 primset.bufferStreams =
126 [
127 model.root.data.coords,
128 model.root.data.normals,
129 model.root.data.uvs
130 ];
131
132 primset.streamUsage =
133 [
134 rdgeConstants.BUFFER_STATIC,
135 rdgeConstants.BUFFER_STATIC,
136 rdgeConstants.BUFFER_STATIC
137 ];
138
139 primset.indexUsage = rdgeConstants.BUFFER_STREAM;
140
141 primset.indexBuffer = model.root.data.indices;
142
143 renderer.createPrimitive( primset );
144
145 model.root.primitive = primset;
146
147 // generate a bounding box for this mesh
148 model.root.bbox = new box();
149
150 var numCoords = model.root.data.coords.length; var idx = 0;
151 while (idx < numCoords - 2)
152 {
153 var thisCoord = [model.root.data.coords[idx+0], model.root.data.coords[idx+1], model.root.data.coords[idx+2]];
154 model.root.bbox.addVec3(thisCoord);
155 idx += 3;
156 }
157
158 this.modelMap[model.root.attribs.name] = model.root;
159
160 // now that the model is load reduce the request count
161 this.requestCounter--;
162
163 this.onLoaded(model.root.attribs.name);
164 //break;
165 }
166
167 }
168}
169
170MeshManager.prototype.isReady = function()
171{
172 return this.readyList.length == 0;
173}
174
175MeshManager.prototype.addOnLoadedCallback = function (callback)
176{
177 this.postMeshLoadCallbackList.push(callback)
178}
179
180MeshManager.prototype.onLoaded = function ( meshName )
181{
182 for (var index = 0 in this.postMeshLoadCallbackList)
183 {
184 // call the functions
185 this.postMeshLoadCallbackList[index].onMeshLoaded(meshName);
186 }
187}
188
189MeshManager.prototype.exportJSON = function ()
190{
191 for(var m in this.modelMap)
192 {
193 this.modelMap[m].primitive.built = false;
194 }
195
196 return JSON.stringify(this.modelMap);
197}
198
199MeshManager.prototype.importJSON = function ( jsonMeshExport )
200{
201 try
202 {
203 var tempModelMap = JSON.parse(jsonMeshExport);
204
205 for(var m in tempModelMap)
206 {
207 if(!this.modelMap[m])
208 {
209 this.modelMap[m] = tempModelMap[m];