Changeset View
Changeset View
Standalone View
Standalone View
source/blender/draw/engines/workbench/workbench_materials_next.cc
- This file was added.
| /* SPDX-License-Identifier: GPL-2.0-or-later */ | |||||
| #include "workbench_private.hh" | |||||
| #include "BLI_hash.h" | |||||
| /* get_image */ | |||||
| #include "BKE_node.h" | |||||
| #include "DNA_node_types.h" | |||||
| #include "ED_uvedit.h" | |||||
| /* get_image */ | |||||
| namespace blender::workbench { | |||||
| Material::Material() = default; | |||||
| Material::Material(float3 color) | |||||
| { | |||||
| base_color = color; | |||||
| packed_data = Material::pack_data(0.0f, 0.4f, 1.0f); | |||||
| } | |||||
| Material::Material(::Object &ob, bool random) | |||||
| { | |||||
| if (random) { | |||||
| uint hash = BLI_ghashutil_strhash_p_murmur(ob.id.name); | |||||
| if (ob.id.lib) { | |||||
| hash = (hash * 13) ^ BLI_ghashutil_strhash_p_murmur(ob.id.lib->filepath); | |||||
| } | |||||
| float3 hsv = float3(BLI_hash_int_01(hash), 0.5f, 0.8f); | |||||
| hsv_to_rgb_v(hsv, base_color); | |||||
| } | |||||
| else { | |||||
| base_color = ob.color; | |||||
| } | |||||
| packed_data = Material::pack_data(0.0f, 0.4f, ob.color[3]); | |||||
| } | |||||
| Material::Material(::Material &mat) | |||||
| { | |||||
| base_color = &mat.r; | |||||
| packed_data = Material::pack_data(mat.metallic, mat.roughness, mat.a); | |||||
| } | |||||
| bool Material::is_transparent() | |||||
| { | |||||
| uint32_t full_alpha_ref = 0x00ff0000; | |||||
| return (packed_data & full_alpha_ref) != full_alpha_ref; | |||||
| } | |||||
| uint32_t Material::pack_data(float metallic, float roughness, float alpha) | |||||
| { | |||||
| /* Remap to Disney roughness. */ | |||||
| roughness = sqrtf(roughness); | |||||
| uint32_t packed_roughness = unit_float_to_uchar_clamp(roughness); | |||||
| uint32_t packed_metallic = unit_float_to_uchar_clamp(metallic); | |||||
| uint32_t packed_alpha = unit_float_to_uchar_clamp(alpha); | |||||
| return (packed_alpha << 16u) | (packed_roughness << 8u) | packed_metallic; | |||||
| } | |||||
| void get_material_image(Object *ob, | |||||
| int material_index, | |||||
| ::Image *&image, | |||||
| ImageUser *&iuser, | |||||
| eGPUSamplerState &sampler_state) | |||||
| { | |||||
| const ::bNode *node = nullptr; | |||||
| ED_object_get_active_image(ob, material_index, &image, &iuser, &node, nullptr); | |||||
| if (node && image) { | |||||
| switch (node->type) { | |||||
| case SH_NODE_TEX_IMAGE: { | |||||
| const NodeTexImage *storage = static_cast<NodeTexImage *>(node->storage); | |||||
| const bool use_filter = (storage->interpolation != SHD_INTERP_CLOSEST); | |||||
| const bool use_mirror = (storage->extension == SHD_IMAGE_EXTENSION_MIRROR); | |||||
| const bool use_repeat = use_mirror || (storage->extension == SHD_IMAGE_EXTENSION_REPEAT); | |||||
| const bool use_clip = (storage->extension == SHD_IMAGE_EXTENSION_CLIP); | |||||
| SET_FLAG_FROM_TEST(sampler_state, use_filter, GPU_SAMPLER_FILTER); | |||||
| SET_FLAG_FROM_TEST(sampler_state, use_repeat, GPU_SAMPLER_REPEAT); | |||||
| SET_FLAG_FROM_TEST(sampler_state, use_clip, GPU_SAMPLER_CLAMP_BORDER); | |||||
| SET_FLAG_FROM_TEST(sampler_state, use_mirror, GPU_SAMPLER_MIRROR_REPEAT); | |||||
| break; | |||||
| } | |||||
| case SH_NODE_TEX_ENVIRONMENT: { | |||||
| const NodeTexEnvironment *storage = static_cast<NodeTexEnvironment *>(node->storage); | |||||
| const bool use_filter = (storage->interpolation != SHD_INTERP_CLOSEST); | |||||
| SET_FLAG_FROM_TEST(sampler_state, use_filter, GPU_SAMPLER_FILTER); | |||||
| break; | |||||
| } | |||||
| default: | |||||
| BLI_assert_msg(0, "Node type not supported by workbench"); | |||||
| } | |||||
| } | |||||
| } | |||||
| } // namespace blender::workbench | |||||