Skip to content

Commit 9c88ff8

Browse files
authored
feat(flame_3d): Add UnlitMaterial support (#3914)
Add `UnlitMaterial` support
1 parent 4338c62 commit 9c88ff8

5 files changed

Lines changed: 104 additions & 0 deletions

File tree

13.5 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export 'material/material.dart';
22
export 'material/spatial_material.dart';
3+
export 'material/unlit_material.dart';
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#version 460 core
2+
3+
in vec2 fragTexCoord;
4+
in vec4 fragColor;
5+
in vec3 fragPosition;
6+
in vec3 fragNormal;
7+
8+
out vec4 outColor;
9+
10+
uniform sampler2D albedoTexture;
11+
12+
uniform Material {
13+
vec4 albedoColor;
14+
} material;
15+
16+
void main() {
17+
vec4 texColor = texture(albedoTexture, fragTexCoord);
18+
outColor = texColor * material.albedoColor;
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#version 460 core
2+
3+
in vec3 vertexPosition;
4+
in vec2 vertexTexCoord;
5+
in vec4 vertexColor;
6+
in vec3 vertexNormal;
7+
in vec4 vertexJoints;
8+
in vec4 vertexWeights;
9+
10+
out vec2 fragTexCoord;
11+
out vec4 fragColor;
12+
out vec3 fragPosition;
13+
out vec3 fragNormal;
14+
15+
uniform VertexInfo {
16+
mat4 model;
17+
mat4 view;
18+
mat4 projection;
19+
} vertex_info;
20+
21+
void main() {
22+
mat4 mvp = vertex_info.projection * vertex_info.view * vertex_info.model;
23+
gl_Position = mvp * vec4(vertexPosition, 1.0);
24+
25+
fragTexCoord = vertexTexCoord;
26+
fragColor = vertexColor;
27+
28+
// Pass through all vertex attributes so the compiler doesn't strip them,
29+
// which would break the vertex buffer layout.
30+
fragPosition = vertexPosition + vertexJoints.xyz * vertexWeights.x;
31+
fragNormal = vertexNormal;
32+
}

0 commit comments

Comments
 (0)