2011-04-27 11:58:34 +00:00
/*
2013-08-18 14:16:15 +00:00
* Copyright 2011-2013 Blender Foundation
2011-04-27 11:58:34 +00:00
*
2013-08-18 14:16:15 +00:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2011-04-27 11:58:34 +00:00
*
2013-08-18 14:16:15 +00:00
* http://www.apache.org/licenses/LICENSE-2.0
2011-04-27 11:58:34 +00:00
*
2013-08-18 14:16:15 +00:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
2011-04-27 11:58:34 +00:00
*/
#include "stdosl.h"
2011-05-14 09:42:02 +00:00
#include "node_color.h"
2011-04-27 11:58:34 +00:00
2014-03-07 22:16:09 +00:00
color image_texture_lookup(string filename, string color_space, float u, float v, output float Alpha, int use_alpha, int is_float, string interpolation)
2012-10-20 15:09:36 +00:00
{
2014-03-07 22:16:09 +00:00
color rgb = (color)texture(filename, u, 1.0 - v, "wrap", "periodic", "interp", interpolation, "alpha", Alpha);
2012-10-20 15:09:36 +00:00
2013-04-17 14:47:58 +00:00
if (use_alpha) {
2013-04-05 16:43:53 +00:00
rgb = color_unpremultiply(rgb, Alpha);
2013-04-17 14:47:58 +00:00
if (!is_float)
rgb = min(rgb, 1.0);
}
2013-04-05 16:43:53 +00:00
2013-06-21 13:05:10 +00:00
if (color_space == "sRGB") {
2012-10-20 15:09:36 +00:00
rgb = color_srgb_to_scene_linear(rgb);
2013-06-21 13:05:10 +00:00
}
2012-10-20 15:09:36 +00:00
return rgb;
}
2011-04-27 11:58:34 +00:00
shader node_image_texture(
2012-11-20 17:40:10 +00:00
int use_mapping = 0,
matrix mapping = matrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
2011-04-27 11:58:34 +00:00
point Vector = P,
string filename = "",
2011-05-13 14:32:08 +00:00
string color_space = "sRGB",
2012-10-20 15:09:36 +00:00
string projection = "Flat",
2014-03-07 22:16:09 +00:00
string interpolation = "smartcubic",
2012-10-20 15:09:36 +00:00
float projection_blend = 0.0,
2013-04-17 14:47:58 +00:00
int is_float = 1,
2013-06-21 13:05:10 +00:00
int use_alpha = 1,
2012-12-11 16:06:03 +00:00
output color Color = 0.0,
2011-09-16 13:14:02 +00:00
output float Alpha = 1.0)
2011-04-27 11:58:34 +00:00
{
2012-11-20 17:40:10 +00:00
point p = Vector;
if (use_mapping)
p = transform(mapping, p);
2013-04-05 16:43:53 +00:00
2012-10-23 11:47:23 +00:00
if (projection == "Flat") {
2014-03-07 22:16:09 +00:00
Color = image_texture_lookup(filename, color_space, p[0], p[1], Alpha, use_alpha, is_float, interpolation);
2012-10-20 15:09:36 +00:00
}
2012-10-23 11:47:23 +00:00
else if (projection == "Box") {
2012-10-20 15:09:36 +00:00
/* 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
2012-10-23 11:47:23 +00:00
* 7 zones, with an if () test for each zone */
2012-10-20 15:09:36 +00:00
vector weight = vector(0.0, 0.0, 0.0);
float blend = projection_blend;
2012-12-04 03:18:08 +00:00
float limit = 0.5 * (1.0 + blend);
2012-10-20 15:09:36 +00:00
/* first test for corners with single texture */
2012-12-04 03:18:08 +00:00
if (Nob[0] > limit * (Nob[0] + Nob[1]) && Nob[0] > limit * (Nob[0] + Nob[2])) {
2012-10-20 15:09:36 +00:00
weight[0] = 1.0;
}
2012-12-04 03:18:08 +00:00
else if (Nob[1] > limit * (Nob[0] + Nob[1]) && Nob[1] > limit * (Nob[1] + Nob[2])) {
2012-10-20 15:09:36 +00:00
weight[1] = 1.0;
}
2012-12-04 03:18:08 +00:00
else if (Nob[2] > limit * (Nob[0] + Nob[2]) && Nob[2] > limit * (Nob[1] + Nob[2])) {
2012-10-20 15:09:36 +00:00
weight[2] = 1.0;
}
2012-10-23 11:47:23 +00:00
else if (blend > 0.0) {
2012-10-20 15:09:36 +00:00
/* in case of blending, test for mixes between two textures */
2012-12-04 03:18:08 +00:00
if (Nob[2] < (1.0 - limit) * (Nob[1] + Nob[0])) {
2012-10-23 11:47:23 +00:00
weight[0] = Nob[0] / (Nob[0] + Nob[1]);
weight[0] = clamp((weight[0] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0);
2012-10-20 15:09:36 +00:00
weight[1] = 1.0 - weight[0];
}
2012-12-04 03:18:08 +00:00
else if (Nob[0] < (1.0 - limit) * (Nob[1] + Nob[2])) {
2012-10-23 11:47:23 +00:00
weight[1] = Nob[1] / (Nob[1] + Nob[2]);
weight[1] = clamp((weight[1] - 0.5 * (1.0 - blend)) / blend, 0.0, 1.0);
2012-10-20 15:09:36 +00:00
weight[2] = 1.0 - weight[1];
}
2012-10-23 11:47:23 +00:00
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);
2012-10-20 15:09:36 +00:00
weight[2] = 1.0 - weight[0];
}
else {
/* last case, we have a mix between three */
2012-10-23 11:47:23 +00:00
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);
2012-10-20 15:09:36 +00:00
}
}
2014-10-07 13:48:39 +00:00
else {
/* Desperate mode, no valid choice anyway, fallback to one side.*/
weight[0] = 1.0;
}
2012-10-20 15:09:36 +00:00
Color = color(0.0, 0.0, 0.0);
Alpha = 0.0;
float tmp_alpha;
2011-05-13 14:32:08 +00:00
2012-10-23 11:47:23 +00:00
if (weight[0] > 0.0) {
2014-03-07 22:16:09 +00:00
Color += weight[0] * image_texture_lookup(filename, color_space, p[1], p[2], tmp_alpha, use_alpha, is_float, interpolation);
2012-12-04 03:18:08 +00:00
Alpha += weight[0] * tmp_alpha;
2012-10-20 15:09:36 +00:00
}
2012-10-23 11:47:23 +00:00
if (weight[1] > 0.0) {
2014-03-07 22:16:09 +00:00
Color += weight[1] * image_texture_lookup(filename, color_space, p[0], p[2], tmp_alpha, use_alpha, is_float, interpolation);
2012-12-04 03:18:08 +00:00
Alpha += weight[1] * tmp_alpha;
2012-10-20 15:09:36 +00:00
}
2012-10-23 11:47:23 +00:00
if (weight[2] > 0.0) {
2014-03-07 22:16:09 +00:00
Color += weight[2] * image_texture_lookup(filename, color_space, p[1], p[0], tmp_alpha, use_alpha, is_float, interpolation);
2012-12-04 03:18:08 +00:00
Alpha += weight[2] * tmp_alpha;
2012-10-20 15:09:36 +00:00
}
}
2011-04-27 11:58:34 +00:00
}