Files
test2/intern/cycles/kernel/osl/shaders/node_hash.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

128 lines
3.4 KiB
C
Raw Normal View History

/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: Apache-2.0 */
#include "stdcycles.h"
#include "vector2.h"
#include "vector4.h"
#define vector3 point
/* **** Hash a float or vector[234] into a float [0, 1] **** */
float hash_float_to_float(float k)
{
return hashnoise(k);
}
float hash_vector2_to_float(vector2 k)
{
return hashnoise(k.x, k.y);
}
float hash_vector3_to_float(vector3 k)
{
return hashnoise(k);
}
float hash_vector4_to_float(vector4 k)
{
return hashnoise(vector3(k.x, k.y, k.z), k.w);
}
/* **** Hash a vector[234] into a vector[234] [0, 1] **** */
vector2 hash_vector2_to_vector2(vector2 k)
{
return vector2(hash_vector2_to_float(k), hash_vector3_to_float(vector3(k.x, k.y, 1.0)));
}
vector3 hash_vector3_to_vector3(vector3 k)
{
return vector3(hash_vector3_to_float(k),
hash_vector4_to_float(vector4(k[0], k[1], k[2], 1.0)),
hash_vector4_to_float(vector4(k[0], k[1], k[2], 2.0)));
}
vector4 hash_vector4_to_vector4(vector4 k)
{
return vector4(hash_vector4_to_float(k),
hash_vector4_to_float(vector4(k.w, k.x, k.y, k.z)),
hash_vector4_to_float(vector4(k.z, k.w, k.x, k.y)),
hash_vector4_to_float(vector4(k.y, k.z, k.w, k.x)));
}
/* **** Hash a float or a vec[234] into a color [0, 1] **** */
color hash_float_to_color(float k)
{
return color(hash_float_to_float(k),
hash_vector2_to_float(vector2(k, 1.0)),
hash_vector2_to_float(vector2(k, 2.0)));
}
color hash_vector2_to_color(vector2 k)
{
return color(hash_vector2_to_float(k),
hash_vector3_to_float(vector3(k.x, k.y, 1.0)),
hash_vector3_to_float(vector3(k.x, k.y, 2.0)));
}
color hash_vector3_to_color(vector3 k)
{
return color(hash_vector3_to_float(k),
hash_vector4_to_float(vector4(k[0], k[1], k[2], 1.0)),
hash_vector4_to_float(vector4(k[0], k[1], k[2], 2.0)));
}
color hash_vector4_to_color(vector4 k)
{
return color(hash_vector4_to_float(k),
hash_vector4_to_float(vector4(k.z, k.x, k.w, k.y)),
hash_vector4_to_float(vector4(k.w, k.z, k.y, k.x)));
}
Nodes: Implement Gabor noise This patch implements a new Gabor noise node based on [1] but with the improvements from [2] and the phasor formulation from [3]. We compare with the most popular existing implementation, that of OSL, from the user's point of view: - This implementation produces C1 continuous noise as opposed to the non continuous OSL implementation, so it can be used for bump mapping and is generally smother. This is achieved by windowing the Gabor kernel using a Hann window. - The Bandwidth input of OSL was hard-coded to 1 and was replaced with a frequency input, which OSL hard codes to 2, since frequency is more natural to control. This is even more true now that that Gabor kernel is windowed as opposed to truncated, which means increasing the bandwidth will just turn the Gaussian component of the Gabor into a Hann window. While decreasing the bandwidth will eliminate the harmonic from the Gabor kernel, which is the point of Gabor noise. - OSL had three discrete modes of operation for orienting the kernel. Anisotropic, Isotropic, and a hybrid mode. While this implementation provides a continuous Anisotropy parameter which users are already familiar with from the Glossy BSDF node. - This implementation provides not just the Gabor noise value, but also its phase and intensity components. The Gabor noise value is basically sin(phase) * intensity, but the phase is arguably more useful since it does not suffer from the low contrast issues that Gabor suffers from. While the intensity is useful to hide the singularities in the phase. - This implementation converges faster that OSL's relative to the impulse count, so we fix the impulses count to 8 for simplicitly. - This implementation does not implement anisotropic filtering. Future improvements to the node includes implementing surface noise and filtering. As well as extending the spectral control of the noise, either by providing specialized kernels as was done in #110802, or by providing some more procedural control over the frequencies of the Gabor. References: [1]: Lagae, Ares, et al. "Procedural noise using sparse Gabor convolution." ACM Transactions on Graphics (TOG) 28.3 (2009): 1-10. [2]: Tavernier, Vincent, et al. "Making gabor noise fast and normalized." Eurographics 2019-40th Annual Conference of the European Association for Computer Graphics. 2019. [3]: Tricard, Thibault, et al. "Procedural phasor noise." ACM Transactions on Graphics (TOG) 38.4 (2019): 1-13. Pull Request: https://projects.blender.org/blender/blender/pulls/121820
2024-06-19 09:33:32 +02:00
/* **** Hash a float or a vec[234] into a vector3 [0, 1] **** */
vector3 hash_float_to_vector3(float k)
{
return vector3(hash_float_to_float(k),
hash_vector2_to_float(vector2(k, 1.0)),
hash_vector2_to_float(vector2(k, 2.0)));
}
vector3 hash_vector2_to_vector3(vector2 k)
{
return vector3(hash_vector2_to_float(k),
hash_vector3_to_float(vector3(k.x, k.y, 1.0)),
hash_vector3_to_float(vector3(k.x, k.y, 2.0)));
}
vector3 hash_vector4_to_vector3(vector4 k)
{
return vector3(hash_vector4_to_float(k),
hash_vector4_to_float(vector4(k.z, k.x, k.w, k.y)),
hash_vector4_to_float(vector4(k.w, k.z, k.y, k.x)));
}
/* Hashing float or vector[234] into vector2 of components in range [0, 1]. */
vector2 hash_float_to_vector2(float k)
{
return vector2(hash_float_to_float(k), hash_vector2_to_float(vector2(k, 1.0)));
}
vector2 hash_vector3_to_vector2(vector3 k)
{
return vector2(hash_vector3_to_float(vector3(k.x, k.y, k.z)),
hash_vector3_to_float(vector3(k.z, k.x, k.y)));
}
vector2 hash_vector4_to_vector2(vector4 k)
{
return vector2(hash_vector4_to_float(vector4(k.x, k.y, k.z, k.w)),
hash_vector4_to_float(vector4(k.z, k.x, k.w, k.y)));
}
#undef vector3