public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar) { if (dataCollector.IsTemplate) { return(GetOutputVectorItem(0, outputId, dataCollector.TemplateDataCollectorInstance.GetWorldSpaceLightDir(CurrentPrecisionType))); } ; dataCollector.AddToIncludes(UniqueId, Constants.UnityCgLibFuncs); dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_POS); return(GetOutputVectorItem(0, outputId, GeneratorUtils.GenerateWorldLightDirection(ref dataCollector, UniqueId, CurrentPrecisionType))); }
public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar) { if (m_outputPorts[0].IsLocalValue(dataCollector.PortCategory)) { return(m_outputPorts[0].LocalValue(dataCollector.PortCategory)); } if (dataCollector.IsFragmentCategory) { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_POS); } string viewdir = string.Empty; if (m_viewType == ViewType.ViewDir) { if (m_viewVecPort.IsConnected) { viewdir = m_viewVecPort.GeneratePortInstructions(ref dataCollector); } else { viewdir = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, ViewSpace.World); } } else { if (m_viewVecPort.IsConnected) { viewdir = m_viewVecPort.GeneratePortInstructions(ref dataCollector); } else { viewdir = GeneratorUtils.GenerateWorldLightDirection(ref dataCollector, UniqueId, CurrentPrecisionType); } } string normal = string.Empty; if (m_normalType == NormalType.WorldNormal || m_normalType == NormalType.TangentNormal) { if (m_normalVecPort.IsConnected) { normal = m_normalVecPort.GeneratePortInstructions(ref dataCollector); if (dataCollector.IsFragmentCategory) { dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); if (m_normalType == NormalType.TangentNormal) { if (dataCollector.IsTemplate) { normal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(UniqueId, CurrentPrecisionType, normal, OutputId); } else { normal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId, CurrentPrecisionType, normal, OutputId); dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, CurrentPrecisionType); dataCollector.ForceNormal = true; } } else { if (m_normalizeVectors) { normal = string.Format("normalize( {0} )", normal); } } } else { if (m_normalType == NormalType.TangentNormal) { string wtMatrix = GeneratorUtils.GenerateWorldToTangentMatrix(ref dataCollector, UniqueId, CurrentPrecisionType); normal = "mul( " + normal + "," + wtMatrix + " )"; } } } else { if (dataCollector.IsFragmentCategory) { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, CurrentPrecisionType); if (dataCollector.DirtyNormal) { dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); } } if (dataCollector.IsTemplate) { normal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(CurrentPrecisionType, normalize: (dataCollector.DirtyNormal && m_normalizeVectors)); } else { normal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId, (dataCollector.DirtyNormal && m_normalizeVectors)); } if (dataCollector.DirtyNormal) { dataCollector.ForceNormal = true; } } } else { // generate HV if (!m_normalVecPort.IsConnected) { string halfView = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, ViewSpace.World); string halfLight = GeneratorUtils.GenerateWorldLightDirection(ref dataCollector, UniqueId, CurrentPrecisionType); normal = "halfVector" + OutputId; dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT3, normal, "normalize( " + halfView + " + " + halfLight + " )"); } else { normal = m_normalVecPort.GeneratePortInstructions(ref dataCollector); if (m_normalizeVectors) { normal = string.Format("normalize( {0} )", normal); } } } string bias = m_biasPort.GeneratePortInstructions(ref dataCollector); string scale = m_scalePort.GeneratePortInstructions(ref dataCollector); string power = m_powerPort.GeneratePortInstructions(ref dataCollector); string fresnelNDotVLocalValue = "dot( " + normal + ", " + viewdir + " )"; string fresnelNDotVLocalVar = "fresnelNdotV" + OutputId; dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, fresnelNDotVLocalVar, fresnelNDotVLocalValue); string fresnelFinalVar = FresnedFinalVar + OutputId; string result = string.Empty; switch (m_fresnelType) { default: case FresnelType.Standard: { result = string.Format("( {0} + {1} * pow( 1.0 - {2}, {3} ) )", bias, scale, fresnelNDotVLocalVar, power); } break; case FresnelType.Schlick: { string f0VarName = "f0" + OutputId; dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, f0VarName, bias); result = string.Format("( {0} + ( 1.0 - {0} ) * pow( 1.0 - {1}, 5 ) )", f0VarName, fresnelNDotVLocalVar); } break; case FresnelType.SchlickIOR: { string iorVarName = "ior" + OutputId; dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, iorVarName, scale); dataCollector.AddLocalVariable(UniqueId, iorVarName + " = pow( ( 1-" + iorVarName + " )/( 1+" + iorVarName + " ), 2 );"); result = string.Format("( {0} + ( 1.0 - {0} ) * pow( 1.0 - {1}, 5 ) )", iorVarName, fresnelNDotVLocalVar); } break; } RegisterLocalVariable(0, result, ref dataCollector, fresnelFinalVar); return(m_outputPorts[0].LocalValue(dataCollector.PortCategory)); }