-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(flame_3d)!: Introduce RenderContext to make GraphicsDevice focused
#3872
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
base: main
Are you sure you want to change the base?
Changes from all commits
dd7e424
a9c0146
66504cb
3dd7210
ebf2bf3
65d942c
b1e7dab
c93cb8e
f0dd04c
6b65757
fd3d22c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export 'core.dart'; | ||
| export 'src/game/flame_game_3d.dart'; | ||
| export 'src/game/game_3d.dart'; | ||
| export 'src/game/notifying_quaternion.dart'; | ||
| export 'src/game/notifying_vector3.dart'; | ||
| export 'src/game/transform_3d.dart'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export 'src/graphics/graphics_device.dart'; | ||
| export 'src/graphics/render_context.dart'; | ||
| export 'src/graphics/render_context_3d.dart'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import 'dart:ui'; | |
| import 'package:flame/components.dart' as flame; | ||
| import 'package:flame_3d/camera.dart'; | ||
| import 'package:flame_3d/components.dart'; | ||
| import 'package:flame_3d/game.dart'; | ||
| import 'package:flame_3d/graphics.dart'; | ||
| import 'package:flame_3d/resources.dart'; | ||
| import 'package:flutter/widgets.dart' show MediaQuery; | ||
|
|
@@ -14,20 +15,17 @@ import 'package:meta/meta.dart'; | |
| /// The primary feature of this component is that it allows [Component3D]s to | ||
| /// render directly to a [GraphicsDevice] instead of the regular rendering. | ||
| /// {@endtemplate} | ||
| class World3D extends flame.World with flame.HasGameReference { | ||
| class World3D extends flame.World with flame.HasGameReference<FlameGame3D> { | ||
| /// {@macro world_3d} | ||
| World3D({ | ||
| super.children, | ||
| super.priority, | ||
| Color clearColor = const Color(0x00000000), | ||
| }) : device = GraphicsDevice(clearValue: clearColor); | ||
|
|
||
| /// The graphical device attached to this world. | ||
| @internal | ||
| final GraphicsDevice device; | ||
| }); | ||
|
|
||
| final List<Light> _lights = []; | ||
|
|
||
| RenderContext3D get context => game.context; | ||
|
|
||
| /// Register a [light] with this world. | ||
| @internal | ||
| void addLight(Light light) => _lights.add(light); | ||
|
|
@@ -36,44 +34,32 @@ class World3D extends flame.World with flame.HasGameReference { | |
| @internal | ||
| void removeLight(Light light) => _lights.remove(light); | ||
|
|
||
| final _paint = Paint(); | ||
|
|
||
| @internal | ||
| @override | ||
| void renderFromCamera(Canvas canvas) { | ||
| final camera = CameraComponent3D.currentCamera!; | ||
| final viewport = camera.viewport; | ||
| final Viewport(virtualSize: size) = camera.viewport; | ||
|
|
||
| final devicePixelRatio = MediaQuery.of(game.buildContext!).devicePixelRatio; | ||
| final size = Size( | ||
| viewport.virtualSize.x * devicePixelRatio, | ||
| viewport.virtualSize.y * devicePixelRatio, | ||
| ); | ||
| final pixelRatio = MediaQuery.devicePixelRatioOf(game.buildContext!); | ||
| final renderSize = Size(size.x * pixelRatio, size.y * pixelRatio); | ||
|
|
||
| device | ||
| context | ||
| ..lights = _lights | ||
| // Set the view matrix | ||
| ..view.setFrom(camera.viewMatrix) | ||
| // Set the projection matrix | ||
| ..projection.setFrom(camera.projectionMatrix) | ||
| ..begin(size); | ||
|
|
||
| culled = 0; | ||
| ..setCamera(camera.viewMatrix, camera.projectionMatrix); | ||
|
|
||
| // ignore: invalid_use_of_internal_member | ||
| game.device.beginPass(renderSize); | ||
| super.renderFromCamera(canvas); | ||
| context.flush(); | ||
|
|
||
| final image = device.end(); | ||
| final image = game.device.endPass(); | ||
| canvas.drawImageRect( | ||
| image, | ||
| Offset.zero & size, | ||
| (-viewport.virtualSize / 2).toOffset() & | ||
| Size(viewport.virtualSize.x, viewport.virtualSize.y), | ||
| Offset.zero & renderSize, | ||
| Offset(-size.x / 2, -size.y / 2) & Size(size.x, size.y), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could just be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes but it would compute another vector so we wouldn't gain much However we should just do a Rect.fromLTWH here honestly, that's the cheapest |
||
| _paint, | ||
| ); | ||
| image.dispose(); | ||
| } | ||
|
|
||
| // TODO(wolfenrain): this is only here for testing purposes | ||
| int culled = 0; | ||
| static final _paint = Paint(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import 'dart:ui'; | ||
|
|
||
| import 'package:flame/game.dart'; | ||
| import 'package:flame_3d/graphics.dart'; | ||
| import 'package:meta/meta.dart'; | ||
|
|
||
| mixin Game3D on Game { | ||
| @internal | ||
| final GraphicsDevice device = GraphicsDevice(); | ||
|
|
||
| @internal | ||
| late final RenderContext3D context = RenderContext3D(device); | ||
|
|
||
| @override | ||
| void render(Canvas canvas) { | ||
| device.begin(); | ||
| super.render(canvas); | ||
| device.end(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it make sense for it to declare
T extends FlameGame3D,HasGameReference<T>, in case a user wants to extend World3D and also have a ref to their own game class?