Skip to content
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

fix: Make flame_3d work with latest stable #3387

Merged
merged 6 commits into from
Dec 14, 2024
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
6 changes: 6 additions & 0 deletions packages/flame_3d/lib/src/extensions/vector4.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ui';

import 'package:flame_3d/game.dart';

/// Represents an immutable [Vector3].
Expand All @@ -18,6 +20,10 @@ class Vector4Utils {
static Vector4 lerp(Vector4 a, Vector4 b, double t) {
return a + (b - a).scaled(t);
}

static Vector4 fromColor(Color color) {
return Vector4(color.r, color.g, color.b, color.a);
}
}

extension Vector4Math on ImmutableVector4 {
Expand Down
30 changes: 14 additions & 16 deletions packages/flame_3d/lib/src/graphics/graphics_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ enum DepthStencilState {
/// {@endtemplate}
class GraphicsDevice {
/// {@macro graphics_device}
GraphicsDevice({this.clearValue = const Color(0x00000000)});
GraphicsDevice({
this.clearValue = const Color(0x00000000),
gpu.GpuContext? gpuContext,
}) : _gpuContext = gpuContext ?? gpu.gpuContext;

final gpu.GpuContext _gpuContext;

/// The clear value, used to clear out the screen.
final Color clearValue;
Expand Down Expand Up @@ -71,8 +76,9 @@ class GraphicsDevice {
// TODO(wolfenrain): used incorrectly
DepthStencilState depthStencilState = DepthStencilState.depthRead,
}) {
_commandBuffer = gpu.gpuContext.createCommandBuffer();
_hostBuffer = gpu.gpuContext.createHostBuffer();
_commandBuffer = _gpuContext.createCommandBuffer();
_hostBuffer = _gpuContext.createHostBuffer();

_renderPass = _commandBuffer.createRenderPass(_getRenderTarget(size))
..setColorBlendEnable(true)
..setColorBlendEquation(
Expand All @@ -84,10 +90,9 @@ class GraphicsDevice {
)
..setDepthWriteEnable(depthStencilState == DepthStencilState.depthRead)
..setDepthCompareOperation(
// TODO(wolfenrain): this is not correctly implemented AT all.
switch (depthStencilState) {
DepthStencilState.none => gpu.CompareFunction.never,
DepthStencilState.standard => gpu.CompareFunction.always,
DepthStencilState.standard => gpu.CompareFunction.lessEqual,
DepthStencilState.depthRead => gpu.CompareFunction.less,
},
);
Expand All @@ -106,9 +111,7 @@ class GraphicsDevice {

/// Bind a [mesh].
void bindMesh(Mesh mesh) {
_renderPass.clearBindings();
mesh.bind(this);
_renderPass.draw();
}

/// Bind a [surface].
Expand Down Expand Up @@ -163,28 +166,23 @@ class GraphicsDevice {
if (_previousSize != size) {
_previousSize = size;

final colorTexture = gpu.gpuContext.createTexture(
final colorTexture = _gpuContext.createTexture(
gpu.StorageMode.devicePrivate,
size.width.toInt(),
size.height.toInt(),
);

final depthTexture = gpu.gpuContext.createTexture(
final depthTexture = _gpuContext.createTexture(
gpu.StorageMode.deviceTransient,
size.width.toInt(),
size.height.toInt(),
format: gpu.gpuContext.defaultDepthStencilFormat,
format: _gpuContext.defaultDepthStencilFormat,
);

_renderTarget = gpu.RenderTarget.singleColor(
gpu.ColorAttachment(
texture: colorTexture!,
clearValue: Vector4(
clearValue.r,
clearValue.g,
clearValue.b,
clearValue.a,
),
clearValue: Vector4Utils.fromColor(clearValue),
),
depthStencilAttachment: gpu.DepthStencilAttachment(
texture: depthTexture!,
Expand Down
2 changes: 1 addition & 1 deletion packages/flame_3d/lib/src/resources/mesh/vertex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Vertex {
_storage = Float32List.fromList([
...position.storage, // 1, 2, 3
...texCoord.storage, // 4, 5
...color.storage, // 6, 7, 8, 9
...[color.r, color.g, color.b, color.a], // 6, 7, 8, 9
...(normal ?? Vector3.zero()).storage, // 10, 11, 12
...(joints ?? Vector4.zero()).storage, // 13, 14, 15, 16
...(weights ?? Vector4.zero()).storage, // 17, 18, 19, 20
Expand Down
Loading