From 3534b7efdb262a768cb60a30867268a1e4b8c7a4 Mon Sep 17 00:00:00 2001 From: Jochum van der Ploeg Date: Fri, 13 Dec 2024 16:37:01 +0100 Subject: [PATCH] fix: make `flame_3d` work with latest stable --- .github/workflows/cicd.yml | 2 ++ .../flame_3d/lib/src/extensions/vector4.dart | 6 ++++ .../lib/src/graphics/graphics_device.dart | 28 +++++++++++-------- .../lib/src/resources/mesh/vertex.dart | 2 +- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 2546fe0f837..a6a3a06f233 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -29,6 +29,8 @@ jobs: with: flutter-version: ${{env.FLUTTER_MIN_VERSION}} - uses: bluefireteam/melos-action@v3 + # flame_3d has a higher lowest supported version so we just remove it here to prevent CI errors + - run: rm -rf packages/flame_3d - name: "Analyze with lowest supported version" uses: invertase/github-action-dart-analyzer@v3 with: diff --git a/packages/flame_3d/lib/src/extensions/vector4.dart b/packages/flame_3d/lib/src/extensions/vector4.dart index 4eecda45160..92aeb46d6ed 100644 --- a/packages/flame_3d/lib/src/extensions/vector4.dart +++ b/packages/flame_3d/lib/src/extensions/vector4.dart @@ -1,3 +1,5 @@ +import 'dart:ui'; + import 'package:flame_3d/game.dart'; /// Represents an immutable [Vector3]. @@ -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 { diff --git a/packages/flame_3d/lib/src/graphics/graphics_device.dart b/packages/flame_3d/lib/src/graphics/graphics_device.dart index 83a24cb8f7e..b9971f31f86 100644 --- a/packages/flame_3d/lib/src/graphics/graphics_device.dart +++ b/packages/flame_3d/lib/src/graphics/graphics_device.dart @@ -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; @@ -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( @@ -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, }, ); @@ -106,9 +111,7 @@ class GraphicsDevice { /// Bind a [mesh]. void bindMesh(Mesh mesh) { - _renderPass.clearBindings(); mesh.bind(this); - _renderPass.draw(); } /// Bind a [surface]. @@ -163,21 +166,24 @@ 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: clearValue), + gpu.ColorAttachment( + texture: colorTexture!, + clearValue: Vector4Utils.fromColor(clearValue), + ), depthStencilAttachment: gpu.DepthStencilAttachment( texture: depthTexture!, depthClearValue: 1.0, diff --git a/packages/flame_3d/lib/src/resources/mesh/vertex.dart b/packages/flame_3d/lib/src/resources/mesh/vertex.dart index a11e8801fda..06efc48fc28 100644 --- a/packages/flame_3d/lib/src/resources/mesh/vertex.dart +++ b/packages/flame_3d/lib/src/resources/mesh/vertex.dart @@ -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