|
| 1 | +import 'dart:ui' hide FragmentShader; |
| 2 | + |
| 3 | +import 'package:flame_3d/graphics.dart'; |
| 4 | +import 'package:flame_3d/resources.dart'; |
| 5 | + |
| 6 | +/// {@template unlit_material} |
| 7 | +/// A material that renders textures without any lighting calculations. |
| 8 | +/// |
| 9 | +/// Unlike [SpatialMaterial], this material outputs the texture color multiplied |
| 10 | +/// by [albedoColor] directly, with no PBR shading, tone mapping, or gamma |
| 11 | +/// correction. This makes it ideal for: |
| 12 | +/// |
| 13 | +/// - 2D canvas content rendered onto 3D surfaces |
| 14 | +/// - UI panels and HUD elements in 3D space |
| 15 | +/// - Emissive/self-lit surfaces |
| 16 | +/// - Sky boxes and debug rendering |
| 17 | +/// {@endtemplate} |
| 18 | +class UnlitMaterial extends Material { |
| 19 | + /// {@macro unlit_material} |
| 20 | + UnlitMaterial({ |
| 21 | + this.albedoColor = const Color(0xFFFFFFFF), |
| 22 | + Texture? albedoTexture, |
| 23 | + }) : albedoTexture = albedoTexture ?? Texture.standard, |
| 24 | + super( |
| 25 | + vertexShader: VertexShader.fromAsset( |
| 26 | + 'packages/flame_3d/assets/shaders/unlit_material.shaderbundle', |
| 27 | + slots: ['VertexInfo'], |
| 28 | + ), |
| 29 | + fragmentShader: FragmentShader.fromAsset( |
| 30 | + 'packages/flame_3d/assets/shaders/unlit_material.shaderbundle', |
| 31 | + slots: ['albedoTexture', 'Material'], |
| 32 | + ), |
| 33 | + ); |
| 34 | + |
| 35 | + /// The material's base color, multiplied with [albedoTexture]. |
| 36 | + Color albedoColor; |
| 37 | + |
| 38 | + /// The texture to render. Multiplied by [albedoColor]. |
| 39 | + Texture albedoTexture; |
| 40 | + |
| 41 | + @override |
| 42 | + void apply(covariant RenderContext3D context) { |
| 43 | + vertexShader |
| 44 | + ..setMatrix4('VertexInfo.model', context.model) |
| 45 | + ..setMatrix4('VertexInfo.view', context.view) |
| 46 | + ..setMatrix4('VertexInfo.projection', context.projection); |
| 47 | + |
| 48 | + fragmentShader |
| 49 | + ..setTexture('albedoTexture', albedoTexture) |
| 50 | + ..setColor('Material.albedoColor', albedoColor); |
| 51 | + } |
| 52 | +} |
0 commit comments