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

node_image_texture.osl « shaders « osl « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 991fb9d161e3fb6b1bf2f07816d90b4d6bfae29f (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2022 Blender Foundation */

#include "node_color.h"
#include "stdcycles.h"

point texco_remap_square(point co)
{
  return (co - point(0.5, 0.5, 0.5)) * 2.0;
}

point map_to_tube(vector dir)
{
  float u, v;
  v = (dir[2] + 1.0) * 0.5;
  float len = sqrt(dir[0] * dir[0] + dir[1] * dir[1]);
  if (len > 0.0) {
    u = (1.0 - (atan2(dir[0] / len, dir[1] / len) / M_PI)) * 0.5;
  }
  else {
    v = u = 0.0; /* To avoid un-initialized variables. */
  }
  return point(u, v, 0.0);
}

point map_to_sphere(vector dir)
{
  float len = length(dir);
  float v, u;
  if (len > 0.0) {
    if (dir[0] == 0.0 && dir[1] == 0.0) {
      u = 0.0; /* Otherwise domain error. */
    }
    else {
      u = (1.0 - atan2(dir[0], dir[1]) / M_PI) / 2.0;
    }
    v = 1.0 - acos(dir[2] / len) / M_PI;
  }
  else {
    v = u = 0.0; /* To avoid un-initialized variables. */
  }
  return point(u, v, 0.0);
}

color image_texture_lookup(string filename,
                           float u,
                           float v,
                           output float Alpha,
                           int compress_as_srgb,
                           int ignore_alpha,
                           int unassociate_alpha,
                           int is_float,
                           int is_tiled,
                           string interpolation,
                           string extension)
{
  /* Flip the y coordinate, but preserve UDIM tiles. */
  float flip_v;
  if (is_tiled) {
    float v_i = (int)v;
    flip_v = v_i + (1.0 - (v - v_i));
  }
  else {
    flip_v = 1.0 - v;
  }
  color rgb = (color)texture(
      filename, u, flip_v, "wrap", extension, "interp", interpolation, "alpha", Alpha);

  if (ignore_alpha) {
    Alpha = 1.0;
  }
  else if (unassociate_alpha) {
    rgb = color_unpremultiply(rgb, Alpha);

    if (!is_float)
      rgb = min(rgb, 1.0);
  }

  if (compress_as_srgb) {
    rgb = color_srgb_to_scene_linear(rgb);
  }

  return rgb;
}

shader node_image_texture(int use_mapping = 0,
                          matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
                          point Vector = P,
                          string filename = "",
                          string projection = "flat",
                          string interpolation = "smartcubic",
                          string extension = "periodic",
                          float projection_blend = 0.0,
                          int compress_as_srgb = 0,
                          int ignore_alpha = 0,
                          int unassociate_alpha = 0,
                          int is_tiled = 0,
                          int is_float = 1,
                          output color Color = 0.0,
                          output float Alpha = 1.0)
{
  point p = Vector;

  if (use_mapping)
    p = transform(mapping, p);

  if (projection == "flat") {
    Color = image_texture_lookup(filename,
                                 p[0],
                                 p[1],
                                 Alpha,
                                 compress_as_srgb,
                                 ignore_alpha,
                                 unassociate_alpha,
                                 is_float,
                                 is_tiled,
                                 interpolation,
                                 extension);
  }
  else if (projection == "box") {
    /* object space normal */
    vector Nob = transform("world", "object", N);

    /* project from direction vector to barycentric coordinates in triangles */
    Nob = vector(fabs(Nob[0]), fabs(Nob[1]), fabs(Nob[2]));
    Nob /= (Nob[0] + Nob[1] + Nob[2]);

    /* basic idea is to think of this as a triangle, each corner representing
     * one of the 3 faces of the cube. in the corners we have single textures,
     * in between we blend between two textures, and in the middle we a blend
     * between three textures.
     *
     * the `Nxyz` values are the barycentric coordinates in an equilateral
     * triangle, which in case of blending, in the middle has a smaller
     * equilateral triangle where 3 textures blend. this divides things into
     * 7 zones, with an if () test for each zone. */

    vector weight = vector(0.0, 0.0, 0.0);
    float blend = projection_blend;
    float limit = 0.5 * (1.0 + blend);

    /* first test for corners with single texture */
    if (Nob[0] > limit * (Nob[0] + Nob[1]) && Nob[0] > limit * (Nob[0] + Nob[2])) {
      weight[0] = 1.0;
    }
    else if (Nob[1] > limit * (Nob[0] + Nob[1]) && Nob[1] > limit * (Nob[1] + Nob[2])) {
      weight[1] = 1.0;
    }
    else if (Nob[2] > limit * (Nob[0] + Nob[2]) && Nob[2] > limit * (Nob[1] + Nob[2])) {
      weight[2] = 1.0;
    }
    else if (blend > 0.0) {
      /* in case of blending, test for mixes between two textures */
      if (Nob[2] < (1.0 - limit) * (Nob[1] + Nob[0])) {
        weight[0] = Nob[0] / (Nob[0] + Nob[1]);
        weight[0] = clamp((weight[0] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0);
        weight[1] = 1.0 - weight[0];
      }
      else if (Nob[0] < (1.0 - limit) * (Nob[1] + Nob[2])) {
        weight[1] = Nob[1] / (Nob[1] + Nob[2]);
        weight[1] = clamp((weight[1] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0);
        weight[2] = 1.0 - weight[1];
      }
      else if (Nob[1] < (1.0 - limit) * (Nob[0] + Nob[2])) {
        weight[0] = Nob[0] / (Nob[0] + Nob[2]);
        weight[0] = clamp((weight[0] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0);
        weight[2] = 1.0 - weight[0];
      }
      else {
        /* last case, we have a mix between three */
        weight[0] = ((2.0 - limit) * Nob[0] + (limit - 1.0)) / (2.0 * limit - 1.0);
        weight[1] = ((2.0 - limit) * Nob[1] + (limit - 1.0)) / (2.0 * limit - 1.0);
        weight[2] = ((2.0 - limit) * Nob[2] + (limit - 1.0)) / (2.0 * limit - 1.0);
      }
    }
    else {
      /* Desperate mode, no valid choice anyway, fallback to one side.*/
      weight[0] = 1.0;
    }

    Color = color(0.0, 0.0, 0.0);
    Alpha = 0.0;

    float tmp_alpha;

    if (weight[0] > 0.0) {
      Color += weight[0] * image_texture_lookup(filename,
                                                p[1],
                                                p[2],
                                                tmp_alpha,
                                                compress_as_srgb,
                                                ignore_alpha,
                                                unassociate_alpha,
                                                is_float,
                                                0,
                                                interpolation,
                                                extension);
      Alpha += weight[0] * tmp_alpha;
    }
    if (weight[1] > 0.0) {
      Color += weight[1] * image_texture_lookup(filename,
                                                p[0],
                                                p[2],
                                                tmp_alpha,
                                                compress_as_srgb,
                                                ignore_alpha,
                                                unassociate_alpha,
                                                is_float,
                                                0,
                                                interpolation,
                                                extension);
      Alpha += weight[1] * tmp_alpha;
    }
    if (weight[2] > 0.0) {
      Color += weight[2] * image_texture_lookup(filename,
                                                p[1],
                                                p[0],
                                                tmp_alpha,
                                                compress_as_srgb,
                                                ignore_alpha,
                                                unassociate_alpha,
                                                is_float,
                                                0,
                                                interpolation,
                                                extension);
      Alpha += weight[2] * tmp_alpha;
    }
  }
  else if (projection == "sphere") {
    point projected = map_to_sphere(texco_remap_square(p));
    Color = image_texture_lookup(filename,
                                 projected[0],
                                 projected[1],
                                 Alpha,
                                 compress_as_srgb,
                                 ignore_alpha,
                                 unassociate_alpha,
                                 is_float,
                                 0,
                                 interpolation,
                                 extension);
  }
  else if (projection == "tube") {
    point projected = map_to_tube(texco_remap_square(p));
    Color = image_texture_lookup(filename,
                                 projected[0],
                                 projected[1],
                                 Alpha,
                                 compress_as_srgb,
                                 ignore_alpha,
                                 unassociate_alpha,
                                 is_float,
                                 0,
                                 interpolation,
                                 extension);
  }
}