Skip to content

Commit 07594dd

Browse files
feat: More Lights! [flame_3d] (#3250)
More Lights! More fun! I am still trying to get arrays to work (not even the fancy SSBOs, just plain fixed arrays). In the meanwhile this puts lights as separate objects. This supports: * point lights * ambient lights * colors * intensity ![image](https://github.com/user-attachments/assets/a2f75a8a-9c64-42d1-bbe5-bdf58fa7df69) --------- Co-authored-by: Jochum van der Ploeg <jochum@vdploeg.net>
1 parent 4fc180a commit 07594dd

20 files changed

Lines changed: 332 additions & 70 deletions

.github/.cspell/people_usernames.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ tavian # tavianator.com
2020
videon # github.com/markvideon
2121
wolfenrain # github.com/wolfenrain
2222
xaha # github.com/xvrh
23+
luan # github.com/luanpotter
25.1 KB
Binary file not shown.
-34.7 KB
Binary file not shown.

packages/flame_3d/example/lib/main.dart

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,43 @@ class ExampleGame3D extends FlameGame<World3D>
3636
@override
3737
FutureOr<void> onLoad() async {
3838
world.addAll([
39+
LightComponent.ambient(
40+
intensity: 1.0,
41+
),
3942
RotatingLight(),
4043

44+
LightComponent.point(
45+
position: Vector3(0, 0.1, 0),
46+
color: const Color(0xFFFF00FF),
47+
),
48+
MeshComponent(
49+
mesh: SphereMesh(
50+
radius: 0.05,
51+
material: SpatialMaterial(
52+
albedoTexture: ColorTexture(
53+
const Color(0xFFFF00FF),
54+
),
55+
),
56+
),
57+
position: Vector3(0, 0.1, 0),
58+
),
59+
60+
LightComponent.point(
61+
position: Vector3(-2, 3, 2),
62+
color: const Color(0xFFFF2255),
63+
),
64+
MeshComponent(
65+
mesh: SphereMesh(
66+
radius: 0.05,
67+
material: SpatialMaterial(
68+
albedoTexture: ColorTexture(
69+
const Color(0xFFFF2255),
70+
),
71+
),
72+
),
73+
position: Vector3(-2, 4, 2),
74+
),
75+
4176
// Add a player box
4277
PlayerBox(),
4378

@@ -50,7 +85,7 @@ class ExampleGame3D extends FlameGame<World3D>
5085
mesh: SphereMesh(
5186
radius: 1,
5287
material: SpatialMaterial(
53-
albedoTexture: ColorTexture(Colors.purple),
88+
albedoTexture: ColorTexture(Colors.green),
5489
),
5590
),
5691
),

packages/flame_3d/example/lib/rotating_light.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import 'dart:math';
2+
import 'dart:ui';
23

34
import 'package:flame_3d/components.dart';
45
import 'package:flame_3d/game.dart';
56

67
class RotatingLight extends LightComponent {
78
RotatingLight()
8-
: super.spot(
9+
: super.point(
910
position: Vector3.zero(),
11+
color: const Color(0xFF00FF00),
12+
intensity: 20.0,
1013
);
1114

1215
@override

packages/flame_3d/lib/src/camera/world_3d.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ class World3D extends flame.World with flame.HasGameReference {
6969
image.dispose();
7070
}
7171

72+
// TODO(luan): consider making this a fixed-size array later
7273
void _prepareDevice() {
73-
device.lights = lights;
74+
device.lightingInfo.lights = lights;
7475
}
7576

7677
// TODO(wolfenrain): this is only here for testing purposes

packages/flame_3d/lib/src/components/light_component.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:ui';
2+
13
import 'package:flame_3d/camera.dart';
24
import 'package:flame_3d/components.dart';
35
import 'package:flame_3d/game.dart';
@@ -10,13 +12,28 @@ class LightComponent extends Component3D {
1012
super.position,
1113
});
1214

13-
LightComponent.spot({
15+
LightComponent.point({
1416
Vector3? position,
17+
Color color = const Color(0xFFFFFFFF),
18+
double intensity = 1.0,
1519
}) : this(
16-
source: SpotLight(),
20+
source: PointLight(
21+
color: color,
22+
intensity: intensity,
23+
),
1724
position: position,
1825
);
1926

27+
LightComponent.ambient({
28+
Color color = const Color(0xFFFFFFFF),
29+
double intensity = 0.2,
30+
}) : this(
31+
source: AmbientLight(
32+
color: color,
33+
intensity: intensity,
34+
),
35+
);
36+
2037
final LightSource source;
2138

2239
late final Light _light = Light(

packages/flame_3d/lib/src/extensions/color.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import 'dart:ui';
33

44
extension ColorExtension on Color {
55
/// Returns a Float32List that represents the color as a vector.
6-
Float32List get storage =>
7-
Float32List.fromList([red / 255, green / 255, blue / 255, opacity]);
6+
Float32List get storage => Float32List.fromList([
7+
opacity,
8+
red.toDouble() / 255,
9+
green.toDouble() / 255,
10+
blue.toDouble() / 255,
11+
]);
812
}

packages/flame_3d/lib/src/graphics/graphics_device.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class GraphicsDevice {
5050

5151
/// Must be set by the rendering pipeline before elements are bound.
5252
/// Can be accessed by elements in their bind method.
53-
Iterable<Light> lights = [];
53+
final LightingInfo lightingInfo = LightingInfo();
5454

5555
/// Begin a new rendering batch.
5656
///
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export 'light/ambient_light.dart';
12
export 'light/light.dart';
23
export 'light/light_source.dart';
3-
export 'light/spot_light.dart';
4+
export 'light/lighting_info.dart';
5+
export 'light/point_light.dart';

0 commit comments

Comments
 (0)