Skip to content

Commit

Permalink
feat minor code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jvenin committed May 2, 2024
1 parent f62ab9e commit fe7894f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 89 deletions.
31 changes: 19 additions & 12 deletions lib/classes/gsf/header2/chunks/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@ import 'dart:typed_data';

import 'package:paraworld_gsf_viewer/classes/gsf/header2/chunks/chunk.dart';
import 'package:paraworld_gsf_viewer/classes/gsf_data.dart';
import 'package:vector_math/vector_math_64.dart';

class LinkChunk extends Chunk {
late final Standard4BytesData<int> guid;
late final Standard4BytesData<double> positionX;
late final Standard4BytesData<double> positionY;
late final Standard4BytesData<double> positionZ;
late final Standard4BytesData<double> quaternionL;
late final Standard4BytesData<double> quaternionI;
late final Standard4BytesData<double> quaternionJ;
late final Standard4BytesData<double> quaternionK;
late final Standard4BytesData<double> quaternionX;
late final Standard4BytesData<double> quaternionY;
late final Standard4BytesData<double> quaternionZ;
late final Standard4BytesData<double> quaternionW;
late final Standard4BytesData<StringNoZero> fourccLink;
// for bone only
late final Standard4BytesData<int>? skeletonIndex;
late final Standard4BytesData<UnknowData>? boneIds;
late final Standard4BytesData<UnknowData>? boneWeights;

Vector3 get position =>
Vector3(positionX.value, positionY.value, positionZ.value);

Quaternion get quaternion => Quaternion(quaternionY.value, quaternionZ.value,
quaternionW.value, quaternionX.value);

@override
String get label => '${type.name} 0x${guid.value.toRadixString(16)}';

Expand Down Expand Up @@ -50,28 +57,28 @@ class LinkChunk extends Chunk {
bytes: bytes,
offset: offset,
);
quaternionL = Standard4BytesData(
quaternionX = Standard4BytesData(
position: positionZ.relativeEnd,
bytes: bytes,
offset: offset,
);
quaternionI = Standard4BytesData(
position: quaternionL.relativeEnd,
quaternionY = Standard4BytesData(
position: quaternionX.relativeEnd,
bytes: bytes,
offset: offset,
);
quaternionJ = Standard4BytesData(
position: quaternionI.relativeEnd,
quaternionZ = Standard4BytesData(
position: quaternionY.relativeEnd,
bytes: bytes,
offset: offset,
);
quaternionK = Standard4BytesData(
position: quaternionJ.relativeEnd,
quaternionW = Standard4BytesData(
position: quaternionZ.relativeEnd,
bytes: bytes,
offset: offset,
);
fourccLink = Standard4BytesData(
position: quaternionK.relativeEnd,
position: quaternionW.relativeEnd,
bytes: bytes,
offset: offset,
);
Expand Down
21 changes: 4 additions & 17 deletions lib/classes/gsf/header2/chunks/skeleton.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ class SkeletonChunk extends Chunk {
: bindPoses.last.getEndOffset(),
);
bindPoses.add(bindPose);
//bones[i].bindPose = bindPose;
}
// unfortunately bind poses are linked to bones recursively so we need to
// fill the bone tree to link them using a sor of stack pointer on the bind poses

// fill bone tree
while (boneTree.length < allBonesCount.value) {
Expand Down Expand Up @@ -144,22 +141,11 @@ class SkeletonChunk extends Chunk {
}
final firstChildIndex =
bone.index + (bone.nextChildOffset.value / Bone.size).ceil();
final firstChild = bones[firstChildIndex];
boneTree[bone.index]!.children.add(firstChildIndex);

if (!boneTree.containsKey(firstChildIndex)) {
_getChildOfBone(
boneTree,
bones,
firstChild,
);
}
int treatedCount = 1;
while (treatedCount < bone.childrenCount.value) {
while (boneTree[bone.index]!.children.length < bone.childrenCount.value) {
final nextBone = _getNextBoneInTree(
boneTree,
bones,
firstChildIndex + 1,
firstChildIndex,
);
if (nextBone == null) {
throw ("Missing ${bone.childrenCount.value - boneTree[bone.index]!.children.length} children for bone $bone");
Expand All @@ -170,7 +156,6 @@ class SkeletonChunk extends Chunk {
bones,
nextBone,
);
treatedCount++;
}
}

Expand Down Expand Up @@ -235,6 +220,8 @@ class SkeletonChunk extends Chunk {
}

List<List<(int?, ModelVertex)>> toModelVertices() {
// unfortunately bind poses are linked to bones recursively so we need to
// fill the bone tree to link them using a sort of stack pointer on the bind poses
final rootBone = bones.first;
rootBone.bindPose = bindPoses.first;
final rootVert = ModelVertex(
Expand Down
117 changes: 61 additions & 56 deletions lib/classes/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,67 @@ class Model {
}
}

void drawSkeleton(
Transformation transformation,
ui.Size size,
ui.Canvas canvas,
Color meshColor,
) {
final projectionData = getProjectionData(size);
skeletonPaint.color = meshColor;
final textStyle = TextStyle(
color: Colors.pink,
fontSize: 12 + transformation.scaleFactor,
fontWeight: FontWeight.bold,
);
final jointPaint = Paint()
..color = Colors.pink
..strokeWidth = 3 + transformation.scaleFactor / 2;
for (final skeleton in skeletons) {
for (final branch in skeleton) {
final List<double> points = [];
for (final joint in branch) {
final coords = joint.$2.project(
widthOffset: projectionData.widthOffset,
heightOffset: projectionData.heightOffset,
maxWidth: projectionData.maxFactor,
maxHeight: projectionData.maxFactor,
transformation: transformation,
);
points.addAll([coords.pointProjection.x, coords.pointProjection.y]);

if (joint.$1 == null) {
continue;
}
final idPainter = TextPainter(
text: TextSpan(
text: joint.$1.toString(),
style: textStyle,
),
textDirection: TextDirection.ltr,
)..layout(
minWidth: 0,
maxWidth: size.width,
);
idPainter.paint(
canvas,
Offset(
coords.pointProjection.x,
coords.pointProjection.y,
),
);
}
final pos = Float32List.fromList(points);
canvas.drawRawPoints(ui.PointMode.lines, pos, skeletonPaint);
canvas.drawRawPoints(
ui.PointMode.points,
pos,
jointPaint,
);
}
}
}

void draw(
Transformation transformation,
ui.Size size,
Expand All @@ -158,7 +219,6 @@ class Model {
bool showNormals = false,
bool showCloths = false,
bool showTexture = false,
bool showSkeleton = false,
}) {
final projectionData = getProjectionData(size);
final listOfMesh = showCloths ? meshes + cloth : meshes;
Expand Down Expand Up @@ -245,60 +305,5 @@ class Model {
? texturePainter
: (_paint..color = meshColor.withOpacity(0.3)),
);

if (showSkeleton && skeletons.isNotEmpty) {
skeletonPaint.color = meshColor;
final textStyle = TextStyle(
color: Colors.pink,
fontSize: 12 + transformation.scaleFactor,
fontWeight: FontWeight.bold,
);
final jointPaint = Paint()
..color = Colors.pink
..strokeWidth = 3 + transformation.scaleFactor / 2;
for (final skeleton in skeletons) {
for (final branch in skeleton) {
final List<double> points = [];
for (final joint in branch) {
final coords = joint.$2.project(
widthOffset: projectionData.widthOffset,
heightOffset: projectionData.heightOffset,
maxWidth: projectionData.maxFactor,
maxHeight: projectionData.maxFactor,
transformation: transformation,
);
points.addAll([coords.pointProjection.x, coords.pointProjection.y]);

if (joint.$1 == null) {
continue;
}
final idPainter = TextPainter(
text: TextSpan(
text: joint.$1.toString(),
style: textStyle,
),
textDirection: TextDirection.ltr,
)..layout(
minWidth: 0,
maxWidth: size.width,
);
idPainter.paint(
canvas,
Offset(
coords.pointProjection.x,
coords.pointProjection.y,
),
);
}
final pos = Float32List.fromList(points);
canvas.drawRawPoints(ui.PointMode.lines, pos, skeletonPaint);
canvas.drawRawPoints(
ui.PointMode.points,
pos,
jointPaint,
);
}
}
}
}
}
8 changes: 4 additions & 4 deletions lib/widgets/header2/widgets/chunks/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class LinkDisplay extends StatelessWidget {
GsfDataTile(label: "Pos X", data: link.positionX),
GsfDataTile(label: "Pos Y", data: link.positionY),
GsfDataTile(label: "Pos Z", data: link.positionZ),
GsfDataTile(label: "Quat X", data: link.quaternionL),
GsfDataTile(label: "Quat Y", data: link.quaternionI),
GsfDataTile(label: "Quat Z", data: link.quaternionJ),
GsfDataTile(label: "Quat W", data: link.quaternionK),
GsfDataTile(label: "Quat X", data: link.quaternionX),
GsfDataTile(label: "Quat Y", data: link.quaternionY),
GsfDataTile(label: "Quat Z", data: link.quaternionZ),
GsfDataTile(label: "Quat W", data: link.quaternionW),
GsfDataTile(label: "FourCC", data: link.fourccLink),
if (link.skeletonIndex != null)
GsfDataTile(label: "Skeleton index", data: link.skeletonIndex!),
Expand Down

0 comments on commit fe7894f

Please sign in to comment.