This repository has been archived by the owner on Jul 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxScriptUtils.ms
1298 lines (1056 loc) · 37.3 KB
/
MaxScriptUtils.ms
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
/*
#####################################################
MaxScriptUtils
---------------------------------------------------
Utilidades generales de MaxScript
#####################################################
*/
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Validar que un objeto sea del tipo Geometry
*/
function Utils_isGeometry o = (
Superclassof o == Geometryclass and classof o != TargetObject
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Invierte el orden de los elementos de un array.
* Devuelve un nuevo array con el orden invertido.
*/
function Utils_reverseArray arrayObj = (
local invertedArray = #()
for i = arrayObj.count to 1 by -1 do (
append invertedArray arrayObj[i]
)
invertedArray
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Truncar precision innecesaria en valores cerca de cero
*/
function Utils_cleanupFloat f = (
if ((f > -0.0001) and (f < 0.0001)) do f = 0
f
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Truncar precision innecesaria en valores cerca de cero
*/
function Utils_cleanupPoint3 pnt3 = (
if ((pnt3.x > -0.0001) and (pnt3.x < 0.0001)) do pnt3.x = 0
if ((pnt3.y > -0.0001) and (pnt3.y < 0.0001)) do pnt3.y = 0
if ((pnt3.z > -0.0001) and (pnt3.z < 0.0001)) do pnt3.z = 0
pnt3
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Convierte un valor de posicion segun el formato de exportacion elegido
*/
function Utils_formatPositionValue p exportFormat = (
--DirectX format
if(exportFormat == 1) then (
newP = Utils_cleanupPoint3 (point3 p.x p.z p.y)
--OpenGL format
) else if(exportFormat == 2) then (
newP = Utils_cleanupPoint3 (point3 p.x p.z (-p.y))
--RAW format
) else (
newP = Utils_cleanupPoint3 (point3 p.x p.y p.z)
)
newP
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Convierte un valor de escala segun el formato de exportacion elegido
*/
function Utils_formatScaleValue p exportFormat = (
--DirectX format
if(exportFormat == 1) then (
newP = Utils_cleanupPoint3 (point3 p.x p.z p.y)
--OpenGL format
) else if(exportFormat == 2) then (
newP = Utils_cleanupPoint3 (point3 p.x p.z (-p.y))
--RAW format
) else (
newP = Utils_cleanupPoint3 (point3 p.x p.y p.z)
)
newP
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Convierte un valor de rotacion en Quaternion segun el formato de exportacion elegido
*/
function Utils_formatQuaternion q exportFormat = (
local x = Utils_cleanupFloat q.x
local y = Utils_cleanupFloat q.y
local z = Utils_cleanupFloat q.z
local w = Utils_cleanupFloat q.w
newQ = quat x z y w
newQ
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Convierte un valor de coordenada de textura segun el formato de exportacion elegido
*/
function Utils_formatTextureCoordinates p exportFormat = (
--DirectX format
if(exportFormat == 1) then (
newP = Utils_cleanupPoint3 (point3 p.x (1-p.y) p.z)
--OpenGL format and RAW format
) else (
newP = Utils_cleanupPoint3 (point3 p.x p.y p.z)
)
newP
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Invierte los ejex Y-Z en una matriz de transformacion
*/
function Utils_flipYZTransform Tform = (
local axis1,axis2,axis3,t,m
-- computes the matrix
axis1 = point3 1 0 0 ;
axis2 = point3 0 0 1 ;
axis3 = point3 0 -1 0 ;
t = point3 0 0 0 ;
m=matrix3 axis1 axis2 axis3 t ;
-- multiplies by the inverse
Tform = Tform*inverse(m) ;
Tform
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Convierte un KeyFrame en un Integer
*/
function Utils_formatKeyFrame t = (
local frameNumber = substring (t as string) 1 ((t as string).count - 1)
frameNumber = (floor (frameNumber as integer) as integer)
frameNumber
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Acomodar objetos que tienen scale negativo.
* Si el objeto pose scale negativo significa que se le aplico Mirror.
* En ese caso se resetea su posicion, escla y rotacion, se aplica XForm y luego NormalModifier
*/
function Utils_resetXFormAndFlip obj = (
--Ver si tiene Scale negativo
if ((obj.scale.x < 0) OR (obj.scale.y < 0) OR (obj.scale.z < 0)) then (
--Reset rotation
local rotvalue = obj.rotation
obj.rotation= (quat 0 0 0 1)
--Obtener inverse de matriz
local M = obj.transform
obj.transform = transMatrix obj.pos
local p = obj.objecttransform * (inverse obj.transform)
--Cargar valores reset
obj.objectoffsetPos = [0,0,0]
obj.objectoffsetRot= (quat 0 0 0 1)
obj.objectoffsetScale = [1,1,1]
M.translation = [0,0,0]
--Multiplicar por inversa
M= p * M
--Aplicar modifier XForm con matriz calculada
local xformMod = XForm()
addmodifier obj xformMod
xformMod.gizmo.transform = M
--Aplicar modifier Normal
obj.rotation=rotvalue
addmodifier obj (Normalmodifier flip:true)
)
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Copia un bitmap a un archivo destino
*/
function Utils_copyTexture targetBitmap destPath = (
deleteFile destPath
local newBitmap = Bitmap targetBitmap.width targetBitmap.height
newBitmap.filename = destPath
copy targetBitmap newBitmap
save newBitmap
--copyFile targetBitmap.filename destPath
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Devuelve una lista con todos los layers existentes. Solo devuelve aquellos layers que poseen al menos un objeto adentro.
*/
function Utils_getExistingLayersWithNodes = (
local layersList = #()
for i = 0 to layerManager.count-1 do (
ilayer = layerManager.getLayer i
ilayer.nodes &layerNodes
if layerNodes.count > 0 then (
append layersList ilayer
)
)
layersList
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Estructura para almacenar SubMaterials
*/
struct TgcSubMaterial
(
parentMaterial,
materialNode,
materialClass,
withBitmap,
alphaBlendEnable
)
/*
* Estructura para almacenar Materials a exportar
*/
struct TgcMaterial
(
materialNode,
materialClass,
sceneMaterialIndex,
matId,
withBitmap,
alphaBlendEnable,
subMaterials
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Devuelve true si un modelo tiene Material asignado.
* Solo contempla StandardMaterial y MultiMaterial.
* El Material se busca dentro de SceneMaterials para ver si existe.
*/
function Utils_meshHasMaterial selectedMesh = (
if selectedMesh.material == undefined then (
return false
)
--Buscar en sceneMaterials
local matId = findItem sceneMaterials selectedMesh.material
if (matId == 0) or (matId > sceneMaterials.count) then (
return false
)
--Ver si el tipo de Material es correcto
local matClass = classof selectedMesh.material
if (matClass != StandardMaterial) and (matClass != MultiMaterial) then (
return false
)
return true
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Devuelve true si un Material tiene un DiffuseMap con Bitmap
*/
function Utils_materialHasBitmap matNode = (
local flag = matNode.diffuseMap != undefined and (hasProperty matNode.diffuseMap "bitmap") and matNode.diffuseMap.bitmap != undefined
flag
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Devuelve true si un Material tiene un OpacityMap con Bitmap
*/
function Utils_materialHasOpacityMapBitmap matNode = (
local flag = matNode.opacityMap != undefined and (hasProperty matNode.opacityMap "bitmap") and matNode.opacityMap .bitmap != undefined
flag
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Crea un array con todos los Materials que van a ser exportados.
* Devuelve una lista de TgcMaterial
* Solo tienen en cuenta los Materials que son del tipo StandardMaterial o MultiMaterial.
* Solo se tienen en cuenta aquellos Materials que son realmente utilizados por algun modelo a exportar
*/
function Utils_createMaterialList objectList = (
local tgcMaterials = #()
for i = 1 to sceneMaterials.count do (
local matNode = sceneMaterials[i]
local matClass = classof matNode
--Validar que tipo de Material es
if (matClass == StandardMaterial) or (matClass == MultiMaterial) then (
--Ver si algun modelo utiliza este material
local materialIsUsed = false
for meshNode in objectList do (
local hasMaterial = Utils_meshHasMaterial meshNode
if hasMaterial == true then (
if meshNode.material == matNode then (
materialIsUsed = true
break
)
)
)
if materialIsUsed == false then (
continue
)
local withBitmap = false
local alphaBlendEnable = false
local subMaterials = #()
--Analizar si tiene Bitmap
if matClass == StandardMaterial then (
withBitmap = Utils_materialHasBitmap matNode
alphaBlendEnable = Utils_materialHasOpacityMapBitmap matNode
--Crear SubMaterials
) else if matClass == MultiMaterial then (
for subMat in matNode.materialList do (
--Solo tener en cuenta SubMaterials que sean StandardMaterial
local subMatClass = classof subMat
if subMatClass == StandardMaterial then (
local subMatWithBitmap = Utils_materialHasBitmap subMat
local subMapAlphaBlendEnable = Utils_materialHasOpacityMapBitmap subMat
if subMapAlphaBlendEnable == true then (
alphaBlendEnable = true
)
local tgcSubMat = TgcSubMaterial materialNode:subMat materialClass:subMatClass withBitmap:subMatWithBitmap alphaBlendEnable:subMapAlphaBlendEnable
append subMaterials tgcSubMat
)
)
)
--agregar a la lista
local tgcMat = TgcMaterial materialNode:matNode materialClass:matClass sceneMaterialIndex:i matId:-1 withBitmap:withBitmap subMaterials:subMaterials alphaBlendEnable:alphaBlendEnable
append tgcMaterials tgcMat
tgcMat.matId = tgcMaterials.count
)
)
--Cargar parentMaterial en SubMaterials
for tgcMat in tgcMaterials do (
if tgcMat.subMaterials.count > 0 then (
for tgcSubMat in tgcMat.subMaterials do (
tgcSubMat.parentMaterial = tgcMat
)
)
)
tgcMaterials
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Devuelve el TgcMaterial de un modelo, en base a la lista de Materials a exportar.
* Se debe llamar a antes a la funcion meshHasMaterial para saber si el modelo tiene Material
*/
function Utils_getMeshMaterial selectedMesh tgcMaterials = (
for tgcMat in tgcMaterials do (
local meshMat = selectedMesh.material
if meshMat == tgcMat.materialNode then (
return tgcMat
)
)
local flag = undefined
flag
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Estructura para almacenar un BoundingBox
*/
struct TgcBoundingBox
(
pMin,
pMax
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Crea el TgcBoundingBox de un EditableMesh, recorriendo los vertices del mismo.
*/
function Utils_getMeshBoundingFromVertices tmesh exportFormat = (
local minX = undefined
local minY = undefined
local minZ = undefined
local maxX = undefined
local maxY = undefined
local maxZ = undefined
--Buscar puntos extremos
for i = 1 to tmesh.numverts do (
local vert = getVert tmesh i
vert = Utils_formatPositionValue vert exportFormat
if (minX == undefined) or (vert.x < minX) then (
minX = vert.x
)
if (minY == undefined) or (vert.y < minY) then (
minY = vert.Y
)
if (minZ == undefined) or (vert.z < minZ) then (
minZ = vert.z
)
if (maxX == undefined) or (vert.x > maxX) then (
maxX = vert.x
)
if (maxY == undefined) or (vert.y > maxY) then (
maxY = vert.y
)
if (maxZ == undefined) or (vert.z > maxZ) then (
maxZ = vert.z
)
)
local minP = point3 minX minY minZ
local maxP = point3 maxX maxY maxZ
local tgcbb = TgcBoundingBox pMin:minP pMax:maxP
tgcbb
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Crea el TgcBoundingBox de un modelo
*/
function Utils_getMeshBoundingBox selectedMesh exportFormat = (
/*Forma tradicional, no devuelve bien los valores cuando el objeto esta rotado
boundingBox = nodeLocalBoundingBox selectedMesh
bbMin = Utils_formatPositionValue boundingBox[1] exportFormat
bbMax = Utils_formatPositionValue boundingBox[2] exportFormat
tgcbb = TgcBoundingBox pMin:bbMin pMax:bbMax
*/
local tmesh = snapshotAsMesh selectedMesh
local bb = Utils_getMeshBoundingFromVertices tmesh exportFormat
bb
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Crea el TgcBoundingBox de un modelo en base a su posicion en un Frame determinado
*/
function Utils_getMeshBoundingBoxAtTime selectedMesh exportFormat t = (
local tmesh = at time t snapshotAsMesh selectedMesh
local bb = Utils_getMeshBoundingFromVertices tmesh exportFormat
bb
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Crea un TgcBoundingBox que contenga a todos los TgcBoundingBox pasados en la lista
*/
function Utils_createBoundingBoxForAll tgcBoundingBoxes = (
local minX = undefined
local minY = undefined
local minZ = undefined
local maxX = undefined
local maxY = undefined
local maxZ = undefined
--Buscar puntos extremos
for bb in tgcBoundingBoxes do (
if (minX == undefined) or (bb.pMin.x < minX) then (
minX = bb.pMin.x
)
if (minY == undefined) or (bb.pMin.y < minY) then (
minY = bb.pMin.y
)
if (minZ == undefined) or (bb.pMin.z < minZ) then (
minZ = bb.pMin.z
)
if (maxX == undefined) or (bb.pMax.x > maxX) then (
maxX = bb.pMax.x
)
if (maxY == undefined) or (bb.pMax.y > maxY) then (
maxY = bb.pMax.y
)
if (maxZ == undefined) or (bb.pMax.z > maxZ) then (
maxZ = bb.pMax.z
)
)
local minP = point3 minX minY minZ
local maxP = point3 maxX maxY maxZ
local tgcbb = TgcBoundingBox pMin:minP pMax:maxP
tgcbb
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Devuelve un array con los 8 vertices extremos de un BoundingBox
*/
function Utils_computeBoundingBoxCorners tgcBB = (
local corners = #()
append corners tgcBB.pMin
append corners (point3 tgcBB.pMin.x tgcBB.pMin.y tgcBB.pMax.z)
append corners (point3 tgcBB.pMin.x tgcBB.pMax.y tgcBB.pMin.z)
append corners (point3 tgcBB.pMin.x tgcBB.pMax.y tgcBB.pMax.z)
append corners (point3 tgcBB.pMax.x tgcBB.pMin.y tgcBB.pMin.z)
append corners (point3 tgcBB.pMax.x tgcBB.pMin.y tgcBB.pMax.z)
append corners (point3 tgcBB.pMax.x tgcBB.pMax.y tgcBB.pMin.z)
append corners tgcBB.pMax
corners
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Devuelve el centro de un BoundingBox
*/
function Utils_computeBoundingBoxCenter tgcBB = (
local size = tgcBB.pMax - tgcBB.pMin
local p = point3 (tgcBB.pMin.x + size.x / 2) (tgcBB.pMin.y + size.y / 2) (tgcBB.pMin.z + size.z / 2)
p
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Estructura para almacenar un Plano
*/
struct TgcPlane
(
normal,
d
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Crea un plano a partir de tres puntos de un poligono
* Hay que especificar los puntos en clockwise-order para que tome la direccion hacia lado requerido
*/
function Utils_createPlaneFromPoints p1 p2 p3 exportFormat = (
local vectorA = p2 - p1
local vectorB = p3 - p1
local planeNormal = normalize (cross vectorA vectorB)
--planeNormal = normalize (cross vectorB vectorA) --Normal hacia afuera
local d = -(planeNormal.x * p1.x + planeNormal.y * p1.y + planeNormal.z * p1.z)
local tgcPlaneObj = TgcPlane normal:(Utils_formatPositionValue planeNormal exportFormat) d:(Utils_cleanupFloat d)
tgcPlaneObj
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Devuelve un array de TgcPlane con un plano por cada cara del mesh.
* Trata el mesh como un EditablePoly. El mesh debe ser convexo.
* Las normales de los planos se devuelven apuntando hacia afuera.
*/
function Utils_getPlanesFromMesh meshNode exportFormat = (
--Tratar mesh como un cuerpo Convexo
local tPoly = convertToPoly meshNode
local numFaces = polyop.getNumFaces tPoly
--Obtener planos de las caras del poligono
local facePlanes = #()
for f = 1 to numFaces do (
local faceVerts = polyop.getFaceVerts tPoly f
local vert1 = polyop.getVert tPoly faceVerts[1]
local vert2 = polyop.getVert tPoly faceVerts[2]
local vert3 = polyop.getVert tPoly faceVerts[3]
--facePlane = Utils_createPlaneFromPoints vert1 vert2 vert3 exportFormat
local facePlane = Utils_createPlaneFromPoints vert3 vert2 vert1 exportFormat
append facePlanes facePlane
)
facePlanes
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Dado un poligono plano en 3D de una sola cara, devuelve un array de vertices con los vertices extremos
* que definen la cara.
* Trata el mesh como un EditablePoly y obtiene los vertices de la primera cara.
* Los vertices se devuelven en el orden original
* Devuelve undefined si el mesh tiene mas de una cara.
*/
function Utils_getPolygonVertices meshNode exportFormat = (
local tPoly = convertToPoly meshNode
local numFaces = polyop.getNumFaces tPoly
local polyVertices = #()
--Obtener vertices de la primera cara del poligono
if numFaces == 1 then (
local faceVerts = polyop.getFaceVerts tPoly 1
for i = 1 to faceVerts.count do (
local v = polyop.getVert tPoly faceVerts[i]
append polyVertices (Utils_formatPositionValue v exportFormat)
)
return polyVertices
--Tiene mas de una cara
) else (
return undefined
)
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Estructura almacenar user properties
*/
struct TgcUserProperty
(
key,
value
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Devuelve un array de TgcUserProperty con las user properties del nodo
*/
function Utils_getUserPropertiesArray theNode = (
local propBuffer = getUserPropBuffer theNode
local tokenArray = filterString propBuffer "=\n"
if tokenArray.count < 1 then (
return undefined
)
if (mod tokenArray.count 2) != 0 then (
return undefined
)
local propArray = #()
local count = tokenArray.count / 2
for i = 1 to count do (
local key = trimRight (trimLeft tokenArray[i*2-1])
local value = trimRight (trimLeft tokenArray[i*2])
local tgcProp = TgcUserProperty key:key value:value
append propArray tgcProp
)
propArray
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Calcula el vector tangente y binormal de un triangulo.
* Devuelve un array con dos valores (tangent, binormal)
* Ya estan normalizados.
* Basado en: http://www.dhpoware.com/demos/d3d9NormalMapping.html
*/
function Utils_computeTangentBinormal pos1 pos2 pos3 texCoord1 texCoord2 texCoord3 normal1 = (
local tangent = point3 0 0 0
local binormal = point3 0 0 0
/* Given the 3 vertices (position and texture coordinates) of a triangle
* calculate and return the triangle's tangent vector. The handedness of
* the local coordinate system is stored in tangent.w. The bitangent is
* then: float3 bitangent = cross(normal, tangent.xyz) * tangent.w.
*/
/* Create 2 vectors in object space.
*
* edge1 is the vector from vertex positions v1 to v2.
* edge2 is the vector from vertex positions v1 to v3.
*/
local edge1 = normalize (pos2 - pos1)
local edge2 = normalize (pos3 - pos1)
/* Create 2 vectors in tangent (texture) space that point in the same
* direction as edge1 and edge2 (in object space).
*
* texEdge1 is the vector from texture coordinates texCoord1 to texCoord2.
* texEdge2 is the vector from texture coordinates texCoord1 to texCoord3.
*/
local texEdge1 = normalize (point2 (texCoord2.x - texCoord1.x) (texCoord2.y - texCoord1.y))
local texEdge2 = normalize (point2 (texCoord3.x - texCoord1.x) (texCoord3.y - texCoord1.y))
/* These 2 sets of vectors form the following system of equations:
*
* edge1 = (texEdge1.x * tangent) + (texEdge1.y * bitangent)
* edge2 = (texEdge2.x * tangent) + (texEdge2.y * bitangent)
*
* Using matrix notation this system looks like:
*
* [ edge1 ] [ texEdge1.x texEdge1.y ] [ tangent ]
* [ ] = [ ] [ ]
* [ edge2 ] [ texEdge2.x texEdge2.y ] [ bitangent ]
*
* The solution is:
*
* [ tangent ] 1 [ texEdge2.y -texEdge1.y ] [ edge1 ]
* [ ] = ------- [ ] [ ]
* [ bitangent ] det A [-texEdge2.x texEdge1.x ] [ edge2 ]
*
* where:
* [ texEdge1.x texEdge1.y ]
* A = [ ]
* [ texEdge2.x texEdge2.y ]
*
* det A = (texEdge1.x * texEdge2.y) - (texEdge1.y * texEdge2.x)
*
* From this solution the tangent space basis vectors are:
*
* tangent = (1 / det A) * ( texEdge2.y * edge1 - texEdge1.y * edge2)
* bitangent = (1 / det A) * (-texEdge2.x * edge1 + texEdge1.x * edge2)
* normal = cross(tangent, bitangent)
*/
local det = (texEdge1.x * texEdge2.y) - (texEdge1.y * texEdge2.x)
if abs(det) < 0.0001 then ( -- almost equal to zero
tangent.x = 1.0
tangent.y = 0.0
tangent.z = 0.0
binormal.x = 0.0
binormal.y = 1.0
binormal.z = 0.0
) else (
det = 1.0 / det;
tangent.x = (texEdge2.y * edge1.x - texEdge1.y * edge2.x) * det
tangent.y = (texEdge2.y * edge1.y - texEdge1.y * edge2.y) * det
tangent.z = (texEdge2.y * edge1.z - texEdge1.y * edge2.z) * det
--tangent.w = 0.0;
binormal.x = (-texEdge2.x * edge1.x + texEdge1.x * edge2.x) * det
binormal.y = (-texEdge2.x * edge1.y + texEdge1.x * edge2.y) * det
binormal.z = (-texEdge2.x * edge1.z + texEdge1.x * edge2.z) * det
tangent = normalize tangent
binormal = normalize binormal
)
/* Calculate the handedness of the local tangent space.
* The bitangent vector is the cross product between the triangle face
* normal vector and the calculated tangent vector. The resulting bitangent
* vector should be the same as the bitangent vector calculated from the
* set of linear equations above. If they point in different directions
* then we need to invert the cross product calculated bitangent vector.
*/
local b = cross normal1 tangent
local w = dot b binormal
if w < 0.0 then (
w = -1.0
) else (
w = 1.0
)
binormal = b * w;
results = #(tangent, binormal)
results
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
--##############################################################################
--################################### XML PARSER ###################################
--##############################################################################
--Devuelve la posicion de la ultima aparicion de la cadena buscada
function Utils_lastIndexOf string search_string =
(
local lastIndex = 0
local text = string
while (index = (findstring text search_string)) != undefined do
(
lastIndex += index
text = substring text (index + 1) text.count
)
if lastIndex == 0 then lastIndex = undefined
lastIndex
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Representa un atributo de un TAG de XML
*/
struct XmlAttribute
(
name,
value
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Representa un TAG de XML
*/
struct XmlElement
(
name,
listAttributes = #(),
content = undefined,
children = #(),
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* PUBLIC
* Devuelve el contenido del TAG con TRIM
*/
function getContentTrim =
(
local aux = trimleft content
aux = trimright aux
return aux
),
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* PUBLIC
* Devuelve el valor del atributo especificado, o undefined si no existe
*/
function getAttributeValue attName =
(
for att in listAttributes do (
if att.name == attName then
(
return att.value
)
)
return undefined
),
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* PUBLIC
* Devuelve una lista de hijos con el nombre especificado, o una lista
* vacia en caso de no haber ninguno
*/
function getChildren childName =
(
local auxChildren = #()
for child in children do (
if child.name == childName then
(
append auxChildren child
)
)
return auxChildren
),
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* PUBLIC
* Devuelve el primer hijo con el nombre especificado, o undefined en caso
* de no encontrarlo
*/
function getChild childName =
(
for child in children do (
if child.name == childName then
(
return child
)
)
return undefined
),
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* PUBLIC
* Devuelve el contenido del hijo especificado
*/
function getChildContent childName =
(
local child = getChild childName
if child != undefined then
return child.content
return undefined
),
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* PUBLIC
* Devuelve el contenido del hijo especificado, haciendo Trim
*/
function getChildContentTrim childName =
(
local childContent = getChildContent childName
if childContent != undefined then
return childContent.getContentTrim()
return undefined
),
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* PUBLIC
* Crea un nuevo atributo con el nombre y el valor especificado
*/
function addAttribute attName attValue =
(
local att = XmlAttribute()
att.name = attName
att.value = attValue
append listAttributes att
),
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* PUBLIC
* Agrega un XmlElement como hijo de este nodo
*/
function addChild xmlElementItem =
(
append children xmlElementItem
)
)
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* Estructura en objetos de un archivo XML
*/
struct XmlDocument
(
root,
---------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* PRIVATE
* Crear un nuevo elemento en base a otro de DotNet.
* Carga nombre y atributos.
* Devuelve el elemento creado.
*/
function createNewElement dotNetElement =
(