/
ArmaturePreview.js
181 lines (145 loc) · 5.04 KB
/
ArmaturePreview.js
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
import Armature from "./Armature.js";
import Shader from "../../fungi/Shader.js";
import Vao from "../../fungi/Vao.js";
import Fungi from "../../fungi/Fungi.js";
import gl from "../../fungi/gl.js";
const ATTRIB_QD_ROT_LOC = 8; //Shader.ATTRIB_JOINT_IDX_LOC = 8;
const ATTRIB_QD_POS_LOC = 9; //Shader.ATTRIB_JOINT_WEIGHT_LOC = 9;
const ATTRIB_LEN_LOC = 10;
const ATTRIB_SCALE_LOC = 11;
class ArmaturePreview{
static init(e){
let arm = (e instanceof Armature)? e : e.com.Armature;
if(arm.joints.length == 0){
console.log("Armature does not have any bones. Bones needed to setup ArmaturePreview");
return e;
}
arm.flatWorldSpace = new Float32Array( arm.joints.length * 8 );
return e;
}
static useStick(e, name){
let verts = [0,0,0,0, 0,1,0,1];
e.com.Drawable.vao = ArmaturePreview.vao(e, name, verts);
e.com.Drawable.drawMode = Fungi.LINE;
e.com.Armature.vaoPreview = e.com.Drawable.vao;
return this;
}
static useDiamond(e, name){
const pxz = 0.06,
py = 0.1;
const verts = [
0, 0, 0, 0, // 0 Bottom
0, 1, 0, 1, // 1 Top
-pxz, py, pxz, 0, // 2 Bot Left
pxz, py, pxz, 0, // 3 Bot Right
pxz, py, -pxz, 0, // 4 Top Right
-pxz, py, -pxz, 0 // 5 Top Left
];
const faces = [ 1,2,3, 1,3,4, 1,4,5, 1,5,2,
0,3,2, 0,4,3, 0,5,4, 0,2,5 ];
e.com.Drawable.vao = ArmaturePreview.vao(e, name, verts, faces);
e.com.Drawable.drawMode = Fungi.TRI;
e.com.Armature.vaoPreview = e.com.Drawable.vao;
return this;
}
static useDiamondWire(e, name){
const pxz = 0.06,
py = 0.1;
const verts = [
0, 0, 0, 0, // 0 Bottom
0, 1, 0, 1, // 1 Top
-pxz, py, pxz, 0, // 2 Bot Left
pxz, py, pxz, 0, // 3 Bot Right
pxz, py, -pxz, 0, // 4 Top Right
-pxz, py, -pxz, 0 // 5 Top Left
];
const faces = [ 1,2,1,3,1,4,1,5,
0,2,0,3,0,4,0,5,
2,3,3,4,4,5,5,2 ];
e.com.Drawable.vao = ArmaturePreview.vao(e, name, verts, faces);
e.com.Drawable.drawMode = Fungi.LINE;
e.com.Armature.vaoPreview = e.com.Drawable.vao;
return this;
}
static useCircleLine(e, name){
const radius = 0.08;
const seg = 10;
let c, s, a,
piOffset = -Math.PI/2,
pi2 = Math.PI*2,
segInv = 1 / seg,
verts = [];
verts.push( 0, 1, -radius, 1);
for(let i=0; i <= seg; i++){
a = piOffset + pi2 * segInv * i;
c = Math.cos(a);
s = Math.sin(a);
verts.push( radius*c, 0, radius*s, 0);
}
e.com.Drawable.vao = ArmaturePreview.vao(e, name, verts);
e.com.Drawable.drawMode = Fungi.LINE_STRIP;
e.com.Armature.vaoPreview = e.com.Drawable.vao;
return this;
}
static vao(e, vName, verts, faces = null){
let arm = (e instanceof Armature)? e : e.com.Armature;
ArmaturePreview.flattenData(arm);
//..........................................
//Get a list of length of the bone that the joint represent
let lenAry = new Float32Array( arm.joints.length );
for(let i=0; i < arm.joints.length; i++){
lenAry[i] = arm.joints[i].length;
}
//..........................................
let oVao = new Vao().create()
.floatBuffer("bVertices", verts, Shader.ATTRIB_POSITION_LOC, 4)
.floatBuffer("bLengths", lenAry, ATTRIB_LEN_LOC, 1, 0, 0, true, true)
.floatBuffer("bOffset", arm.flatWorldSpace, ATTRIB_QD_ROT_LOC, 4, 32, 0, true, true) // QR (Rotation)
.partitionFloatBuffer(ATTRIB_QD_POS_LOC, 4, 32, 16, true) // QD (Translation)
.setInstanced( arm.joints.length );
if(faces) oVao.indexBuffer("bIndex", faces);
if(arm.useScale){
let aryScale = Armature.flatScale(arm);
oVao.floatBuffer("bScale", aryScale, ATTRIB_SCALE_LOC, 3, 0, 0, true, true);
}
let vao = oVao.finalize(name);
oVao.cleanup();
return vao;
}
static flattenData(e){
let arm = (e instanceof Armature)? e : e.com.Armature;
Armature.flatWorldSpace(arm, arm.flatWorldSpace);
return this;
}
static updateBuffer(e){
gl.ctx.bindBuffer(gl.ctx.ARRAY_BUFFER, e.com.Armature.vaoPreview.bOffset.id);
gl.ctx.bufferSubData(gl.ctx.ARRAY_BUFFER, 0, e.com.Armature.flatWorldSpace, 0, null);
gl.ctx.bindBuffer(gl.ctx.ARRAY_BUFFER, null);
return this;
}
/*
constructor(name, arm, jointLen = 0.5, matName = "ArmaturePreview"){
super(name, null, matName);
this.armature = arm;
this.drawMode = gl.ctx.LINES;
var verts = [0,0,0,0, 0,jointLen,0,1],
offset = arm.getFlatWorldSpace(),
floatBuffer(name, aryData, attrLoc, compLen=3, stride=0, offset=0, isStatic=true, isInstance=false){
oVao = new Vao().create()
.floatBuffer("bVertices", verts, Shader.ATTRIB_POSITION_LOC, 4)
.floatBuffer("bOffset", offset, ATTRIB_QD_ROT_LOC, 4, 32, 0, true, true) // QR (Rotation)
.partitionFloatBuffer(ATTRIB_QD_POS_LOC, 4, 32, 16, true) // QD (Translation)
.setInstanced( offset.length / 8 );
this.vao = oVao.finalize(name);
oVao.cleanup();
}
//TODO Only Update if dirty;
update(){
var offset = this.armature.getFlatWorldSpace(offset);
gl.ctx.bindBuffer(gl.ctx.ARRAY_BUFFER, this.vao.bOffset.id);
gl.ctx.bufferSubData(gl.ctx.ARRAY_BUFFER, 0, offset, 0, null);
gl.ctx.bindBuffer(gl.ctx.ARRAY_BUFFER, null);
}
*/
}
export default ArmaturePreview;