-
-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(flame_3d): Make shader api more useful (#3085)
Co-authored-by: Luan Nico <[email protected]>
- Loading branch information
1 parent
3091b12
commit c9cf609
Showing
50 changed files
with
964 additions
and
319 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file modified
BIN
+24.6 KB
(340%)
packages/flame_3d/assets/shaders/standard_material.shaderbundle
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:flame_3d/components.dart'; | ||
import 'package:flame_3d/game.dart'; | ||
|
||
class RotatingLight extends LightComponent { | ||
RotatingLight() | ||
: super.spot( | ||
position: Vector3.zero(), | ||
); | ||
|
||
@override | ||
void update(double dt) { | ||
const radius = 15; | ||
final angle = DateTime.now().millisecondsSinceEpoch / 4000; | ||
final x = cos(angle) * radius; | ||
final z = sin(angle) * radius; | ||
position.setValues(x, 10, z); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export 'src/components/component_3d.dart'; | ||
export 'src/components/light_component.dart'; | ||
export 'src/components/mesh_component.dart'; | ||
export 'src/components/object_3d.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
export 'package:vector_math/vector_math.dart' | ||
show Vector2, Vector3, Vector4, Matrix3, Matrix4, Quaternion, Aabb3; | ||
show | ||
Vector2, | ||
Vector3, | ||
Vector4, | ||
Matrix2, | ||
Matrix3, | ||
Matrix4, | ||
Quaternion, | ||
Aabb3; | ||
|
||
export 'extensions.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,76 @@ | ||
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/graphics.dart'; | ||
import 'package:flame_3d/resources.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
/// {@template world_3d} | ||
/// The root component for all 3D world elements. | ||
/// | ||
/// 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 { | ||
class World3D extends flame.World with flame.HasGameReference { | ||
/// {@macro world_3d} | ||
World3D({ | ||
super.children, | ||
super.priority, | ||
Color clearColor = const Color(0x00000000), | ||
}) : graphics = GraphicsDevice(clearValue: clearColor); | ||
}) : device = GraphicsDevice(clearValue: clearColor) { | ||
children.register<LightComponent>(); | ||
} | ||
|
||
/// The graphical device attached to this world. | ||
final GraphicsDevice graphics; | ||
@internal | ||
final GraphicsDevice device; | ||
|
||
Iterable<Light> get lights => | ||
children.query<LightComponent>().map((component) => component.light); | ||
|
||
final _paint = Paint(); | ||
|
||
@internal | ||
@override | ||
void renderFromCamera(Canvas canvas) { | ||
final camera = CameraComponent3D.currentCamera!; | ||
|
||
final viewport = camera.viewport; | ||
graphics.begin( | ||
Size(viewport.virtualSize.x, viewport.virtualSize.y), | ||
transformMatrix: camera.projectionMatrix, | ||
|
||
final devicePixelRatio = MediaQuery.of(game.buildContext!).devicePixelRatio; | ||
final size = Size( | ||
viewport.virtualSize.x * devicePixelRatio, | ||
viewport.virtualSize.y * devicePixelRatio, | ||
); | ||
|
||
device | ||
// Set the view matrix | ||
..view.setFrom(camera.viewMatrix) | ||
// Set the projection matrix | ||
..projection.setFrom(camera.projectionMatrix) | ||
..begin(size); | ||
|
||
culled = 0; | ||
|
||
_prepareDevice(); | ||
// ignore: invalid_use_of_internal_member | ||
super.renderFromCamera(canvas); | ||
|
||
final image = graphics.end(); | ||
canvas.drawImage(image, (-viewport.virtualSize / 2).toOffset(), _paint); | ||
final image = device.end(); | ||
canvas.drawImageRect( | ||
image, | ||
Offset.zero & size, | ||
(-viewport.virtualSize / 2).toOffset() & | ||
Size(viewport.virtualSize.x, viewport.virtualSize.y), | ||
_paint, | ||
); | ||
image.dispose(); | ||
} | ||
|
||
// TODO(wolfen): this is only here for testing purposes | ||
void _prepareDevice() { | ||
device.lights = lights; | ||
} | ||
|
||
// TODO(wolfenrain): this is only here for testing purposes | ||
int culled = 0; | ||
} |
Oops, something went wrong.