Blender V4.3
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_hash.h"
8#include "BLI_math_color.h"
9/* get_image */
10#include "BKE_node.hh"
11#include "DNA_node_types.h"
12#include "ED_uvedit.hh"
13/* get_image */
14
15namespace blender::workbench {
16
17Material::Material() = default;
18
19Material::Material(float3 color)
20{
22 packed_data = Material::pack_data(0.0f, 0.4f, 1.0f);
23}
24
25Material::Material(::Object &ob, bool random)
26{
27 if (random) {
29 if (ob.id.lib) {
31 }
32 float3 hsv = float3(BLI_hash_int_01(hash), 0.5f, 0.8f);
34 }
35 else {
36 base_color = ob.color;
37 }
38 packed_data = Material::pack_data(0.0f, 0.4f, ob.color[3]);
39}
40
41Material::Material(::Material &mat)
42{
43 base_color = &mat.r;
44 packed_data = Material::pack_data(mat.metallic, mat.roughness, mat.a);
45}
46
47bool Material::is_transparent()
48{
49 uint32_t full_alpha_ref = 0x00ff0000;
50 return (packed_data & full_alpha_ref) != full_alpha_ref;
51}
52
53uint32_t Material::pack_data(float metallic, float roughness, float alpha)
54{
55 /* Remap to Disney roughness. */
56 roughness = sqrtf(roughness);
57 uint32_t packed_roughness = unit_float_to_uchar_clamp(roughness);
59 uint32_t packed_alpha = unit_float_to_uchar_clamp(alpha);
60 return (packed_alpha << 16u) | (packed_roughness << 8u) | packed_metallic;
61}
62
64{
65 const ::bNode *node = nullptr;
66
67 ::Image *image = nullptr;
68 ImageUser *user = nullptr;
69 ED_object_get_active_image(ob, material_index + 1, &image, &user, &node, nullptr);
70 if (!node || !image) {
71 return;
72 }
73
74 switch (node->type) {
75 case SH_NODE_TEX_IMAGE: {
76 const NodeTexImage *storage = static_cast<NodeTexImage *>(node->storage);
77 const bool use_filter = (storage->interpolation != SHD_INTERP_CLOSEST);
78 sampler_state.set_filtering_flag_from_test(GPU_SAMPLER_FILTERING_LINEAR, use_filter);
79 switch (storage->extension) {
81 default:
84 break;
88 break;
92 break;
96 break;
97 }
98 break;
99 }
101 const NodeTexEnvironment *storage = static_cast<NodeTexEnvironment *>(node->storage);
102 const bool use_filter = (storage->interpolation != SHD_INTERP_CLOSEST);
103 sampler_state.set_filtering_flag_from_test(GPU_SAMPLER_FILTERING_LINEAR, use_filter);
104 break;
105 }
106 default:
107 BLI_assert_msg(0, "Node type not supported by workbench");
108 }
109
111 premultiplied = image->alpha_mode == IMA_ALPHA_PREMUL;
113 name = image->id.name;
114}
115
123
124} // namespace blender::workbench
ImageGPUTextures BKE_image_get_gpu_material_texture(Image *image, ImageUser *iuser, const bool use_tile_mapping)
Definition image_gpu.cc:486
#define SH_NODE_TEX_IMAGE
Definition BKE_node.hh:930
#define SH_NODE_TEX_ENVIRONMENT
Definition BKE_node.hh:940
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:57
unsigned int BLI_ghashutil_strhash_p_murmur(const void *ptr)
BLI_INLINE float BLI_hash_int_01(unsigned int k)
Definition BLI_hash.h:96
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_IMAGE_EXTENSION_MIRROR
@ SHD_IMAGE_EXTENSION_CLIP
@ SHD_IMAGE_EXTENSION_REPEAT
@ SHD_IMAGE_EXTENSION_EXTEND
@ SHD_INTERP_CLOSEST
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
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Brightness Control the brightness and contrast of the input color Vector Map input vector components with curves Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert Invert a producing a negative Combine Generate a color from its and blue Hue Saturation Apply a color transformation in the HSV color model Specular Similar to the Principled BSDF node but uses the specular workflow instead of metallic
input_tx image(0, GPU_RGBA16F, Qualifier::WRITE, ImageType::FLOAT_2D, "preview_img") .compute_source("compositor_compute_preview.glsl") .do_static_compilation(true)
#define sqrtf(x)
IMETHOD void random(Vector &a)
addDelta operator for displacement rotational velocity.
Definition frames.inl:1282
#define unit_float_to_uchar_clamp(val)
VecBase< float, 3 > float3
#define hash
Definition noise.c:154
unsigned int uint32_t
Definition stdint.h:80
struct Library * lib
Definition DNA_ID.h:419
char name[66]
Definition DNA_ID.h:425
char filepath[1024]
Definition DNA_ID.h:531
float color[4]