Blender V4.5
workbench_materials.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BLI_ghash.h"
8#include "BLI_hash.h"
9#include "BLI_math_color.h"
10/* get_image */
12#include "DNA_material_types.h"
13#include "DNA_node_types.h"
14#include "ED_uvedit.hh"
15/* get_image */
16
17namespace blender::workbench {
18
19Material::Material() = default;
20
21Material::Material(float3 color)
22{
24 packed_data = Material::pack_data(0.0f, 0.4f, 1.0f);
25}
26
27Material::Material(::Object &ob, bool random)
28{
29 if (random) {
31 if (ob.id.lib) {
33 }
34 float3 hsv = float3(BLI_hash_int_01(hash), 0.5f, 0.8f);
36 }
37 else {
38 base_color = ob.color;
39 }
40 packed_data = Material::pack_data(0.0f, 0.4f, ob.color[3]);
41}
42
43Material::Material(::Material &mat)
44{
45 base_color = &mat.r;
46 packed_data = Material::pack_data(mat.metallic, mat.roughness, mat.a);
47}
48
49bool Material::is_transparent()
50{
51 uint32_t full_alpha_ref = 0x00ff0000;
52 return (packed_data & full_alpha_ref) != full_alpha_ref;
53}
54
55uint32_t Material::pack_data(float metallic, float roughness, float alpha)
56{
57 /* Remap to Disney roughness. */
58 roughness = sqrtf(roughness);
59 uint32_t packed_roughness = unit_float_to_uchar_clamp(roughness);
60 uint32_t packed_metallic = unit_float_to_uchar_clamp(metallic);
61 uint32_t packed_alpha = unit_float_to_uchar_clamp(alpha);
62 return (packed_alpha << 16u) | (packed_roughness << 8u) | packed_metallic;
63}
64
66{
67 const ::bNode *node = nullptr;
68
69 ::Image *image = nullptr;
70 ImageUser *user = nullptr;
71 ED_object_get_active_image(ob, material_index + 1, &image, &user, &node, nullptr);
72 if (!node || !image) {
73 return;
74 }
75
76 switch (node->type_legacy) {
77 case SH_NODE_TEX_IMAGE: {
78 const NodeTexImage *storage = static_cast<NodeTexImage *>(node->storage);
79 const bool use_filter = (storage->interpolation != SHD_INTERP_CLOSEST);
80 sampler_state.set_filtering_flag_from_test(GPU_SAMPLER_FILTERING_LINEAR, use_filter);
81 switch (storage->extension) {
83 default:
86 break;
90 break;
94 break;
98 break;
99 }
100 break;
101 }
103 const NodeTexEnvironment *storage = static_cast<NodeTexEnvironment *>(node->storage);
104 const bool use_filter = (storage->interpolation != SHD_INTERP_CLOSEST);
105 sampler_state.set_filtering_flag_from_test(GPU_SAMPLER_FILTERING_LINEAR, use_filter);
106 break;
107 }
108 default:
109 BLI_assert_msg(0, "Node type not supported by workbench");
110 }
111
112 gpu = BKE_image_get_gpu_material_texture(image, user, true);
115 name = image->id.name;
116}
117
125
126} // namespace blender::workbench
ImageGPUTextures BKE_image_get_gpu_material_texture(Image *image, ImageUser *iuser, const bool use_tile_mapping)
Definition image_gpu.cc:496
#define SH_NODE_TEX_IMAGE
#define SH_NODE_TEX_ENVIRONMENT
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
unsigned int BLI_ghashutil_strhash_p_murmur(const void *ptr)
BLI_INLINE float BLI_hash_int_01(unsigned int k)
Definition BLI_hash.h:92
void hsv_to_rgb_v(const float hsv[3], float r_rgb[3])
Definition math_color.cc:57
unsigned int uint
#define ELEM(...)
@ IMA_ALPHA_IGNORE
@ IMA_ALPHA_PREMUL
@ IMA_ALPHA_CHANNEL_PACKED
@ SHD_INTERP_CLOSEST
@ SHD_IMAGE_EXTENSION_MIRROR
@ SHD_IMAGE_EXTENSION_CLIP
@ SHD_IMAGE_EXTENSION_REPEAT
@ SHD_IMAGE_EXTENSION_EXTEND
bool ED_object_get_active_image(Object *ob, int mat_nr, Image **r_ima, ImageUser **r_iuser, const bNode **r_node, const bNodeTree **r_ntree)
@ GPU_SAMPLER_EXTEND_MODE_MIRRORED_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
@ GPU_SAMPLER_FILTERING_LINEAR
#define sqrtf(x)
IMETHOD void random(Vector &a)
addDelta operator for displacement rotational velocity.
Definition frames.inl:1282
MINLINE unsigned char unit_float_to_uchar_clamp(float val)
VecBase< float, 3 > float3
#define hash
Definition noise_c.cc:154
struct Library * lib
Definition DNA_ID.h:410
char name[66]
Definition DNA_ID.h:415
char alpha_mode
char filepath[1024]
Definition DNA_ID.h:507
float color[4]