Blender V4.3
bsdf_toon.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2010 Sony Pictures Imageworks Inc., et al. All Rights Reserved.
2 * SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Adapted code from Open Shading Language. */
7
8#pragma once
9
11
12typedef struct ToonBsdf {
14
15 float size;
16 float smooth;
18
19static_assert(sizeof(ShaderClosure) >= sizeof(ToonBsdf), "ToonBsdf is too large!");
20
22{
23 bsdf->size = clamp(bsdf->size, 1e-5f, 1.0f) * M_PI_2_F;
24 bsdf->smooth = saturatef(bsdf->smooth) * M_PI_2_F;
25
27}
28
29/* DIFFUSE TOON */
30
36
37ccl_device float bsdf_toon_get_intensity(float max_angle, float smooth, float angle)
38{
39 float is;
40
41 if (angle < max_angle) {
42 is = 1.0f;
43 }
44 else if (angle < (max_angle + smooth) && smooth != 0.0f) {
45 is = (1.0f - (angle - max_angle) / smooth);
46 }
47 else {
48 is = 0.0f;
49 }
50
51 return is;
52}
53
54ccl_device float bsdf_toon_get_sample_angle(float max_angle, float smooth)
55{
56 return fminf(max_angle + smooth, M_PI_2_F);
57}
58
60 const float3 wi,
61 const float3 wo,
62 ccl_private float *pdf)
63{
64 ccl_private const ToonBsdf *bsdf = (ccl_private const ToonBsdf *)sc;
65 float max_angle = bsdf->size;
66 float smooth = bsdf->smooth;
67 float cosNO = dot(bsdf->N, wo);
68
69 if (cosNO >= 0.0f) {
70 float angle = safe_acosf(fmaxf(cosNO, 0.0f));
71 float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
72
73 if (angle < sample_angle) {
74 float eval = bsdf_toon_get_intensity(max_angle, smooth, angle);
75 *pdf = M_1_2PI_F / one_minus_cos(sample_angle);
76 return make_spectrum(*pdf * eval);
77 }
78 }
79
80 *pdf = 0.0f;
81 return zero_spectrum();
82}
83
85 float3 Ng,
86 float3 wi,
87 float2 rand,
90 ccl_private float *pdf)
91{
92 ccl_private const ToonBsdf *bsdf = (ccl_private const ToonBsdf *)sc;
93 float max_angle = bsdf->size;
94 float smooth = bsdf->smooth;
95 float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
96
97 float cosNO;
98 *wo = sample_uniform_cone(bsdf->N, one_minus_cos(sample_angle), rand, &cosNO, pdf);
99
100 if (dot(Ng, *wo) > 0.0f) {
101 float angle = acosf(cosNO);
102 *eval = make_spectrum(*pdf * bsdf_toon_get_intensity(max_angle, smooth, angle));
104 }
105
106 *pdf = 0.0f;
107 *eval = zero_spectrum();
108 return LABEL_NONE;
109}
110
111/* GLOSSY TOON */
112
118
120 const float3 wi,
121 const float3 wo,
122 ccl_private float *pdf)
123{
124 ccl_private const ToonBsdf *bsdf = (ccl_private const ToonBsdf *)sc;
125 float max_angle = bsdf->size;
126 float smooth = bsdf->smooth;
127 float cosNI = dot(bsdf->N, wi);
128 float cosNO = dot(bsdf->N, wo);
129
130 if (cosNI > 0 && cosNO > 0) {
131 /* reflect the view vector */
132 float3 R = (2 * cosNI) * bsdf->N - wi;
133 float cosRO = dot(R, wo);
134
135 float angle = safe_acosf(fmaxf(cosRO, 0.0f));
136 float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
137
138 if (angle < sample_angle) {
139 float eval = bsdf_toon_get_intensity(max_angle, smooth, angle);
140 *pdf = M_1_2PI_F / one_minus_cos(sample_angle);
141 return make_spectrum(*pdf * eval);
142 }
143 }
144 *pdf = 0.0f;
145 return zero_spectrum();
146}
147
149 float3 Ng,
150 float3 wi,
151 float2 rand,
152 ccl_private Spectrum *eval,
154 ccl_private float *pdf)
155{
156 ccl_private const ToonBsdf *bsdf = (ccl_private const ToonBsdf *)sc;
157 float max_angle = bsdf->size;
158 float smooth = bsdf->smooth;
159 float cosNI = dot(bsdf->N, wi);
160
161 if (cosNI > 0) {
162 /* reflect the view vector */
163 float3 R = (2 * cosNI) * bsdf->N - wi;
164
165 float sample_angle = bsdf_toon_get_sample_angle(max_angle, smooth);
166
167 float cosRO;
168 *wo = sample_uniform_cone(R, one_minus_cos(sample_angle), rand, &cosRO, pdf);
169
170 /* make sure the direction we chose is still in the right hemisphere */
171 if (dot(Ng, *wo) > 0.0f && dot(bsdf->N, *wo) > 0.0f) {
172 float angle = acosf(cosRO);
173 *eval = make_spectrum(*pdf * bsdf_toon_get_intensity(max_angle, smooth, angle));
175 }
176 }
177
178 *pdf = 0.0f;
179 *eval = zero_spectrum();
180 return LABEL_NONE;
181}
182
MINLINE float safe_acosf(float a)
ccl_device int bsdf_diffuse_toon_sample(ccl_private const ShaderClosure *sc, float3 Ng, float3 wi, float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf)
Definition bsdf_toon.h:84
ccl_device int bsdf_glossy_toon_sample(ccl_private const ShaderClosure *sc, float3 Ng, float3 wi, float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf)
Definition bsdf_toon.h:148
ccl_device int bsdf_diffuse_toon_setup(ccl_private ToonBsdf *bsdf)
Definition bsdf_toon.h:31
ccl_device float bsdf_toon_get_intensity(float max_angle, float smooth, float angle)
Definition bsdf_toon.h:37
ccl_device float bsdf_toon_get_sample_angle(float max_angle, float smooth)
Definition bsdf_toon.h:54
ccl_device Spectrum bsdf_glossy_toon_eval(ccl_private const ShaderClosure *sc, const float3 wi, const float3 wo, ccl_private float *pdf)
Definition bsdf_toon.h:119
CCL_NAMESPACE_BEGIN struct ToonBsdf ToonBsdf
ccl_device_inline int bsdf_toon_setup_common(ccl_private ToonBsdf *bsdf)
Definition bsdf_toon.h:21
ccl_device int bsdf_glossy_toon_setup(ccl_private ToonBsdf *bsdf)
Definition bsdf_toon.h:113
ccl_device Spectrum bsdf_diffuse_toon_eval(ccl_private const ShaderClosure *sc, const float3 wi, const float3 wo, ccl_private float *pdf)
Definition bsdf_toon.h:59
additional_info("compositor_sum_squared_difference_float_shared") .push_constant(Type output_img float dot(value.rgb, luminance_coefficients)") .define("LOAD(value)"
#define ccl_device
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define saturatef(x)
#define fmaxf(x, y)
#define fminf(x, y)
#define acosf(x)
@ CLOSURE_BSDF_DIFFUSE_TOON_ID
@ CLOSURE_BSDF_GLOSSY_TOON_ID
@ SD_BSDF_HAS_EVAL
@ SD_BSDF
@ LABEL_DIFFUSE
@ LABEL_NONE
@ LABEL_GLOSSY
@ LABEL_REFLECT
ShaderClosure
#define R
ccl_device_inline float3 sample_uniform_cone(const float3 N, const float one_minus_cos_angle, const float2 rand, ccl_private float *cos_theta, ccl_private float *pdf)
#define M_PI_2_F
Definition sky_float3.h:20
float smooth
Definition bsdf_toon.h:16
float size
Definition bsdf_toon.h:15
SHADER_CLOSURE_BASE
Definition bsdf_toon.h:13
#define zero_spectrum
#define make_spectrum(f)
SPECTRUM_DATA_TYPE Spectrum
ccl_device_inline float one_minus_cos(const float angle)
Definition util/math.h:803
ccl_device_inline int clamp(int a, int mn, int mx)
Definition util/math.h:379