void UpdateBone(BasicMotion motion) { float weight = 1.0f; float[] value = new float[4]; foreach (var fcurve in motion.FCurves) { if (fcurve.TargetType == BasicFCurveTargetType.Bone) { EvalFCurve(fcurve, motion.Frame, value); BasicBone bone = Bones[fcurve.TargetIndex]; switch (fcurve.ChannelType) { case BasicFCurveChannelType.Translation: Vector3 translation = new Vector3(value[0], value[1], value[2]); bone.Translation = bone.Translation.Lerp(translation, weight); break; case BasicFCurveChannelType.Rotation: Quaternion rotation; if (fcurve.DimCount == 4) { rotation = new Quaternion(value[0], value[1], value[2], value[3]); } else { Vector3 angles = new Vector3(value[0], value[1], value[2]); // [note] 旧API // rotation = Quaternion.FromEulerAnglesZYX( angles ) ; rotation = Quaternion.RotationZyx(angles); } // [note] 旧API // bone.Rotation = Quaternion.Slerp( bone.Rotation, rotation, weight, 0.0f ) ; bone.Rotation = bone.Rotation.Slerp(rotation, weight); break; case BasicFCurveChannelType.Scaling: Vector3 scaling = new Vector3(value[0], value[1], value[2]); bone.Scaling = bone.Scaling.Lerp(scaling, weight); break; } } else if (fcurve.TargetType == BasicFCurveTargetType.Material) { EvalFCurve(fcurve, motion.Frame, value); BasicMaterial material = Materials[fcurve.TargetIndex]; switch (fcurve.ChannelType) { // 追加 case BasicFCurveChannelType.Diffuse: material.Diffuse.X = value[0]; material.Diffuse.Y = value[1]; material.Diffuse.Z = value[2]; material.Diffuse.W = value[3]; break; } } } }
public void Update() { foreach (var bone in Bones) { Matrix4 local = Matrix4.Transformation(bone.Translation, bone.Rotation, bone.Scaling); BasicBone parent = (bone.ParentBone < 0) ? null : Bones[bone.ParentBone]; if (parent == null) { bone.WorldMatrix = WorldMatrix * local; } else { bone.WorldMatrix = parent.WorldMatrix * local; } } }
void LoadBone(Chunk chunk, BasicBone bone) { int nParts = CountChild(chunk, ChunkType.DrawPart); bone.DrawParts = new int[nParts]; int iPart = 0; bone.Name = chunk.Name; Chunk child; for (int pos = chunk.Child; pos < chunk.Next; pos = child.Next) { child = ReadChunk(pos); int args = child.Args; switch (child.Type) { case ChunkType.ParentBone: bone.ParentBone = ReadIndex(args); break; case ChunkType.Visibility: bone.Visibility = ReadUInt32(args); break; case ChunkType.BlendBones: int nBones = ReadInt32(args); bone.BlendBones = new int[nBones]; for (int i = 0; i < nBones; i++) { bone.BlendBones[i] = ReadIndex(args + 4 + 4 * i); } break; case ChunkType.BlendOffsets: { int nOffsets = ReadInt32(args); bone.BlendOffsets = new Matrix4[nOffsets]; for (int i = 0; i < nOffsets; i++) { bone.BlendOffsets[i] = ReadMatrix4(args + 4 + 64 * i); } break; } case ChunkType.Pivot: bone.Pivot = ReadVector3(args); break; case ChunkType.Translate: bone.Translation = ReadVector3(args); break; case ChunkType.RotateZYX: // [note] 旧API // bone.Rotation = Quaternion.FromEulerAnglesZYX( ReadVector3( args ) ) ; bone.Rotation = Quaternion.RotationZyx(ReadVector3(args)); break; case ChunkType.RotateYXZ: // [note] 旧API // bone.Rotation = Quaternion.FromEulerAnglesYXZ( ReadVector3( args ) ) ; bone.Rotation = Quaternion.RotationYxz(ReadVector3(args)); break; case ChunkType.RotateQ: bone.Rotation = ReadQuaternion(args); break; case ChunkType.Scale: bone.Scaling = ReadVector3(args); break; case ChunkType.Scale2: bone.Scaling = ReadVector3(args); break; case ChunkType.DrawPart: bone.DrawParts[iPart++] = ReadIndex(args); break; } } }
void LoadBone( Chunk chunk, BasicBone bone ) { int nParts = CountChild( chunk, ChunkType.DrawPart ) ; bone.DrawParts = new int[ nParts ] ; int iPart = 0 ; bone.Name = chunk.Name; Chunk child ; for ( int pos = chunk.Child ; pos < chunk.Next ; pos = child.Next ) { child = ReadChunk( pos ) ; int args = child.Args ; switch ( child.Type ) { case ChunkType.ParentBone : bone.ParentBone = ReadIndex( args ) ; break ; case ChunkType.Visibility : bone.Visibility = ReadUInt32( args ) ; break ; case ChunkType.BlendBones : int nBones = ReadInt32( args ) ; bone.BlendBones = new int[ nBones ] ; for ( int i = 0 ; i < nBones ; i ++ ) { bone.BlendBones[ i ] = ReadIndex( args + 4 + 4 * i ) ; } break ; case ChunkType.BlendOffsets : { int nOffsets = ReadInt32( args ) ; bone.BlendOffsets = new Matrix4[ nOffsets ] ; for ( int i = 0 ; i < nOffsets ; i ++ ) { bone.BlendOffsets[ i ] = ReadMatrix4( args + 4 + 64 * i ) ; } break ; } case ChunkType.Pivot : bone.Pivot = ReadVector3( args ) ; break ; case ChunkType.Translate : bone.Translation = ReadVector3( args ) ; break ; case ChunkType.RotateZYX : // [note] 旧API // bone.Rotation = Quaternion.FromEulerAnglesZYX( ReadVector3( args ) ) ; bone.Rotation = Quaternion.RotationZyx( ReadVector3( args ) ) ; break ; case ChunkType.RotateYXZ : // [note] 旧API // bone.Rotation = Quaternion.FromEulerAnglesYXZ( ReadVector3( args ) ) ; bone.Rotation = Quaternion.RotationYxz( ReadVector3( args ) ) ; break ; case ChunkType.RotateQ : bone.Rotation = ReadQuaternion( args ) ; break ; case ChunkType.Scale : bone.Scaling = ReadVector3( args ) ; break ; case ChunkType.Scale2 : bone.Scaling = ReadVector3( args ) ; break ; case ChunkType.DrawPart : bone.DrawParts[ iPart ++ ] = ReadIndex( args ) ; break ; } } }