1/* Beta-0.4.7: MOSAIC surface shader for Blender to RenderMan shader integration */ 2 3#define SURFACE_UTILITIES 1 4#include "MOSAICfunctions.h" 5 6//////// Begin surface shader 7surface 8MOSAICsurface( 9 uniform float LayerBlend = 2; 10 uniform float LayerFactor = 1; 11 uniform float DiffPtcSSS = 1; 12 uniform float DiffPtcRadScale = 0.01; 13 uniform float ColPtcRadScale = 0.01; 14 uniform float PtcMaxSolidAngle = 0.05; 15 uniform float SSSOccGiBlur = 0; 16 uniform float EnvReflMaxDist = 0; 17 uniform float EnvRefrMaxDist = 0; 18 uniform float EnvFadeMode = 1; 19 uniform float EnvDispFac = 1; 20 uniform float EnvSelfOcc = 0; 21 uniform color OccColor = 1; 22 uniform float OccBendNormal[3] = {0,0,0}; 23 uniform float OccEnergy = 1; 24 uniform float OccBlend = 0; 25 uniform float OccMaxDist = 10; 26 uniform float OccSamples = 256; 27 uniform float OccFalloff = 0; 28 uniform float OccMapBlur = 0.02; 29 uniform float OccMapBias = 0; 30 uniform string OccHdrMap = ""; 31 uniform float OccHdrBlur = 0; 32 uniform float OccHdrSamples = 2; 33 uniform float OccHdrFactor = 1; 34 uniform color GIColor = 1; 35 uniform float GIEnergy = 1; 36 uniform float GIBlend = 0; 37 uniform float GIMaxDist = 30; 38 uniform float GISamples = 128; 39 uniform float GIDispFac = 1; 40 uniform float GIMapBlur = 0.4; 41 uniform float GISelfOcc = 0; 42 uniform string GIHdrMap = ""; 43 uniform float GIHdrBlur = 0; 44 uniform float GIHdrSamples = 2; 45 uniform float GIHdrFactor = 1; 46 uniform string OccMap = ""; 47 uniform string EnvMap = ""; 48 uniform string IndMap = ""; 49 uniform string OccPoint = ""; 50 uniform string IndPoint = ""; 51 uniform string DifBrick = ""; 52 uniform string ColBrick = ""; 53 uniform float GlobUF_X0[2] = {0, 0}; 54 uniform float MatUF_X0[61] = {0.5, 0.8, 0.5, 12.5, 1, 1, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 1.3, 0.0, 1.0, 0.0, 1.25, 8, 0.0, 1.0, 0.0, 1.25, 8, 0.0, 0.0, 0.0, 0.0, 1.3, 0.05, 0.1, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 4.0, 0.5, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0}; 55 uniform color MatUC_X0[7] = {color(0.8, 0.8, 0.8), color(1.0, 1.0, 1.0), color(1.0, 1.0, 1.0), color(1.0, 1.0, 1.0), color(0.0, 0.0, 0.0), color(0.0, 0.0, 0.0), color(1.0, 1.0, 1.0)}; 56 uniform string TexUS_X0[10] = {"", "", "", "", "", "", "", "", "", ""}; 57 varying float TexVF_X0[41] = {16.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0}; 58 varying float TexVF_X1[41] = {16.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0}; 59 varying float TexVF_X2[41] = {16.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0}; 60 varying float TexVF_X3[41] = {16.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0}; 61 varying float TexVF_X4[41] = {16.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0}; 62 varying float TexVF_X5[41] = {16.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0}; 63 varying float TexVF_X6[41] = {16.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0}; 64 varying float TexVF_X7[41] = {16.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0}; 65 varying float TexVF_X8[41] = {16.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0}; 66 varying float TexVF_X9[41] = {16.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0}; 67 varying float s1 = 0, t1 = 0, s2 = 0, t2 = 0, s3 = 0, t3 = 0, s4 = 0, t4 = 0, s5 = 0, t5 = 0, s6 = 0, t6 = 0, s7 = 0, t7 = 0, s8 = 0, t8 = 0; // Texture uv layers 68 varying float v0 = 1, v1 = 1, v2 = 1, v3 = 1, v4 = 1, v5 = 1, v6 = 1, v7 = 1, v8 = 1, v9 = 1; // Vertex groups 69 varying float Ms = -1; // Material index number from mesh (for multipass and layered shading) 70 uniform string Mx = ""; // TexFace image names from mesh (if enabled in exporter) 71 output varying float __nonfog = 0; 72 output varying float __shadowalpha = 1; 73 output varying float __useshadows = 1; 74 output varying color __surfacein = 0; // Input parameter for layered shading 75 output varying color __surfaceout = 0; // Output parameter for layered shading 76 output varying color _color = 0; // Base color channel for AOV 77 output varying color _diffuse = 0; // Diffuse color channel for AOV 78 output varying color _specular = 0; // Specular color channel for AOV 79 output varying color _ambient = 0; // Ambient color channel for AOV 80 output varying color _radiosity = 0; // Radiosity color channel for AOV 81 output varying color _reflect = 0; // Reflection color channel for AOV 82 output varying color _refract = 0; // Refraction color channel for AOV 83 output varying color _shadow = 0; // Shadow color channel for AOV 84 output varying color _caustic = 0; // Caustic color channel for AOV 85 output varying color _diffshad = 0; // Shadow*Diffuse color channel for AOV 86 output varying normal _uv = 0; // UV->st as normal channel for AOV 87 output varying float _alpha = 0; // Alpha float channel for AOV 88 output varying float _z = 0; // Z depth float channel for AOV 89 output varying float _index = 0;) // Pass index float channel for AOV 90{ 91 // Only shade a surface element belonging to the current material index unless disabled 92 if (GlobUF_X0[MAT_UF_SETMODE] == 0 || Ms == -1 || GlobUF_X0[MAT_UF_SETINDEX] == Ms) 93 { 94 //// Lets setup general purpose variables... 95 uniform string Name = ""; 96 uniform float a = 0, i = 0, channels = 3; 97 varying float sAdj, tAdj, p1, p2, p3, p4, p5, p6, p7, sssTex = 0, result = 0, mask = 1, warps = 0, warpt = 0; 98 varying color Cp1, Cp2, Cp3, Cp4, Cp5, Cp6, Cp7; 99 float stLayers[16] = {s, t, s1, t1, s2, t2, s3, t3, s4, t4, s5, t5, s6, t6, s7, t7}; 100 float vGroups[10] = {v0, v1, v2, v3, v4, v5, v6, v7, v8, v9}; 101 normal Nn = normalize(N); 102 normal Nf = faceforward(Nn, I); 103 vector In = normalize(I); 104 vector Eye = -In; 105 float Li = length(I); 106 107 // These are the material channels defined in Blender's material system per texture for blending 108 color Alpha = 1, Ref = 1, Spec = 0, Csp = 0, Cmir = 0, Amb = 0, Hard = 0, RayMir = 0, Emit = 0, Translu = 0; 109 110 //// Lets setup local render layer variables... 111 varying color layer_color = 1; 112 varying color layer_diffuse = 0; 113 varying color layer_specular = 0; 114 varying color layer_ambient = 0; 115 varying color layer_radiosity = 0; 116 varying color layer_reflect = 0; 117 varying color layer_refract = 0; 118 varying color layer_shadow = 0; 119 varying color layer_caustic = 0; 120 121 //// Lets grab any user attributes from RIB 122 string rendertype = "beauty"; 123 float renderpass = 1; 124 float rendermulti = 1; 125 float renderset = 1; 126 string objectname = ""; 127 string objecttype = ""; 128 float objectid = 0; 129 float objectscale = 1; 130 float objectpass = 0; 131 string geometrytype = ""; 132 string materialname = ""; 133 float materialid = 0; 134 float surfacelayer = 0; 135 attribute("user:render_type", rendertype); 136 attribute("user:render_pass", renderpass); 137 attribute("user:render_multi", rendermulti); 138 attribute("user:render_set", renderset); 139 attribute("user:object_name", objectname); 140 attribute("user:object_type", objecttype); 141 attribute("user:object_id", objectid); 142 attribute("user:object_scale", objectscale); 143 attribute("user:object_pass", objectpass); 144 attribute("user:material_name", materialname); 145 attribute("user:material_id", materialid); 146 attribute("user:surface_layer", surfacelayer); 147 148 // These are output variables to be used by other shaders 149 __nonfog = MatUF_X0[MAT_UF_ISNOMIST]; 150 __shadowalpha = MatUF_X0[MAT_UF_SHADALPHA]; 151 __useshadows = MatUF_X0[MAT_UF_ISSHADOW]; 152 153 // Special setup for 3d baking passes... 154 if (rendertype == "occlusion_point" || rendertype == "indirect_point" || rendertype == "diffuse_brick" || rendertype == "color_brick") 155 { 156 // Fake out shaders that normally use faceforward to see fully around object for baking 157 Nf = Nn; 158 } 159 160 //// Lets do initial setup for material channels... 161 if (rendertype != "occlusion_point" && (ColBrick == "" || rendertype == "color_brick")) 162 { 163 // Lets setup the initial color channel... 164 if (MatUF_X0[MAT_UF_ISVCOLLIGHT] != 0) 165 layer_ambient = Cs; 166 if (MatUF_X0[MAT_UF_SSSUSE] != 0) 167 sssTex = MatUF_X0[MAT_UF_SSSTEX]; 168 if (MatUF_X0[MAT_UF_TEXFACE] != 0 && Mx != "") 169 { 170 Name = Mx; 171 layer_color = texture(Name, s, t, "blur", sssTex); 172 } 173 else if (MatUF_X0[MAT_UF_ISVCOLPAINT] == 0) 174 layer_color = MatUC_X0[MAT_UC_DIFCOL]; 175 if (MatUF_X0[MAT_UF_ISVCOLPAINT] != 0) 176 layer_color *= Cs; 177 if (MatUF_X0[MAT_UF_ISOBJCOL] != 0) 178 layer_color *= MatUC_X0[MAT_UC_OBJCOL]; 179 180 // Lets setup the initial alpha channel... 181 if (MatUF_X0[MAT_UF_ISTEXFACEALPHA] != 0 && Name != "") 182 Alpha = color(float texture(Name[3], s, t, "blur", sssTex, "fill", 1)); 183 else if (MatUF_X0[MAT_UF_ISTEXFACEALPHA] == 0 || MatUF_X0[MAT_UF_ISVCOLPAINT] == 0) 184 Alpha = color(MatUF_X0[MAT_UF_ALPHA]); 185 if (MatUF_X0[MAT_UF_ISWIRE] != 0 && (mod(s*40, 1) > 0.1 && mod(t*40, 1) > 0.1)) 186 Alpha *= color(0); 187 if (MatUF_X0[MAT_UF_ISOBJCOL] != 0) 188 Alpha *= color(MatUF_X0[MAT_UF_OBJALPHA]); 189 190 // Initial setup if using shaders... 191 if (MatUF_X0[MAT_UF_ISSHADLESS] == 0) 192 { 193 Ref = color(MatUF_X0[MAT_UF_REF]); 194 Spec = color(MatUF_X0[MAT_UF_SPEC]); 195 Hard = color(MatUF_X0[MAT_UF_HARD]); 196 Translu = color(MatUF_X0[MAT_UF_TRALU]); 197 Amb = color(MatUF_X0[MAT_UF_AMB]); 198 Emit = MatUF_X0[MAT_UF_EMIT]; 199 Csp = MatUC_X0[MAT_UC_SPECCOL]; 200 RayMir = color(MatUF_X0[MAT_UF_RAYMIR]); 201 Cmir = MatUC_X0[MAT_UC_MIRCOL]; 202 } 203 // Otherwise setup for shadeless... 204 else 205 { 206 layer_diffuse = color(1); 207 layer_shadow = color(1); 208 } 209 210 //// Cycle through all texture layers and blend to each material channels 211 for (i = 0; i < GLOB_TEX_CHANNELS; i += 1) 212 { 213 Name = TexUS_X0[i]; 214 215 // Make sure theres a texture before we process it! 216 if (Name != "") 217 { 218 // Lets initialize shared variables... 219 float tex_paras[TEX_VF_PARALEN]; 220 uniform float tex_channels = 0; 221 textureinfo(Name, "channels", tex_channels); 222 223 // Lets load up a temporary array of the current textures parameters 224 for (a = 0; a < TEX_VF_PARALEN; a += 1) 225 { 226 if (i == 0) tex_paras[a] = TexVF_X0[a]; 227 else if (i == 1) tex_paras[a] = TexVF_X1[a]; 228 else if (i == 2) tex_paras[a] = TexVF_X2[a]; 229 else if (i == 3) tex_paras[a] = TexVF_X3[a]; 230 else if (i == 4) tex_paras[a] = TexVF_X4[a]; 231 else if (i == 5) tex_paras[a] = TexVF_X5[a]; 232 else if (i == 6) tex_paras[a] = TexVF_X6[a]; 233 else if (i == 7) tex_paras[a] = TexVF_X7[a]; 234 else if (i == 8) tex_paras[a] = TexVF_X8[a]; 235 else if (i == 9) tex_paras[a] = TexVF_X9[a]; 236 } 237 238 float stLayer = tex_paras[TEX_VF_STLAYER]; 239 float stCoor = tex_paras[TEX_VF_STCOOR]; 240 241 // Adjust offset and size of st coordinates... 242 p1 = tex_paras[TEX_VF_OFSX]; 243 p2 = tex_paras[TEX_VF_OFSY]; 244 p3 = tex_paras[TEX_VF_SIZEX]; 245 p4 = tex_paras[TEX_VF_SIZEY]; 246 // Is this texture using "sticky" coordinates? 247 if (stCoor == 256) 248 { 249 sAdj = s8; 250 tAdj = t8; 251 } 252 // Is this texture using a UV layer? 253 else if (stCoor == 16) 254 { 255 sAdj = stLayers[stLayer*2]; 256 tAdj = stLayers[stLayer*2+1]; 257 } 258 // If using a coordinate mode not supported just use standard st 259 else 260 { 261 sAdj = s; 262 tAdj = t; 263 } 264 // Rotate and mirror texture? 265 if (tex_paras[TEX_VF_ROT90] != 0) 266 { 267 uniform float rot = radians(-90); 268 sAdj -= 0.5; 269 tAdj -= 0.5; 270 p1 = (cos(rot)*sAdj-sin(rot)*tAdj)+0.5; 271 p2 = (sin(rot)*sAdj+cos(rot)*tAdj)+0.5; 272 sAdj = 1-p1; 273 tAdj = p2; 274 } 275 stAdjust(sAdj, tAdj, p1, p2, p3, p4); 276 sAdj += warps; 277 tAdj += warpt; 278 279 // Get and adjust color value from texture... 280 uniform float alpha_channel = tex_channels-1; 281 if (sssTex > 0) p1 = sssTex; 282 else p1 = tex_paras[TEX_VF_FILTER]; 283 if (tex_channels < 3) 284 Cp1 = color(float texture(Name[0], sAdj, tAdj, "blur", p1)); 285 else 286 Cp1 = color texture(Name, sAdj, tAdj, "blur", p1); 287 p6 = comp(ctransform("hsv", Cp1), 2); 288 289 // Get and adjust alpha value from texture... 290 if (tex_paras[TEX_VF_USEALPHA] != 0 && (tex_channels == 2 || tex_channels == 4)) 291 p1 = float texture(Name[alpha_channel], sAdj, tAdj, "blur", p1, "fill", 1); 292 else 293 p1 = 1; 294 if (tex_paras[TEX_VF_NEGALPHA] != 0) 295 p1 = 1-p1; 296 if (tex_paras[TEX_VF_CALCALPHA] != 0 || (tex_paras[TEX_VF_USEALPHA] == 0 && tex_paras[TEX_VF_ISALPHA] != 0)) 297 { 298 p1 *= p6; 299 if (tex_paras[TEX_VF_ISALPHA] == 0) 300 p3 *= p1; 301 } 302 if (tex_paras[TEX_VF_STENCIL] != 0) 303 mask *= p1; 304 p1 *= mask; 305 306 // Alter texture colors before blending... 307 Cp2 = color(tex_paras[TEX_VF_TEXR], tex_paras[TEX_VF_TEXG], tex_paras[TEX_VF_TEXB]); 308 Cp1 *= Cp2; 309 if (tex_paras[TEX_VF_CONTR] != 1) 310 Cp1 = (Cp1-0.5*Cp2)*tex_paras[TEX_VF_CONTR]+0.5*Cp2; 311 if (tex_paras[TEX_VF_BRIGHT] != 1) 312 Cp1 = clamp(Cp1+Cp2*(tex_paras[TEX_VF_BRIGHT]-1), color(0), color(2)); 313 if (tex_paras[TEX_VF_NEG] != 0) 314 Cp1 = color(1)-Cp1; 315 if (tex_paras[TEX_VF_NORGB] != 0) 316 Cp1 = colorblend(color(p6), color(tex_paras[TEX_VF_R], tex_paras[TEX_VF_G], tex_paras[TEX_VF_B]), 7, 1, 1); 317 318 // Lets start blending texture according to assigned channel 319 p2 = tex_paras[TEX_VF_BLENDMODE]; 320 p3 = tex_paras[TEX_VF_BLENDCOL]*p1; 321 p4 = tex_paras[TEX_VF_BLENDVAR]*p1; 322 p5 = tex_paras[TEX_VF_BLENDDVAR]*p1; 323 if (tex_paras[TEX_VF_ISWARP] != 0) 324 { 325 warps = vector(1, 0, 0).(vector(Cp1)*2-vector(1))*tex_paras[TEX_VF_WARPFAC]; 326 warpt = vector(0, 1, 0).(vector(Cp1)*2-vector(1))*tex_paras[TEX_VF_WARPFAC]; 327 } 328 if (tex_paras[TEX_VF_ISCOL] != 0) 329 { 330 layer_color = colorblend(layer_color, Cp1, p2, p3, 1); 331 } 332 if (tex_paras[TEX_VF_ISCSP] != 0) 333 { 334 Csp = colorblend(Csp, Cp1, p2, p3, 1); 335 } 336 if (tex_paras[TEX_VF_ISCMIR] != 0) 337 { 338 Cmir = colorblend(Cmir, Cp1, p2, p3, 1); 339 } 340 if (tex_paras[TEX_VF_ISREF] != 0) 341 { 342 Cp1 = mix(Ref, color(p5), p6); 343 p7 = tex_paras[TEX_VF_ISREF]; 344 Ref = colorblend(Ref, Cp1, p2, p4, p7); 345 } 346 if (tex_paras[TEX_VF_ISSPEC] != 0) 347 { 348 Cp1 = mix(Spec, color(p5), p6); 349 p7 = tex_paras[TEX_VF_ISSPEC]; 350 Spec = colorblend(Spec, Cp1, p2, p4, p7); 351 } 352 if (tex_paras[TEX_VF_ISAMB] != 0) 353 { 354 Cp1 = mix(Amb, color(p5), p6); 355 p7 = tex_paras[TEX_VF_ISAMB]; 356 Amb = colorblend(Amb, Cp1, p2, p4, p7); 357 } 358 if (tex_paras[TEX_VF_ISHARD] != 0) 359 { 360 Cp1 = mix(Hard, color(p5), p6); 361 p7 = tex_paras[TEX_VF_ISHARD]; 362 Hard = colorblend(Hard, Cp1, p2, p4, p7); 363 } 364 if (tex_paras[TEX_VF_ISRAYMIR] != 0) 365 { 366 Cp1 = mix(RayMir, color(p5), p6); 367 p7 = tex_paras[TEX_VF_ISRAYMIR]; 368 RayMir = colorblend(RayMir, Cp1, p2, p4, p7); 369 } 370 if (tex_paras[TEX_VF_ISALPHA] != 0) 371 { 372 Cp1 = mix(Alpha, color(p5), p1); 373 p7 = tex_paras[TEX_VF_ISALPHA]; 374 Alpha = colorblend(Alpha, Cp1, p2, p4, p7); 375 } 376 if (tex_paras[TEX_VF_ISEMIT] != 0) 377 { 378 Cp1 = mix(Emit, color(p5), p6); 379 p7 = tex_paras[TEX_VF_ISEMIT]; 380 Emit = colorblend(Emit, Cp1, p2, p4, p7); 381 } 382 if (tex_paras[TEX_VF_ISTRANSLU] != 0) 383 { 384 Cp1 = mix(Translu, color(p5), p6); 385 p7 = tex_paras[TEX_VF_ISTRANSLU]; 386 Translu = colorblend(Translu, Cp1, p2, p4, p7); 387 } 388 } 389 } 390 } 391 392 393 //// Lets run shaders for channels... 394 395 #ifndef AQSIS 396 // If we have diffusion brickmap load it into diffuse channel... 397 if(DifBrick != "" && rendertype != "diffuse_brick" && (ColBrick == "" || rendertype == "color_brick")) 398 { 399 point pP = P; 400 normal pNf = Nf; 401 // If not global brick map then transform to object space 402 if (match("GLOBAL", DifBrick) != 1.0) 403 { 404 pP = transform("object", pP); 405 pNf = transform("object", pNf); 406 } 407 p1 = DiffPtcRadScale; 408 layer_color = color(1); 409 layer_shadow = color(1); 410 layer_caustic = color(0); 411 layer_ambient = color(0); 412 #ifdef AIR 413 texture3d(DifBrick, pP, pNf, "blur", p1, "_diffusion", layer_diffuse); 414 #endif 415 #ifdef PIXIE 416 texture3d(DifBrick, pP, pNf, "radius", p1, "_diffusion", layer_diffuse); 417 #endif 418 #ifndef AIR 419 #ifndef PIXIE 420 texture3d(DifBrick, pP, pNf, "filterradius", p1, "_diffusion", layer_diffuse); 421 #endif 422 #endif 423 // If using point sub surface scattering... 424 if (MatUF_X0[MAT_UF_SSSUSE] != 0 && DiffPtcSSS > 0) 425 { 426 Cp1 = MatUC_X0[MAT_UC_SSSCOL]; 427 Cp2 = color(MatUF_X0[MAT_UF_SSSRADIUSR], MatUF_X0[MAT_UF_SSSRADIUSG], MatUF_X0[MAT_UF_SSSRADIUSB]); 428 p1 = MatUF_X0[MAT_UF_SSSFRONT]; 429 p2 = MatUF_X0[MAT_UF_SSSBACK]; 430 p3 = MatUF_X0[MAT_UF_SSSIOR]*0.5; 431 p4 = (11-MatUF_X0[MAT_UF_SSSERROR])*10; 432 p5 = MatUF_X0[MAT_UF_SSSSCALE]; 433 result = sssdiff(DifBrick, 0, p1, p2, p3, p4, p5, Cp1, Cp2, layer_diffuse, layer_shadow, layer_caustic); 434 if (result > 0) 435 layer_diffuse *= mix(color(1), MatUC_X0[MAT_UC_SSSCOL], MatUF_X0[MAT_UF_SSSCOLBLEND]); 436 } 437 } 438 #endif 439 440 // If using only shadows... 441 if (MatUF_X0[MAT_UF_ISONLYSHADOW] != 0 && rendertype != "occlusion_point" && (ColBrick == "" || rendertype == "color_brick")) 442 { 443 if (DifBrick == "" || rendertype == "diffuse_brick") 444 { 445 layer_shadow = onlyshadow(Nf); 446 // If ENV is enabled then use white diffuse and adjust output alpha by shadow channel 447 if (MatUF_X0[MAT_UF_ISENV] != 0) 448 { 449 layer_diffuse = color(0); 450 layer_color = color(1); 451 Alpha = 1-layer_shadow; 452 } 453 // Else use solid horizon diffuse color to act as color key 454 else 455 { 456 layer_diffuse = color(1); 457 layer_color = MatUC_X0[MAT_UC_HORCOL]; 458 } 459 } 460 } 461 // If not shadeless then do diffuse and specular 462 else if (MatUF_X0[MAT_UF_ISSHADLESS] == 0 && rendertype != "occlusion_point" && (ColBrick == "" || rendertype == "color_brick")) 463 { 464 if (rendertype != "irradiance_cache") 465 { 466 // Do diffuse shading if specified 467 if (layer_color != color(0)) 468 { 469 if ((Emit != color(0) || Ref != color(0)) && (DifBrick == "" || rendertype == "diffuse_brick")) 470 { 471 p1 = MatUF_X0[MAT_UF_ISCUBIC]; 472 p2 = comp(ctransform("hsv", Translu), 2); 473 474 // If using anisotropic diffusion... 475 if (MatUF_X0[MAT_UF_ISTANGENT] != 0 || (MatUF_X0[MAT_UF_ISSTRANDTAN] != 0 && objecttype == "particles")) 476 { 477 layer_diffuse = anisodiff(Nf, p1, p2, layer_shadow, layer_caustic); 478 } 479 // If using standard lambert diffusion... 480 else if (MatUF_X0[MAT_UF_DIFFUSETYPE] == 0) 481 { 482 layer_diffuse = lambertdiff(Nf, p1, p2, layer_shadow, layer_caustic); 483 } 484 // If using Oren-Nayar diffusion... 485 else if (MatUF_X0[MAT_UF_DIFFUSETYPE] == 1) 486 { 487 p3 = MatUF_X0[MAT_UF_ROUGHNESS]; 488 layer_diffuse = orennayardiff(Nf, In, Eye, p3, p1, p2, layer_shadow, layer_caustic); 489 } 490 // If using Toon diffusion... 491 else if (MatUF_X0[MAT_UF_DIFFUSETYPE] == 2) 492 { 493 p3 = MatUF_X0[MAT_UF_DIFFSIZE]; 494 p4 = MatUF_X0[MAT_UF_DIFFSMOOTH]; 495 layer_diffuse = toondiff(Nf, p3, p4, p1, p2, layer_shadow, layer_caustic); 496 } 497 // If using Minnaert diffusion... 498 else if (MatUF_X0[MAT_UF_DIFFUSETYPE] == 3) 499 { 500 p3 = MatUF_X0[MAT_UF_DIFFDARK]-1; 501 layer_diffuse = minnaertdiff(Nf, In, p3, p1, p2, layer_shadow, layer_caustic); 502 } 503 // If using Fresnel diffusion... 504 else if (MatUF_X0[MAT_UF_DIFFUSETYPE] == 4) 505 { 506 p3 = MatUF_X0[MAT_UF_DIFFSMOOTH]; 507 p4 = MatUF_X0[MAT_UF_DIFFSIZE]; 508 layer_diffuse = fresneldiff(Nf, p3, p4, p1, p2, layer_shadow, layer_caustic); 509 } 510 511 // If using sub surface scattering... 512 if (MatUF_X0[MAT_UF_SSSUSE] != 0) 513 { 514 Cp1 = MatUC_X0[MAT_UC_SSSCOL]; 515 Cp2 = color(MatUF_X0[MAT_UF_SSSRADIUSR], MatUF_X0[MAT_UF_SSSRADIUSG], MatUF_X0[MAT_UF_SSSRADIUSB]); 516 Cp3 = mix(color(1), MatUC_X0[MAT_UC_SSSCOL], MatUF_X0[MAT_UF_SSSCOLBLEND]); 517 p1 = comp(ctransform("hsv", layer_diffuse), 2); 518 p2 = MatUF_X0[MAT_UF_SSSFRONT]; 519 p3 = MatUF_X0[MAT_UF_SSSBACK]; 520 p4 = MatUF_X0[MAT_UF_SSSIOR]-1.0; 521 p5 = (11-MatUF_X0[MAT_UF_SSSERROR])*10; 522 p6 = MatUF_X0[MAT_UF_SSSSCALE]; 523 result = sssdiff("", p1, p2, p3, p4, p5, p6, Cp1, Cp2, layer_diffuse, layer_shadow, layer_caustic); 524 if (result > 0) 525 { 526 layer_diffuse *= Cp3; 527 layer_shadow *= Cp3; 528 } 529 } 530 531 // Figure shadow and diffuse layers then adjust for emit channel... 532 if (Ref != color(0)) 533 { 534 float Dr = comp(layer_diffuse, 0); 535 float Dg = comp(layer_diffuse, 1); 536 float Db = comp(layer_diffuse, 2); 537 float Sr = (Dr != 0 ? comp(layer_shadow, 0)/Dr : 0); 538 float Sg = (Dg != 0 ? comp(layer_shadow, 1)/Dg : 0); 539 float Sb = (Db != 0 ? comp(layer_shadow, 2)/Db : 0); 540 layer_shadow = color(Sr, Sg, Sb); 541 layer_diffuse *= Ref; 542 } 543 else 544 { 545 layer_shadow = color(1); 546 layer_diffuse = color(0); 547 } 548 if (Emit != color(0)) 549 { 550 float Cr = comp(layer_color, 0); 551 float Cg = comp(layer_color, 1); 552 float Cb = comp(layer_color, 2); 553 float Ir = (Cr != 0 ? 1/Cr : 0); 554 float Ig = (Cg != 0 ? 1/Cg : 0); 555 float Ib = (Cb != 0 ? 1/Cb : 0); 556 color InvC = color(Ir, Ig, Ib); 557 layer_shadow = mix(layer_shadow, InvC, Emit); 558 layer_diffuse += layer_color*Emit; 559 } 560 } 561 } 562 563 // Do specular shading if specified 564 if (Spec != color(0) && Csp != color(0) && rendertype != "indirect_map" && rendertype != "indirect_point" && rendertype != "diffuse_brick") 565 { 566 // If using anisotropic specularity... 567 if (MatUF_X0[MAT_UF_ISTANGENT] != 0 || (MatUF_X0[MAT_UF_ISSTRANDTAN] != 0 && objecttype == "particles")) 568 { 569 p1 = PI/comp(Hard, 0); 570 layer_specular = Csp*anisospec(Nf, -In, p1); 571 } 572 // If using Cooktorr specularity... 573 else if (MatUF_X0[MAT_UF_SPECULARTYPE] == 0) 574 { 575 p1 = pow(PI, 2)/comp(Hard, 0); 576 layer_specular = Csp*cooktorrspec(Nf, -In, p1); 577 } 578 // If using Phong specularity... 579 else if (MatUF_X0[MAT_UF_SPECULARTYPE] == 1) 580 { 581 p1 = comp(Hard, 0)/PI; 582 p2 = pow(PI, 2)/comp(Hard, 0); 583 layer_specular = Csp*phongspec(Nf, -In, p1, p2); 584 } 585 // If using Blinn specularity... 586 else if (MatUF_X0[MAT_UF_SPECULARTYPE] == 2) 587 { 588 p1 = pow(PI, 2)/comp(Hard, 0); 589 p2 = MatUF_X0[MAT_UF_SPECREFRACT]; 590 layer_specular = Csp*blinnspec(Nf, -In, p1, p2); 591 } 592 // If using Toon specularity... 593 else if (MatUF_X0[MAT_UF_SPECULARTYPE] == 3) 594 { 595 p1 = MatUF_X0[MAT_UF_SPECSIZE]; 596 p2 = MatUF_X0[MAT_UF_SPECSMOOTH]; 597 p3 = pow(PI, 2)/comp(Hard, 0); 598 layer_specular = Csp*toonspec(Nf, -In, p1, p2, p3); 599 } 600 // If using Wardlso specularity... 601 else if (MatUF_X0[MAT_UF_SPECULARTYPE] == 4) 602 { 603 p1 = MatUF_X0[MAT_UF_SPECRMS]; 604 p2 = pow(PI, 2)/comp(Hard, 0); 605 layer_specular = Csp*wardspec(Nf, -In, p1, p2); 606 } 607 608 layer_specular *= Spec; 609 } 610 } 611 612 // Do any ambient shading if specified 613 if (Amb != color(0) && rendertype != "indirect_point" && (DifBrick == "" || rendertype == "diffuse_brick")) 614 { 615 layer_ambient += MatUC_X0[MAT_UC_AMBCOL]; 616 617 // If usng either occlusion map or raytracing... 618 if (OccPoint != "" || OccMap != "") 619 { 620 vector bend = vector "world" (OccBendNormal[0], OccBendNormal[1], OccBendNormal[2]); 621 p1 = OccMapBlur; 622 // If using SSS then apply percent of SSS Scale to occlusion blur... 623 if (MatUF_X0[MAT_UF_SSSUSE] != 0) 624 p1 += MatUF_X0[MAT_UF_SSSSCALE]*SSSOccGiBlur; 625 p2 = OccMapBias; 626 p3 = OccFalloff; 627 p4 = OccHdrBlur; 628 p5 = OccHdrSamples; 629 p6 = OccHdrFactor; 630 Cp1 = OccColor; 631 Cp2 = hdrocclusion(OccMap, OccPoint, OccHdrMap, Nf, bend, OccSamples, OccMaxDist, PtcMaxSolidAngle, p1, p2, p3, p4, p5, p6, Cp1); 632 633 if (OccBlend == 1) 634 Cp2 = -(1-Cp2); 635 if (OccBlend == 2) 636 Cp2 += -(1-Cp2); 637 if (OccBlend == 3) 638 layer_ambient *= Cp2*OccEnergy; 639 else 640 layer_ambient += Cp2*OccEnergy; 641 } 642 643 layer_ambient *= Amb; 644 645 // If usng either indirect diffusion map or raytracing... 646 if (IndPoint != "" || (IndMap != "" && (IndMap == "raytrace" || rendertype != "indirect_map" || rendermulti > 1))) 647 { 648 p1 = GIMapBlur; 649 // If using SSS then apply percent of SSS Scale to occlusion blur... 650 if (MatUF_X0[MAT_UF_SSSUSE] != 0) 651 p1 += MatUF_X0[MAT_UF_SSSSCALE]*SSSOccGiBlur; 652 p2 = GIDispFac; 653 p3 = objectscale; 654 p4 = GIHdrBlur; 655 p5 = GIHdrSamples; 656 p6 = GIHdrFactor; 657 Cp1 = GIColor; 658 Cp2 = hdrindirectdiff(IndMap, IndPoint, GIHdrMap, Nf, GISamples, GIMaxDist, PtcMaxSolidAngle, p1, p2, p3, p4, p5, p6, Cp1); 659 660 // If self occlusion is enabled then modulate occlusion data onto diffusion map 661 if (GISelfOcc > 0) 662 { 663 Cp2 *= pow(comp(ctransform("hsv", layer_ambient), 2), GISelfOcc); 664 } 665 if (GIBlend == 1) 666 Cp2 = -(1-Cp2); 667 if (GIBlend == 2) 668 Cp2 += -(1-Cp2); 669 if (GIBlend == 3) 670 layer_radiosity *= Cp2*GIEnergy; 671 else 672 layer_radiosity += Cp2*GIEnergy; 673 } 674 675 layer_radiosity *= Amb; 676 } 677 678 //// Do reflection/refraction if specified 679 if (rendertype != "irradiance_cache" && rendertype != "indirect_map" && rendertype != "indirect_point" && rendertype != "diffuse_brick" && 680 (EnvMap != "" || MatUF_X0[MAT_UF_ISTRANSPSHADOW] != 0)) 681 { 682 float F = 1-Nf.Eye; 683 float Ft = 1-pow(pow(F, MatUF_X0[MAT_UF_TRANSPFRESNEL]), MatUF_X0[MAT_UF_TRANSPFRESFAC]-1); 684 color fulldiffuse = layer_color*layer_diffuse*layer_shadow+layer_caustic; 685 RayMir *= pow(pow(F, MatUF_X0[MAT_UF_MIRFRESNEL]), MatUF_X0[MAT_UF_MIRFRESFAC]-1); 686 if (EnvFadeMode == 0) Cp2 = color(0); 687 else if (EnvFadeMode == 1) Cp2 = MatUC_X0[MAT_UC_HORCOL]; 688 else Cp2 = fulldiffuse; 689 690 // If TranspShadow is enabled add fresnel effect to output alpha 691 if (MatUF_X0[MAT_UF_ISTRANSPSHADOW] != 0) 692 { 693 // If using deep shadow maps... 694 if (rendertype == "shadow_map") 695 Alpha *= 1-(Ft*(1-RayMir)); 696 // If using deep caustic maps... 697 else if (rendertype == "caustic_map") 698 Alpha *= Ft*(1-RayMir); 699 // If already have alpha force edges opaque... 700 else if (Alpha != color(1)) 701 Alpha += 1-(Ft*(1-RayMir)); 702 } 703 704 // Only process reflection/refraction if an actual map is specified... 705 if (EnvMap != "" && (EnvMap == "raytrace" || rendertype != "environment_map" || rendermulti > 1)) 706 { 707 // Lets setup for refractions... 708 if ((EnvMap != "raytrace" || MatUF_X0[MAT_UF_ISRAYTRANSP] == 1) && Ft > 0 && (1-RayMir) != color(0)) 709 { 710 uniform float Tp1 = MatUF_X0[MAT_UF_TRANSPSAMPLES]; 711 uniform float Tp2 = (EnvRefrMaxDist == 0 ? 1000.0 : EnvRefrMaxDist); 712 uniform float Tp3 = MatUF_X0[MAT_UF_TRANSPGLOSS]; 713 p1 = MatUF_X0[MAT_UF_IOR]; 714 p2 = MatUF_X0[MAT_UF_TRANSPRAYDEPTH]; 715 p3 = EnvDispFac; 716 p4 = EnvFadeMode; 717 Cp1 = mix(color(1), MatUC_X0[MAT_UC_DIFCOL], MatUF_X0[MAT_UF_TRANSPFILTER]); 718 layer_refract = mirrortransp(EnvMap, In, Nf, 1, Tp1, Tp2, Tp3, p1, p2, p3, p4, Cp1, Cp2); 719 layer_refract = ((layer_refract-fulldiffuse)*Ft+layer_specular*MatUF_X0[MAT_UF_TRANSPSPECTRA])*(1-RayMir); 720 } 721 722 // Lets setup for reflections... 723 if ((EnvMap != "raytrace" || MatUF_X0[MAT_UF_ISRAYMIRROR] == 1) && RayMir != color(0)) 724 { 725 uniform float Rp1 = MatUF_X0[MAT_UF_MIRSAMPLES]; 726 uniform float Rp2 = (EnvReflMaxDist == 0 ? 1000.0 : EnvReflMaxDist); 727 uniform float Rp3 = MatUF_X0[MAT_UF_MIRGLOSS]; 728 p1 = MatUF_X0[MAT_UF_MIRRAYDEPTH]; 729 p2 = EnvDispFac; 730 p3 = EnvFadeMode; 731 Cp1 = Cmir; 732 layer_reflect = mirrortransp(EnvMap, In, Nf, 0, Rp1, Rp2, Rp3, 1, p1, p2, p3, Cp1, Cp2); 733 734 // If self occlusion is enabled then modulate occlusion data onto env map 735 if (EnvSelfOcc > 0 && EnvMap != "raytrace") 736 { 737 layer_reflect *= pow(comp(ctransform("hsv", layer_ambient), 2), EnvSelfOcc); 738 } 739 740 layer_reflect = (layer_reflect-fulldiffuse)*RayMir; 741 } 742 } 743 } 744 } 745 746 //// If this is a standard or point baking pass then do standard channel layers 747 if (rendertype != "occlusion_point" && rendertype != "indirect_point") 748 { 749 //// Lets blend Ci from previous shader layer into this layers ambient channel... 750 if (surfacelayer > 0) 751 layer_ambient = colorblend(layer_ambient, __surfacein, LayerBlend, LayerFactor, 1); 752 753 #ifndef AQSIS 754 // If we have color brickmap load it in Ci... 755 if(ColBrick != "" && rendertype != "color_brick") 756 { 757 point pP = P; 758 normal pNf = Nf; 759 // If not global brick map then transform to object space 760 if (match("GLOBAL", ColBrick) != 1.0) 761 { 762 pP = transform("object", pP); 763 pNf = ntransform("object", pNf); 764 } 765 p1 = ColPtcRadScale; 766 #ifdef AIR 767 texture3d(ColBrick, pP, pNf, "blur", p1, "_color", layer_color); 768 #endif 769 #ifdef PIXIE 770 texture3d(ColBrick, pP, pNf, "radius", p1, "_color", layer_color); 771 #endif 772 #ifndef AIR 773 #ifndef PIXIE 774 texture3d(ColBrick, pP, pNf, "filterradius", p1, "_color", layer_color); 775 #endif 776 #endif 777 Ci = layer_color; 778 } 779 // Otherwise lets combine channels together... 780 else 781 #endif 782 Ci = layer_color*(layer_ambient+layer_radiosity+(layer_diffuse*layer_shadow)+layer_caustic)+layer_refract+layer_reflect+layer_specular; 783 784 // Lets apply output opacity and alpha modulation... 785 if (Alpha != color(1)) 786 Oi = mix((color(1)-layer_color)*Alpha, color(1), Alpha); 787 else 788 Oi = Os; 789 Ci *= Oi; 790 791 #ifndef AQSIS 792 // Are we baking diffusion data to point cloud? 793 if (DifBrick != "" && rendertype == "diffuse_brick") 794 { 795 point pP = P; 796 normal pNn = Nn; 797 // If not global brick map then transform to object space 798 if (match("GLOBAL", DifBrick) != 1.0) 799 { 800 pP = transform("object", pP); 801 pNn = ntransform("object", pNn); 802 } 803 Cp1 = layer_color*(layer_ambient+layer_radiosity+(layer_diffuse*layer_shadow)+layer_caustic); 804 #ifdef AIR 805 bake3d(DifBrick, "_diffusion", pP, pNn, "_diffusion", Cp1); 806 #endif 807 #ifndef AIR 808 bake3d(DifBrick, "_diffusion", pP, pNn, "interpolate", 1, "_diffusion", Cp1); 809 #endif 810 } 811 // Are we baking all color layers to point cloud? 812 if (ColBrick != "" && rendertype == "color_brick") 813 { 814 point pP = P; 815 normal pNn = Nn; 816 // If not global brick map then transform to object space 817 if (match("GLOBAL", ColBrick) != 1.0) 818 { 819 pP = transform("object", pP); 820 pNn = ntransform("object", pNn); 821 } 822 #ifdef AIR 823 bake3d(ColBrick, "_color", pP, pNn, "_color", Ci); 824 #endif 825 #ifndef AIR 826 bake3d(ColBrick, "_color", pP, pNn, "interpolate", 1, "_color", Ci); 827 #endif 828 } 829 #endif 830 } 831 #ifndef AIR 832 #ifndef AQSIS 833 //// If this is a occlusion or indirect diffusion point cloud pass then bake out data 834 else 835 { 836 float a = area(P, "dicing"); 837 if (rendertype == "occlusion_point") 838 { 839 Ci = Cs; 840 Oi = color(1); 841 bake3d(OccPoint, "_area", P, Nn, "coordsystem", "world", "interpolate", 1, "_area", a); 842 } 843 else 844 { 845 Ci = layer_color*layer_diffuse*layer_shadow+layer_caustic; 846 Oi = color(1); 847 bake3d(IndPoint, "_area,_radiosity", P, Nn, "coordsystem", "world", "interpolate", 1, "_area", a, "_radiosity", Ci); 848 } 849 } 850 #endif 851 #endif 852 853 // Lets apply local render layers to output parameters... 854 _alpha = comp(ctransform("hsv", Alpha), 2); 855 _color = layer_color; 856 _diffuse = layer_diffuse; 857 _specular = layer_specular; 858 _ambient = layer_ambient; 859 _radiosity = layer_radiosity; 860 _reflect = layer_reflect; 861 _refract = layer_refract; 862 _shadow = layer_shadow; 863 _caustic = layer_caustic; 864 _diffshad = layer_diffuse*layer_shadow; 865 _uv = normal(s, t, 0); 866 _z = Li; 867 _index = objectpass; 868 __surfaceout = Ci; 869 } 870 //// If multipass material mode clear surface elements not in this material set 871 else if (GlobUF_X0[MAT_UF_SETMODE] == 1) 872 { 873 Ci = 0; 874 Oi = 0; 875 } 876} 877