public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar) { if (dataCollector.IsTemplate) { string varName = (m_viewDirSpace == ViewSpace.World) ? dataCollector.TemplateDataCollectorInstance.GetViewDir(true, MasterNodePortCategory.Fragment, m_safeNormalize?NormalizeType.Safe:NormalizeType.Regular) : dataCollector.TemplateDataCollectorInstance.GetTangentViewDir(m_currentPrecisionType, true, MasterNodePortCategory.Fragment, m_safeNormalize ? NormalizeType.Safe : NormalizeType.Regular); return(GetOutputVectorItem(0, outputId, varName)); } if (dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation) { string result = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, m_viewDirSpace); return(GetOutputVectorItem(0, outputId, result)); } else { if (m_viewDirSpace == ViewSpace.World) { if (dataCollector.DirtyNormal || m_safeNormalize) { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_POS); string result = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId); return(GetOutputVectorItem(0, outputId, result)); } else { dataCollector.AddToInput(UniqueId, SurfaceInputs.VIEW_DIR, PrecisionType.Float); return(GetOutputVectorItem(0, outputId, m_currentInputValueStr)); //return base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalVar ); } } else { if (m_safeNormalize) { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, m_currentPrecisionType); dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); dataCollector.ForceNormal = true; dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_POS); string result = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, ViewSpace.Tangent); return(GetOutputVectorItem(0, outputId, result)); } else { dataCollector.ForceNormal = true; return(base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalVar)); } } } }
public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar) { if (dataCollector.IsTemplate) { string varName = (m_viewDirSpace == ViewSpace.World)? dataCollector.TemplateDataCollectorInstance.GetNormalizedViewDir(): dataCollector.TemplateDataCollectorInstance.GetTangenViewDir(); return(GetOutputVectorItem(0, outputId, varName)); } if (dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation) { string result = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, m_viewDirSpace); return(GetOutputVectorItem(0, outputId, result)); } else { if (m_viewDirSpace == ViewSpace.World) { if (dataCollector.DirtyNormal) { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_POS); string result = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId); return(GetOutputVectorItem(0, outputId, result)); } else { dataCollector.AddToInput(UniqueId, SurfaceInputs.VIEW_DIR, PrecisionType.Float); return(GetOutputVectorItem(0, outputId, m_currentInputValueStr)); //return base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalVar ); } } else { dataCollector.ForceNormal = true; return(base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalVar)); } } }
public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar) { if (!m_texPort.IsConnected) { UIUtils.ShowMessage(UniqueId, "Parallax Occlusion Mapping node only works if a Texture Object is connected to its Tex (R) port"); return("0"); } base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar); WirePortDataType texType = (m_pomTexType == POMTexTypes.Texture3D)?WirePortDataType.SAMPLER3D: WirePortDataType.SAMPLER2D; GeneratePOMfunction(); string arrayIndex = m_arrayIndexPort.Visible?m_arrayIndexPort.GeneratePortInstructions(ref dataCollector):"0"; string textcoords = m_uvPort.GeneratePortInstructions(ref dataCollector); if (m_pomTexType == POMTexTypes.Texture3D) { string texName = "pomTexCoord" + OutputId; dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT3, texName, string.Format("float3({0},{1})", textcoords, arrayIndex)); textcoords = texName; } string texture = m_texPort.GenerateShaderForOutput(ref dataCollector, texType, false, true); string scale = m_defaultScale.ToString(); if (m_scalePort.IsConnected) { scale = m_scalePort.GeneratePortInstructions(ref dataCollector); } string viewDirTan = ""; if (!m_viewdirTanPort.IsConnected) { if (!dataCollector.DirtyNormal) { dataCollector.ForceNormal = true; } if (dataCollector.IsTemplate) { viewDirTan = dataCollector.TemplateDataCollectorInstance.GetTangentViewDir(CurrentPrecisionType); } else { viewDirTan = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, ViewSpace.Tangent); //dataCollector.AddToInput( UniqueId, SurfaceInputs.VIEW_DIR, m_currentPrecisionType ); //viewDirTan = Constants.InputVarStr + "." + UIUtils.GetInputValueFromType( SurfaceInputs.VIEW_DIR ); } } else { viewDirTan = m_viewdirTanPort.GeneratePortInstructions(ref dataCollector); } //generate world normal string normalWorld = string.Empty; if (dataCollector.IsTemplate) { normalWorld = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(CurrentPrecisionType); } else { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, CurrentPrecisionType); dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); normalWorld = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId); } //string normalWorld = "WorldNormalVector( " + Constants.InputVarStr + ", float3( 0, 0, 1 ) )"; //generate viewDir in world space //string worldPos = string.Empty; //if( dataCollector.IsTemplate ) //{ // worldPos = dataCollector.TemplateDataCollectorInstance.GetWorldPos(); //} //else //{ // dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_POS ); // worldPos = Constants.InputVarStr + ".worldPos"; //} //if( !dataCollector.IsTemplate ) // dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_POS ); string worldViewDir = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, ViewSpace.World); //dataCollector.AddToLocalVariables( UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, WorldDirVarStr, TemplateHelperFunctions.WorldSpaceViewDir( dataCollector, worldPos, true ) ); string dx = "ddx(" + textcoords + ")"; string dy = "ddy(" + textcoords + ")"; string refPlane = m_defaultRefPlane.ToString(); if (m_refPlanePort.IsConnected) { refPlane = m_refPlanePort.GeneratePortInstructions(ref dataCollector); } string curvature = "float2(" + m_CurvatureVector.x + "," + m_CurvatureVector.y + ")"; if (m_useCurvature) { dataCollector.AddToProperties(UniqueId, "[Header(Parallax Occlusion Mapping)]", 300); dataCollector.AddToProperties(UniqueId, "_CurvFix(\"Curvature Bias\", Range( 0 , 1)) = 1", 301); dataCollector.AddToUniforms(UniqueId, "uniform float _CurvFix;"); if (m_curvaturePort.IsConnected) { curvature = m_curvaturePort.GeneratePortInstructions(ref dataCollector); } } string localVarName = "OffsetPOM" + OutputId; string textCoordsST = string.Empty; //string textureSTType = dataCollector.IsSRP ? "float4 " : "uniform float4 "; //dataCollector.AddToUniforms( UniqueId, textureSTType + texture +"_ST;"); if (m_texCoordsHelper == null) { m_texCoordsHelper = CreateInstance <Vector4Node>(); m_texCoordsHelper.ContainerGraph = ContainerGraph; m_texCoordsHelper.SetBaseUniqueId(UniqueId, true); m_texCoordsHelper.RegisterPropertyOnInstancing = false; m_texCoordsHelper.AddGlobalToSRPBatcher = true; } if (UIUtils.CurrentWindow.OutsideGraph.IsInstancedShader) { m_texCoordsHelper.CurrentParameterType = PropertyType.InstancedProperty; } else { m_texCoordsHelper.CurrentParameterType = PropertyType.Global; } m_texCoordsHelper.ResetOutputLocals(); m_texCoordsHelper.SetRawPropertyName(texture + "_ST"); textCoordsST = m_texCoordsHelper.GenerateShaderForOutput(0, ref dataCollector, false); ////// if (m_pomTexType == POMTexTypes.TextureArray) { dataCollector.UsingArrayDerivatives = true; } string textureArgs = string.Empty; if (m_pomTexType == POMTexTypes.TextureArray) { if (UIUtils.CurrentWindow.OutsideGraph.IsSRP) { textureArgs = "TEXTURE2D_ARRAY_PARAM(" + texture + " , " + "sampler##" + texture + ")"; } else { textureArgs = "UNITY_PASS_TEX2DARRAY(" + texture + ")"; } } else { bool sampleThroughMacros = UIUtils.CurrentWindow.OutsideGraph.SamplingThroughMacros; if (sampleThroughMacros) { dataCollector.AddToUniforms(UniqueId, string.Format(Constants.SamplerDeclarationSRPMacros[TextureType.Texture2D], texture)); textureArgs = string.Format("{0},sampler{0}", texture); } else { textureArgs = texture; } } //string functionResult = dataCollector.AddFunctions( m_functionHeader, m_functionBody, ( (m_pomTexType == POMTexTypes.TextureArray) ? "UNITY_PASS_TEX2DARRAY(" + texture + ")": texture), textcoords, dx, dy, normalWorld, worldViewDir, viewDirTan, m_minSamples, m_maxSamples, scale, refPlane, texture+"_ST.xy", curvature, arrayIndex ); string functionResult = dataCollector.AddFunctions(m_functionHeader, m_functionBody, textureArgs, textcoords, dx, dy, normalWorld, worldViewDir, viewDirTan, m_inlineMinSamples.GetValueOrProperty(false), m_inlineMinSamples.GetValueOrProperty(false), scale, refPlane, textCoordsST + ".xy", curvature, arrayIndex); dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, m_pomUVPort.DataType, localVarName, functionResult); return(GetOutputVectorItem(0, outputId, localVarName)); }
public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar) { if (dataCollector.IsTemplate) { dataCollector.AddToIncludes(UniqueId, Constants.UnityLightingLib); //string worldPos = dataCollector.TemplateDataCollectorInstance.GetWorldPos(); string worldViewDir = dataCollector.TemplateDataCollectorInstance.GetViewDir(false, MasterNodePortCategory.Fragment); //string worldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal( PrecisionType.Float ); string worldNormal = string.Empty; if (m_inputPorts[0].IsConnected) { if (m_normalSpace == ViewSpace.Tangent) { worldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(UniqueId, m_currentPrecisionType, m_inputPorts[0].GeneratePortInstructions(ref dataCollector), OutputId); } else { worldNormal = m_inputPorts[0].GeneratePortInstructions(ref dataCollector); } } else { worldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(PrecisionType.Float, false, MasterNodePortCategory.Fragment); } string tempsmoothness = m_inputPorts[1].GeneratePortInstructions(ref dataCollector); string tempocclusion = m_inputPorts[2].GeneratePortInstructions(ref dataCollector); dataCollector.AddLocalVariable(UniqueId, "UnityGIInput data;"); dataCollector.AddLocalVariable(UniqueId, "UNITY_INITIALIZE_OUTPUT( UnityGIInput, data );"); //dataCollector.AddLocalVariable( UniqueId, "data.worldPos = " + worldPos + ";" ); //dataCollector.AddLocalVariable( UniqueId, "data.worldViewDir = " + worldViewDir + ";" ); dataCollector.AddLocalVariable(UniqueId, "data.probeHDR[0] = unity_SpecCube0_HDR;"); dataCollector.AddLocalVariable(UniqueId, "data.probeHDR[1] = unity_SpecCube1_HDR;"); dataCollector.AddLocalVariable(UniqueId, "#if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTION //specdataif0"); dataCollector.AddLocalVariable(UniqueId, "\tdata.boxMin[0] = unity_SpecCube0_BoxMin;"); dataCollector.AddLocalVariable(UniqueId, "#endif //specdataif0"); dataCollector.AddLocalVariable(UniqueId, "#if UNITY_SPECCUBE_BOX_PROJECTION //specdataif1"); dataCollector.AddLocalVariable(UniqueId, "\tdata.boxMax[0] = unity_SpecCube0_BoxMax;"); dataCollector.AddLocalVariable(UniqueId, "\tdata.probePosition[0] = unity_SpecCube0_ProbePosition;"); dataCollector.AddLocalVariable(UniqueId, "\tdata.boxMax[1] = unity_SpecCube1_BoxMax;"); dataCollector.AddLocalVariable(UniqueId, "\tdata.boxMin[1] = unity_SpecCube1_BoxMin;"); dataCollector.AddLocalVariable(UniqueId, "\tdata.probePosition[1] = unity_SpecCube1_ProbePosition;"); dataCollector.AddLocalVariable(UniqueId, "#endif //specdataif1"); dataCollector.AddLocalVariable(UniqueId, "Unity_GlossyEnvironmentData g" + OutputId + " = UnityGlossyEnvironmentSetup( " + tempsmoothness + ", " + worldViewDir + ", " + worldNormal + ", float3(0,0,0));"); dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, "indirectSpecular" + OutputId, "UnityGI_IndirectSpecular( data, " + tempocclusion + ", " + worldNormal + ", g" + OutputId + " )"); return("indirectSpecular" + OutputId); } if (dataCollector.GenType == PortGenType.NonCustomLighting || dataCollector.CurrentCanvasMode != NodeAvailability.CustomLighting) { return("float3(0,0,0)"); } string normal = string.Empty; if (m_inputPorts[0].IsConnected) { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, m_currentPrecisionType); dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); dataCollector.ForceNormal = true; normal = m_inputPorts[0].GeneratePortInstructions(ref dataCollector); if (m_normalSpace == ViewSpace.Tangent) { normal = "WorldNormalVector( " + Constants.InputVarStr + " , " + normal + " )"; } dataCollector.AddLocalVariable(UniqueId, "float3 indirectNormal" + OutputId + " = " + normal + ";"); normal = "indirectNormal" + OutputId; } else { if (dataCollector.IsFragmentCategory) { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, m_currentPrecisionType); if (dataCollector.DirtyNormal) { dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); dataCollector.ForceNormal = true; } } normal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId); } string smoothness = m_inputPorts[1].GeneratePortInstructions(ref dataCollector); string occlusion = m_inputPorts[2].GeneratePortInstructions(ref dataCollector); string viewDir = "data.worldViewDir"; if (dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation) { string worldPos = GeneratorUtils.GenerateWorldPosition(ref dataCollector, UniqueId); viewDir = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId); dataCollector.AddLocalVariable(UniqueId, "UnityGIInput data;"); dataCollector.AddLocalVariable(UniqueId, "UNITY_INITIALIZE_OUTPUT( UnityGIInput, data );"); dataCollector.AddLocalVariable(UniqueId, "data.worldPos = " + worldPos + ";"); dataCollector.AddLocalVariable(UniqueId, "data.worldViewDir = " + viewDir + ";"); dataCollector.AddLocalVariable(UniqueId, "data.probeHDR[0] = unity_SpecCube0_HDR;"); dataCollector.AddLocalVariable(UniqueId, "data.probeHDR[1] = unity_SpecCube1_HDR;"); dataCollector.AddLocalVariable(UniqueId, "#if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTION //specdataif0"); dataCollector.AddLocalVariable(UniqueId, "data.boxMin[0] = unity_SpecCube0_BoxMin;"); dataCollector.AddLocalVariable(UniqueId, "#endif //specdataif0"); dataCollector.AddLocalVariable(UniqueId, "#if UNITY_SPECCUBE_BOX_PROJECTION //specdataif1"); dataCollector.AddLocalVariable(UniqueId, "data.boxMax[0] = unity_SpecCube0_BoxMax;"); dataCollector.AddLocalVariable(UniqueId, "data.probePosition[0] = unity_SpecCube0_ProbePosition;"); dataCollector.AddLocalVariable(UniqueId, "data.boxMax[1] = unity_SpecCube1_BoxMax;"); dataCollector.AddLocalVariable(UniqueId, "data.boxMin[1] = unity_SpecCube1_BoxMin;"); dataCollector.AddLocalVariable(UniqueId, "data.probePosition[1] = unity_SpecCube1_ProbePosition;"); dataCollector.AddLocalVariable(UniqueId, "#endif //specdataif1"); } dataCollector.AddLocalVariable(UniqueId, "Unity_GlossyEnvironmentData g" + OutputId + " = UnityGlossyEnvironmentSetup( " + smoothness + ", " + viewDir + ", " + normal + ", float3(0,0,0));"); dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, "indirectSpecular" + OutputId, "UnityGI_IndirectSpecular( data, " + occlusion + ", " + normal + ", g" + OutputId + " )"); return("indirectSpecular" + OutputId); }
public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar) { if (m_outputPorts[0].IsLocalValue) { return(m_outputPorts[0].LocalValue); } if (dataCollector.IsFragmentCategory) { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_POS); } string viewdir = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, ViewSpace.World); string normal = string.Empty; if (m_inputPorts[0].IsConnected) { normal = m_inputPorts[0].GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT3, ignoreLocalvar, true); if (dataCollector.IsFragmentCategory) { dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); if (m_normalSpace == ViewSpace.Tangent) { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, m_currentPrecisionType); dataCollector.ForceNormal = true; normal = "WorldNormalVector( " + Constants.InputVarStr + " , " + normal + " )"; } } else { if (m_normalSpace == ViewSpace.Tangent) { string wtMatrix = GeneratorUtils.GenerateWorldToTangentMatrix(ref dataCollector, UniqueId, m_currentPrecisionType); normal = "mul( " + normal + "," + wtMatrix + " )"; } } } else { if (dataCollector.IsFragmentCategory) { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, m_currentPrecisionType); if (dataCollector.DirtyNormal) { dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); } } normal = dataCollector.IsTemplate ? dataCollector.TemplateDataCollectorInstance.GetWorldNormal() : GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId); } string bias = m_inputPorts[1].GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT, ignoreLocalvar, true); string scale = m_inputPorts[2].GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT, ignoreLocalvar, true); string power = m_inputPorts[3].GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT, ignoreLocalvar, true); string fresnelNDotVLocalValue = "dot( " + normal + ", " + viewdir + " )"; string fresnelNDotVLocalVar = "fresnelNDotV" + OutputId; dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT, fresnelNDotVLocalVar, fresnelNDotVLocalValue); string fresnelFinalVar = FresnedFinalVar + OutputId; string result = string.Format("( {0} + {1} * pow( 1.0 - {2}, {3} ) )", bias, scale, fresnelNDotVLocalVar, power); RegisterLocalVariable(0, result, ref dataCollector, fresnelFinalVar); return(m_outputPorts[0].LocalValue); }
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)); }
public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar) { if (dataCollector.IsTemplate) { if (m_inputPorts[0].IsConnected) { if (m_outputPorts[0].IsLocalValue) { return(m_outputPorts[0].LocalValue); } string value = dataCollector.TemplateDataCollectorInstance.GetWorldReflection(m_inputPorts[0].GeneratePortInstructions(ref dataCollector)); RegisterLocalVariable(0, value, ref dataCollector, "worldRefl" + OutputId); return(m_outputPorts[0].LocalValue); } else { return(GetOutputVectorItem(0, outputId, dataCollector.TemplateDataCollectorInstance.GetWorldReflection())); } } bool isVertex = (dataCollector.PortCategory == MasterNodePortCategory.Tessellation || dataCollector.PortCategory == MasterNodePortCategory.Vertex); if (isVertex) { if (m_inputPorts[0].IsConnected) { if (m_outputPorts[0].IsLocalValue) { return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue)); } string normal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId); string tangent = GeneratorUtils.GenerateWorldTangent(ref dataCollector, UniqueId); dataCollector.AddToVertexLocalVariables(UniqueId, "float3x3 tangentToWorld = CreateTangentToWorldPerVertex( " + normal + ", " + tangent + ", " + Constants.VertexShaderInputStr + ".tangent.w );"); string inputTangent = m_inputPorts[0].GeneratePortInstructions(ref dataCollector); dataCollector.AddToVertexLocalVariables(UniqueId, "float3 tangentNormal" + OutputId + " = " + inputTangent + ";"); string viewDir = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId); dataCollector.AddToVertexLocalVariables(UniqueId, "float3 modWorldNormal" + OutputId + " = ( tangentToWorld[0] * tangentNormal" + OutputId + ".x + tangentToWorld[1] * tangentNormal" + OutputId + ".y + tangentToWorld[2] * tangentNormal" + OutputId + ".z);"); RegisterLocalVariable(0, "reflect( -" + viewDir + ", modWorldNormal" + OutputId + " )", ref dataCollector, "modReflection" + OutputId); return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue)); } else { if (m_outputPorts[0].IsLocalValue) { return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue)); } string worldNormal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId); string viewDir = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId); RegisterLocalVariable(0, "reflect( -" + viewDir + ", " + worldNormal + " )", ref dataCollector, ReflectionVecValStr + OutputId); return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue)); } } else { if (m_outputPorts[0].IsLocalValue) { return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue)); } dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_REFL, m_currentPrecisionType); string result = string.Empty; if (m_inputPorts[0].IsConnected) { dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); dataCollector.ForceNormal = true; result = "WorldReflectionVector( " + Constants.InputVarStr + " , " + m_inputPorts[0].GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT3, ignoreLocalVar) + " )"; int connCount = 0; for (int i = 0; i < m_outputPorts.Count; i++) { connCount += m_outputPorts[i].ConnectionCount; } if (connCount > 1 || outputId > 0) { dataCollector.AddToLocalVariables(UniqueId, string.Format(ReflectionVecDecStr, ReflectionVecValStr + OutputId, result)); RegisterLocalVariable(0, result, ref dataCollector, ReflectionVecValStr + OutputId); return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue)); } } else { dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); result = GeneratorUtils.GenerateWorldReflection(ref dataCollector, UniqueId); } return(GetOutputVectorItem(0, outputId, result)); //RegisterLocalVariable( 0, result, ref dataCollector, "worldrefVec" + OutputId ); //return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue ); } }
public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar) { if (dataCollector.CurrentCanvasMode != NodeAvailability.CustomLighting) { return("float3(0,0,0)"); } string normal = string.Empty; if (m_inputPorts[0].IsConnected) { dataCollector.AddToInput(UniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_NORMAL), true); dataCollector.AddToInput(UniqueId, Constants.InternalData, false); dataCollector.ForceNormal = true; normal = m_inputPorts[0].GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT3, ignoreLocalvar); if (m_normalSpace == ViewSpace.Tangent) { normal = "WorldNormalVector( " + Constants.InputVarStr + " , " + normal + " )"; } dataCollector.AddLocalVariable(UniqueId, "float3 indirectNormal" + OutputId + " = " + normal + ";"); normal = "indirectNormal" + OutputId; } else { if (dataCollector.IsFragmentCategory) { dataCollector.AddToInput(UniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_NORMAL), true); } normal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId); } string roughness = m_inputPorts[1].GeneratePortInstructions(ref dataCollector); string occlusion = m_inputPorts[2].GeneratePortInstructions(ref dataCollector); string viewDir = "data.worldViewDir"; if (dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation) { string worldPos = GeneratorUtils.GenerateWorldPosition(ref dataCollector, UniqueId); viewDir = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId); dataCollector.AddLocalVariable(UniqueId, "UnityGIInput data;"); dataCollector.AddLocalVariable(UniqueId, "UNITY_INITIALIZE_OUTPUT( UnityGIInput, data );"); dataCollector.AddLocalVariable(UniqueId, "data.worldPos = " + worldPos + ";"); dataCollector.AddLocalVariable(UniqueId, "data.worldViewDir = " + viewDir + ";"); dataCollector.AddLocalVariable(UniqueId, "data.probeHDR[0] = unity_SpecCube0_HDR;"); dataCollector.AddLocalVariable(UniqueId, "data.probeHDR[1] = unity_SpecCube1_HDR;"); dataCollector.AddLocalVariable(UniqueId, "#if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTION //specdataif0"); dataCollector.AddLocalVariable(UniqueId, "data.boxMin[0] = unity_SpecCube0_BoxMin;"); dataCollector.AddLocalVariable(UniqueId, "#endif //specdataif0"); dataCollector.AddLocalVariable(UniqueId, "#if UNITY_SPECCUBE_BOX_PROJECTION //specdataif1"); dataCollector.AddLocalVariable(UniqueId, "data.boxMax[0] = unity_SpecCube0_BoxMax;"); dataCollector.AddLocalVariable(UniqueId, "data.probePosition[0] = unity_SpecCube0_ProbePosition;"); dataCollector.AddLocalVariable(UniqueId, "data.boxMax[1] = unity_SpecCube1_BoxMax;"); dataCollector.AddLocalVariable(UniqueId, "data.boxMin[1] = unity_SpecCube1_BoxMin;"); dataCollector.AddLocalVariable(UniqueId, "data.probePosition[1] = unity_SpecCube1_ProbePosition;"); dataCollector.AddLocalVariable(UniqueId, "#endif //specdataif1"); } dataCollector.AddLocalVariable(UniqueId, "Unity_GlossyEnvironmentData g" + OutputId + ";"); dataCollector.AddLocalVariable(UniqueId, "g" + OutputId + ".roughness = " + roughness + ";"); dataCollector.AddLocalVariable(UniqueId, "g" + OutputId + ".reflUVW = reflect( -" + viewDir + ", " + normal + " );"); dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, "indirectSpecular" + OutputId, "UnityGI_IndirectSpecular( data, " + occlusion + ", " + normal + ", g" + OutputId + " )"); return("indirectSpecular" + OutputId); }
public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar) { base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar); WirePortDataType texType = (m_pomTexType == POMTexTypes.Texture3D)?WirePortDataType.SAMPLER3D: WirePortDataType.SAMPLER2D; GeneratePOMfunction(); string arrayIndex = m_arrayIndexPort.Visible?m_arrayIndexPort.GeneratePortInstructions(ref dataCollector):"0"; string textcoords = m_uvPort.GeneratePortInstructions(ref dataCollector); if (m_pomTexType == POMTexTypes.Texture3D) { string texName = "pomTexCoord" + OutputId; dataCollector.AddToLocalVariables(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, texName, string.Format("float3({0},{1})", textcoords, arrayIndex)); textcoords = texName; } string texture = m_texPort.GenerateShaderForOutput(ref dataCollector, texType, false, true); string scale = m_defaultScale.ToString(); if (m_scalePort.IsConnected) { scale = m_scalePort.GeneratePortInstructions(ref dataCollector); } string viewDirTan = ""; if (!m_viewdirTanPort.IsConnected) { if (!dataCollector.DirtyNormal) { dataCollector.ForceNormal = true; } if (dataCollector.IsTemplate) { viewDirTan = dataCollector.TemplateDataCollectorInstance.GetTangentViewDir(m_currentPrecisionType); } else { viewDirTan = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, ViewSpace.Tangent); //dataCollector.AddToInput( UniqueId, SurfaceInputs.VIEW_DIR, m_currentPrecisionType ); //viewDirTan = Constants.InputVarStr + "." + UIUtils.GetInputValueFromType( SurfaceInputs.VIEW_DIR ); } } else { viewDirTan = m_viewdirTanPort.GeneratePortInstructions(ref dataCollector); } //generate world normal string normalWorld = string.Empty; if (dataCollector.IsTemplate) { normalWorld = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(m_currentPrecisionType); } else { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, m_currentPrecisionType); dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); normalWorld = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId); } //string normalWorld = "WorldNormalVector( " + Constants.InputVarStr + ", float3( 0, 0, 1 ) )"; //generate viewDir in world space //string worldPos = string.Empty; //if( dataCollector.IsTemplate ) //{ // worldPos = dataCollector.TemplateDataCollectorInstance.GetWorldPos(); //} //else //{ // dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_POS ); // worldPos = Constants.InputVarStr + ".worldPos"; //} //if( !dataCollector.IsTemplate ) // dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_POS ); string worldViewDir = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, ViewSpace.World); //dataCollector.AddToLocalVariables( UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, WorldDirVarStr, TemplateHelperFunctions.WorldSpaceViewDir( dataCollector, worldPos, true ) ); string dx = "ddx(" + textcoords + ")"; string dy = "ddy(" + textcoords + ")"; string refPlane = m_defaultRefPlane.ToString(); if (m_refPlanePort.IsConnected) { refPlane = m_refPlanePort.GeneratePortInstructions(ref dataCollector); } string curvature = "float2(" + m_CurvatureVector.x + "," + m_CurvatureVector.y + ")"; if (m_useCurvature) { dataCollector.AddToProperties(UniqueId, "[Header(Parallax Occlusion Mapping)]", 300); dataCollector.AddToProperties(UniqueId, "_CurvFix(\"Curvature Bias\", Range( 0 , 1)) = 1", 301); dataCollector.AddToUniforms(UniqueId, "uniform float _CurvFix;"); if (m_curvaturePort.IsConnected) { curvature = m_curvaturePort.GeneratePortInstructions(ref dataCollector); } } string localVarName = "OffsetPOM" + UniqueId; dataCollector.AddToUniforms(UniqueId, "uniform float4 " + texture + "_ST;"); if (m_pomTexType == POMTexTypes.TextureArray) { dataCollector.UsingArrayDerivatives = true; } string functionResult = dataCollector.AddFunctions(m_functionHeader, m_functionBody, ((m_pomTexType == POMTexTypes.TextureArray) ? "UNITY_PASS_TEX2DARRAY(" + texture + ")": texture), textcoords, dx, dy, normalWorld, worldViewDir, viewDirTan, m_minSamples, m_maxSamples, scale, refPlane, texture + "_ST.xy", curvature, arrayIndex); dataCollector.AddToLocalVariables(UniqueId, m_currentPrecisionType, m_pomUVPort.DataType, localVarName, functionResult); return(GetOutputVectorItem(0, outputId, localVarName)); }