Flutter Engine
The Flutter Engine
material.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
11#include "impeller/scene/importer/scene_flatbuffers.h"
14#include "impeller/scene/shaders/unlit.frag.h"
15
16#include <memory>
17
18namespace impeller {
19namespace scene {
20
21//------------------------------------------------------------------------------
22/// Material
23///
24
25Material::~Material() = default;
26
27std::unique_ptr<Material> Material::MakeFromFlatbuffer(
28 const fb::Material& material,
29 const std::vector<std::shared_ptr<Texture>>& textures) {
30 switch (material.type()) {
31 case fb::MaterialType::kUnlit:
32 return UnlitMaterial::MakeFromFlatbuffer(material, textures);
33 case fb::MaterialType::kPhysicallyBased:
34 return PhysicallyBasedMaterial::MakeFromFlatbuffer(material, textures);
35 }
36}
37
38std::unique_ptr<UnlitMaterial> Material::MakeUnlit() {
39 return std::make_unique<UnlitMaterial>();
40}
41
42std::unique_ptr<PhysicallyBasedMaterial> Material::MakePhysicallyBased() {
43 return std::make_unique<PhysicallyBasedMaterial>();
44}
45
47 vertex_color_weight_ = weight;
48}
49
51 blend_config_ = blend_config;
52}
53
55 stencil_config_ = stencil_config;
56}
57
58void Material::SetTranslucent(bool is_translucent) {
59 is_translucent_ = is_translucent;
60}
61
63 // TODO(bdero): Pipeline blend and stencil config.
64 return {.sample_count = pass.GetRenderTarget().GetSampleCount()};
65}
66
67//------------------------------------------------------------------------------
68/// UnlitMaterial
69///
70
71std::unique_ptr<UnlitMaterial> UnlitMaterial::MakeFromFlatbuffer(
72 const fb::Material& material,
73 const std::vector<std::shared_ptr<Texture>>& textures) {
74 if (material.type() != fb::MaterialType::kUnlit) {
75 VALIDATION_LOG << "Cannot unpack unlit material because the ipscene "
76 "material type is not unlit.";
77 return nullptr;
78 }
79
81
82 if (material.base_color_factor()) {
83 result->SetColor(importer::ToColor(*material.base_color_factor()));
84 }
85
86 if (material.base_color_texture() >= 0 &&
87 material.base_color_texture() < static_cast<int32_t>(textures.size())) {
88 result->SetColorTexture(textures[material.base_color_texture()]);
89 }
90
91 return result;
92}
93
95
97 color_ = color;
98}
99
100void UnlitMaterial::SetColorTexture(std::shared_ptr<Texture> color_texture) {
101 color_texture_ = std::move(color_texture);
102}
103
104// |Material|
107}
108
109// |Material|
112 RenderPass& pass) const {
113 // Uniform buffer.
114 UnlitFragmentShader::FragInfo info;
115 info.color = color_;
116 info.vertex_color_weight = vertex_color_weight_;
117 UnlitFragmentShader::BindFragInfo(pass, buffer.EmplaceUniform(info));
118
119 // Textures.
120 SamplerDescriptor sampler_descriptor;
121 sampler_descriptor.label = "Trilinear";
122 sampler_descriptor.min_filter = MinMagFilter::kLinear;
123 sampler_descriptor.mag_filter = MinMagFilter::kLinear;
124 sampler_descriptor.mip_filter = MipFilter::kLinear;
125 UnlitFragmentShader::BindBaseColorTexture(
126 pass,
127 color_texture_ ? color_texture_ : scene_context.GetPlaceholderTexture(),
128 scene_context.GetContext()->GetSamplerLibrary()->GetSampler(
129 sampler_descriptor));
130}
131
132//------------------------------------------------------------------------------
133/// StandardMaterial
134///
135
136std::unique_ptr<PhysicallyBasedMaterial>
138 const fb::Material& material,
139 const std::vector<std::shared_ptr<Texture>>& textures) {
140 if (material.type() != fb::MaterialType::kPhysicallyBased) {
141 VALIDATION_LOG << "Cannot unpack unlit material because the ipscene "
142 "material type is not unlit.";
143 return nullptr;
144 }
145
147
148 result->SetAlbedo(material.base_color_factor()
149 ? importer::ToColor(*material.base_color_factor())
150 : Color::White());
151 result->SetRoughness(material.roughness_factor());
152 result->SetMetallic(material.metallic_factor());
153
154 if (material.base_color_texture() >= 0 &&
155 material.base_color_texture() < static_cast<int32_t>(textures.size())) {
156 result->SetAlbedoTexture(textures[material.base_color_texture()]);
157 result->SetVertexColorWeight(0);
158 }
159 if (material.metallic_roughness_texture() >= 0 &&
160 material.metallic_roughness_texture() <
161 static_cast<int32_t>(textures.size())) {
162 result->SetMetallicRoughnessTexture(
163 textures[material.metallic_roughness_texture()]);
164 }
165 if (material.normal_texture() >= 0 &&
166 material.normal_texture() < static_cast<int32_t>(textures.size())) {
167 result->SetNormalTexture(textures[material.normal_texture()]);
168 }
169 if (material.occlusion_texture() >= 0 &&
170 material.occlusion_texture() < static_cast<int32_t>(textures.size())) {
171 result->SetOcclusionTexture(textures[material.occlusion_texture()]);
172 }
173
174 return result;
175}
176
178
180 albedo_ = albedo;
181}
182
184 roughness_ = roughness;
185}
186
188 metallic_ = metallic;
189}
190
192 std::shared_ptr<Texture> albedo_texture) {
193 albedo_texture_ = std::move(albedo_texture);
194}
195
197 std::shared_ptr<Texture> metallic_roughness_texture) {
198 metallic_roughness_texture_ = std::move(metallic_roughness_texture);
199}
200
202 std::shared_ptr<Texture> normal_texture) {
203 normal_texture_ = std::move(normal_texture);
204}
205
207 std::shared_ptr<Texture> occlusion_texture) {
208 occlusion_texture_ = std::move(occlusion_texture);
209}
210
212 std::shared_ptr<Texture> environment_map) {
213 environment_map_ = std::move(environment_map);
214}
215
216// |Material|
218 // TODO(bdero): Replace this once a PBR shader has landed.
220}
221
222// |Material|
225 RenderPass& pass) const {}
226
227} // namespace scene
228} // namespace impeller
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
const RenderTarget & GetRenderTarget() const
Definition: render_pass.cc:39
SampleCount GetSampleCount() const
static std::unique_ptr< Material > MakeFromFlatbuffer(const fb::Material &material, const std::vector< std::shared_ptr< Texture > > &textures)
Definition: material.cc:27
void SetVertexColorWeight(Scalar weight)
Definition: material.cc:46
void SetStencilConfig(StencilConfig stencil_config)
Definition: material.cc:54
void SetTranslucent(bool is_translucent)
Definition: material.cc:58
SceneContextOptions GetContextOptions(const RenderPass &pass) const
Definition: material.cc:62
static std::unique_ptr< PhysicallyBasedMaterial > MakePhysicallyBased()
Definition: material.cc:42
StencilConfig stencil_config_
Definition: material.h:69
void SetBlendConfig(BlendConfig blend_config)
Definition: material.cc:50
static std::unique_ptr< UnlitMaterial > MakeUnlit()
Definition: material.cc:38
BlendConfig blend_config_
Definition: material.h:68
void SetEnvironmentMap(std::shared_ptr< Texture > environment_map)
Definition: material.cc:211
void SetMetallicRoughnessTexture(std::shared_ptr< Texture > metallic_roughness_texture)
Definition: material.cc:196
static std::unique_ptr< PhysicallyBasedMaterial > MakeFromFlatbuffer(const fb::Material &material, const std::vector< std::shared_ptr< Texture > > &textures)
Definition: material.cc:137
MaterialType GetMaterialType() const override
Definition: material.cc:217
void SetNormalTexture(std::shared_ptr< Texture > normal_texture)
Definition: material.cc:201
void SetAlbedoTexture(std::shared_ptr< Texture > albedo_texture)
Definition: material.cc:191
void SetOcclusionTexture(std::shared_ptr< Texture > occlusion_texture)
Definition: material.cc:206
void SetRoughness(Scalar roughness)
Definition: material.cc:183
void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, RenderPass &pass) const override
Definition: material.cc:223
std::shared_ptr< Context > GetContext() const
std::shared_ptr< Texture > GetPlaceholderTexture() const
static std::unique_ptr< UnlitMaterial > MakeFromFlatbuffer(const fb::Material &material, const std::vector< std::shared_ptr< Texture > > &textures)
Definition: material.cc:71
MaterialType GetMaterialType() const override
Definition: material.cc:105
void BindToCommand(const SceneContext &scene_context, HostBuffer &buffer, RenderPass &pass) const override
Definition: material.cc:110
void SetColorTexture(std::shared_ptr< Texture > color_texture)
Definition: material.cc:100
void SetColor(Color color)
Definition: material.cc:96
DlColor color
GAsyncResult * result
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
Color ToColor(const fb::Color &c)
Definition: conversions.cc:46
float Scalar
Definition: scalar.h:18
@ kLinear
Sample from the two nearest mip levels and linearly interpolate.
static constexpr Color White()
Definition: color.h:266
#define VALIDATION_LOG
Definition: validation.h:73