public void getChildJoints(string code) { NumberFormatInfo nf = new CultureInfo("en-US", false).NumberFormat; Stack s = new Stack(); Regex r = new Regex(@"\s*Frame (joint[0-9]+)+"); char[] separators = new char[2]; separators[0] = ','; separators[1] = ';'; string[] el; int currJointIndex = 0; MatchCollection m = r.Matches(code); Match m2; Match m3; for (int i = 0; i < m.Count; i++) { m2 = Regex.Match(m[i].Groups[0].ToString(), @"^\s*"); int depth = m2.ToString().Length / 4; m3 = Regex.Match(code, m[i].Groups[1].ToString() + @" \{\s*FrameTransformMatrix \{([^\}]+)\}"); el = m3.Groups[1].ToString().Split(separators); JointInfo jointInfo = new JointInfo(m[i].Groups[1].ToString()); jointInfo.index = currJointIndex; currJointIndex++; Matrix4x4 t = new Matrix4x4(Double.Parse(el[0], nf), Double.Parse(el[1], nf), Double.Parse(el[2], nf), Double.Parse(el[3], nf), Double.Parse(el[4], nf), Double.Parse(el[5], nf), Double.Parse(el[6], nf), Double.Parse(el[7], nf), Double.Parse(el[8], nf), Double.Parse(el[9], nf), Double.Parse(el[10], nf), Double.Parse(el[11], nf), Double.Parse(el[12], nf), Double.Parse(el[13], nf), Double.Parse(el[14], nf), Double.Parse(el[15], nf)); jointInfo.setTransform(t); int maxJointVertexIndex = readSkinWeights(jointInfo, code); if (maxJointVertexIndex > maxVertexIndex) { maxVertexIndex = maxJointVertexIndex; } while (depth < s.Count) { s.Pop(); } if (depth > 0) { jointInfo.Parent = (JointInfo)s.Peek(); //((JointInfo)s.Peek()).AddChild(jointInfo); } s.Push(jointInfo); } while (s.Count > 1) { s.Pop(); } outputJoint = (JointInfo)s.Peek(); outputSkeleton = new Skeleton(outputJoint); outputSkeleton.BuildJointTable(); }
public JointInfo clone(JointInfo parent) { JointInfo ret = new JointInfo(); ret.guid = new System.Guid(this.guid.ToString()); ret.Name = this.Name; ret.index = this.index; ret.parent = parent; for (int i = 0; i <= 15; i++) { ret.baseTransform[i] = this.baseTransform[i]; ret.animationTransform[i] = this.animationTransform[i]; ret.preTransform[i] = this.preTransform[i]; } for (int i = 0; i < 3; i++) { ret.rotation[i] = this.rotation[i]; } ret.constraints.Clear(); for (int i = 0; i < 3; i++) { Vector2D v = new Vector2D(this.constraints[i].x, this.constraints[i].y); ret.constraints.Add(v); } JointInfo child; for (int i = 0; i < this.children.Count; i++) { child = ((JointInfo)this.children[i]).clone(ret); ret.children.Add(child); } return(ret); }
public int readSkinWeights(JointInfo currJoint, string fileContents) { NumberFormatInfo nf = new CultureInfo("en-US", false).NumberFormat; char[] separators = new char[1]; separators[0] = ','; string[] vertexNums; string[] weights; string[] transformCoords; int influenceCount = 0; int maxJointVertexIndex = 0; Match m = Regex.Match(fileContents, @"SkinWeights \{\s*." + currJoint.shortname + @".;([0-9\s]+);([^;]+);([^;]+);([^;]+);"); bool res = int.TryParse(m.Groups[1].ToString().Trim(), out influenceCount); if (m.ToString() == "") { return(0); } vertexNums = m.Groups[2].ToString().Split(separators); weights = m.Groups[3].ToString().Split(separators); int vertexNum; double weight; for (int i = 0; i < vertexNums.Length; i++) { if (vertexNums[i].Trim() != "") { vertexNum = int.Parse(vertexNums[i].Trim()); weight = Double.Parse(weights[i].Trim(), nf); if (vertexNum > maxJointVertexIndex) { maxJointVertexIndex = vertexNum; } if (skinWeights.ContainsKey(vertexNum)) { skinWeights[vertexNum].Add(currJoint.index, weight); } else { Dictionary <int, double> boneWeights = new Dictionary <int, double>(); boneWeights.Add(currJoint.index, weight); skinWeights.Add(vertexNum, boneWeights); } } } transformCoords = m.Groups[4].ToString().Split(separators); Matrix4x4 inverseBindPoseMatrix = new Matrix4x4(); inverseBindPoseMatrix.m11 = Double.Parse(transformCoords[0].Trim(), nf); inverseBindPoseMatrix.m12 = Double.Parse(transformCoords[1].Trim(), nf); inverseBindPoseMatrix.m13 = Double.Parse(transformCoords[2].Trim(), nf); inverseBindPoseMatrix.m14 = Double.Parse(transformCoords[3].Trim(), nf); inverseBindPoseMatrix.m21 = Double.Parse(transformCoords[4].Trim(), nf); inverseBindPoseMatrix.m22 = Double.Parse(transformCoords[5].Trim(), nf); inverseBindPoseMatrix.m23 = Double.Parse(transformCoords[6].Trim(), nf); inverseBindPoseMatrix.m24 = Double.Parse(transformCoords[7].Trim(), nf); inverseBindPoseMatrix.m31 = Double.Parse(transformCoords[8].Trim(), nf); inverseBindPoseMatrix.m32 = Double.Parse(transformCoords[9].Trim(), nf); inverseBindPoseMatrix.m33 = Double.Parse(transformCoords[10].Trim(), nf); inverseBindPoseMatrix.m34 = Double.Parse(transformCoords[11].Trim(), nf); inverseBindPoseMatrix.m41 = Double.Parse(transformCoords[12].Trim(), nf); inverseBindPoseMatrix.m42 = Double.Parse(transformCoords[13].Trim(), nf); inverseBindPoseMatrix.m43 = Double.Parse(transformCoords[14].Trim(), nf); inverseBindPoseMatrix.m44 = Double.Parse(transformCoords[15].Trim(), nf); inverseBindPoseMatrices.Add(currJoint.index, bindShapeMatrix * inverseBindPoseMatrix); return(maxJointVertexIndex); }
//here we go, thats the method called by vvvv each frame //all data handling should be in here public void Evaluate(int SpreadMax) { //if any of the inputs has changed //recompute the outputs bool recalculate = false; if (FJointNameInput.PinIsChanged || FBaseTransformInput.PinIsChanged || FOffsetModeInput.PinIsChanged || FParentNameInput.PinIsChanged || FConstraintsInput.PinIsChanged || recalculate) { skeleton = new Skeleton(); int currId = 0; for (int i = 0; i < FJointNameInput.SliceCount; i++) { string jointName; string parentName; FJointNameInput.GetString(i % FJointNameInput.SliceCount, out jointName); FParentNameInput.GetString(i % FParentNameInput.SliceCount, out parentName); IJoint currJoint = new JointInfo(jointName); Matrix4x4 baseTransform; FBaseTransformInput.GetMatrix(i % FBaseTransformInput.SliceCount, out baseTransform); currJoint.BaseTransform = baseTransform; //VMath.Translate(basePositionX, basePositionY, basePositionZ); currJoint.Constraints.Clear(); for (int j = i * 3; j < i * 3 + 3; j++) { double constraintMin, constraintMax; FConstraintsInput.GetValue2D(j % FConstraintsInput.SliceCount, out constraintMin, out constraintMax); currJoint.Constraints.Add(new Vector2D(constraintMin, constraintMax)); } if (string.IsNullOrEmpty(parentName)) { if (skeleton.Root == null) { skeleton.Root = currJoint; currJoint.Id = currId; currId++; skeleton.BuildJointTable(); } } else { if (skeleton.JointTable.ContainsKey(parentName)) { currJoint.Parent = skeleton.JointTable[parentName]; currJoint.Id = currId; currId++; } skeleton.BuildJointTable(); } } int positionInWorldSpace = 0; FOffsetModeInput.GetOrd(0, out positionInWorldSpace); if (positionInWorldSpace > 0) { List <Vector3D> offsetList = new List <Vector3D>(); foreach (KeyValuePair <string, IJoint> pair in skeleton.JointTable) { Vector3D worldPos = pair.Value.BaseTransform * (new Vector3D(0)); Vector3D parentWorldPos; if (pair.Value.Parent != null) { parentWorldPos = pair.Value.Parent.BaseTransform * (new Vector3D(0)); } else { parentWorldPos = new Vector3D(0); } Vector3D offset = worldPos - parentWorldPos; offsetList.Add(offset); } int i = 0; foreach (KeyValuePair <string, IJoint> pair in skeleton.JointTable) { pair.Value.BaseTransform = VMath.Translate(offsetList[i]); i++; } } FSkeletonOutput.MarkPinAsChanged(); } FSkeletonOutput.SetInterface(skeleton); }