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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
| -------------------------------------------------------------------------------------
-- ThreeJSExporter.ms
-- Exports geometry from 3ds max to Three.js models in ASCII JSON format v3
-- By alteredq / http://alteredqualia.com
-------------------------------------------------------------------------------------
rollout ThreeJSExporter "ThreeJSExporter"
(
-- Variables
local ostream,
headerFormat = "\"metadata\":
{
\"sourceFile\": \"%\",
\"generatedBy\": \"3ds max ThreeJSExporter\",
\"formatVersion\": 3.1,
\"vertices\": %,
\"normals\": %,
\"colors\": %,
\"uvs\": %,
\"triangles\": %,
\"materials\": %
},
",
vertexFormat = "%,%,%",
vertexNormalFormat = "%,%,%",
UVFormat = "%,%",
triFormat = "%,%,%,%",
triUVFormat = "%,%,%,%,%,%,%",
triNFormat = "%,%,%,%,%,%,%",
triUVNFormat = "%,%,%,%,%,%,%,%,%,%",
footerFormat = "}"
-------------------------------------------------------------------------------------
-- User interface
group "ThreeJSExporter v0.8"
(
label msg "Exports selected meshes in Three.js ascii JSON format" align:#left
hyperLink lab1 "Original source at GitHub" address:"https://github.com/alteredq/three.js/blob/master/utils/exporters/max/ThreeJSExporter.ms" color:(color 255 120 0) align:#left
label dummy1 "--------------------------------------------------------" align:#left
checkbox exportColor "Export vertex colors" checked:false enabled:true
checkbox exportUv "Export uvs" checked:true enabled:true
checkbox exportNormal "Export normals" checked:true enabled:true
checkbox smoothNormal "Use vertex normals" checked:false enabled:true
label dummy2 "--------------------------------------------------------" align:#left
checkbox flipYZ "Flip YZ" checked:true enabled:true
checkbox flipUV "Flip UV" checked:false enabled:true
checkbox flipFace "Flip all faces" checked:false enabled:true
checkbox autoflipFace "Try fixing flipped faces" checked:false enabled:true
label dummy3 "--------------------------------------------------------" align:#left
button btn_export "Export selected objects"
)
-------------------------------------------------------------------------------------
-- Dump vertices
function DumpVertices src =
(
Format "\"vertices\": [" to:ostream
num = src.count
if num > 0 then
(
for i = 1 to num do
(
vert = src[i]
if flipYZ.checked then
(
x = vert.x
y = vert.z
z = vert.y
z *= -1
)
else
(
x = vert.x
y = vert.y
z = vert.z
)
Format vertexFormat x y z to:ostream
if i < num then Format "," to:ostream
)
)
Format "],\n\n" to:ostream
)
-------------------------------------------------------------------------------------
-- Dump colors
function DumpColors src useColors =
(
Format "\"colors\": [" to:ostream
num = src.count
if num > 0 and useColors then
(
for i = 1 to num do
(
col = src[i]
r = col.r as Integer
g = col.g as Integer
b = col.b as Integer
hexNum = ( bit.shift r 16 ) + ( bit.shift g 8 ) + b
-- hexColor = formattedPrint hexNum format:"#x"
-- Format "%" hexColor to:ostream
decColor = formattedPrint hexNum format:"#d"
Format "%" decColor to:ostream
if i < num then Format "," to:ostream
)
)
Format "],\n\n" to:ostream
)
-------------------------------------------------------------------------------------
-- Dump normals
function DumpNormals src =
(
Format "\"normals\": [" to:ostream
num = src.count
if num > 0 and exportNormal.checked then
(
for i = 1 to num do
(
normal = src[i]
normal = normalize normal as point3
if flipYZ.checked then
(
x = normal.x
y = normal.z
z = normal.y
z *= -1
)
else
(
x = normal.x
y = normal.y
z = normal.z
)
Format vertexNormalFormat x y z to:ostream
if i < num then Format "," to:ostream
)
)
Format "],\n\n" to:ostream
)
-------------------------------------------------------------------------------------
-- Dump uvs
function DumpUvs src =
(
Format "\"uvs\": [[" to:ostream
num = src.count
if num > 0 and exportUv.checked then
(
for i = 1 to num do
(
uvw = src[i]
u = uvw.x
if flipUV.checked then
(
v = 1 - uvw.y
)
else
(
v = uvw.y
)
Format UVFormat u v to:ostream
if i < num then Format "," to:ostream
)
)
Format "]],\n\n" to:ostream
)
-------------------------------------------------------------------------------------
-- Dump faces
function DumpFaces src useColors =
(
Format "\"faces\": [" to:ostream
num = src.count
if num > 0 then
(
for i = 1 to num do
(
zface = src[i]
fv = zface[1]
fuv = zface[2]
m = zface[3] - 1
fc = zface[4]
needsFlip = zface[5]
isTriangle = true
hasMaterial = true
hasFaceUvs = false
hasFaceVertexUvs = ((classof fuv == Point3) and exportUv.checked)
hasFaceNormals = false
hasFaceVertexNormals = (exportNormal.checked)
hasFaceColors = false
hasFaceVertexColors = ((classof fc == Point3) and useColors)
faceType = 0
faceType = bit.set faceType 1 (not isTriangle)
faceType = bit.set faceType 2 hasMaterial
faceType = bit.set faceType 3 hasFaceUvs
faceType = bit.set faceType 4 hasFaceVertexUvs
faceType = bit.set faceType 5 hasFaceNormals
faceType = bit.set faceType 6 hasFaceVertexNormals
faceType = bit.set faceType 7 hasFaceColors
faceType = bit.set faceType 8 hasFaceVertexColors
if i > 1 then
(
Format "," faceType to:ostream
)
Format "%" faceType to:ostream
if isTriangle then
(
va = (fv.x - 1) as Integer
vb = (fv.y - 1) as Integer
vc = (fv.z - 1) as Integer
if flipFace.checked or needsFlip then
(
tmp = vb
vb = vc
vc = tmp
)
Format ",%,%,%" va vb vc to:ostream
if hasMaterial then
(
Format ",%" m to:ostream
)
if hasFaceVertexUvs then
(
ua = (fuv.x - 1) as Integer
ub = (fuv.y - 1) as Integer
uc = (fuv.z - 1) as Integer
if flipFace.checked or needsFlip then
(
tmp = ub
ub = uc
uc = tmp
)
Format ",%,%,%" ua ub uc to:ostream
)
if hasFaceVertexNormals then
(
if smoothNormal.checked then
(
-- normals have the same indices as vertices
na = va
nb = vb
nc = vc
)
else
(
-- normals have the same indices as face
na = i - 1
nb = na
nc = na
)
if flipFace.checked or needsFlip then
(
tmp = nb
nb = nc
nc = tmp
)
Format ",%,%,%" na nb nc to:ostream
)
if hasFaceVertexColors then
(
ca = (fc.x - 1) as Integer
cb = (fc.y - 1) as Integer
cc = (fc.z - 1) as Integer
if flipFace.checked or needsFlip then
(
tmp = cb
cb = cc
cc = tmp
)
Format ",%,%,%" ca cb cc to:ostream
)
)
)
)
Format "]\n\n" to:ostream
)
-------------------------------------------------------------------------------------
-- Dump color
function DumpColor pcolor label =
(
r = pcolor.r / 255
g = pcolor.g / 255
b = pcolor.b / 255
fr = formattedPrint r format:".4f"
fg = formattedPrint g format:".4f"
fb = formattedPrint b format:".4f"
Format "\"%\" : [%, %, %],\n" label fr fg fb to:ostream
)
-------------------------------------------------------------------------------------
-- Dump map
function DumpMap pmap label =
(
if classof pmap == BitmapTexture then
(
bm = pmap.bitmap
if bm != undefined then
(
fname = filenameFromPath bm.filename
Format "\"%\" : \"%\",\n" label fname to:ostream
)
)
)
-------------------------------------------------------------------------------------
-- Export materials
function ExportMaterials zmaterials zcolors =
(
Format "\"materials\": [\n" to:ostream
totalMaterials = zmaterials.count
for i = 1 to totalMaterials do
(
mat = zmaterials[i]
Format "{\n" to:ostream
-- debug
Format "\"DbgIndex\" : %,\n" (i-1) to:ostream
if classof mat != BooleanClass then
(
useVertexColors = zcolors[i]
Format "\"DbgName\" : \"%\",\n" mat.name to:ostream
-- colors
DumpColor mat.diffuse "colorDiffuse"
DumpColor mat.ambient "colorAmbient"
DumpColor mat.specular "colorSpecular"
t = mat.opacity / 100
s = mat.glossiness
Format "\"transparency\" : %,\n" t to:ostream
Format "\"specularCoef\" : %,\n" s to:ostream
-- maps
DumpMap mat.diffuseMap "mapDiffuse"
DumpMap mat.ambientMap "mapAmbient"
DumpMap mat.specularMap "mapSpecular"
DumpMap mat.bumpMap "mapBump"
DumpMap mat.opacityMap "mapAlpha"
)
else
(
useVertexColors = false
Format "\"DbgName\" : \"%\",\n" "dummy" to:ostream
DumpColor red "colorDiffuse"
)
Format "\"vertexColors\" : %\n" useVertexColors to:ostream
Format "}" to:ostream
if ( i < totalMaterials ) then Format "," to:ostream
Format "\n\n" to:ostream
)
Format "],\n\n" to:ostream
)
-------------------------------------------------------------------------------------
-- Extract vertices from mesh
function ExtractVertices obj whereto =
(
n = obj.numVerts
for i = 1 to n do
(
v = GetVert obj i
append whereto v
)
)
-------------------------------------------------------------------------------------
-- Extract vertex colors from mesh
function ExtractColors obj whereto =
(
nColors = GetNumCPVVerts obj
if nColors > 0 then
(
for i = 1 to nColors do
(
c = GetVertColor obj i
append whereto c
)
)
)
-------------------------------------------------------------------------------------
-- Extract normals from mesh
function ExtractNormals obj whereto needsFlip =
(
if smoothNormal.checked then
(
num = obj.numVerts
for i = 1 to num do
(
n = GetNormal obj i
if flipFace.checked or needsFlip then
(
n.x *= -1
n.y *= -1
n.z *= -1
)
append whereto n
)
)
else
(
num = obj.numFaces
for i = 1 to num do
(
n = GetFaceNormal obj i
if flipFace.checked or needsFlip then
(
n.x *= -1
n.y *= -1
n.z *= -1
)
append whereto n
)
)
)
-------------------------------------------------------------------------------------
-- Extract uvs from mesh
function ExtractUvs obj whereto =
(
n = obj.numTVerts
for i = 1 to n do
(
v = GetTVert obj i
append whereto v
)
)
-------------------------------------------------------------------------------------
-- Extract faces from mesh
function ExtractFaces objMesh objMaterial whereto allMaterials needsFlip hasVColors offsetVert offsetUv offsetColor =
(
n = objMesh.numFaces
hasUVs = objMesh.numTVerts > 0
useMultiMaterial = false
materialIDList = #()
materialClass = classof objMaterial
if materialClass == StandardMaterial then
(
fm = findItem allMaterials objMaterial
)
else if materialClass == MultiMaterial then
(
useMultiMaterial = true
for i = 1 to n do
(
mID = GetFaceMatID objMesh i
materialIndex = findItem objMaterial.materialIDList mID
if materialIndex > 0 then
(
subMaterial = objMaterial.materialList[materialIndex]
mMergedIndex = findItem allMaterials subMaterial
if mMergedIndex > 0 then
(
materialIDList[mID] = mMergedIndex
)
else
(
materialIDList[mID] = findItem allMaterials false
)
)
else
(
materialIDList[mID] = findItem allMaterials false
)
)
)
else
(
-- undefined material
fm = findItem allMaterials false
)
for i = 1 to n do
(
zface = #()
fv = GetFace objMesh i
fv.x += offsetVert
fv.y += offsetVert
fv.z += offsetVert
if useMultiMaterial then
(
mID = GetFaceMatID objMesh i
fm = materialIDList[mID]
)
if hasUVs then
(
fuv = GetTVFace objMesh i
fuv.x += offsetUv
fuv.y += offsetUv
fuv.z += offsetUv
)
else
(
fuv = false
)
if hasVColors then
(
fc = GetVCFace objMesh i
fc.x += offsetColor
fc.y += offsetColor
fc.z += offsetColor
)
else
(
fc = false
)
append zface fv
append zface fuv
append zface fm
append zface fc
append zface needsFlip
append whereto zface
)
)
-------------------------------------------------------------------------------------
-- Extract materials from eventual multi-material
function ExtractMaterials objMesh objMaterial whereto wheretoColors zname hasVColors =
(
materialClass = classof objMaterial
if materialClass == StandardMaterial then
(
if ( findItem whereto objMaterial ) == 0 then
(
append whereto objMaterial
append wheretoColors hasVColors
)
)
else if materialClass == MultiMaterial then
(
n = objMesh.numFaces
for i = 1 to n do
(
mID = getFaceMatId objMesh i
materialIndex = findItem objMaterial.materialIDList mID
if materialIndex > 0 then
(
subMaterial = objMaterial.materialList[materialIndex]
if ( findItem whereto subMaterial ) == 0 then
(
append whereto subMaterial
append wheretoColors hasVColors
)
)
)
)
else
(
-- unknown or undefined material
append whereto false
append wheretoColors false
)
)
-------------------------------------------------------------------------------------
-- Hack to figure out if normals are messed up
function NeedsFaceFlip node =
(
needsFlip = false
local tmp = Snapshot node
face_normal = normalize ( getfacenormal tmp 1 )
face = getface tmp 1
va = getvert tmp face[1]
vb = getvert tmp face[2]
vc = getvert tmp face[3]
computed_normal = normalize ( cross (vc - vb) (va - vb) )
if distance computed_normal face_normal > 0.1 then needsFlip = true
delete tmp
return needsFlip
)
-------------------------------------------------------------------------------------
-- Extract only things that either already are or can be converted to meshes
function ExtractMesh node =
(
if SuperClassOf node == GeometryClass then
(
needsFlip = false
hasVColors = false
zmesh = SnapshotAsMesh node
if autoflipFace.checked then
(
needsFlip = NeedsFaceFlip node
)
if exportColor.checked and ( getNumCPVVerts zmesh ) > 0 then
(
hasVColors = true
)
return #( zmesh, node.name, node.material, needsFlip, hasVColors )
)
-- Not geometry ... could be a camera, light, etc.
return #( false, node.name, 0, false, false )
)
-------------------------------------------------------------------------------------
-- Export scene
function ExportScene =
(
-- Extract meshes
meshObjects = #()
mergedVertices = #()
mergedNormals = #()
mergedColors = #()
mergedUvs = #()
mergedFaces = #()
mergedMaterials = #()
mergedMaterialsColors = #()
sceneHasVColors = false
for obj in selection do
(
result = ExtractMesh obj
meshObj = result[1]
if ClassOf meshObj == TriMesh then
(
meshName = result[2]
meshMaterial = result[3]
needsFlip = result[4]
hasVColors = result[5]
sceneHasVColors = sceneHasVColors or hasVColors
append meshObjects result
vertexOffset = mergedVertices.count
uvOffset = mergedUvs.count
colorOffset = mergedColors.count
ExtractMaterials meshObj meshMaterial mergedMaterials mergedMaterialsColors meshName hasVColors
ExtractVertices meshObj mergedVertices
ExtractNormals meshObj mergedNormals needsFlip
ExtractColors meshObj mergedColors
ExtractUvs meshObj mergedUvs
ExtractFaces meshObj meshMaterial mergedFaces mergedMaterials needsFlip hasVColors vertexOffset uvOffset colorOffset
)
)
totalVertices = mergedVertices.count
totalFaces = mergedFaces.count
totalMaterials = mergedMaterials.count
totalColors = 0
totalNormals = 0
totalUvs = 0
useColors = false
if sceneHasVColors and exportColor.checked then
(
totalColors = mergedColors.count
useColors = true
)
if exportNormal.checked then
(
totalNormals = mergedNormals.count
)
if exportUv.checked then
(
totalUvs = mergedUvs.count
)
-- Dump objects (debug)
-- Format "// Source objects:\n\n" to:ostream
-- i = 0
-- for obj in meshObjects do
-- (
-- meshName = obj[2]
-- Format "// %: %\n" i meshName to:ostream
-- i += 1
-- )
-- Dump model
Format "{\n\n" to:ostream
-- Dump header
Format headerFormat maxFileName totalVertices totalNormals totalColors totalUvs totalFaces totalMaterials to:ostream
-- Dump all materials in the scene
ExportMaterials mergedMaterials mergedMaterialsColors
-- Dump merged data from all selected geometries
DumpVertices mergedVertices
DumpNormals mergedNormals
DumpColors mergedColors useColors
DumpUvs mergedUvs
DumpFaces mergedFaces useColors
-- Dump footer
Format footerFormat to:ostream
)
-------------------------------------------------------------------------------------
-- Open and prepare a file handle for writing
function GetSaveFileStream =
(
zname = getFilenameFile maxFileName
zname += ".js"
fname = GetSaveFileName filename:zname types:"JavaScript file (*.js)|*.js|All Files(*.*)|*.*|"
if fname == undefined then
(
return undefined
)
ostream = CreateFile fname
if ostream == undefined then
(
MessageBox "Couldn't open file for writing !"
return undefined
)
return ostream
)
-------------------------------------------------------------------------------------
-- Export button click handler
on btn_export pressed do
(
ostream = GetSaveFileStream()
if ostream != undefined then
(
ExportScene()
close ostream
)
)
)
createDialog ThreeJSExporter width:300 |
Partager