//**************************************************************************/ // Copyright 2016 Autodesk, Inc. // All rights reserved. // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. //**************************************************************************/ // // Maya Blinn (cgfx) // // Vertex input structure struct vertexInS { float3 Pm : POSITION; float3 Nm : NORMAL; }; // Vertex output structure struct vertOutS { float3 Nw : TEXCOORD0; float3 Pw : TEXCOORD1; float3 Vw : TEXCOORD2; float4 Pc : POSITION; }; // Globals uniform float4x4 WorldIT : worldinversetranspose; uniform float4x4 World : world; uniform float3 cameraPosition : worldcameraposition; uniform float DepthPriority : depthpriority; uniform float4x4 WorldViewProj : worldviewprojection; // Vertex shader fragments // float3 iNw( float3 nm, float4x4 worldITC ) { return mul( worldITC, float4(nm,0) ).xyz; } float3 iPw( float3 pm, float4x4 world ) { return mul( world, float4(pm, 1.0f) ).xyz; } float3 iVw( float3 Pm, float3 Ew, float4x4 world ) { float4 Pw = mul( world, float4(Pm,1) ); return Ew - Pw.xyz; } float4 iPcPriority( float3 pm, float depthPriority, float4x4 worldViewProjectionC ) { float4 P = mul( worldViewProjectionC, float4(pm,1) ); P.z -= P.w * 2.0f * depthPriority; return P; } // Vertex Shader vertOutS MainVertexShader( vertexInS inputs ) { vertOutS vertexOutput; vertexOutput.Nw = iNw( inputs.Nm, WorldIT ); vertexOutput.Pw = iPw( inputs.Pm, World ); vertexOutput.Vw = iVw( inputs.Pm, cameraPosition, World ); vertexOutput.Pc = iPcPriority ( inputs.Pm, DepthPriority, WorldViewProj ); return vertexOutput; } ////////////////////// // Pixel shader // struct fragInS { float3 Nw : TEXCOORD0; float3 Pw : TEXCOORD1; float3 Vw : TEXCOORD2; float facingFactor : FACE; }; // Declarations struct irradiance { float3 diffuseI; float3 specularI; float3 Ld; float3 Ls; }; struct mayaSurfaceShaderOutput { float3 outColor; float3 outTransparency; float3 outGlowColor; float3 outMatteOpacity; float outAlphaPassThrough; }; // Globals uniform float mayaNormalMultiplier = 1.000000; uniform float mayaPointLight_lightOn = 1.000000; uniform float mayaPointLight_intensity = 1.000000; uniform float3 mayaPointLight_color = { 1.000000, 1.000000, 1.000000 }; uniform float mayaPointLight_emitsDiffuse = 1.000000; uniform float mayaPointLight_emitsSpecular = 1.000000; // Light position semantic:lightposition0, lightposition1, ..., lightposition15 // uniform float3 mayaPointLight_position : lightposition0; uniform textureCUBE mayaShadowPointLight_shadowMap; uniform samplerCUBE mayaShadowPointLight_shadowSamp = sampler_state { Texture = ; }; uniform float mayaShadowPointLight_shadowBias = 0.002200; uniform float mayaShadowPointLight_filterRadius = 1.000000; uniform int mayaShadowPointLight_shadowMapSize = 512; uniform float3 mayaShadowPointLight_shadowColor = { 0.000000, 0.000000, 0.000000 }; uniform bool mayaShadowPointLight_mayaGlobalShadowOn = 0; uniform bool mayaShadowPointLight_mayaShadowOn = 0; uniform float2 mayaShadowPointLight_depthRange = { 0.000000, 0.000000 }; uniform float blinn1eccentricity = 0.300000; uniform float blinn1specularRollOff = 0.700000; uniform float blinn1translucence = 0.000000; uniform float blinn1translucenceDepth = 0.500000; uniform float blinn1translucenceFocus = 0.500000; uniform float3 blinn1color = { 0.200000, 0.600000, 1.000000 }; uniform float3 blinn1transparency = { 0.000000, 0.000000, 0.000000 }; uniform float3 blinn1ambientColor = { 0.000000, 0.000000, 0.000000 }; uniform float3 blinn1incandescence = { 0.000000, 0.000000, 0.000000 }; uniform float blinn1diffuse = 0.800000; uniform float3 blinn1specularColor = { 1.000000, 1.000000, 1.000000 }; uniform float blinn1reflectivity = 0.500000; uniform float3 blinn1reflectedColor = { 0.000000, 0.000000, 0.000000 }; uniform float blinn1glowIntensity = 0.000000; uniform bool blinn1hideSource = 0; uniform float blinn1matteOpacity = 1.000000; uniform int blinn1matteOpacityMode = 2; uniform float blinn1outAlphaPassThrough = 1.000000; uniform float3 ambientShader_ambientIllum = { 0.000000, 0.000000, 0.000000 }; uniform float specularRollOff = 0.700000; uniform float extraOpacity = 1.000000; // Pixel Shader Fragments float3 Nw( float3 iNw ) { return normalize( iNw ); } float FacingFactor( float facing ) { return facing; } bool mayaIsBackFacing(float facingFactor) { return (facingFactor <= 0.0f); } float3 mayaNormalFlip(float3 Nw, bool backFacing, float normalMultiplier) { return normalMultiplier * (backFacing ? -Nw : Nw); } float3 mayaNwPassThrough(float3 Nw) { return Nw; } irradiance mayaPointLight( float lightOn, float intensity, float3 color, float emitDiff, float emitSpec, float3 lightPosition, float3 Pw) { const float epsilon = 0.00001f; float3 L = lightPosition - Pw; float d = length( L ) + epsilon; float atten = 1.0f / pow( d + 1.0f, 0.0 + epsilon); float3 c = lightOn * atten * intensity * color; irradiance irrad; irrad.diffuseI = emitDiff * c; irrad.specularI = emitSpec * c; irrad.Ld = irrad.Ls = L / d; return irrad; } float3 Pw( float3 ipw ) { return ipw; } irradiance mayaShadowPointLight( irradiance irradIn, float3 lightPosition, float3 Pw, textureCUBE map, samplerCUBE samp, float bias, float2 range, float filterRadius, float3 shadowColor, bool globalShadowOn, bool shadowOn) { if ( !( globalShadowOn && shadowOn ) ) return irradIn; const float3 avgXYZ = float3(0.3333,0.3334,0.3333); // dot with this for average float lightGain = 0.0f; float3 lookup = Pw - lightPosition; float Distance = length(lookup); float depthRange = range.y-range.x; float3 normalizeLookup = normalize(lookup); float SampledDistance = (texCUBE(samp, lookup).r)*depthRange + range.x; if (Distance > depthRange) { lightGain = 1.0f; } else { if (Distance <= SampledDistance + bias*Distance) lightGain = 1.0; else lightGain = 0.0; } lightGain = lightGain; float dIntens = saturate(dot(irradIn.diffuseI, avgXYZ)); irradIn.diffuseI = lerp( dIntens * shadowColor, irradIn.diffuseI, lightGain ); irradIn.specularI = irradIn.specularI * lightGain; return irradIn; } float3 Vw( float3 iVw ) { return normalize( iVw ); } float3 mayaHVector(float3 L, float3 V) { return normalize(L + V); } float4 mayaShaderGeom(float3 N, float3 L, float3 V, float3 H) { float NL = saturate(dot(N, L)); float NV = saturate(dot(N, V)); float NH = saturate(dot(N, H)); float VH = saturate(dot(V, H)); return float4(NL, NV, NH, VH); } float3 mayaLambertDiffuse(float4 NL_NV_NH_VH, float3 diffuseI) { return NL_NV_NH_VH.x * diffuseI; } float3 mayaBlinnSpecular( float3 specularI, float4 NL_NV_NH_VH, float ecc, float rolloff) { float ecc2 = ecc * ecc - 1.0f; float NH = NL_NV_NH_VH.z; float d = (ecc2 + 1.0f) / (1.0f + ecc2 * NH * NH); d *= d; float NL = NL_NV_NH_VH.x; float NV = NL_NV_NH_VH.y; float VH = NL_NV_NH_VH.w; NH *= 2.0f; float g = 1.0f; if (NV < NL) g = (NV * NH < VH) ? NH / VH : 1.0f / NV; else g = (NL * NH < VH) ? (NL * NH) / (VH * NV) : 1.0f / NV; float k = 1.0f - VH; k = k * k * k; float f = k + (1.0f - k) * rolloff; return specularI * saturate(d * g * f); } float mayaTranslucence( float4 NL_NV_NH_VH, float translucence, float depth, float focus) { const float TRANSL_FADE = 0.15f; const float TRANSL_ANGLE_FAC = -11.5f; float bright = 0.0f; float cosMinAngle = TRANSL_ANGLE_FAC * depth; if (translucence != 0.0f && NL_NV_NH_VH.x > cosMinAngle) { if (focus == 0.0f) { bright = translucence; } else { float VL = saturate(NL_NV_NH_VH.w * NL_NV_NH_VH.w); bright = translucence * pow(VL, focus /(1.00001f - focus)); } float fade = saturate(TRANSL_FADE - cosMinAngle); if (NL_NV_NH_VH.x < (fade + cosMinAngle)) { bright *= (NL_NV_NH_VH.x - cosMinAngle) / fade; } } return bright; } float3 ambientShader( float3 color ) { return color; } float mayaBlinnReflectionRolloff( float4 NL_NV_NH_VH, float reflectance, float rolloff) { float nv3 = 1.0f - NL_NV_NH_VH.y; nv3 = nv3 * nv3 * nv3; float r = saturate(reflectance); r *= nv3 + (1.0f - nv3) * rolloff; return r; } mayaSurfaceShaderOutput mayaADSRCombiner( float3 diffuseIrradIn, float3 specularIrradIn, float translucenceIn, float3 color, float3 transparency, float3 ambientColor, float3 ambientIn, float3 incandescence, float diffuse, float3 specularColor, float reflectivity, float3 reflectedColor, float glowIntensity, bool hideSource, float matteOpacity, int matteOpacityMode, float outAlphaPassThrough) { mayaSurfaceShaderOutput result; result.outColor = (ambientColor+ambientIn) * color; result.outColor += (diffuse + translucenceIn) * diffuseIrradIn * color; result.outColor *= saturate(1.0f - transparency); result.outColor += specularIrradIn * specularColor; result.outColor += incandescence; result.outColor += reflectivity * reflectedColor; result.outTransparency = transparency; result.outGlowColor = glowIntensity * result.outColor; if (hideSource) { result.outColor = float3(0.0f, 0.0f, 0.0f); result.outTransparency = float3(1.0f, 1.0f, 1.0f); } if (matteOpacityMode == 0) { result.outMatteOpacity = float3(-1.0e+06f, -1.0e+06f, -1.0e+06f); } else if (matteOpacityMode == 1) { result.outMatteOpacity = float3(matteOpacity, matteOpacity, matteOpacity); } else { result.outMatteOpacity = (1.0f - result.outTransparency) * matteOpacity; } result.outAlphaPassThrough = outAlphaPassThrough; return result; } float4 mayaSurfaceToFloat4( mayaSurfaceShaderOutput input, float extraOpacity) { const float3 intenseVec = { 0.3333, 0.3333, 0.3333 }; float3 opacity = saturate(1.0f - input.outTransparency); float cutoutA = input.outAlphaPassThrough * extraOpacity; return float4(cutoutA*input.outColor, cutoutA*dot(opacity, intenseVec)); } // Pixel Shader // float4 MainPixelShader( fragInS inputs ) : COLOR0 { mayaSurfaceShaderOutput PixelOutput; float3 v_Nw = Nw( inputs.Nw ); float v_facingFactor = FacingFactor( inputs.facingFactor ); bool v_mayaIsBackFacing = mayaIsBackFacing( v_facingFactor ); float3 v_mayaNormalFlip = mayaNormalFlip( v_Nw, v_mayaIsBackFacing, mayaNormalMultiplier ); float3 v_mayaNwPassThrough = mayaNwPassThrough( v_mayaNormalFlip ); irradiance s_mayaIrradiance; irradiance v_mayaPointLight = mayaPointLight( mayaPointLight_lightOn, mayaPointLight_intensity, mayaPointLight_color, mayaPointLight_emitsDiffuse, mayaPointLight_emitsSpecular, mayaPointLight_position, inputs.Pw ); float3 v_Pw = Pw( inputs.Pw ); s_mayaIrradiance = mayaShadowPointLight( v_mayaPointLight, mayaPointLight_position, v_Pw, mayaShadowPointLight_shadowMap, mayaShadowPointLight_shadowSamp, mayaShadowPointLight_shadowBias, mayaShadowPointLight_depthRange, mayaShadowPointLight_filterRadius, mayaShadowPointLight_shadowColor, mayaShadowPointLight_mayaGlobalShadowOn, mayaShadowPointLight_mayaShadowOn ); float3 v_Vw = Vw( inputs.Vw ); float3 v_mayaHVector = mayaHVector( s_mayaIrradiance.Ls, v_Vw ); float4 v_mayaShaderGeom = mayaShaderGeom( v_mayaNwPassThrough, s_mayaIrradiance.Ld, v_Vw, v_mayaHVector ); float3 v_mayaLambertDiffuse = mayaLambertDiffuse( v_mayaShaderGeom, s_mayaIrradiance.diffuseI ); float3 v_mayaBlinnSpecular = mayaBlinnSpecular( s_mayaIrradiance.specularI, v_mayaShaderGeom, blinn1eccentricity, blinn1specularRollOff ); float v_mayaTranslucence = mayaTranslucence( v_mayaShaderGeom, blinn1translucence, blinn1translucenceDepth, blinn1translucenceFocus ); float3 v_ambientShader = ambientShader( ambientShader_ambientIllum ); float v_mayaBlinnReflectionRolloff = mayaBlinnReflectionRolloff( v_mayaShaderGeom, blinn1reflectivity, specularRollOff ); PixelOutput = mayaADSRCombiner( v_mayaLambertDiffuse, v_mayaBlinnSpecular, v_mayaTranslucence, blinn1color, blinn1transparency, blinn1ambientColor, v_ambientShader, blinn1incandescence, blinn1diffuse, blinn1specularColor, v_mayaBlinnReflectionRolloff, blinn1reflectedColor, blinn1glowIntensity, blinn1hideSource, blinn1matteOpacity, blinn1matteOpacityMode, blinn1outAlphaPassThrough ); float4 v_mayaSurfaceToFloat = mayaSurfaceToFloat4( PixelOutput, extraOpacity ); return v_mayaSurfaceToFloat; } technique main { pass P0 { VertexProgram = compile gp4vp MainVertexShader(); GeometryProgram = NULL; FragmentProgram = compile gp4fp MainPixelShader(); } }