-
-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: More Lights! [flame_3d] #3250
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a693ec9
working on lights
luanpotter 51faa44
Make it work
luanpotter 886e114
Export world config
luanpotter f7c0cb1
Update to use component
luanpotter d0f58fc
Apply suggestions from code review
luanpotter cf82ed1
Update packages/flame_3d/lib/src/resources/light/lighting_info.dart
luanpotter 79d3061
Fix build
luanpotter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 = []; | ||
|
luanpotter marked this conversation as resolved.
|
||
|
|
||
| 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.