From 689f748f5d36b5cb9119d5158dbe39a417375d9f Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Tue, 23 Aug 2022 20:34:56 +0200 Subject: [PATCH] [flutter] Fix up vertex & texture color modulation, go back to passing native views directly to avoid copy. --- spine-flutter/example/lib/main.dart | 5 +---- spine-flutter/lib/spine_flutter.dart | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/spine-flutter/example/lib/main.dart b/spine-flutter/example/lib/main.dart index a14293f50..62f787786 100644 --- a/spine-flutter/example/lib/main.dart +++ b/spine-flutter/example/lib/main.dart @@ -65,12 +65,9 @@ class _SpinePainter extends CustomPainter { canvas.save(); canvas.translate(size.width / 2, size.height); for (final cmd in commands) { - canvas.drawVertices(cmd.vertices, BlendMode.srcOut, drawable.atlas.atlasPagePaints[cmd.atlasPageIndex]); + canvas.drawVertices(cmd.vertices, BlendMode.modulate, drawable.atlas.atlasPagePaints[cmd.atlasPageIndex]); } canvas.restore(); - canvas.drawLine(Offset(0, 0), Offset(size.width, size.height), Paint() - ..color = Colors.blue - ..strokeWidth = 4); } @override diff --git a/spine-flutter/lib/spine_flutter.dart b/spine-flutter/lib/spine_flutter.dart index 44846f818..ae8a0078c 100644 --- a/spine-flutter/lib/spine_flutter.dart +++ b/spine-flutter/lib/spine_flutter.dart @@ -14,10 +14,10 @@ int minorVersion() => _bindings.spine_minor_version(); class SpineAtlas { Pointer _atlas; - List _atlasPages; + List atlasPages; List atlasPagePaints; - SpineAtlas(this._atlas, this._atlasPages, this.atlasPagePaints); + SpineAtlas(this._atlas, this.atlasPages, this.atlasPagePaints); static Future fromAsset(AssetBundle assetBundle, String atlasFileName) async { final atlasData = await assetBundle.loadString(atlasFileName); @@ -42,7 +42,10 @@ class SpineAtlas { final FrameInfo frameInfo = await codec.getNextFrame(); final Image image = frameInfo.image; atlasPages.add(image); - atlasPagePaints.add(Paint()..shader = ImageShader(image, TileMode.clamp, TileMode.clamp, Matrix4.identity().storage)); + atlasPagePaints.add(Paint() + ..shader = ImageShader(image, TileMode.clamp, TileMode.clamp, Matrix4.identity().storage, filterQuality: FilterQuality.high) + ..isAntiAlias = true + ); } return SpineAtlas(atlas, atlasPages, atlasPagePaints); @@ -98,7 +101,7 @@ class SpineSkeletonDrawable { Pointer nativeCmd = _bindings.spine_skeleton_drawable_render(_drawable); List commands = []; while(nativeCmd.address != nullptr.address) { - final atlasPage = atlas._atlasPages[nativeCmd.ref.atlasPage]; + final atlasPage = atlas.atlasPages[nativeCmd.ref.atlasPage]; commands.add(SpineRenderCommand(nativeCmd, atlasPage.width.toDouble(), atlasPage.height.toDouble())); nativeCmd = nativeCmd.ref.next; } @@ -114,10 +117,7 @@ class SpineRenderCommand { atlasPageIndex = nativeCmd.ref.atlasPage; int numVertices = nativeCmd.ref.numVertices; int numIndices = nativeCmd.ref.numIndices; - final positions = Float32List.fromList(nativeCmd.ref.positions.asTypedList(numVertices * 2)); - final uvs = Float32List.fromList(nativeCmd.ref.uvs.asTypedList(numVertices * 2)); - final colors = Int32List.fromList(nativeCmd.ref.colors.asTypedList(numVertices)); - final indices = Uint16List.fromList(nativeCmd.ref.indices.asTypedList(numIndices)); + final uvs = nativeCmd.ref.uvs.asTypedList(numVertices * 2); for (int i = 0; i < numVertices * 2; i += 2) { uvs[i] *= pageWidth; uvs[i+1] *= pageHeight; @@ -127,10 +127,10 @@ class SpineRenderCommand { // render call. See the implementation of Vertices.raw() here: // https://github.com/flutter/engine/blob/5c60785b802ad2c8b8899608d949342d5c624952/lib/ui/painting/vertices.cc#L21 vertices = Vertices.raw(VertexMode.triangles, - positions, + nativeCmd.ref.positions.asTypedList(numVertices * 2), textureCoordinates: uvs, - colors: colors, - indices: indices + colors: nativeCmd.ref.colors.asTypedList(numVertices), + indices: nativeCmd.ref.indices.asTypedList(numIndices) ); } }