Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix cache breaking and equality #16

Merged
merged 3 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions flutter/mesh/lib/src/data/overtex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ class OVertex {
/// Check if two [OVertex] objects are equal.
@override
bool operator ==(Object other) =>
other is OVertex && x == other.x && y == other.y;
runtimeType == other.runtimeType &&
other is OVertex &&
x == other.x &&
y == other.y;

@override
int get hashCode => Object.hash(x, y);

@override
String toString() => 'OVertex($x, $y)';
}

/// Extension on [ui.Offset] to convert it to an [OVertex].
Expand Down Expand Up @@ -268,6 +274,7 @@ class BezierOVertex extends OVertex {

@override
bool operator ==(Object other) =>
runtimeType == other.runtimeType &&
other is BezierOVertex &&
x == other.x &&
y == other.y &&
Expand All @@ -278,6 +285,10 @@ class BezierOVertex extends OVertex {

@override
int get hashCode => Object.hash(x, y, north, east, south, west);

@override
String toString() => 'BezierOVertex($x, $y, north: $north, '
'east: $east, south: $south, west: $west)';
}

/// Extension on [OVertex] to create a new [OVertex] with bezier control points.
Expand All @@ -291,12 +302,22 @@ extension BezierizeOVertex on OVertex {
OVertex? east,
OVertex? south,
OVertex? west,
}) =>
BezierOVertex.oVertex(
this,
north: north,
east: east,
south: south,
west: west,
);
}) {
final converted = BezierOVertex.oVertex(
this,
);
final t = this;
if (t is BezierOVertex) {
return converted
..north = north ?? t.north
..east = east ?? t.east
..south = south ?? t.south
..west = west ?? t.west;
}
return converted
..north = north
..east = east
..south = south
..west = west;
}
}
96 changes: 59 additions & 37 deletions flutter/mesh/lib/src/widgets/omesh_rect_paint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import 'dart:ui';

import 'package:cached_value/cached_value.dart';
import 'package:flutter/foundation.dart';

import 'package:flutter/rendering.dart'
show BlendMode, Canvas, Color, Matrix4, Paint, Rect;

import 'package:flutter_shaders/flutter_shaders.dart';
import 'package:mesh/internal_stuff.dart';
import 'package:mesh/mesh.dart';
Expand Down Expand Up @@ -78,7 +80,9 @@ class OMeshRectPaint {
/// The guiding mesh to be rendered.
OMeshRect get meshRect => _meshRect;
set meshRect(OMeshRect value) {
if (value == _meshRect) return;
if (value == _meshRect) {
return;
}
_meshRect = value.clone();
_needsRepaint = true;
}
Expand Down Expand Up @@ -185,6 +189,8 @@ class OMeshRectPaint {
});
})
.withDependency(() => meshRect.colors)
.withDependency(() => meshRect.smoothColors)
.withDependency(() => meshRect.colorSpace)
.withDependency(() => (meshRect.width, meshRect.height));

/// A [CachedValue] that holds the tessellated mesh.
Expand Down Expand Up @@ -232,16 +238,24 @@ class OMeshRectPaint {
for (final tuple in patches.reversed.indexed) {
final (patchIndex, [index00, index01, index10, index11]) = tuple;

final (colors, biases) = vertexColors;
final patchColors = [
colors[index00], colors[index01], //
colors[index10], colors[index11], //
];
final patchBiases = [
biases[index00], biases[index01], //
biases[index10], biases[index11], //
];

final gradeintPaint = _enableImpellerCompatibility
? _getPrerenderedPaint(rect)
: _getShaderPaint(
patchIndex: patchIndex,
index00: index00,
index01: index01,
index10: index10,
index11: index11,
textureVertices: textureVertices,
vertexColors: vertexColors,
textureTopLeft: textureVertices[index00],
textureBottomRight: textureVertices[index11],
patchColors: patchColors,
patchBiases: patchBiases,
rect: rect,
);

Expand All @@ -267,36 +281,24 @@ class OMeshRectPaint {

Paint _getShaderPaint({
required int patchIndex,
required int index00,
required int index01,
required int index10,
required int index11,
required List<OVertex> textureVertices,
required (List<Color>, List<bool>) vertexColors,
required OVertex textureTopLeft,
required OVertex textureBottomRight,
required List<Color> patchColors,
required List<bool> patchBiases,
required Rect rect,
}) {
final (colors, biases) = vertexColors;
final patchColors = [
colors[index00], colors[index01], //
colors[index10], colors[index11], //
];
final patchBiases = [
biases[index00], biases[index01], //
biases[index10], biases[index11], //
];

return Paint()
..shader = (shaderProvider.getShaderFor(patchIndex)
..setFloatUniforms(
(s) => s
..setSize(rect.size)
..setFloats([
textureVertices[index00].x,
textureVertices[index00].y,
textureTopLeft.x,
textureTopLeft.y,
])
..setFloats([
textureVertices[index11].x,
textureVertices[index11].y,
textureBottomRight.x,
textureBottomRight.y,
])
..setColorsWide(patchColors)
..setBools(patchBiases)
Expand Down Expand Up @@ -333,31 +335,51 @@ class OMeshRectPaint {
final recorder = PictureRecorder();
final canvas = Canvas(recorder);

for (final tuple in patches.reversed.indexed) {
for (final tuple in patches.indexed) {
final (patchIndex, [index00, index01, index10, index11]) = tuple;

final (colors, biases) = vertexColors;
final patchColors = [
colors[index00], colors[index01], //
colors[index10], colors[index11], //
];
final patchBiases = [
biases[index00], biases[index01], //
biases[index10], biases[index11], //
];

final textureTopLeft = textureVertices[index00];
final textureBottomRight = textureVertices[index11];

final paintShader = _getShaderPaint(
patchIndex: patchIndex,
index00: index00,
index01: index01,
index10: index10,
index11: index11,
textureVertices: textureVertices,
vertexColors: vertexColors,
textureTopLeft: textureTopLeft,
textureBottomRight: textureBottomRight,
patchColors: patchColors,
patchBiases: patchBiases,
rect: _rect,
);

canvas.drawRect(_rect, paintShader);
canvas.drawRect(
_rect,
paintShader
..isAntiAlias = false
..filterQuality = FilterQuality.high,
);
}

final picture = recorder.endRecording();
return picture.toImageSync(
_rect.size.width.ceil(),
_rect.size.height.ceil(),
_rect.size.width.round(),
_rect.size.height.round(),
);
})
.withDependency(() => _rect)
.withDependency(() => debugMode)
.withDependency(() => meshRect.fallbackColor)
.withDependency(() => meshRect.colors)
.withDependency(() => meshRect.smoothColors)
.withDependency(() => meshRect.colorSpace)
.withDependency(() => (meshRect.width, meshRect.height));
}

Expand Down
12 changes: 6 additions & 6 deletions flutter/mesh/test/src/data/overtex_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@ void main() {
);

final vertex2 = BezierOVertex(
2,
1,
north: OVertex(2, 1),
east: OVertex(1, 1),
south: OVertex(2, 2),
west: OVertex(1, 2),
2,
north: OVertex(1, 2),
east: OVertex(2, 2),
south: OVertex(1, 2),
west: OVertex(2, 1),
);

expect(vertex1, isNot(vertex2));
expect(vertex1, isNot(equals(vertex2)));
});
});

Expand Down