Blender V5.3
io/usd/hydra/world.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "world.hh"
6
7#include <pxr/base/gf/rotation.h>
8#include <pxr/base/gf/vec3f.h>
9#include <pxr/imaging/hd/light.h>
10#include <pxr/imaging/hd/retainedDataSource.h>
11#include <pxr/imaging/hd/tokens.h>
12#include <pxr/usd/sdf/assetPath.h>
13#include <pxr/usd/usdLux/tokens.h>
14
15#include "BLI_math_constants.hh"
16
17#include "BKE_studiolight.h"
18
19#include "DNA_scene_types.h"
20#include "DNA_view3d_types.h"
21#include "DNA_world_types.h"
22
23#include "image.hh"
24#include "light.hh"
25#include "populate_context.hh"
26#include "usd_private.hh"
27
28/* TODO: add custom #TfToken "transparency"? */
29
30/* NOTE: opacity and blur aren't supported by USD */
31
32namespace blender::io::hydra {
33
34pxr::HdContainerDataSourceHandle build_world_data_source(Main *bmain,
35 Scene *scene,
36 const View3D *view3d,
37 pxr::GfMatrix4d *r_transform)
38{
39 pxr::GfVec3f color(1.0f, 1.0f, 1.0f);
40 float intensity = 1.0f;
41 pxr::SdfAssetPath texture_file;
42 pxr::GfMatrix4d transform(1.0);
43
44 const bool use_scene_world = !view3d || V3D_USES_SCENE_WORLD(view3d);
45
46 if (use_scene_world) {
47 if (!scene->world) {
48 return nullptr;
49 }
50
53
54 if (res.image) {
55 const std::string file_path = cache_or_get_image_file(bmain, scene, res.image, res.iuser);
56 if (!file_path.empty()) {
57 texture_file = pxr::SdfAssetPath(file_path, file_path);
58 }
59
60 if (res.mult_found) {
61 color = pxr::GfVec3f(res.color_mult);
62 }
63 }
64 else if (res.color_found) {
65 const std::string file_path = io::usd::cache_image_color(res.color);
66 texture_file = pxr::SdfAssetPath(file_path, file_path);
67 intensity = res.intensity;
68 }
69 else {
70 intensity = 0.0f;
71 color = pxr::GfVec3f(0.0f, 0.0f, 0.0f);
72 }
73
74 transform = res.transform;
75 }
76 else {
79 if (sl != nullptr && sl->flag & STUDIOLIGHT_TYPE_WORLD) {
80 texture_file = pxr::SdfAssetPath(sl->filepath, sl->filepath);
81 /* Coefficient to follow Cycles result */
82 intensity = view3d->shading.studiolight_intensity / 2.0f;
83 }
84 else {
85 return nullptr;
86 }
87
88 transform = pxr::GfMatrix4d().SetRotate(pxr::GfRotation(
89 pxr::GfVec3d(0.0, 0.0, -1.0), RAD2DEGF(view3d->shading.studiolight_rot_z)));
90 }
91
92 *r_transform = transform;
93
95 b.add(pxr::UsdLuxTokens->orientToStageUpAxis, true);
96 b.add(pxr::HdLightTokens->intensity, intensity);
97 b.add(pxr::HdLightTokens->exposure, 0.0f);
98 b.add(pxr::HdLightTokens->color, color);
99 b.add(pxr::HdLightTokens->textureFile, texture_file);
100 return b.build();
101}
102
104 Main *bmain,
105 Scene *scene,
106 const View3D *view3d,
107 const pxr::SdfPath &world_path,
108 const bool world_shading_changed)
109{
110 Inputs inputs;
111 inputs.world = scene->world;
112 inputs.use_scene_world = !view3d || V3D_USES_SCENE_WORLD(view3d);
113 if (view3d) {
114 inputs.shading_type = view3d->shading.type;
115 STRNCPY(inputs.studiolight, view3d->shading.lookdev_light);
116 inputs.studiolight_intensity = view3d->shading.studiolight_intensity;
117 inputs.studiolight_rot_z = view3d->shading.studiolight_rot_z;
118 }
119
120 if (used_ && inputs == cached_inputs_ && !world_shading_changed) {
121 ctx.new_paths.add(world_path);
122 return;
123 }
124
125 pxr::GfMatrix4d world_transform(1.0);
126 pxr::HdContainerDataSourceHandle world_params = build_world_data_source(
127 bmain, scene, view3d, &world_transform);
128 if (world_params) {
129 pxr::HdContainerDataSourceHandle world_prim = build_light_prim_data_source(
130 world_params, world_transform, true);
131 ctx.emit_prim(world_path, pxr::HdPrimTypeTokens->domeLight, world_prim);
132 }
133 used_ = (world_params != nullptr);
134 cached_inputs_ = inputs;
135}
136
138{
139 used_ = false;
140 cached_inputs_ = Inputs();
141}
142
143} // namespace blender::io::hydra
#define STUDIOLIGHT_ORIENTATIONS_MATERIAL_MODE
#define RAD2DEGF(_rad)
#define V3D_USES_SCENE_WORLD(v3d)
bool add(const Key &key)
Definition BLI_set.hh:248
void emit(PopulateContext &ctx, Main *bmain, Scene *scene, const View3D *view3d, const pxr::SdfPath &world_path, bool world_shading_changed)
std::string cache_or_get_image_file(Main *bmain, Scene *scene, Image *image, ImageUser *iuser)
pxr::HdContainerDataSourceHandle build_light_prim_data_source(const pxr::HdContainerDataSourceHandle &light_params, const pxr::GfMatrix4d &transform, const bool visible)
pxr::HdContainerDataSourceHandle build_world_data_source(Main *bmain, Scene *scene, const View3D *view3d, pxr::GfMatrix4d *r_transform)
void world_material_to_dome_light(const USDExportParams &params, const Scene *scene, pxr::UsdStageRefPtr stage)
std::string cache_image_color(const float color[4])
@ STUDIOLIGHT_TYPE_WORLD
char * STRNCPY(char(&dst)[N], const char *src)
struct StudioLight * BKE_studiolight_find(const char *name, int flag)
struct World * world
char filepath[FILE_MAX]
View3DShading shading
void emit_prim(const pxr::SdfPath &path, const pxr::TfToken &type, const pxr::HdContainerDataSourceHandle &ds)