-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttach.vsl
132 lines (103 loc) · 2.8 KB
/
Attach.vsl
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
////////////////////////
// Description : Edit by ghomist
extern bool IfKeepOrigin;
void attachEnt(Entity3D ent1,Entity3D ent2)
{
Vector pos,newPos;
Vector pos1,pos2;
ent1.GetPosition(pos1,null);
ent2.GetPosition(pos2,null);
Mesh m1 = ent1.GetCurrentMesh();
Mesh m2 = ent2.GetCurrentMesh();
int vCount1 = m1.GetVertexCount();
int vCount2 = m2.GetVertexCount();
int newCount = vCount1 + vCount2;
m1.SetVertexCount(newCount);
float u,v;
for (int i=0; i<vCount2; i++) {
m2.GetVertexPosition(i,pos);
newPos = -pos + pos1 - pos2;
m1.SetVertexPosition(i+vCount1,-newPos);
m2.GetVertexNormal(i,pos);
newPos = pos;
m1.SetVertexNormal(i+vCount1,newPos);
m2.GetVertexTextureCoordinates(i,u,v,-1);
m1.SetVertexTextureCoordinates(i+vCount1,u,v,-1);
}
int fCount1 = m1.GetFaceCount();
int fCount2 = m2.GetFaceCount();
int newfCount = fCount1 + fCount2;
m1.SetFaceCount(newfCount);
int v1,v2,v3;
for (int i=0; i<fCount2; i++) {
m2.GetFaceVertexIndex(i,v1,v2,v3);
m1.SetFaceVertexIndex(i+fCount1,v1+vCount1,v2+vCount1,v3+vCount1);
m1.SetLitMode(m2.GetLitMode());//
m1.SetFaceMaterial(i+fCount1, m2.GetFaceMaterial(i));
}
}
void axisReset(Entity3D ent)
{
Vector pos;
Vector posNew;
Vector entPos;
Vector entPosNew;
Vector vMax(0,0,0);
Vector vMin(0,0,0);
Vector vCenter(0,0,0);
Vector z(0,0,1);
Vector up(0,1,0),right(1,0,0);
Matrix mat;
ent.GetPosition(entPos,null);
Mesh m = ent.GetCurrentMesh();
int vCount = m.GetVertexCount();
m.GetVertexPosition(0,vMax);
m.GetVertexPosition(0,vMin);
for (int index=0; index<vCount; index++){
m.GetVertexPosition(index,pos);
if (pos.x > vMax.x) vMax.x = pos.x;
if (pos.y > vMax.y) vMax.y = pos.y;
if (pos.z > vMax.z) vMax.z = pos.z;
if (pos.x < vMin.x) vMin.x = pos.x;
if (pos.y < vMin.y) vMin.y = pos.y;
if (pos.z < vMin.z) vMin.z = pos.z;
}
vCenter = (vMax + vMin)/2;
for (int index=0; index<vCount; index++){
m.GetVertexPosition(index,pos);
posNew = pos - vCenter;
m.SetVertexPosition(index,posNew);
}
entPosNew = entPos + vCenter;
ent.SetPosition(entPosNew,null,false);
mat = ent.GetLocalMatrix();
for (int index=0; index<vCount; index++){
m.GetVertexPosition(index,pos);
pos.Rotate(mat);
m.SetVertexPosition(index,pos);
m.GetVertexNormal(index,pos);
pos.Rotate(mat);
m.SetVertexNormal(index,pos);
}
ent.SetOrientation(z,up,right,null,false);
}
void main()
{
Entity3D firstEnt;
bool first = true;
int count = ac.selection.Size();
for (int i=0; i<count; ++i) {
Entity3D ent = Entity3D.Cast(ac.selection[i]);
if (!ent) continue;
if (IfKeepOrigin) ent = Entity3D.Cast(ac.Copy(ent,false,true));
if (first) {
firstEnt = ent;
axisReset(firstEnt);
first = false;
} else {
axisReset(ent);
attachEnt(firstEnt,ent);
ac.DestroyObject(ent,false);
}
}
}