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

feat: Add baseColor to Shadow3DDecorator #3375

Merged
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
17 changes: 14 additions & 3 deletions packages/flame/lib/src/rendering/shadow3d_decorator.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dart:ui';

import 'package:flame/src/palette.dart';
import 'package:flame/src/rendering/decorator.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math_64.dart' show Matrix4, Vector2;

/// [Shadow3DDecorator] casts a realistic-looking shadow from the component
/// onto the ground.
Expand All @@ -24,13 +25,15 @@ class Shadow3DDecorator extends Decorator {
double? yScale,
double? blur,
double? opacity,
Color? baseColor,
}) : _base = base?.clone() ?? Vector2.zero(),
_ascent = ascent ?? 0,
_angle = angle ?? -1.4,
_shift = xShift ?? 100.0,
_scale = yScale ?? 1.0,
_blur = blur ?? 0,
_opacity = opacity ?? 0.6;
_opacity = opacity ?? 0.6,
_baseColor = baseColor ?? BasicPalette.black.color;

/// Coordinates of the point where the component "touches the ground". If the
/// component is airborne (i.e. [ascent] is non-zero), then this should be the
Expand Down Expand Up @@ -117,10 +120,18 @@ class Shadow3DDecorator extends Decorator {
_paint = null;
}

/// Shadow's base color before opacity. This defaults to pitch-black.
Color get baseColor => _baseColor;
Color _baseColor;
set baseColor(Color value) {
_baseColor = value;
_paint = null;
}

Paint? _paint;
Paint _makePaint() {
final paint = Paint();
final color = Color.fromRGBO(0, 0, 0, opacity);
final color = baseColor.withOpacity(opacity);
paint.colorFilter = ColorFilter.mode(color, BlendMode.srcIn);
if (_blur > 0) {
paint.imageFilter = ImageFilter.blur(sigmaX: blur, sigmaY: blur / _scale);
Expand Down
Binary file modified packages/flame/test/_goldens/shadow3d_decorator_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/flame/test/_goldens/shadow3d_decorator_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion packages/flame/test/rendering/shadow3d_decorator_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flame/palette.dart';
import 'package:flame/rendering.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
Expand All @@ -16,6 +17,7 @@ void main() {
expect(decorator.yScale, 1.0);
expect(decorator.blur, 0.0);
expect(decorator.opacity, 0.6);
expect(decorator.baseColor, BasicPalette.black.color);
});

testGolden(
Expand Down Expand Up @@ -79,7 +81,8 @@ void main() {
..xShift = 250.0
..yScale = 1.5
..opacity = 0.4
..blur = 1.0,
..blur = 1.0
..baseColor = BasicPalette.red.color,
),
]);
},
Expand Down
Loading