[692] | 1 | include "ogre/lib/ogreSkeletonLib_usefulfns.ms" |
---|
| 2 | include "ogre/lib/ogrebipedlib.ms" |
---|
| 3 | |
---|
| 4 | ---------------------------------------------------------------------------------------- |
---|
| 5 | -- ----------------------------------- WRITE MESH ----------------------------------- -- |
---|
| 6 | ---------------------------------------------------------------------------------------- |
---|
| 7 | |
---|
| 8 | ----------------------------------------------------------------- |
---|
| 9 | -- Global 'variables' |
---|
| 10 | -- There is an array with vertices datas for each submesh |
---|
| 11 | -- There is also an array made up of faces. |
---|
| 12 | ----------------------------------------------------------------- |
---|
| 13 | |
---|
| 14 | verticesArrays=#() ; |
---|
| 15 | facesArrays=#() ; |
---|
| 16 | boneAssignments=#() ; |
---|
| 17 | submeshesId=#() ; |
---|
| 18 | materialName = "" ; |
---|
| 19 | |
---|
| 20 | -- verticesArrays[i] will describe the subMesh number i and its elements will be ogreVertices where |
---|
| 21 | -- ogreVert = #(x,y,z,nx,ny,nz,r,g,b,a,u1,v1,u2,v2....) |
---|
| 22 | -- ----- -------- ------- --------------- |
---|
| 23 | -- Pos Nmal RGBA UVs sets |
---|
| 24 | -- RGBA being the vertex color when relevant (Optional) |
---|
| 25 | -- A vertex can contain multiple UVs sets (Optional) |
---|
| 26 | |
---|
| 27 | -- facesArrays[i] is an array of Point3 describing the subMesh i. Each Point3 contains vertices indices for a face that is gonna be exported |
---|
| 28 | |
---|
| 29 | -- boneAssignments[i] describes the bones assignements for subMesh i. It is an array of triplets like #(vertexIndex,boneIndex,weight) |
---|
| 30 | |
---|
| 31 | -- submeshesId is the list of material Ids |
---|
| 32 | |
---|
| 33 | -- materialName is the name that Ogre will use |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | ---------------------------------------------------------------- |
---|
| 37 | -- exploreMesh returns a string which is "OK" or an error message |
---|
| 38 | -- if there are warnings, it displays message boxes. |
---|
| 39 | ----------------------------------------------------------------- |
---|
| 40 | function exploreMesh pmesh exportUV exportColours= |
---|
| 41 | ( |
---|
| 42 | local material, answer, m ; |
---|
| 43 | |
---|
| 44 | answer = "" ; |
---|
| 45 | m = snapShotAsMesh pmesh ; |
---|
| 46 | |
---|
| 47 | -- first, is a material defined ? |
---|
| 48 | material = pmesh.material ; |
---|
| 49 | if (material == undefined) then |
---|
| 50 | answer = "This mesh doesn't have any material, please apply one\n" ; |
---|
| 51 | else |
---|
| 52 | -- then, are UVW coords set up ? |
---|
| 53 | if (exportUV and getNumTVerts m == 0) then |
---|
| 54 | answer = "This mesh must have UVW coords in order to be exported" ; |
---|
| 55 | |
---|
| 56 | if (answer == "") then |
---|
| 57 | answer = "OK" ; |
---|
| 58 | |
---|
| 59 | delete m ; |
---|
| 60 | answer ; |
---|
| 61 | ) |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | ---------------------------------------------------------------- |
---|
| 65 | -- returns the correct normal for the vertex |
---|
| 66 | -- according to smoothing groups |
---|
| 67 | ----------------------------------------------------------------- |
---|
| 68 | function getVertexNormalAccordingToSmoothingGroups selectedMesh faceIndex vertexIndex = |
---|
| 69 | ( |
---|
| 70 | local normal; |
---|
| 71 | local faceSmoothingGroup = getFaceSmoothGroup selectedMesh faceIndex |
---|
| 72 | |
---|
| 73 | if faceSmoothingGroup == 0 then |
---|
| 74 | ( |
---|
| 75 | -- if not smooth use face normal |
---|
| 76 | normal = getFaceNormal selectedMesh faceIndex |
---|
| 77 | ) |
---|
| 78 | else |
---|
| 79 | ( |
---|
| 80 | local vertexIndexes = getFace selectedMesh faceIndex; |
---|
| 81 | local vI = vertexIndexes[vertexIndex] |
---|
| 82 | local n = [0, 0, 0] -- point3 |
---|
| 83 | for adjoiningFaceIndex in (meshop.getFacesUsingVert selectedMesh vI) do |
---|
| 84 | ( |
---|
| 85 | sg = getFaceSmoothGroup selectedMesh adjoiningFaceIndex; |
---|
| 86 | and_sg = bit.and sg faceSmoothingGroup ; |
---|
| 87 | if (and_sg != 0) then |
---|
| 88 | n += getFaceNormal selectedMesh adjoiningFaceIndex ; |
---|
| 89 | ) |
---|
| 90 | normal = normalize n; |
---|
| 91 | ) |
---|
| 92 | |
---|
| 93 | normal; |
---|
| 94 | ) |
---|
| 95 | |
---|
| 96 | function tabEquals tab1 tab2 = |
---|
| 97 | ( |
---|
| 98 | if (tab1.count != tab2.count) then |
---|
| 99 | return false; |
---|
| 100 | |
---|
| 101 | for i=1 to tab1.count do |
---|
| 102 | if (tab1[i] != tab2[i]) then |
---|
| 103 | return false; |
---|
| 104 | |
---|
| 105 | return true; |
---|
| 106 | ) |
---|
| 107 | |
---|
| 108 | function doesVertexExistAlready searchTable vertexIndex vertexNormal vertexColorIndex vertexAlphaIndex UVs exportUV exportColor = |
---|
| 109 | ( |
---|
| 110 | local start_uvs; |
---|
| 111 | vertex = #(); |
---|
| 112 | --format "vertex a tester % % \n" vertexIndex vertexNormal; |
---|
| 113 | |
---|
| 114 | if (searchTable[vertexIndex]==undefined) then |
---|
| 115 | return 0; |
---|
| 116 | |
---|
| 117 | for vertex in searchTable[vertexIndex] do |
---|
| 118 | ( |
---|
| 119 | if (vertex[2]!=vertexNormal) then |
---|
| 120 | continue; |
---|
| 121 | |
---|
| 122 | if (exportColor) then |
---|
| 123 | ( |
---|
| 124 | if (vertex[3]!=vertexColorIndex) then |
---|
| 125 | continue; |
---|
| 126 | |
---|
| 127 | if (vertex[4]!=vertexAlphaIndex) then |
---|
| 128 | continue; |
---|
| 129 | |
---|
| 130 | start_uvs = 4; |
---|
| 131 | ) |
---|
| 132 | else |
---|
| 133 | ( |
---|
| 134 | start_uvs = 2; |
---|
| 135 | ) |
---|
| 136 | |
---|
| 137 | if (exportUV) then |
---|
| 138 | ( |
---|
| 139 | local equal = true; |
---|
| 140 | for i=1 to UVs.count do |
---|
| 141 | ( |
---|
| 142 | if (vertex[start_uvs+i]!=UVs[i]) then |
---|
| 143 | ( |
---|
| 144 | equal = false; |
---|
| 145 | exit; |
---|
| 146 | ) |
---|
| 147 | ) |
---|
| 148 | if (not equal) then |
---|
| 149 | continue; |
---|
| 150 | ) |
---|
| 151 | |
---|
| 152 | return vertex[1] ; |
---|
| 153 | ) |
---|
| 154 | return 0; |
---|
| 155 | ) |
---|
| 156 | |
---|
| 157 | ---------------------------------------------------------------------------- |
---|
| 158 | -- Retrieves al the datas which wil be used |
---|
| 159 | -- tmesh is the mesh object |
---|
| 160 | -- flipyz = true if you want coords to fit in OGRE. |
---|
| 161 | -- flipNormal = true if you want to flip normals |
---|
| 162 | -- scale : the scale factor |
---|
| 163 | -- exportUV = true if you want to export texture |
---|
| 164 | -- numUVsets : number of texture coordinates to export (if exportUV is true, the minimum is 1. if exportUV is false, this parameter is irrelevant) |
---|
| 165 | -- exportColor = true if you want to export vertices colors |
---|
| 166 | -- sk : the skin modifier (may be undefined) |
---|
| 167 | -- phy : the physique modifier (may be undefined) |
---|
| 168 | -- stores datas in verticesArrays and facesArrays. |
---|
| 169 | ---------------------------------------------------------------------------- |
---|
| 170 | function getDatas tmesh flipyz scale flipNormal exportUV numUVsets exportColours exportHelpers sk phy = |
---|
| 171 | ( |
---|
| 172 | local face; --index of the current face |
---|
| 173 | local localVertexIndex; --index of the current vertex (relative to the current face) ie : 1, 2 or 3 |
---|
| 174 | local vertexNormal; --normal of the current vertex (Point3) |
---|
| 175 | local vertexPosition; --position of the current vertex (Point3) |
---|
| 176 | local faceVerticesIndices; --indices of the vertex for the current face (Point3) |
---|
| 177 | local vertexIndex; --index of the current vertex (in the mesh) |
---|
| 178 | local matId; --material Id of the current face |
---|
| 179 | local numFaces; --number of faces in the mesh |
---|
| 180 | |
---|
| 181 | local faceVertexColorIndices; --indices of the vertices for the current face in the vertex color table of the mesh (Point3) |
---|
| 182 | local vertexColorIndex; --index of the current vertex in the vertex color table of the mesh |
---|
| 183 | local vertexColor; --vertex color for the current vertex (color) |
---|
| 184 | local faceVertexAlphaIndices; --indices of the vertices for the current face in the vertex alpha table of the mesh (Point3) |
---|
| 185 | local vertexAlpha; --vertex alpha of the current alpha |
---|
| 186 | local vertexAlphaIndex; --index of the current vertex in the vertex alpha map channel (-2) |
---|
| 187 | |
---|
| 188 | local ogreFace; --indices of the vertices for the current face (references the list of vertices that is gonna be exported, not the vertices of the original mesh) |
---|
| 189 | |
---|
| 190 | local vertexWeightCount; --number of bones influencing the current vertex |
---|
| 191 | local vertexWeight; --current weight |
---|
| 192 | |
---|
| 193 | local searchTable ; |
---|
| 194 | local k ; |
---|
| 195 | local rootId ; |
---|
| 196 | |
---|
| 197 | local UVcoords; -- will contain the texture coordinates for the current vertex. its size will be numUVsets*2 if exportUV is true. |
---|
| 198 | |
---|
| 199 | -- searchTables will contain a searchTable for every subMesh. |
---|
| 200 | -- searchTable will be the current table for the current subMesh. |
---|
| 201 | -- Every vertex will be stored here to check for duplicates. |
---|
| 202 | searchTable=#() ; |
---|
| 203 | searchTables=#() ; |
---|
| 204 | |
---|
| 205 | -- initialisation of the current ogre face as a vector |
---|
| 206 | ogreFace=Point3 12 12 12 ; |
---|
| 207 | |
---|
| 208 | -- Data arrays init. |
---|
| 209 | verticesArrays=#() ; |
---|
| 210 | facesArrays=#() ; |
---|
| 211 | boneAssignments = #() ; |
---|
| 212 | submeshesId = #() ; |
---|
| 213 | UVcoords=#() ; |
---|
| 214 | |
---|
| 215 | -- compute bones list for the model. |
---|
| 216 | BonesList=#() |
---|
| 217 | computeBonesList phy sk exportHelpers ; |
---|
| 218 | |
---|
| 219 | numFaces = (getNumFaces tmesh); |
---|
| 220 | |
---|
| 221 | -- For each face |
---|
| 222 | for face = 1 to numFaces do -- LOOP on FACES -- |
---|
| 223 | ( |
---|
| 224 | OgreExportObject.exportProgress.value = (100.0*face/numFaces); |
---|
| 225 | |
---|
| 226 | faceVerticesIndices = getFace tmesh face ; |
---|
| 227 | matId = getFaceMatId tmesh face ; |
---|
| 228 | |
---|
| 229 | -- Vertex Color Face |
---|
| 230 | if (exportColours) then |
---|
| 231 | ( |
---|
| 232 | faceVertexColorIndices = meshOp.getMapFace tmesh 0 face ; -- 0 is the vertex color channel |
---|
| 233 | faceVertexAlphaIndices = meshOp.getMapFace tmesh -2 face ; -- -2 is the vertex alpha channel |
---|
| 234 | ) |
---|
| 235 | else |
---|
| 236 | ( |
---|
| 237 | faceVertexColorIndices = Point3 1 1 1 ; |
---|
| 238 | faceVertexAlphaIndices = Point3 1 1 1 ; |
---|
| 239 | ) |
---|
| 240 | |
---|
| 241 | -- For each vertex whose face is made up of. |
---|
| 242 | for localVertexIndex=1 to 3 do -- LOOP on VERTICES -- |
---|
| 243 | ( |
---|
| 244 | vertexIndex = (int) (faceVerticesIndices[localVertexIndex]) ; |
---|
| 245 | vertexColorIndex = (int) (faceVertexColorIndices[localVertexIndex]) ; |
---|
| 246 | vertexAlphaIndex = (int) (faceVertexAlphaIndices[localVertexIndex]); |
---|
| 247 | vertexNormal = getVertexNormalAccordingToSmoothingGroups tmesh face localVertexIndex; |
---|
| 248 | -- flip normal capability |
---|
| 249 | if (flipNormal) then |
---|
| 250 | ( |
---|
| 251 | vertexNormal = vertexNormal * -1 |
---|
| 252 | ) |
---|
| 253 | |
---|
| 254 | -- we retrieve datas. it depends on options. |
---|
| 255 | -- UV face |
---|
| 256 | if (exportUV) then |
---|
| 257 | ( |
---|
| 258 | UVcoords=#(); |
---|
| 259 | for ch=1 to numUVsets do |
---|
| 260 | ( |
---|
| 261 | local vertInds = meshOp.getMapFace tmesh ch face ; |
---|
| 262 | local UVW = meshOp.getMapVert tmesh ch vertInds[localVertexIndex] ; -- retrieve the UV for the corresponding channel. |
---|
| 263 | append UVcoords UVW[1]; |
---|
| 264 | append UVcoords UVW[2]; -- don't care about the W coordinate |
---|
| 265 | ) |
---|
| 266 | ) |
---|
| 267 | else |
---|
| 268 | ( |
---|
| 269 | UVcoords=#(); |
---|
| 270 | ) |
---|
| 271 | |
---|
| 272 | -- we try to find the corresponding searchtable. |
---|
| 273 | if (searchTables[matId] == undefined ) then |
---|
| 274 | searchTables[matId]=#() ; -- we found a new subMesh, create the searchTable for it |
---|
| 275 | |
---|
| 276 | searchTable = searchTables[matId] ; -- set the searchTable to the current subMesh |
---|
| 277 | |
---|
| 278 | newVertex = 1 ; |
---|
| 279 | ogreVertNum = 1; |
---|
| 280 | -- Maybe this vertex has already been found. |
---|
| 281 | -- So we use searchTable. |
---|
| 282 | res = doesVertexExistAlready searchTable vertexIndex vertexNormal vertexColorIndex vertexAlphaIndex UVcoords exportUV exportColours; |
---|
| 283 | if (res==0) then |
---|
| 284 | ( |
---|
| 285 | newVertex = 1 ; |
---|
| 286 | --format "nouveau vertex % % %\n" vertexIndex vertexNormal UVcoords; |
---|
| 287 | ) |
---|
| 288 | else |
---|
| 289 | ( |
---|
| 290 | newVertex = 0; |
---|
| 291 | ogreVertNum = res; |
---|
| 292 | ) |
---|
| 293 | |
---|
| 294 | if (newVertex == 1) then -- That is to say it has not been found. |
---|
| 295 | ( |
---|
| 296 | -- Maybe the material found defines a new submesh... |
---|
| 297 | if (verticesArrays[matId] == undefined) then |
---|
| 298 | ( |
---|
| 299 | format "- Submesh detected: material ID = %\n" matId |
---|
| 300 | verticesArrays[matId] = #() ; |
---|
| 301 | boneAssignments[matId] = #() ; |
---|
| 302 | append submeshesId matId ; |
---|
| 303 | ) |
---|
| 304 | |
---|
| 305 | -- the vertex number for the current submesh must be updated |
---|
| 306 | -- vertex number is increased |
---|
| 307 | ogreVertNum = verticesArrays[matId].count + 1; |
---|
| 308 | |
---|
| 309 | -- it is added to the searchTable |
---|
| 310 | if (searchTable[vertexIndex]==undefined) then |
---|
| 311 | ( |
---|
| 312 | searchTable[vertexIndex] = #() ; |
---|
| 313 | ) |
---|
| 314 | local data = #(); |
---|
| 315 | append data ogreVertNum ; |
---|
| 316 | n = copy vertexNormal ; |
---|
| 317 | append data n ; |
---|
| 318 | if (exportColours) then |
---|
| 319 | ( |
---|
| 320 | append data VertexColorIndex ; |
---|
| 321 | append data VertexAlphaIndex ; |
---|
| 322 | ) |
---|
| 323 | if (exportUV) then |
---|
| 324 | ( |
---|
| 325 | if (UVcoords.count > 0) then |
---|
| 326 | ( |
---|
| 327 | for uv=1 to UVcoords.count do |
---|
| 328 | append data UVcoords[uv]; |
---|
| 329 | ) |
---|
| 330 | ) |
---|
| 331 | append searchTable[vertexIndex] data ; |
---|
| 332 | |
---|
| 333 | -- we retrieve data |
---|
| 334 | vertexPosition = getVert tmesh faceVerticesIndices[localVertexIndex] ; |
---|
| 335 | |
---|
| 336 | vertexColor = Point3 0 0 0; |
---|
| 337 | vertexAlpha = 255; |
---|
| 338 | if (exportColours) then |
---|
| 339 | ( |
---|
| 340 | vertexColor = (meshOp.getMapVert tmesh 0 vertexColorIndex); |
---|
| 341 | vertexAlpha = (meshOp.getMapVert tmesh -2 vertexAlphaIndex)[1]; |
---|
| 342 | ) |
---|
| 343 | |
---|
| 344 | -- change scale |
---|
| 345 | vertexPosition = vertexPosition * scale ; |
---|
| 346 | |
---|
| 347 | -- flip axes |
---|
| 348 | if (flipyz) then |
---|
| 349 | ( |
---|
| 350 | vertTmp = copy vertexPosition ; |
---|
| 351 | vertexPosition[2] = vertTmp[3] ; |
---|
| 352 | vertexPosition[3] = -vertTmp[2] ; |
---|
| 353 | |
---|
| 354 | nmalTmp = copy vertexNormal ; |
---|
| 355 | vertexNormal[2] = nmalTmp[3] ; |
---|
| 356 | vertexNormal[3] = -nmalTmp[2] ; |
---|
| 357 | ) |
---|
| 358 | |
---|
| 359 | -- store the vertex in verticesArrays |
---|
| 360 | vertexData = #(vertexPosition[1],vertexPosition[2],vertexPosition[3],vertexNormal[1],vertexNormal[2],vertexNormal[3],vertexColor[1],vertexColor[2],vertexColor[3],vertexAlpha) ; |
---|
| 361 | if (exportUV) then |
---|
| 362 | ( |
---|
| 363 | for ch=1 to numUVsets do |
---|
| 364 | ( |
---|
| 365 | append vertexData UVcoords[1+(ch-1)*2]; |
---|
| 366 | append vertexData UVcoords[2+(ch-1)*2]; |
---|
| 367 | ) |
---|
| 368 | ) |
---|
| 369 | append verticesArrays[matId] vertexData ; -- Vertex is added to datas |
---|
| 370 | |
---|
| 371 | -- And now, bone assignments. (if a skin modifier is present) |
---|
| 372 | -- It seems that there are issues when just few vertices have bone assignments. |
---|
| 373 | -- So there is at least the root assignment. |
---|
| 374 | if (sk != undefined) then |
---|
| 375 | ( |
---|
| 376 | vertexWeightCount = skinOps.getVertexWeightCount sk vertexIndex ; |
---|
| 377 | |
---|
| 378 | if (vertexWeightCount > 4) then |
---|
| 379 | ( |
---|
| 380 | if (not g_MAX) then |
---|
| 381 | ( |
---|
| 382 | format "*** vertex % has more than 4 bones assigned...\n" vertexIndex; |
---|
| 383 | ) |
---|
| 384 | ) |
---|
| 385 | |
---|
| 386 | for k=1 to vertexWeightCount do |
---|
| 387 | ( |
---|
| 388 | bid = skinOps.getVertexWeightBoneID sk vertexIndex k ; |
---|
| 389 | bname = skinOps.getBoneName sk bid 1 ; |
---|
| 390 | bname = replaceSpaces bname; |
---|
| 391 | vertexWeight = skinOps.getVertexWeight sk vertexIndex k ; |
---|
| 392 | |
---|
| 393 | id_bone = findItem BonesList bname; |
---|
| 394 | if (id_bone != 0) then |
---|
| 395 | append boneAssignments[matId] #(ogreVertNum-1,id_bone-1,vertexWeight) ; |
---|
| 396 | ) |
---|
| 397 | -- assignment to the root bone. |
---|
| 398 | if (vertexWeightCount==0) then |
---|
| 399 | ( |
---|
| 400 | -- gets the root Id: |
---|
| 401 | rootId=getRootsId sk |
---|
| 402 | rootname = skinOps.getBoneName sk rootId[1] 1 ; |
---|
| 403 | id_bone = findItem BonesList rootname ; |
---|
| 404 | if (id_bone != 0) then |
---|
| 405 | append boneAssignments[matId] #(ogreVertNum-1,id_bone-1,1) ; |
---|
| 406 | ) |
---|
| 407 | ) |
---|
| 408 | -- same thing with physique modifier if defined |
---|
| 409 | if (phy != undefined) then |
---|
| 410 | ( |
---|
| 411 | vertexWeightCount = physiqueOps.getVertexBoneCount $ vertexIndex ; |
---|
| 412 | |
---|
| 413 | if (vertexWeightCount > 4) then |
---|
| 414 | ( |
---|
| 415 | if (not g_MAX) then |
---|
| 416 | ( |
---|
| 417 | format "*** vertex % has more than 4 bones assigned...\n" vertexIndex; |
---|
| 418 | ) |
---|
| 419 | ) |
---|
| 420 | |
---|
| 421 | for k=1 to vertexWeightCount do |
---|
| 422 | ( |
---|
| 423 | bone = physiqueOps.getVertexBone $ vertexIndex k |
---|
| 424 | vertexWeight = physiqueOps.getVertexWeight $ vertexIndex k |
---|
| 425 | |
---|
| 426 | -- search the bone number |
---|
| 427 | bname = replaceSpaces bone.name; |
---|
| 428 | id_bone = findItem BonesList bname ; |
---|
| 429 | if (id_bone!=0) then |
---|
| 430 | append boneAssignments[matId] #(ogreVertNum-1,id_bone-1,vertexWeight) ; |
---|
| 431 | ) |
---|
| 432 | -- assignment to the first bone if the vertex was not assigned. |
---|
| 433 | if (vertexWeightCount==0) then |
---|
| 434 | ( |
---|
| 435 | -- gets the root Id: |
---|
| 436 | append boneAssignments[matId] #(ogreVertNum-1,0,1) ; |
---|
| 437 | ) |
---|
| 438 | ) |
---|
| 439 | |
---|
| 440 | ) |
---|
| 441 | |
---|
| 442 | ogreFace[localVertexIndex] = ogreVertNum - 1; |
---|
| 443 | ) |
---|
| 444 | |
---|
| 445 | -- flip normal capability |
---|
| 446 | if (flipNormal) then |
---|
| 447 | ( |
---|
| 448 | faceTmp = copy ogreFace ; |
---|
| 449 | ogreFace[2] = faceTmp[3] ; |
---|
| 450 | ogreFace[3] = faceTmp[2] ; |
---|
| 451 | ) |
---|
| 452 | |
---|
| 453 | if (facesArrays[matId] == undefined) then |
---|
| 454 | facesArrays[matId] = #() ; |
---|
| 455 | append facesArrays[matId] #(ogreFace[1],ogreFace[2],ogreFace[3]) ; -- Face is added to datas |
---|
| 456 | ) |
---|
| 457 | ) |
---|
| 458 | |
---|
| 459 | ------------------------------------------------- |
---|
| 460 | -- writes in the mesh.xml file |
---|
| 461 | -- outName : filename |
---|
| 462 | -- exportUV = true if you want to export texture |
---|
| 463 | -- numUVsets : number of UVs sets per vertex to be exported (only relevant if exportUV = true) |
---|
| 464 | -- exportColor = true if you want to export vertices colors |
---|
| 465 | -- material : material used by the mesh |
---|
| 466 | -- Uses the arrays verticesArrays and facesArrays |
---|
| 467 | ------------------------------------------------- |
---|
| 468 | function writeM exportUV numUVsets exportColours material outName = |
---|
| 469 | ( |
---|
| 470 | local a,v,f,submatname,hasSkeleton,outFile,hasColours,matId ; |
---|
| 471 | |
---|
| 472 | hasSkeleton = false ; |
---|
| 473 | texCoordString = "" ; |
---|
| 474 | if (exportUV) then |
---|
| 475 | ( |
---|
| 476 | texCoordString = "texture_coords=\"" + (numUVsets as string) + "\" " ; |
---|
| 477 | for num=1 to numUVsets do |
---|
| 478 | ( |
---|
| 479 | texCoordString = texCoordString + "texture_coords_dimensions_" + ((num-1) as string) + "=\"2\" "; -- I really don't care about the W coordinates |
---|
| 480 | ) |
---|
| 481 | ) |
---|
| 482 | hasColours = "false" ; |
---|
| 483 | if (exportColours) then |
---|
| 484 | hasColours = "true" ; |
---|
| 485 | |
---|
| 486 | |
---|
| 487 | -- the final file is created |
---|
| 488 | if (g_MAX) then |
---|
| 489 | ( |
---|
| 490 | ClearListener(); |
---|
| 491 | if (g_MAX_use_listener) then |
---|
| 492 | format("<ogrestartfile>%</ogrestartfile><ogrestartdata>\n") (outName + ".mesh.xml"); |
---|
| 493 | outFile = listener; |
---|
| 494 | ) |
---|
| 495 | else |
---|
| 496 | ( |
---|
| 497 | outFile = createfile (outName + ".mesh.xml") ; |
---|
| 498 | ) |
---|
| 499 | |
---|
| 500 | -- writes the header |
---|
| 501 | format("<mesh>\n") to:outFile ; |
---|
| 502 | |
---|
| 503 | -- submeshes start |
---|
| 504 | format("\t<submeshes>\n") to:outFile ; |
---|
| 505 | |
---|
| 506 | -- for each submesh |
---|
| 507 | for matId in submeshesId do |
---|
| 508 | ( |
---|
| 509 | -- Do we need 32-bit indexes? |
---|
| 510 | use32bitindexes = "false"; |
---|
| 511 | if arrayLength verticesArrays[matId] > 65535 then |
---|
| 512 | use32bitindexes = "true"; |
---|
| 513 | |
---|
| 514 | -- get the name of the sub material if needed. |
---|
| 515 | submatname = replacespaces material.name ; |
---|
| 516 | |
---|
| 517 | if (classof material == MultiMaterial) then |
---|
| 518 | ( |
---|
| 519 | if (material.materialList[matId]==undefined) then |
---|
| 520 | ( |
---|
| 521 | msg = ""; |
---|
| 522 | format "You are using in your mesh a material Id (%) that does not exist in your MultiMaterial (%)\nMaterial information won't be properly exported." matId submatname to:msg ; |
---|
| 523 | messageBox msg; |
---|
| 524 | ) |
---|
| 525 | else |
---|
| 526 | ( |
---|
| 527 | submatname += "/" + replacespaces material.materiallist[matId].name ; |
---|
| 528 | ) |
---|
| 529 | ) |
---|
| 530 | |
---|
| 531 | -- HELLO ! -- |
---|
| 532 | --------------------------------------------------------------------------------- |
---|
| 533 | -- IF YOU COME HERE BECAUSE YOUR OBJECT FAILED EXPORTING, MAYBE THAT'S BECAUSE -- |
---|
| 534 | -- YOU USE MATERIAL ID IN YOUR MESH THAT DOESN'T EXIST IN YOUR MULTIMATERIAL -- |
---|
| 535 | --------------------------------------------------------------------------------- |
---|
| 536 | |
---|
| 537 | format("\t\t<submesh material = \"%\" usesharedvertices=\"false\" use32bitindexes=\"%\">\n") submatname use32bitindexes to:outFile ; |
---|
| 538 | |
---|
| 539 | if (not g_MAX) then |
---|
| 540 | format "- writing faces...\n" |
---|
| 541 | format("\t\t\t<faces count=\"%\">\n") (arraylength facesArrays[matId]) to:outFile; |
---|
| 542 | |
---|
| 543 | -- for each face |
---|
| 544 | for f in facesArrays[matId] do |
---|
| 545 | ( |
---|
| 546 | format("\t\t\t\t<face ") to:outFile ; |
---|
| 547 | format("v1=\"%\" v2=\"%\" v3=\"%\" />\n") ((int)f[1]) ((int)f[2]) ((int)f[3]) to:outFile ; |
---|
| 548 | ) |
---|
| 549 | format("\t\t\t</faces>\n") to:outFile ; |
---|
| 550 | |
---|
| 551 | if (not g_MAX) then |
---|
| 552 | format "- writing vertices...\n" |
---|
| 553 | format("\t\t\t<geometry vertexcount=\"%\">\n") (arrayLength verticesArrays[matId] ) to:outFile; |
---|
| 554 | format("\t\t\t\t<vertexbuffer positions=\"true\" normals=\"true\" colours_diffuse=\"%\" %>\n") hasColours TexCoordString to:outFile ; |
---|
| 555 | |
---|
| 556 | -- for each vertex |
---|
| 557 | for v in verticesArrays[matId] do |
---|
| 558 | ( |
---|
| 559 | format("\t\t\t\t\t<vertex>\n") to:outFile ; |
---|
| 560 | format("\t\t\t\t\t\t<position x=\"%\" y=\"%\" z=\"%\" />\n") v[1] v[2] v[3] to:outFile ; |
---|
| 561 | format("\t\t\t\t\t\t<normal x=\"%\" y=\"%\" z=\"%\" />\n") v[4] v[5] v[6] to:outFile ; |
---|
| 562 | if (exportUV) then |
---|
| 563 | ( |
---|
| 564 | for ch=1 to numUVsets do |
---|
| 565 | format("\t\t\t\t\t\t<texcoord u=\"%\" v=\"%\" />\n") v[11+((ch-1)*2)] (1 - v[12+((ch-1)*2)]) to:outFile ; |
---|
| 566 | ) |
---|
| 567 | if (exportColours) then |
---|
| 568 | ( |
---|
| 569 | color_string = (v[7] as string) + " " + (v[8] as string) + " " + (v[9] as string) + " " +(v[10] as string); |
---|
| 570 | format("\t\t\t\t\t\t<colour_diffuse value=\"%\" />\n") color_string to:outFile ; |
---|
| 571 | ) |
---|
| 572 | format("\t\t\t\t\t</vertex>\n") to:outFile ; |
---|
| 573 | ) |
---|
| 574 | format("\t\t\t\t</vertexbuffer>\n") to:outFile ; |
---|
| 575 | format("\t\t\t</geometry>\n") to:outFile ; |
---|
| 576 | |
---|
| 577 | |
---|
| 578 | -- and now bone assignments (and skeleton), if there is at least one element in boneAssignments array. |
---|
| 579 | if ((arrayLength boneAssignments[matId]) != 0) then |
---|
| 580 | ( |
---|
| 581 | hasSkeleton = true ; |
---|
| 582 | if (not g_MAX) then |
---|
| 583 | format "- writing bone assignments...\n" |
---|
| 584 | format("\t\t\t<boneassignments>\n") to:outFile ; |
---|
| 585 | |
---|
| 586 | for a in boneAssignments[matId] do |
---|
| 587 | ( |
---|
| 588 | format("\t\t\t\t<vertexboneassignment vertexindex=\"%\" boneindex=\"%\" weight=\"%\" />\n") a[1] a[2] a[3] to:outFile ; |
---|
| 589 | ) |
---|
| 590 | format("\t\t\t</boneassignments>\n") to:outFile ; |
---|
| 591 | ) |
---|
| 592 | |
---|
| 593 | -- submesh end |
---|
| 594 | format("\t\t</submesh>\n") to:outFile ; |
---|
| 595 | ) |
---|
| 596 | |
---|
| 597 | -- submeshes end |
---|
| 598 | format("\t</submeshes>\n") to:outFile ; |
---|
| 599 | |
---|
| 600 | -- Skeleton link if there is at least one bone assignement. |
---|
| 601 | if (hasSkeleton) then |
---|
| 602 | ( |
---|
| 603 | t = filterstring outName "\\" ; |
---|
| 604 | format ("\t<skeletonlink name=\"%\"/>\n") (t[arrayLength t] + ".skeleton") to:outFile ; |
---|
| 605 | ) |
---|
| 606 | |
---|
| 607 | format("</mesh>\n") to: outFile ; |
---|
| 608 | |
---|
| 609 | if (not g_MAX) then |
---|
| 610 | ( |
---|
| 611 | close outFile ; |
---|
| 612 | ) |
---|
| 613 | else |
---|
| 614 | ( |
---|
| 615 | if (g_MAX_use_listener) then |
---|
| 616 | format("</ogrestartdata>\n") to: outFile; |
---|
| 617 | ) |
---|
| 618 | ) |
---|
| 619 | |
---|
| 620 | |
---|
| 621 | --------------------------------- |
---|
| 622 | -- writes the mesh: main function |
---|
| 623 | --------------------------------- |
---|
| 624 | |
---|
| 625 | function writeMesh pmesh exportOptions out_name = |
---|
| 626 | ( |
---|
| 627 | local m,sk,outFile,message,phy ; |
---|
| 628 | |
---|
| 629 | m = snapshotAsMesh pmesh ; |
---|
| 630 | |
---|
| 631 | -- tries to find errors |
---|
| 632 | message = exploreMesh pmesh exportOptions.exportUV exportOptions.exportColours; |
---|
| 633 | if (message != "OK") then |
---|
| 634 | ( |
---|
| 635 | MessageBox ("\n There is a problem with your mesh:\n" + message + "\n\nOperation aborted") ; |
---|
| 636 | delete m ; |
---|
| 637 | return false ; |
---|
| 638 | ) |
---|
| 639 | else |
---|
| 640 | ( |
---|
| 641 | format "\n\n" |
---|
| 642 | format "------------------------------------------\n" |
---|
| 643 | format "------- OGRE Mesh Exporter Log -------\n" |
---|
| 644 | format "----- -----\n" |
---|
| 645 | |
---|
| 646 | -- get the skin modifier ( may be undefined ) |
---|
| 647 | -- and physique modifier |
---|
| 648 | phy = getPhysique pmesh ; |
---|
| 649 | sk = getSkin pmesh; |
---|
| 650 | |
---|
| 651 | if (sk != undefined) then |
---|
| 652 | format "Skin modifier detected.\n" |
---|
| 653 | |
---|
| 654 | if (phy != undefined) then |
---|
| 655 | format "Physique modifier detected.\n" |
---|
| 656 | |
---|
| 657 | -- if not undefined, skin modifier is selected in modifier tab. Or there will be an error. |
---|
| 658 | if (sk != undefined) then |
---|
| 659 | ( |
---|
| 660 | -- in order to perform, skin should be opened |
---|
| 661 | max modify mode ; |
---|
| 662 | modPanel.setCurrentObject pmesh.modifiers[#Skin] ; |
---|
| 663 | ) |
---|
| 664 | |
---|
| 665 | -- physique |
---|
| 666 | if (phy != undefined) then |
---|
| 667 | ( |
---|
| 668 | -- in order to perform, skin should be opened |
---|
| 669 | max modify mode ; |
---|
| 670 | modPanel.setCurrentObject pmesh.modifiers[#Physique] ; |
---|
| 671 | --physiqueOps.setInitialPose pmesh true ; |
---|
| 672 | ) |
---|
| 673 | |
---|
| 674 | OgreExportObject.exportProgress.value = 0; |
---|
| 675 | |
---|
| 676 | -- retieving material |
---|
| 677 | if (pmesh.material != undefined) then ( |
---|
| 678 | materialName = pmesh.material.name ; |
---|
| 679 | replaceSpaces materialName ; |
---|
| 680 | ) |
---|
| 681 | else materialName = pmesh.name + "Material" ; |
---|
| 682 | |
---|
| 683 | format "Material name exported : \n- %/*** \n" materialName ; |
---|
| 684 | |
---|
| 685 | -- retrieving all datas |
---|
| 686 | format "Retrieving vertices and faces data : \n" ; |
---|
| 687 | |
---|
| 688 | getDatas m exportOptions.flipyz exportOptions.scale exportOptions.flipNormal exportOptions.exportUV exportOptions.UVchannels exportOptions.exportColours exportOptions.exportHelpers sk phy; |
---|
| 689 | |
---|
| 690 | -- writing in the file |
---|
| 691 | format "Writing in file %.mesh.xml : \n" out_name ; |
---|
| 692 | WriteM exportOptions.exportUV exportOptions.UVchannels exportOptions.exportColours pmesh.material out_name ; |
---|
| 693 | |
---|
| 694 | if (not g_MAX) then |
---|
| 695 | format "Export successful.\n" |
---|
| 696 | |
---|
| 697 | delete m ; |
---|
| 698 | |
---|
| 699 | if (not g_MAX) then |
---|
| 700 | ( |
---|
| 701 | format "----- -----\n" |
---|
| 702 | format "---------- END ---------\n" |
---|
| 703 | format "------------------------------------------\n" |
---|
| 704 | ) |
---|
| 705 | |
---|
| 706 | MessageBox ("Exporting mesh successful !") ; |
---|
| 707 | |
---|
| 708 | OgreExportObject.exportProgress.value = 100; |
---|
| 709 | ) |
---|
| 710 | return true ; |
---|
| 711 | ) |
---|
| 712 | |
---|
| 713 | |
---|
| 714 | |
---|