-
-
Notifications
You must be signed in to change notification settings - Fork 913
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
1 parent
4fc180a
commit 07594dd
Showing
20 changed files
with
332 additions
and
70 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
Binary file modified
BIN
+25.1 KB
(180%)
packages/flame_3d/assets/shaders/spatial_material.shaderbundle
Binary file not shown.
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
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,3 +1,5 @@ | ||
export 'light/ambient_light.dart'; | ||
export 'light/light.dart'; | ||
export 'light/light_source.dart'; | ||
export 'light/spot_light.dart'; | ||
export 'light/lighting_info.dart'; | ||
export 'light/point_light.dart'; |
15 changes: 15 additions & 0 deletions
15
packages/flame_3d/lib/src/resources/light/ambient_light.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'dart:ui' show Color; | ||
|
||
import 'package:flame_3d/resources.dart'; | ||
|
||
class AmbientLight extends LightSource { | ||
AmbientLight({ | ||
super.color = const Color(0xFFFFFFFF), | ||
super.intensity = 0.2, | ||
}); | ||
|
||
void apply(Shader shader) { | ||
shader.setColor('AmbientLight.color', color); | ||
shader.setFloat('AmbientLight.intensity', intensity); | ||
} | ||
} |
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
14 changes: 11 additions & 3 deletions
14
packages/flame_3d/lib/src/resources/light/light_source.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,8 +1,16 @@ | ||
import 'dart:ui' show Color; | ||
|
||
import 'package:flame_3d/resources.dart'; | ||
|
||
/// Describes the properties of a light source. | ||
/// There are three types of light sources: directional, point, and spot. | ||
/// Currently only [SpotLight] is implemented. | ||
/// There are three types of light sources: point, directional, and spot. | ||
/// Currently only [PointLight] is implemented. | ||
abstract class LightSource { | ||
void apply(Shader shader); | ||
final Color color; | ||
final double intensity; | ||
|
||
LightSource({ | ||
required this.color, | ||
required this.intensity, | ||
}); | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/flame_3d/lib/src/resources/light/lighting_info.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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import 'package:flame_3d/resources.dart'; | ||
|
||
class LightingInfo { | ||
Iterable<Light> lights = []; | ||
|
||
void apply(Shader shader) { | ||
_applyAmbientLight(shader); | ||
_applyPointLights(shader); | ||
} | ||
|
||
void _applyAmbientLight(Shader shader) { | ||
final ambient = _extractAmbientLight(lights); | ||
ambient.apply(shader); | ||
} | ||
|
||
void _applyPointLights(Shader shader) { | ||
final pointLights = lights.where((e) => e.source is PointLight); | ||
final numLights = pointLights.length; | ||
if (numLights > 3) { | ||
// temporary, until we support dynamic arrays | ||
throw Exception('At most 3 point lights are allowed'); | ||
} | ||
|
||
shader.setUint('LightsInfo.numLights', numLights); | ||
for (final (idx, light) in pointLights.indexed) { | ||
light.apply(idx, shader); | ||
} | ||
} | ||
|
||
AmbientLight _extractAmbientLight(Iterable<Light> lights) { | ||
final ambient = lights.where((e) => e.source is AmbientLight); | ||
if (ambient.isEmpty) { | ||
return AmbientLight(); | ||
} | ||
if (ambient.length > 1) { | ||
throw Exception('At most one ambient light is allowed'); | ||
} | ||
return ambient.first.source as AmbientLight; | ||
} | ||
|
||
static List<UniformSlot> shaderSlots = [ | ||
UniformSlot.value('AmbientLight', {'color', 'intensity'}), | ||
UniformSlot.value('LightsInfo', {'numLights'}), | ||
UniformSlot.value('Light0', {'position', 'color', 'intensity'}), | ||
UniformSlot.value('Light1', {'position', 'color', 'intensity'}), | ||
UniformSlot.value('Light2', {'position', 'color', 'intensity'}), | ||
]; | ||
} |
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,9 @@ | ||
import 'package:flame_3d/resources.dart'; | ||
|
||
/// A point light that emits light in all directions equally. | ||
class PointLight extends LightSource { | ||
PointLight({ | ||
required super.color, | ||
required super.intensity, | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.