private PMDFormat.Joint ReadJoint() { PMDFormat.Joint result = new PMDFormat.Joint(); result.joint_name = ConvertByteToString(binary_reader_.ReadBytes(20), ""); result.joint_rigidbody_a = binary_reader_.ReadUInt32(); result.joint_rigidbody_b = binary_reader_.ReadUInt32(); result.joint_pos = ReadSinglesToVector3(binary_reader_); result.joint_rot = ReadSinglesToVector3(binary_reader_); result.constrain_pos_1 = ReadSinglesToVector3(binary_reader_); result.constrain_pos_2 = ReadSinglesToVector3(binary_reader_); result.constrain_rot_1 = ReadSinglesToVector3(binary_reader_); result.constrain_rot_2 = ReadSinglesToVector3(binary_reader_); result.spring_pos = ReadSinglesToVector3(binary_reader_); result.spring_rot = ReadSinglesToVector3(binary_reader_); return(result); }
private static PMXFormat.Joint ConvertJoint(PMDFormat.Joint pmd_joint) { PMXFormat.Joint result = new PMXFormat.Joint(); result.name = pmd_joint.joint_name; result.english_name = ""; result.operation_type = PMXFormat.Joint.OperationType.Spring6DOF; result.rigidbody_a = pmd_joint.joint_rigidbody_a; result.rigidbody_b = pmd_joint.joint_rigidbody_b; result.position = pmd_joint.joint_pos; result.rotation = pmd_joint.joint_rot; result.constrain_pos_lower = pmd_joint.constrain_pos_1; result.constrain_pos_upper = pmd_joint.constrain_pos_2; result.constrain_rot_lower = pmd_joint.constrain_rot_1; result.constrain_rot_upper = pmd_joint.constrain_rot_2; result.spring_position = pmd_joint.spring_pos; result.spring_rotation = pmd_joint.spring_rot; return(result); }
// ばねの設定など void SetDrive(PMDFormat.Joint joint, ConfigurableJoint conf) { JointDrive drive; // Position if (joint.spring_pos.x != 0f) { drive = new JointDrive(); drive.positionSpring = joint.spring_pos.x * scale_; conf.xDrive = drive; } if (joint.spring_pos.y != 0f) { drive = new JointDrive(); drive.positionSpring = joint.spring_pos.y * scale_; conf.yDrive = drive; } if (joint.spring_pos.z != 0f) { drive = new JointDrive(); drive.positionSpring = joint.spring_pos.z * scale_; conf.zDrive = drive; } // Angular if (joint.spring_rot.x != 0f) { drive = new JointDrive { positionSpring = joint.spring_rot.x }; conf.angularXDrive = drive; } if (joint.spring_rot.y == 0f && joint.spring_rot.z == 0f) { return; } drive = new JointDrive { positionSpring = (joint.spring_rot.y + joint.spring_rot.z) * 0.5f }; conf.angularYZDrive = drive; }
// ばねの設定など void SetDrive(PMDFormat.Joint joint, ConfigurableJoint conf) { JointDrive drive; // Position if (joint.spring_pos.x != 0f) { drive = new JointDrive(); drive.positionSpring = joint.spring_pos.x * scale_; conf.xDrive = drive; } if (joint.spring_pos.y != 0f) { drive = new JointDrive(); drive.positionSpring = joint.spring_pos.y * scale_; conf.yDrive = drive; } if (joint.spring_pos.z != 0f) { drive = new JointDrive(); drive.positionSpring = joint.spring_pos.z * scale_; conf.zDrive = drive; } // Angular if (joint.spring_rot.x != 0f) { drive = new JointDrive(); drive.mode = JointDriveMode.PositionAndVelocity; drive.positionSpring = joint.spring_rot.x; conf.angularXDrive = drive; } if (joint.spring_rot.y != 0f || joint.spring_rot.z != 0f) { drive = new JointDrive(); drive.mode = JointDriveMode.PositionAndVelocity; drive.positionSpring = (joint.spring_rot.y + joint.spring_rot.z) * 0.5f; conf.angularYZDrive = drive; } }
private PMDFormat.Joint ReadJoint() { PMDFormat.Joint result = new PMDFormat.Joint(); result.joint_name = ConvertByteToString(binary_reader_.ReadBytes(20), ""); result.joint_rigidbody_a = binary_reader_.ReadUInt32(); result.joint_rigidbody_b = binary_reader_.ReadUInt32(); result.joint_pos = ReadSinglesToVector3(binary_reader_); result.joint_rot = ReadSinglesToVector3(binary_reader_); result.constrain_pos_1 = ReadSinglesToVector3(binary_reader_); result.constrain_pos_2 = ReadSinglesToVector3(binary_reader_); result.constrain_rot_1 = ReadSinglesToVector3(binary_reader_); result.constrain_rot_2 = ReadSinglesToVector3(binary_reader_); result.spring_pos = ReadSinglesToVector3(binary_reader_); result.spring_rot = ReadSinglesToVector3(binary_reader_); return result; }
// ConfigurableJointの値を設定する void SetAttributeConfigurableJoint(PMDFormat.Joint joint, ConfigurableJoint conf) { SetMotionAngularLock(joint, conf); SetDrive(joint, conf); }
// 移動や回転制限 void SetMotionAngularLock(PMDFormat.Joint joint, ConfigurableJoint conf) { SoftJointLimit jlim; // Motionの固定 if (joint.constrain_pos_1.x == 0f && joint.constrain_pos_2.x == 0f) { conf.xMotion = ConfigurableJointMotion.Locked; } else { conf.xMotion = ConfigurableJointMotion.Limited; } if (joint.constrain_pos_1.y == 0f && joint.constrain_pos_2.y == 0f) { conf.yMotion = ConfigurableJointMotion.Locked; } else { conf.yMotion = ConfigurableJointMotion.Limited; } if (joint.constrain_pos_1.z == 0f && joint.constrain_pos_2.z == 0f) { conf.zMotion = ConfigurableJointMotion.Locked; } else { conf.zMotion = ConfigurableJointMotion.Limited; } // 角度の固定 if (joint.constrain_rot_1.x == 0f && joint.constrain_rot_2.x == 0f) { conf.angularXMotion = ConfigurableJointMotion.Locked; } else { conf.angularXMotion = ConfigurableJointMotion.Limited; float hlim = Mathf.Max(-joint.constrain_rot_1.x, -joint.constrain_rot_2.x); //回転方向が逆なので負数 float llim = Mathf.Min(-joint.constrain_rot_1.x, -joint.constrain_rot_2.x); SoftJointLimit jhlim = new SoftJointLimit(); jhlim.limit = Mathf.Clamp(hlim * Mathf.Rad2Deg, -180.0f, 180.0f); conf.highAngularXLimit = jhlim; SoftJointLimit jllim = new SoftJointLimit(); jllim.limit = Mathf.Clamp(llim * Mathf.Rad2Deg, -180.0f, 180.0f); conf.lowAngularXLimit = jllim; } if (joint.constrain_rot_1.y == 0f && joint.constrain_rot_2.y == 0f) { conf.angularYMotion = ConfigurableJointMotion.Locked; } else { // 値がマイナスだとエラーが出るので注意 conf.angularYMotion = ConfigurableJointMotion.Limited; float lim = Mathf.Min(Mathf.Abs(joint.constrain_rot_1.y), Mathf.Abs(joint.constrain_rot_2.y)); //絶対値の小さい方 jlim = new SoftJointLimit(); jlim.limit = lim * Mathf.Clamp(Mathf.Rad2Deg, 0.0f, 180.0f); conf.angularYLimit = jlim; } if (joint.constrain_rot_1.z == 0f && joint.constrain_rot_2.z == 0f) { conf.angularZMotion = ConfigurableJointMotion.Locked; } else { conf.angularZMotion = ConfigurableJointMotion.Limited; float lim = Mathf.Min(Mathf.Abs(-joint.constrain_rot_1.z), Mathf.Abs(-joint.constrain_rot_2.z)); //絶対値の小さい方//回転方向が逆なので負数 jlim = new SoftJointLimit(); jlim.limit = Mathf.Clamp(lim * Mathf.Rad2Deg, 0.0f, 180.0f); conf.angularZLimit = jlim; } }