aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/MeshManager.js
blob: 4fa3c9089ec7c34459ee419daca22218fd4ccd1e (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */

// RDGE namespaces
var RDGE = RDGE || {};

RDGE.Model = function (name, mesh) {
    this.name = name;
    this.mesh = mesh;
    this.camera = null;
};

/*
 *	Maintains a list of meshes to allow instancing of data
 */
RDGE.MeshManager = function () {
    this.contentUrl					= "assets_web/mesh/";
    this.modelMap					= {};
    this.readyList					= [];		// meshes that have data ready
    this.meshesLoading				= true;		// indicates that no meshes have loaded or that they are still loading
    this.postMeshLoadCallbackList	= [];
    this.tempSphere					= null;
    this.requestCounter				= 0;
};

/*
 * Pass the scene meshNode stump, loads temp object while real mesh is downloading
 */
RDGE.MeshManager.prototype.loadMesh = function (meshStump, tempMesh) {
    // if it exists already, return the mesh requested
    if ( this.modelMap[meshStump.name] !== undefined )
        return this.modelMap[meshStump.name];

    meshStump.ready = false;
    meshStump.addr = this.contentUrl + meshStump.name + "_mesh.json";
    meshStump.ctxID = RDGE.globals.engine.getContext().renderer.id;

	// sets a temp mesh up in place of the final mesh to load
    if (!tempMesh) {
        if (this.tempSphere == null) {
            this.tempSphere = RDGE.renderUtils.makeSphere(RDGE.globals.engine.getContext().renderer.ctx, 25, 5, 5);
        }

        tempMesh = this.tempSphere;
    }

	// add the temp mesh to the map of loaded meshes
    this.modelMap[meshStump.name] = tempMesh;
    
    // update the request counter - we now have one more mesh to load
    this.requestCounter++;

    RDGE.requestMesh(meshStump);

    return null;
};

/*
 * Deletes the passed mesh from the manager as well as all renderers
 */
RDGE.MeshManager.prototype.deleteMesh = function (name) {
	var model = this.modelMap[name];
	
    if (model) {
        RDGE.globals.engine.ctxMan.forEach(function (context) {
			context.renderer.deletePrimitive(model.primitive);
		});

		delete this.modelMap[name];
	}
};

RDGE.MeshManager.prototype.getModelByName = function (name) {
    return this.modelMap[name];
};

RDGE.MeshManager.prototype.getModelNames = function () {
    var names = [];
    for (var index in this.modelMap) {
        names.push(this.modelList[index].name);
    }

    return names;
};


RDGE.MeshManager.prototype.processMeshData = function () {
    var renderer = RDGE.globals.engine.getContext().renderer;
	
    // loop through meshes and load ready data
    for (var index in this.readyList) {
        // if item is ready load it
        if (this.readyList[index] && this.readyList[index].ready && renderer.id === this.readyList[index].ctxID) {
        

            // pop the item
            var model = this.readyList[index];
            this.readyList.splice(index, 1);
            
            var primset = new RDGE.rdgePrimitiveDefinition();
            
            primset.vertexDefinition = 
            {
				// this shows two ways to map this data to an attribute
                "vert": { 'type': RDGE.rdgeConstants.VS_ELEMENT_POS, 'bufferIndex': 0, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_pos": { 'type': RDGE.rdgeConstants.VS_ELEMENT_POS, 'bufferIndex': 0, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "normal": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex': 1, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_norm": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex': 1, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_normal": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT3, 'bufferIndex': 1, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "texcoord": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex': 2, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_texcoord": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex': 2, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_texcoords": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex': 2, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC },
                "a_uv": { 'type': RDGE.rdgeConstants.VS_ELEMENT_FLOAT2, 'bufferIndex': 2, 'bufferUsage': RDGE.rdgeConstants.BUFFER_STATIC }
            };
            
            primset.bufferStreams = 
            [
				model.root.data.coords,
				model.root.data.normals,
				model.root.data.uvs
            ];
            
            primset.streamUsage = 
            [
				RDGE.rdgeConstants.BUFFER_STATIC,
				RDGE.rdgeConstants.BUFFER_STATIC,
				RDGE.rdgeConstants.BUFFER_STATIC
            ];
            
            primset.indexUsage = RDGE.rdgeConstants.BUFFER_STREAM;
            
            primset.indexBuffer = model.root.data.indices;

			renderer.createPrimitive( primset );
			
			model.root.primitive = primset;

            // generate a bounding box for this mesh
            model.root.bbox = new RDGE.box();

            var numCoords = model.root.data.coords.length; var idx = 0;
            while (idx < numCoords - 2) {
              var thisCoord = [model.root.data.coords[idx+0], model.root.data.coords[idx+1], model.root.data.coords[idx+2]];
              model.root.bbox.addVec3(thisCoord);
              idx += 3;
            }

            this.modelMap[model.root.attribs.name] = model.root;
            
            // now that the model is load reduce the request count
            this.requestCounter--;
            
            this.onLoaded(model.root.attribs.name);
            //break;
        }

    }
};

RDGE.MeshManager.prototype.isReady = function () {
	return this.readyList.length == 0; 
};

RDGE.MeshManager.prototype.addOnLoadedCallback = function (callback) {
    this.postMeshLoadCallbackList.push(callback)
};

RDGE.MeshManager.prototype.onLoaded = function (meshName) {
    for (var index = 0 in this.postMeshLoadCallbackList) {
        // call the functions
        this.postMeshLoadCallbackList[index].onMeshLoaded(meshName);
    }
};

RDGE.MeshManager.prototype.exportJSON = function () {
    for (var m in this.modelMap) {
		this.modelMap[m].primitive.built = false;
	}
	
	return JSON.stringify(this.modelMap);
};

RDGE.MeshManager.prototype.importJSON = function (jsonMeshExport) {
    try {
		var tempModelMap = JSON.parse(jsonMeshExport);
		
        for (var m in tempModelMap) {
            if (!this.modelMap[m]) {
				this.modelMap[m] = tempModelMap[m];
			}
		}
		window.console.log("meshes imported");
    } catch (e) {
		window.console.error("error importing meshes: " + e.description );		
	}
};

/*
 *	global function for the mesh manager to make mesh file requests
 */ 
RDGE.requestMesh = function (mesh) {
    var request = new XMLHttpRequest();
    request.mesh = mesh;
    request.onreadystatechange = function () {
        if (request.readyState == 4) {
            if (request.status == 200 || window.location.href.indexOf("http") == -1) {
                var mesh = eval("(" + request.responseText + ")"); //retrieve result as an JavaScript object
                mesh.ready = true;
                mesh.ctxID = request.mesh.ctxID;
                RDGE.globals.meshMan.readyList.push(mesh);
            }
            else {
                alert("An error has occured making the request");
            }
        }
    }

    request.open("GET", mesh.addr, true);
    request.send(null);
};