|
| 1 | + |
| 2 | +// |
| 3 | +// This source file is part of appleseed. |
| 4 | +// Visit https://appleseedhq.net/ for additional information and resources. |
| 5 | +// |
| 6 | +// This software is released under the MIT license. |
| 7 | +// |
| 8 | +// Copyright (c) 2019 Luis Barrancos, The appleseedhq Organization |
| 9 | +// |
| 10 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | +// of this software and associated documentation files (the "Software"), to deal |
| 12 | +// in the Software without restriction, including without limitation the rights |
| 13 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | +// copies of the Software, and to permit persons to whom the Software is |
| 15 | +// furnished to do so, subject to the following conditions: |
| 16 | +// |
| 17 | +// The above copyright notice and this permission notice shall be included in |
| 18 | +// all copies or substantial portions of the Software. |
| 19 | +// |
| 20 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | +// THE SOFTWARE. |
| 27 | +// |
| 28 | + |
| 29 | +#include "appleseed/math/as_math_helpers.h" |
| 30 | + |
| 31 | +shader as_blend_normal |
| 32 | +[[ |
| 33 | + string help = "Tangent space normal blend utility node.", |
| 34 | + string icon = "asBlendNormal.png", |
| 35 | + string URL = "https://appleseed.readthedocs.io/projects/appleseed-maya/en/latest/shaders/utilities/as_blend_normal.html", |
| 36 | + |
| 37 | + string as_node_name = "asBlendNormal", |
| 38 | + string as_category = "utility", |
| 39 | + |
| 40 | + string as_max_class_id = "1585865021 797245718", |
| 41 | + string as_max_plugin_type = "texture", |
| 42 | + |
| 43 | + int as_maya_type_id = 0x00127a09, |
| 44 | + string as_maya_classification = "drawdb/shader:rendernode/appleseed/utility" |
| 45 | +]] |
| 46 | +( |
| 47 | + normal in_base_normal = normal(0) |
| 48 | + [[ |
| 49 | + string as_maya_attribute_name = "baseNormal", |
| 50 | + string as_maya_attribute_short_name = "bnr", |
| 51 | + string label = "Base Normal", |
| 52 | + string page = "Base Normal", |
| 53 | + string help = "The base tangent space normal map.", |
| 54 | + int divider = 1 |
| 55 | + ]], |
| 56 | + string in_base_normal_mode = "Unsigned" |
| 57 | + [[ |
| 58 | + string as_maya_attribute_name = "baseNormalMode", |
| 59 | + string as_maya_attribute_short_name = "brm", |
| 60 | + string widget = "popup", |
| 61 | + string options = "Unsigned|Signed", |
| 62 | + string label = "Normal Map Mode", |
| 63 | + string page = "Base Normal", |
| 64 | + int as_maya_attribute_connectable = 0, |
| 65 | + int as_maya_attribute_keyable = 0, |
| 66 | + int as_maya_attribute_hidden = 1, |
| 67 | + int as_blender_input_socket = 0, |
| 68 | + int gafferNoduleLayoutVisible = 0, |
| 69 | + int divider = 1 |
| 70 | + ]], |
| 71 | + int in_base_normal_flip_r = 0 |
| 72 | + [[ |
| 73 | + string as_maya_attribute_name = "baseNormalFlipR", |
| 74 | + string as_maya_attribute_short_name = "bfr", |
| 75 | + string widget = "checkBox", |
| 76 | + string label = "Flip R", |
| 77 | + string page = "Base Normal", |
| 78 | + int as_maya_attribute_connectable = 0, |
| 79 | + int as_maya_attribute_keyable = 0, |
| 80 | + int as_maya_attribute_hidden = 1, |
| 81 | + int as_blender_input_socket = 0, |
| 82 | + int gafferNoduleLayoutVisible = 0 |
| 83 | + ]], |
| 84 | + int in_base_normal_flip_g = 0 |
| 85 | + [[ |
| 86 | + string as_maya_attribute_name = "baseNormalFlipG", |
| 87 | + string as_maya_attribute_short_name = "bfg", |
| 88 | + string widget = "checkBox", |
| 89 | + string label = "Flip G", |
| 90 | + string page = "Base Normal", |
| 91 | + int as_maya_attribute_connectable = 0, |
| 92 | + int as_maya_attribute_keyable = 0, |
| 93 | + int as_maya_attribute_hidden = 1, |
| 94 | + int as_blender_input_socket = 0, |
| 95 | + int gafferNoduleLayoutVisible = 0 |
| 96 | + ]], |
| 97 | + int in_base_normal_swap_rg = 0 |
| 98 | + [[ |
| 99 | + string as_maya_attribute_name = "baseNormalSwapRG", |
| 100 | + string as_maya_attribute_short_name = "bsw", |
| 101 | + string widget = "checkBox", |
| 102 | + string label = "Swap RG", |
| 103 | + string page = "Base Normal", |
| 104 | + int as_maya_attribute_connectable = 0, |
| 105 | + int as_maya_attribute_keyable = 0, |
| 106 | + int as_maya_attribute_hidden = 1, |
| 107 | + int as_blender_input_socket = 0, |
| 108 | + int gafferNoduleLayoutVisible = 0, |
| 109 | + int divider = 1 |
| 110 | + ]], |
| 111 | + |
| 112 | + normal in_detail_normal = normal(0) |
| 113 | + [[ |
| 114 | + string as_maya_attribute_name = "detailNormal", |
| 115 | + string as_maya_attribute_short_name = "dno", |
| 116 | + string label = "Detail Normal", |
| 117 | + string page = "Detail Normal", |
| 118 | + string help = "The detail tangent space normal to blend over the base.", |
| 119 | + int divider = 1 |
| 120 | + ]], |
| 121 | + string in_detail_normal_mode = "Unsigned" |
| 122 | + [[ |
| 123 | + string as_maya_attribute_name = "detailNormalMode", |
| 124 | + string as_maya_attribute_short_name = "dnm", |
| 125 | + string widget = "popup", |
| 126 | + string options = "Unsigned|Signed", |
| 127 | + string label = "Normal Map Mode", |
| 128 | + string page = "Detail Normal", |
| 129 | + int as_maya_attribute_connectable = 0, |
| 130 | + int as_maya_attribute_keyable = 0, |
| 131 | + int as_maya_attribute_hidden = 1, |
| 132 | + int as_blender_input_socket = 0, |
| 133 | + int gafferNoduleLayoutVisible = 0, |
| 134 | + int divider = 1 |
| 135 | + ]], |
| 136 | + int in_detail_normal_flip_r = 0 |
| 137 | + [[ |
| 138 | + string as_maya_attribute_name = "detailNormalFlipR", |
| 139 | + string as_maya_attribute_short_name = "dfr", |
| 140 | + string widget = "checkBox", |
| 141 | + string label = "Flip R", |
| 142 | + string page = "Detail Normal", |
| 143 | + int as_maya_attribute_connectable = 0, |
| 144 | + int as_maya_attribute_keyable = 0, |
| 145 | + int as_maya_attribute_hidden = 1, |
| 146 | + int as_blender_input_socket = 0, |
| 147 | + int gafferNoduleLayoutVisible = 0 |
| 148 | + ]], |
| 149 | + int in_detail_normal_flip_g = 0 |
| 150 | + [[ |
| 151 | + string as_maya_attribute_name = "detailNormalFlipG", |
| 152 | + string as_maya_attribute_short_name = "dfg", |
| 153 | + string widget = "checkBox", |
| 154 | + string label = "Flip G", |
| 155 | + string page = "Detail Normal", |
| 156 | + int as_maya_attribute_connectable = 0, |
| 157 | + int as_maya_attribute_keyable = 0, |
| 158 | + int as_maya_attribute_hidden = 1, |
| 159 | + int as_blender_input_socket = 0, |
| 160 | + int gafferNoduleLayoutVisible = 0 |
| 161 | + ]], |
| 162 | + int in_detail_normal_swap_rg = 0 |
| 163 | + [[ |
| 164 | + string as_maya_attribute_name = "detailNormalSwapRG", |
| 165 | + string as_maya_attribute_short_name = "dsw", |
| 166 | + string widget = "checkBox", |
| 167 | + string label = "Swap RG", |
| 168 | + string page = "Detail Normal", |
| 169 | + int as_maya_attribute_connectable = 0, |
| 170 | + int as_maya_attribute_keyable = 0, |
| 171 | + int as_maya_attribute_hidden = 1, |
| 172 | + int as_blender_input_socket = 0, |
| 173 | + int gafferNoduleLayoutVisible = 0, |
| 174 | + int divider = 1 |
| 175 | + ]], |
| 176 | + float in_detail_normal_weight = 1.0 |
| 177 | + [[ |
| 178 | + string as_maya_attribute_name = "detailNormalWeight", |
| 179 | + string as_maya_attribute_short_name = "dwe", |
| 180 | + float min = 0.0, |
| 181 | + float max = 1.0, |
| 182 | + string label = "Blending Weight", |
| 183 | + string page = "Blending", |
| 184 | + string help = "Detail normal blending weight.", |
| 185 | + int divider = 1 |
| 186 | + ]], |
| 187 | + vector Tn = vector(0) |
| 188 | + [[ |
| 189 | + int lockgeom = 0, |
| 190 | + string widget = "null", |
| 191 | + int as_maya_attribute_hidden = 1, |
| 192 | + int as_blender_input_socket = 0, |
| 193 | + int gafferNoduleLayoutVisible = 0 |
| 194 | + ]], |
| 195 | + vector Bn = vector(0) |
| 196 | + [[ |
| 197 | + int lockgeom = 0, |
| 198 | + string widget = "null", |
| 199 | + int as_maya_attribute_hidden = 1, |
| 200 | + int as_blender_input_socket = 0, |
| 201 | + int gafferNoduleLayoutVisible = 0 |
| 202 | + ]], |
| 203 | + |
| 204 | + output normal out_normal = color(0) |
| 205 | + [[ |
| 206 | + string as_maya_attribute_name = "outNormal", |
| 207 | + string as_maya_attribute_short_name = "on", |
| 208 | + string widget = "null", |
| 209 | + string label = "Output Normal", |
| 210 | + string help = "Output unsigned tangent space normal map" |
| 211 | + ]] |
| 212 | +) |
| 213 | +{ |
| 214 | + normal baseN = (in_base_normal_mode == "Unsigned") |
| 215 | + ? in_base_normal * normal(2) - normal(1) |
| 216 | + : in_base_normal; |
| 217 | + |
| 218 | + if (in_base_normal_flip_r) baseN[0] *= -1; |
| 219 | + if (in_base_normal_flip_g) baseN[1] *= -1; |
| 220 | + |
| 221 | + if (in_base_normal_swap_rg) |
| 222 | + { |
| 223 | + float tmp = baseN[0]; |
| 224 | + baseN[0] = baseN[1]; |
| 225 | + baseN[1] = tmp; |
| 226 | + } |
| 227 | + |
| 228 | + normal detailN = (in_detail_normal_mode == "Unsigned") |
| 229 | + ? in_detail_normal * normal(2) - normal(1) |
| 230 | + : in_detail_normal; |
| 231 | + |
| 232 | + if (in_detail_normal_flip_r) detailN[0] *= -1; |
| 233 | + if (in_detail_normal_flip_g) detailN[1] *= -1; |
| 234 | + |
| 235 | + if (in_detail_normal_swap_rg) |
| 236 | + { |
| 237 | + float tmp = detailN[0]; |
| 238 | + detailN[0] = detailN[1]; |
| 239 | + detailN[1] = tmp; |
| 240 | + } |
| 241 | + |
| 242 | + baseN = normalize(baseN); |
| 243 | + detailN = normalize(detailN); |
| 244 | + |
| 245 | + baseN[2] += 1; |
| 246 | + detailN[0] *= -in_detail_normal_weight; |
| 247 | + detailN[1] *= -in_detail_normal_weight; |
| 248 | + |
| 249 | + normal O = baseN * dot(baseN, detailN) / baseN[2] - detailN; |
| 250 | + |
| 251 | + out_normal = normalize(O); |
| 252 | + out_normal = out_normal * normal(0.5) + normal(0.5); |
| 253 | +} |
0 commit comments