Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_noise_texture.osl « shaders « osl « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f8df07ebb1c38b6ddcb84f40c201fae3686673fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2022 Blender Foundation */

#include "node_noise.h"
#include "stdcycles.h"
#include "vector2.h"
#include "vector4.h"

#define vector3 point

/* The following offset functions generate random offsets to be added to texture
 * coordinates to act as a seed since the noise functions don't have seed values.
 * A seed value is needed for generating distortion textures and color outputs.
 * The offset's components are in the range [100, 200], not too high to cause
 * bad precision and not too small to be noticeable. We use float seed because
 * OSL only support float hashes.
 */

float random_float_offset(float seed)
{
  return 100.0 + noise("hash", seed) * 100.0;
}

vector2 random_vector2_offset(float seed)
{
  return vector2(100.0 + noise("hash", seed, 0.0) * 100.0,
                 100.0 + noise("hash", seed, 1.0) * 100.0);
}

vector3 random_vector3_offset(float seed)
{
  return vector3(100.0 + noise("hash", seed, 0.0) * 100.0,
                 100.0 + noise("hash", seed, 1.0) * 100.0,
                 100.0 + noise("hash", seed, 2.0) * 100.0);
}

vector4 random_vector4_offset(float seed)
{
  return vector4(100.0 + noise("hash", seed, 0.0) * 100.0,
                 100.0 + noise("hash", seed, 1.0) * 100.0,
                 100.0 + noise("hash", seed, 2.0) * 100.0,
                 100.0 + noise("hash", seed, 3.0) * 100.0);
}

float noise_texture(float co, float detail, float roughness, float distortion, output color Color)
{
  float p = co;
  if (distortion != 0.0) {
    p += safe_snoise(p + random_float_offset(0.0)) * distortion;
  }

  float value = fractal_noise(p, detail, roughness);
  Color = color(value,
                fractal_noise(p + random_float_offset(1.0), detail, roughness),
                fractal_noise(p + random_float_offset(2.0), detail, roughness));
  return value;
}

float noise_texture(
    vector2 co, float detail, float roughness, float distortion, output color Color)
{
  vector2 p = co;
  if (distortion != 0.0) {
    p += vector2(safe_snoise(p + random_vector2_offset(0.0)) * distortion,
                 safe_snoise(p + random_vector2_offset(1.0)) * distortion);
  }

  float value = fractal_noise(p, detail, roughness);
  Color = color(value,
                fractal_noise(p + random_vector2_offset(2.0), detail, roughness),
                fractal_noise(p + random_vector2_offset(3.0), detail, roughness));
  return value;
}

float noise_texture(
    vector3 co, float detail, float roughness, float distortion, output color Color)
{
  vector3 p = co;
  if (distortion != 0.0) {
    p += vector3(safe_snoise(p + random_vector3_offset(0.0)) * distortion,
                 safe_snoise(p + random_vector3_offset(1.0)) * distortion,
                 safe_snoise(p + random_vector3_offset(2.0)) * distortion);
  }

  float value = fractal_noise(p, detail, roughness);
  Color = color(value,
                fractal_noise(p + random_vector3_offset(3.0), detail, roughness),
                fractal_noise(p + random_vector3_offset(4.0), detail, roughness));
  return value;
}

float noise_texture(
    vector4 co, float detail, float roughness, float distortion, output color Color)
{
  vector4 p = co;
  if (distortion != 0.0) {
    p += vector4(safe_snoise(p + random_vector4_offset(0.0)) * distortion,
                 safe_snoise(p + random_vector4_offset(1.0)) * distortion,
                 safe_snoise(p + random_vector4_offset(2.0)) * distortion,
                 safe_snoise(p + random_vector4_offset(3.0)) * distortion);
  }

  float value = fractal_noise(p, detail, roughness);
  Color = color(value,
                fractal_noise(p + random_vector4_offset(4.0), detail, roughness),
                fractal_noise(p + random_vector4_offset(5.0), detail, roughness));
  return value;
}

shader node_noise_texture(int use_mapping = 0,
                          matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
                          string dimensions = "3D",
                          vector3 Vector = vector3(0, 0, 0),
                          float W = 0.0,
                          float Scale = 5.0,
                          float Detail = 2.0,
                          float Roughness = 0.5,
                          float Distortion = 0.0,
                          output float Fac = 0.0,
                          output color Color = 0.0)
{
  vector3 p = Vector;
  if (use_mapping)
    p = transform(mapping, p);

  p *= Scale;
  float w = W * Scale;

  if (dimensions == "1D")
    Fac = noise_texture(w, Detail, Roughness, Distortion, Color);
  else if (dimensions == "2D")
    Fac = noise_texture(vector2(p[0], p[1]), Detail, Roughness, Distortion, Color);
  else if (dimensions == "3D")
    Fac = noise_texture(p, Detail, Roughness, Distortion, Color);
  else if (dimensions == "4D")
    Fac = noise_texture(vector4(p[0], p[1], p[2], w), Detail, Roughness, Distortion, Color);
  else
    error("Unknown dimension!");
}