Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
11 changes: 7 additions & 4 deletions packages/flame_3d/assets/shaders/spatial_material.wgslbundle

Large diffs are not rendered by default.

Binary file modified packages/flame_3d/assets/shaders/unlit_material.shaderbundle
Binary file not shown.
11 changes: 7 additions & 4 deletions packages/flame_3d/assets/shaders/unlit_material.wgslbundle

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ class _ObjectGroup({
this : super(children: children);

@override
void draw(RenderContext context) {}
void submit(RenderContext3D context) {}
}
2 changes: 2 additions & 0 deletions packages/flame_3d/example/lib/scenarios/game_scenario.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flame_3d_example/scenarios/boxes_scenario.dart';
import 'package:flame_3d_example/scenarios/colors_scenario.dart';
import 'package:flame_3d_example/scenarios/culling_scenario.dart';
import 'package:flame_3d_example/scenarios/models_scenario.dart';
import 'package:flame_3d_example/scenarios/transparency_scenario.dart';

abstract class GameScenario() {
Future<void> onLoad();
Expand All @@ -16,6 +17,7 @@ abstract class GameScenario() {
'colors': ColorsScenario(),
'culling': CullingScenario(),
'models': ModelsScenario(),
'transparency': TransparencyScenario(),
};

static void defaultSetup(ExampleGame3D game) {
Expand Down
113 changes: 113 additions & 0 deletions packages/flame_3d/example/lib/scenarios/transparency_scenario.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import 'dart:math' as math;
import 'dart:typed_data';

import 'package:flame/extensions.dart';
import 'package:flame_3d/components.dart';
import 'package:flame_3d/graphics.dart';
import 'package:flame_3d/resources.dart';
import 'package:flame_3d_example/example_game_3d.dart';
import 'package:flame_3d_example/scenarios/game_scenario.dart';

/// One quad per [Transparency] mode in front of an opaque wall, and two
/// overlapping quads whose `renderPriority` draws the far one on top.
class TransparencyScenario() implements GameScenario {
late final Texture _softDisc = _buildSoftDiscTexture(128);

@override
Future<void> onLoad() async {}

@override
void setup(ExampleGame3D game) {
// The player spawns at (0, 1, 0) facing +Z.
const z = 4.5;

for (final (i, mode) in Transparency.values.indexed) {
game.world.add(
_BobbingQuad(
position: Vector3((i - 2) * 1.5, 1.6, z),
material: _quadMaterial(_tints[i])..transparency = mode,
),
);
}

game.world.addAll([
_BobbingQuad(
position: Vector3(0, 3.2, z - 0.35),
material: _quadMaterial(const Color(0xFFFF4477))
..transparency = Transparency.alpha
..renderPriority = -1,
),
_BobbingQuad(
position: Vector3(0, 3.2, z + 0.35),
material: _quadMaterial(const Color(0xFF44DDFF))
..transparency = Transparency.alpha
..renderPriority = 1,
),
MeshComponent(
position: Vector3(0, 1.6, z + 0.6),
rotation: _upright,
mesh: PlaneMesh(
size: Vector2(10, 3.5),
material: UnlitMaterial(albedoColor: const Color(0xFF334466))
..cullMode = CullMode.none,
),
),
]);
}

UnlitMaterial _quadMaterial(Color tint) =>
UnlitMaterial(albedoColor: tint, albedoTexture: _softDisc)
..cullMode = CullMode.none;

static const _tints = [
Color(0xFFE0E0E0),
Color(0xFFFFCC44),
Color(0xFF66FF66),
Color(0xFFFF66AA),
Color(0xFF66CCFF),
];
}

// PlaneMesh lies in the XZ plane, so rotate it to stand upright.
final _upright = Quaternion.axisAngle(Vector3(1, 0, 0), math.pi / 2);

/// A quad that bobs up and down, so the alphaHash dither shows that it
/// stays fixed to the surface.
class _BobbingQuad({
required Vector3 position,
required UnlitMaterial material,
}) extends MeshComponent {
this
: super(
position: position,
rotation: _upright,
mesh: PlaneMesh(size: Vector2.all(1.35), material: material),
);

late final double _baseY = position.y;
double _t = 0;

@override
void update(double dt) {
super.update(dt);
_t += dt;
position.y = _baseY + math.sin(_t * 2) * 0.4;
}
}

/// White texture with a radial alpha ramp, so each mode looks different.
Texture _buildSoftDiscTexture(int size) {
final bytes = Uint8List(size * size * 4)..fillRange(0, size * size * 4, 255);
final center = (size - 1) / 2;
final maxRadius = size * 0.48;

for (var y = 0; y < size; y++) {
for (var x = 0; x < size; x++) {
final dist = math.sqrt(math.pow(x - center, 2) + math.pow(y - center, 2));
final alpha = (1 - dist / maxRadius).clamp(0.0, 1.0);
bytes[(y * size + x) * 4 + 3] = (alpha * 255).round();
}
}

return Texture(ByteData.view(bytes.buffer), width: size, height: size);
}
8 changes: 3 additions & 5 deletions packages/flame_3d/lib/src/components/mesh_component.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flame_3d/components.dart';
import 'package:flame_3d/game.dart';
import 'package:flame_3d/core.dart';
import 'package:flame_3d/graphics.dart';
import 'package:flame_3d/resources.dart';

Expand All @@ -22,9 +22,7 @@ class MeshComponent({
Aabb3? computeLocalAabb() => mesh.aabb;

@override
void draw(covariant RenderContext3D context) {
context
..model.setFrom(worldTransformMatrix)
..drawMesh(mesh);
void submit(RenderContext3D context) {
context.submitMesh(mesh, worldTransformMatrix);
}
}
6 changes: 4 additions & 2 deletions packages/flame_3d/lib/src/components/object_3d.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ abstract class Object3D({
_ancestorFullyInside = wasAncestorFullyInside;

if (cullResult == CullResult.inside || isVisible(camera!)) {
worldRef.context.submitDraw(this, worldTransformMatrix);
submit(worldRef.context);
}
}

void draw(RenderContext context);
/// Queue this object for drawing, for example with
/// [RenderContext3D.submitMesh].
void submit(RenderContext3D context);

/// Whether this component itself should be drawn when its [aabb] is not
/// fully inside the [camera]'s frustum.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,55 +257,18 @@ class const _FlutterGpuRenderTarget(
class const _FlutterGpuFrame(final GpuBackend _backend)
implements base.GpuFrame {
@override
base.GpuRenderPass beginRenderPass(
GpuRenderTarget target, {
required BlendState blend,
required DepthStencilState depthStencil,
}) {
base.GpuRenderPass beginRenderPass(GpuRenderTarget target) {
final commandBuffer = _backend._context.createCommandBuffer();
final renderPass =
commandBuffer.createRenderPass((target as _FlutterGpuRenderTarget).raw)
..setWindingOrder(gpu.WindingOrder.counterClockwise)
..setColorBlendEnable(blend != BlendState.opaque)
..setColorBlendEquation(_blendEquation(blend))
..setDepthWriteEnable(depthStencil == DepthStencilState.standard)
..setDepthCompareOperation(_depthCompare(depthStencil));
return _FlutterGpuRenderPass(_backend, commandBuffer, renderPass);
final renderPass = commandBuffer.createRenderPass(
(target as _FlutterGpuRenderTarget).raw,
)..setWindingOrder(gpu.WindingOrder.counterClockwise);
return _FlutterGpuRenderPass(_backend, commandBuffer, renderPass)
..setBlendState(BlendState.opaque)
..setDepthStencilState(DepthStencilState.standard);
}

@override
void end() => _backend._hostBuffer.reset();

static gpu.ColorBlendEquation _blendEquation(BlendState blend) {
return switch (blend) {
BlendState.opaque => gpu.ColorBlendEquation(
sourceColorBlendFactor: gpu.BlendFactor.one,
destinationColorBlendFactor: gpu.BlendFactor.zero,
sourceAlphaBlendFactor: gpu.BlendFactor.one,
destinationAlphaBlendFactor: gpu.BlendFactor.zero,
),
BlendState.alphaBlend => gpu.ColorBlendEquation(
sourceColorBlendFactor: gpu.BlendFactor.one,
destinationColorBlendFactor: gpu.BlendFactor.oneMinusSourceAlpha,
sourceAlphaBlendFactor: gpu.BlendFactor.one,
destinationAlphaBlendFactor: gpu.BlendFactor.oneMinusSourceAlpha,
),
BlendState.additive => gpu.ColorBlendEquation(
sourceColorBlendFactor: gpu.BlendFactor.one,
destinationColorBlendFactor: gpu.BlendFactor.one,
sourceAlphaBlendFactor: gpu.BlendFactor.one,
destinationAlphaBlendFactor: gpu.BlendFactor.one,
),
};
}

static gpu.CompareFunction _depthCompare(DepthStencilState state) {
return switch (state) {
DepthStencilState.standard => gpu.CompareFunction.less,
DepthStencilState.depthRead => gpu.CompareFunction.lessEqual,
DepthStencilState.none => gpu.CompareFunction.always,
};
}
}

class _FlutterGpuRenderPass(
Expand All @@ -318,6 +281,20 @@ class _FlutterGpuRenderPass(
int _vertexCount = 0;
int? _indexCount;

@override
void setBlendState(BlendState blend) {
_renderPass
..setColorBlendEnable(blend != BlendState.opaque)
..setColorBlendEquation(_blendEquation(blend));
}

@override
void setDepthStencilState(DepthStencilState depthStencil) {
_renderPass
..setDepthWriteEnable(depthStencil == DepthStencilState.standard)
..setDepthCompareOperation(_depthCompare(depthStencil));
}

@override
void bindPipeline(GpuPipeline pipeline, CullMode cullMode) {
_renderPass
Expand Down Expand Up @@ -390,4 +367,41 @@ class _FlutterGpuRenderPass(
lengthInBytes: view.lengthInBytes,
);
}

static gpu.ColorBlendEquation _blendEquation(BlendState blend) {
return switch (blend) {
BlendState.opaque => gpu.ColorBlendEquation(
sourceColorBlendFactor: gpu.BlendFactor.one,
destinationColorBlendFactor: gpu.BlendFactor.zero,
sourceAlphaBlendFactor: gpu.BlendFactor.one,
destinationAlphaBlendFactor: gpu.BlendFactor.zero,
),
BlendState.alphaBlend => gpu.ColorBlendEquation(
sourceColorBlendFactor: gpu.BlendFactor.one,
destinationColorBlendFactor: gpu.BlendFactor.oneMinusSourceAlpha,
sourceAlphaBlendFactor: gpu.BlendFactor.one,
destinationAlphaBlendFactor: gpu.BlendFactor.oneMinusSourceAlpha,
),
BlendState.additive => gpu.ColorBlendEquation(
sourceColorBlendFactor: gpu.BlendFactor.one,
destinationColorBlendFactor: gpu.BlendFactor.one,
sourceAlphaBlendFactor: gpu.BlendFactor.one,
destinationAlphaBlendFactor: gpu.BlendFactor.one,
),
BlendState.noColor => gpu.ColorBlendEquation(
sourceColorBlendFactor: gpu.BlendFactor.zero,
destinationColorBlendFactor: gpu.BlendFactor.one,
sourceAlphaBlendFactor: gpu.BlendFactor.zero,
destinationAlphaBlendFactor: gpu.BlendFactor.one,
),
};
}

static gpu.CompareFunction _depthCompare(DepthStencilState state) {
return switch (state) {
DepthStencilState.standard => gpu.CompareFunction.less,
DepthStencilState.depthRead => gpu.CompareFunction.lessEqual,
DepthStencilState.none => gpu.CompareFunction.always,
};
}
}
16 changes: 9 additions & 7 deletions packages/flame_3d/lib/src/graphics/backend/gpu_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,9 @@ abstract base class GpuBackend() {
/// Owns the transient uniform storage shared across the frame's render passes.
/// Obtain one via [GpuBackend.beginFrame] and finalize it with [end].
abstract interface class GpuFrame() {
/// Begins a render pass that draws into [target] using [blend] and
/// [depthStencil] state.
GpuRenderPass beginRenderPass(
GpuRenderTarget target, {
required BlendState blend,
required DepthStencilState depthStencil,
});
/// Begins a render pass that draws into [target], with
/// [BlendState.opaque] and [DepthStencilState.standard].
GpuRenderPass beginRenderPass(GpuRenderTarget target);

/// Finalizes the frame, releasing its transient storage.
void end();
Expand All @@ -99,6 +95,12 @@ abstract interface class GpuFrame() {
///
/// Obtain one via [GpuFrame.beginRenderPass] and submit it with [submit].
abstract interface class GpuRenderPass() {
/// Sets the color blending state for subsequent draws.
void setBlendState(BlendState blend);

/// Sets the depth/stencil behavior for subsequent draws.
void setDepthStencilState(DepthStencilState depthStencil);

/// Binds the render [pipeline] and applies [cullMode].
void bindPipeline(GpuPipeline pipeline, CullMode cullMode);

Expand Down
3 changes: 3 additions & 0 deletions packages/flame_3d/lib/src/graphics/backend/gpu_enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ enum BlendState() {

/// Additive blending: `color = src * 1 + dst * 1`.
additive,

/// No color write: `color = src * 0 + dst * 1`. Used for depth-only draws.
noColor,
}

/// Depth buffer behavior for a render pass.
Expand Down
Loading
Loading