diff --git a/doc/flame/flame.md b/doc/flame/flame.md index 1e309465e81..7df5c339678 100644 --- a/doc/flame/flame.md +++ b/doc/flame/flame.md @@ -8,7 +8,7 @@ - [Platforms](platforms.md) - [Collision Detection](collision_detection.md) - [Effects](effects.md) -- [Camera Component](camera) +- [Camera](camera.md) - [Inputs](inputs/inputs.md) - [Rendering](rendering/rendering.md) - [Layout](layout/layout.md) 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 cbaac3e7e65..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,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!, diff --git a/packages/flame_3d/lib/src/resources/material/spatial_material.dart b/packages/flame_3d/lib/src/resources/material/spatial_material.dart index eb3b088dfda..b15022767f0 100644 --- a/packages/flame_3d/lib/src/resources/material/spatial_material.dart +++ b/packages/flame_3d/lib/src/resources/material/spatial_material.dart @@ -12,8 +12,9 @@ class SpatialMaterial extends Material { this.roughness = 0.6, }) : albedoTexture = albedoTexture ?? Texture.standard, super( - vertexShader: Shader( - name: 'TextureVertex', + vertexShader: Shader.vertex( + asset: + 'packages/flame_3d/assets/shaders/spatial_material.shaderbundle', slots: [ UniformSlot.value('VertexInfo', { 'model', @@ -26,8 +27,9 @@ class SpatialMaterial extends Material { ), ], ), - fragmentShader: Shader( - name: 'TextureFragment', + fragmentShader: Shader.fragment( + asset: + 'packages/flame_3d/assets/shaders/spatial_material.shaderbundle', slots: [ UniformSlot.sampler('albedoTexture'), UniformSlot.value('Material', { 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 diff --git a/packages/flame_3d/lib/src/resources/shader/shader.dart b/packages/flame_3d/lib/src/resources/shader/shader.dart index a7e1bd5f4fa..30e645130e5 100644 --- a/packages/flame_3d/lib/src/resources/shader/shader.dart +++ b/packages/flame_3d/lib/src/resources/shader/shader.dart @@ -6,49 +6,53 @@ import 'package:flame_3d/graphics.dart'; import 'package:flame_3d/resources.dart'; import 'package:flutter_gpu/gpu.dart' as gpu; -/// {@template shader} +/// {@template shader_resource} /// /// {@endtemplate} class ShaderResource extends Resource { - final Shader shader; + /// {@macro shader_resource} + factory ShaderResource.createFromAsset({ + required String asset, + required String shaderName, + required List slots, + }) { + final library = gpu.ShaderLibrary.fromAsset(asset)!; - /// {@macro shader} - ShaderResource( - super.resource, { - required String name, - List slots = const [], - }) : shader = Shader(name: name, slots: slots) { - for (final slot in slots) { - slot.resource = resource.getUniformSlot(slot.name); + final shader = library[shaderName]; + if (shader == null) { + throw StateError('Shader "$shaderName" not found in library "$asset"'); } + return ShaderResource._(shader, slots: slots); } - factory ShaderResource.create({ - required String name, - required List slots, + ShaderResource._( + super.resource, { + List slots = const [], }) { - final shader = _library[name]; - if (shader == null) { - throw StateError('Shader "$name" not found in library'); + for (final slot in slots) { + slot.resource = resource.getUniformSlot(slot.name); } - return ShaderResource(shader, name: name, slots: slots); } - - static final _library = gpu.ShaderLibrary.fromAsset( - 'packages/flame_3d/assets/shaders/spatial_material.shaderbundle', - )!; } class Shader { + final String asset; final String name; final List slots; final Map instances = {}; Shader({ + required this.asset, required this.name, required this.slots, }); + Shader.vertex({required String asset, required List slots}) + : this(asset: asset, name: 'TextureVertex', slots: slots); + + Shader.fragment({required String asset, required List slots}) + : this(asset: asset, name: 'TextureFragment', slots: slots); + /// Set a [Texture] at the given [key] on the buffer. void setTexture(String key, Texture texture) => _setTypedValue(key, texture); @@ -140,6 +144,10 @@ class Shader { } ShaderResource compile() { - return ShaderResource.create(name: name, slots: slots); + return ShaderResource.createFromAsset( + asset: asset, + shaderName: name, + slots: slots, + ); } } diff --git a/packages/flame_3d/test/resources/shader/uniform_binding_test.dart b/packages/flame_3d/test/resources/shader/uniform_binding_test.dart index f628a34df6e..151dc97a24a 100644 --- a/packages/flame_3d/test/resources/shader/uniform_binding_test.dart +++ b/packages/flame_3d/test/resources/shader/uniform_binding_test.dart @@ -107,6 +107,7 @@ void main() { Shader _createShader(List slots) { return Shader( + asset: 'none', name: '-test-', slots: slots, );