Blender V5.3
gpu_shader_material_random_value.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9[[node]]
10void random_value_vector(float3 min_value, float3 max_value, int id, int seed, float3 &result)
11{
12 const float x = hash_uint3_to_float(uint(seed), uint(id), 0u);
13 const float y = hash_uint3_to_float(uint(seed), uint(id), 1u);
14 const float z = hash_uint3_to_float(uint(seed), uint(id), 2u);
15 result = float3(x, y, z) * (max_value - min_value) + min_value;
16}
17
18[[node]]
19void random_value_float(float min_value, float max_value, int id, int seed, float &result)
20{
21 const float value = hash_uint2_to_float(uint(seed), uint(id));
22 result = value * (max_value - min_value) + min_value;
23}
24
25[[node]]
26void random_value_int(int min_value, int max_value, int id, int seed, int &result)
27{
28 if (min_value > max_value) {
29 const int tmp = min_value;
30 min_value = max_value;
31 max_value = tmp;
32 }
33
34 const uint hash = hash_uint2(uint(id), uint(seed));
35
36 /* Calculate range using unsigned types to fit the entire 32-bit space. */
37 const uint range = uint(max_value) - uint(min_value) + 1u;
38
39 /* Range wraps around to 0 when min_value is INT_MIN and max_value is INT_MAX.
40 * so the modulo is unnecessary and would cause a division by zero. */
41 const uint modulo_result = (range == 0u) ? hash : (hash % range);
42
43 result = int(uint(min_value) + modulo_result);
44}
45
46[[node]]
47void random_value_bool(float probability, int id, int seed, bool &result)
48{
49 result = hash_uint2_to_float(uint(id), uint(seed)) <= probability;
50}
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
uint hash_uint2(uint kx, uint ky)
float hash_uint3_to_float(uint kx, uint ky, uint kz)
float hash_uint2_to_float(uint kx, uint ky)
void random_value_bool(float probability, int id, int seed, bool &result)
void random_value_vector(float3 min_value, float3 max_value, int id, int seed, float3 &result)
void random_value_int(int min_value, int max_value, int id, int seed, int &result)
void random_value_float(float min_value, float max_value, int id, int seed, float &result)
#define hash
Definition noise_c.cc:156