예제 #1
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            dataCollector.AddToInput(m_uniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_POS), true);
            dataCollector.AddToLocalVariables(m_uniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, WorldDirVarStr, "normalize( UnityWorldSpaceViewDir( " + Constants.InputVarStr + ".worldPos ) )");

            string normal = string.Empty;

            if (m_inputPorts[0].IsConnected)
            {
                normal = m_inputPorts[0].GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT3, ignoreLocalvar, true);
            }
            else
            {
                dataCollector.AddToInput(m_uniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_NORMAL), true);
                dataCollector.AddToInput(m_uniqueId, Constants.InternalData, false);
                normal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, m_uniqueId);
                //string normalWorld = "WorldNormalVector( " + Constants.InputVarStr + ", float3( 0, 0, 1 ) );";
                //dataCollector.AddToLocalVariables( m_uniqueId, "float3 worldNormal = "+ normalWorld );
                //normal = "worldNormal";
                //dataCollector.ForceNormal = true;
            }

            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 ndotl = "dot( " + normal + ", " + WorldDirVarStr + " )";

            string fresnalFinalVar = FresnedFinalVar + m_uniqueId;

            dataCollector.AddToLocalVariables(m_uniqueId, string.Format(FresnesFinalOp, fresnalFinalVar, string.Format("({0} + {1}*pow( 1.0 - {2} , {3}))", bias, scale, ndotl, power)));
            return(CreateOutputLocalVariable(0, fresnalFinalVar, ref dataCollector));
        }
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (ContainerGraph.CurrentCanvasMode == NodeAvailability.TemplateShader || ContainerGraph.CurrentStandardSurface.CurrentLightingModel != StandardShaderLightModel.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 + " )";
                }
            }
            else
            {
                normal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId);
                dataCollector.AddToInput(UniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_NORMAL), true);
            }

            dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, "indirectDiffuse" + OutputId, "ShadeSH9( float4( " + normal + ", 1 ) )");

            return("indirectDiffuse" + OutputId);
        }
예제 #3
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            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].GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT3, ignoreLocalvar);
                if (m_normalSpace == ViewSpace.Tangent)
                {
                    normal = "WorldNormalVector( " + Constants.InputVarStr + " , " + normal + " )";
                }
            }
            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);
            }


            if (dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation)
            {
                dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, "indirectDiffuse" + OutputId, "ShadeSH9( float4( " + normal + ", 1 ) )");
            }
            else
            {
                dataCollector.AddLocalVariable(UniqueId, "UnityGI gi" + OutputId + " = gi;");
                dataCollector.AddLocalVariable(UniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, "diffNorm" + OutputId, normal);
                dataCollector.AddLocalVariable(UniqueId, "gi" + OutputId + " = UnityGI_Base( data, 1, diffNorm" + OutputId + " );");
                dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, "indirectDiffuse" + OutputId, "gi" + OutputId + ".indirect.diffuse + diffNorm" + OutputId + " * 0.0001");
            }

            return("indirectDiffuse" + OutputId);
        }
예제 #4
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (m_outputPorts[0].IsLocalValue)
            {
                return(m_outputPorts[0].LocalValue);
            }

            dataCollector.AddToInput(UniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_POS), true);
            if (dataCollector.IsFragmentCategory)
            {
                dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, WorldDirVarStr, string.Format(WorldDirFuncStr, Constants.InputVarStr + ".worldPos"));
            }
            else
            {
                string worldPosVar = GeneratorUtils.GenerateWorldPosition(ref dataCollector, UniqueId);
                dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, WorldDirVarStr, string.Format(WorldDirFuncStr, worldPosVar));
            }

            string normal = string.Empty;

            if (m_inputPorts[0].IsConnected)
            {
                normal = m_inputPorts[0].GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT3, ignoreLocalvar, true);
            }
            else
            {
                dataCollector.AddToInput(UniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_NORMAL), true);
                dataCollector.AddToInput(UniqueId, Constants.InternalData, false);
                normal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId);
                //string normalWorld = "WorldNormalVector( " + Constants.InputVarStr + ", float3( 0, 0, 1 ) );";
                //dataCollector.AddToLocalVariables( m_uniqueId, "float3 worldNormal = "+ normalWorld );
                //normal = "worldNormal";
                //dataCollector.ForceNormal = true;
            }

            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 ndotl = "dot( " + normal + ", " + WorldDirVarStr + " )";

            string fresnelFinalVar = FresnedFinalVar + OutputId;
            string result          = string.Format("({0} + {1}*pow( 1.0 - {2} , {3}))", bias, scale, ndotl, power);

            RegisterLocalVariable(0, result, ref dataCollector, fresnelFinalVar);
            return(m_outputPorts[0].LocalValue);
        }
예제 #5
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (ContainerGraph.CurrentCanvasMode == NodeAvailability.TemplateShader || ContainerGraph.CurrentStandardSurface.CurrentLightingModel != StandardShaderLightModel.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
            {
                normal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId);
                dataCollector.AddToInput(UniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_NORMAL), true);
            }

            string roughness = m_inputPorts[1].GeneratePortInstructions(ref dataCollector);
            string occlusion = m_inputPorts[2].GeneratePortInstructions(ref dataCollector);

            dataCollector.AddLocalVariable(UniqueId, "Unity_GlossyEnvironmentData g" + OutputId + ";");
            dataCollector.AddLocalVariable(UniqueId, "g" + OutputId + ".roughness = " + roughness + ";");
            dataCollector.AddLocalVariable(UniqueId, "g" + OutputId + ".reflUVW = reflect( -data.worldViewDir, " + 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)
        {
            if (dataCollector.GenType == PortGenType.NonCustomLighting || dataCollector.CurrentCanvasMode != NodeAvailability.CustomLighting)
            {
                return("float3(0,0,0)");
            }

            if (m_outputPorts[0].IsLocalValue(dataCollector.PortCategory))
            {
                return(m_outputPorts[0].LocalValue(dataCollector.PortCategory));
            }

            string specularMode = string.Empty;

            if (m_workflow == ASEStandardSurfaceWorkflow.Specular)
            {
                specularMode = "Specular";
            }

            dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, CurrentPrecisionType);

            if (dataCollector.DirtyNormal)
            {
                dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false);
                dataCollector.ForceNormal = true;
            }

            dataCollector.AddLocalVariable(UniqueId, "SurfaceOutputStandard" + specularMode + " s" + OutputId + " = (SurfaceOutputStandard" + specularMode + " ) 0;");
            dataCollector.AddLocalVariable(UniqueId, "s" + OutputId + ".Albedo = " + m_inputPorts[0].GeneratePortInstructions(ref dataCollector) + ";");

            string normal = string.Empty;

            if (m_inputPorts[1].IsConnected)
            {
                normal = m_inputPorts[1].GeneratePortInstructions(ref dataCollector);
                if (m_normalSpace == ViewSpace.Tangent)
                {
                    normal = "WorldNormalVector( " + Constants.InputVarStr + " , " + normal + " )";
                }
            }
            else
            {
                normal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId);
            }



            dataCollector.AddLocalVariable(UniqueId, "s" + OutputId + ".Normal = " + normal + ";");
            dataCollector.AddLocalVariable(UniqueId, "s" + OutputId + ".Emission = " + m_inputPorts[2].GeneratePortInstructions(ref dataCollector) + ";");
            if (m_workflow == ASEStandardSurfaceWorkflow.Specular)
            {
                dataCollector.AddLocalVariable(UniqueId, "s" + OutputId + ".Specular = " + m_inputPorts[3].GeneratePortInstructions(ref dataCollector) + ";");
            }
            else
            {
                dataCollector.AddLocalVariable(UniqueId, "s" + OutputId + ".Metallic = " + m_inputPorts[3].GeneratePortInstructions(ref dataCollector) + ";");
            }
            dataCollector.AddLocalVariable(UniqueId, "s" + OutputId + ".Smoothness = " + m_inputPorts[4].GeneratePortInstructions(ref dataCollector) + ";");
            dataCollector.AddLocalVariable(UniqueId, "s" + OutputId + ".Occlusion = " + m_inputPorts[5].GeneratePortInstructions(ref dataCollector) + ";\n");

            dataCollector.AddLocalVariable(UniqueId, "data.light = gi.light;\n", true);

            dataCollector.AddLocalVariable(UniqueId, "UnityGI gi" + OutputId + " = gi;");
            dataCollector.AddLocalVariable(UniqueId, "#ifdef UNITY_PASS_FORWARDBASE", true);

            dataCollector.AddLocalVariable(UniqueId, "Unity_GlossyEnvironmentData g" + OutputId + " = UnityGlossyEnvironmentSetup( s" + OutputId + ".Smoothness, data.worldViewDir, s" + OutputId + ".Normal, float3(0,0,0));");
            dataCollector.AddLocalVariable(UniqueId, "gi" + OutputId + " = UnityGlobalIllumination( data, s" + OutputId + ".Occlusion, s" + OutputId + ".Normal, g" + OutputId + " );");
            dataCollector.AddLocalVariable(UniqueId, "#endif\n", true);
            dataCollector.AddLocalVariable(UniqueId, "float3 surfResult" + OutputId + " = LightingStandard" + specularMode + " ( s" + OutputId + ", viewDir, gi" + OutputId + " ).rgb;");
            //Emission must be always added to trick Unity, so it knows what needs to be created p.e. world pos
            dataCollector.AddLocalVariable(UniqueId, "surfResult" + OutputId + " += s" + OutputId + ".Emission;\n");

            m_outputPorts[0].SetLocalValue("surfResult" + OutputId, dataCollector.PortCategory);

            //Remove emission contribution from Forward Add
            dataCollector.AddLocalVariable(UniqueId, "#ifdef UNITY_PASS_FORWARDADD//" + OutputId);
            dataCollector.AddLocalVariable(UniqueId, string.Format("surfResult{0} -= s{0}.Emission;", OutputId));
            dataCollector.AddLocalVariable(UniqueId, "#endif//" + OutputId);

            return(m_outputPorts[0].LocalValue(dataCollector.PortCategory));
        }
예제 #7
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar);

            GeneratePOMfunction();

            string textcoords = m_uvPort.GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT2, false, true);
            string texture    = m_texPort.GenerateShaderForOutput(ref dataCollector, WirePortDataType.SAMPLER2D, false, true);
            string scale      = m_defaultScale.ToString();

            if (m_scalePort.IsConnected)
            {
                scale = m_scalePort.GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT, false, true);
            }

            string viewDirTan = "";

            if (!m_viewdirTanPort.IsConnected)
            {
                if (!dataCollector.DirtyNormal)
                {
                    dataCollector.ForceNormal = true;
                }


                if (dataCollector.IsTemplate)
                {
                    viewDirTan = dataCollector.TemplateDataCollectorInstance.GetTangenViewDir();
                }
                else
                {
                    string inputViewDirTan = UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.VIEW_DIR);
                    dataCollector.AddToInput(UniqueId, inputViewDirTan, true);
                    viewDirTan = Constants.InputVarStr + "." + UIUtils.GetInputValueFromType(AvailableSurfaceInputs.VIEW_DIR);
                }
            }
            else
            {
                viewDirTan = m_viewdirTanPort.GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT3, false, true);
            }

            //generate world normal
            string normalWorld = string.Empty;

            if (dataCollector.IsTemplate)
            {
                normalWorld = dataCollector.TemplateDataCollectorInstance.GetWorldNormal();
            }
            else
            {
                dataCollector.AddToInput(UniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_NORMAL), true);
                dataCollector.AddToInput(UniqueId, Constants.InternalData, 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, UIUtils.GetInputDeclarationFromType(PrecisionType.Float, AvailableSurfaceInputs.WORLD_POS), true);
                worldPos = Constants.InputVarStr + ".worldPos";
            }
            dataCollector.AddToLocalVariables(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, WorldDirVarStr, string.Format("normalize( UnityWorldSpaceViewDir( {0} ) )", worldPos));

            //dataCollector.AddToInput( m_uniqueId, string.Format( WorldDirVarDecStr, UIUtils.FinalPrecisionWirePortToCgType( m_currentPrecisionType, WirePortDataType.FLOAT3 ), WorldDirVarStr ), false );
            //dataCollector.AddVertexInstruction( WorldDirVarDefStr, m_uniqueId );

            string dx = "ddx(" + textcoords + ")";
            string dy = "ddx(" + 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;");

            string arrayIndex = m_arrayIndexPort.GeneratePortInstructions(ref dataCollector);

            if (m_useTextureArray)
            {
                dataCollector.UsingArrayDerivatives = true;
            }

            string functionResult = dataCollector.AddFunctions(m_functionHeader, m_functionBody, (m_useTextureArray ? "UNITY_PASS_TEX2DARRAY(" + texture + ")": texture), textcoords, dx, dy, normalWorld, WorldDirVarStr, 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));
        }
예제 #8
0
        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);
        }
예제 #9
0
        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));
        }
예제 #10
0
        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.GetWorldNormal(m_inputPorts[0].GeneratePortInstructions(ref dataCollector));
                    RegisterLocalVariable(0, value, ref dataCollector, "worldNormal" + OutputId);
                    return(m_outputPorts[0].LocalValue);
                }
                else
                {
                    return(GetOutputVectorItem(0, outputId, dataCollector.TemplateDataCollectorInstance.GetWorldNormal()));
                }
            }

            if (dataCollector.PortCategory == MasterNodePortCategory.Fragment || dataCollector.PortCategory == MasterNodePortCategory.Debug)
            {
                dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, m_currentPrecisionType);

                string result = string.Empty;
                if (m_inputPorts[0].IsConnected)
                {
                    dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false);
                    dataCollector.ForceNormal = true;

                    result = "WorldNormalVector( " + 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(NormalVecDecStr, NormalVecValStr + OutputId, result));
                        return(GetOutputVectorItem(0, outputId, NormalVecValStr + OutputId));
                    }
                }
                else
                {
                    if (!dataCollector.DirtyNormal)
                    {
                        result = Constants.InputVarStr + ".worldNormal";
                    }
                    else
                    {
                        dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false);
                        result = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId);
                        dataCollector.ForceNormal = true;
                    }
                }

                return(GetOutputVectorItem(0, outputId, result));
            }
            else
            {
                if (m_inputPorts[0].IsConnected)
                {
                    string inputTangent = m_inputPorts[0].GeneratePortInstructions(ref dataCollector);

                    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 );");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "float3 tangentNormal" + OutputId + " = " + inputTangent + ";");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "float3 modWorldNormal" + OutputId + " = (tangentToWorld[0] * tangentNormal" + OutputId + ".x + tangentToWorld[1] * tangentNormal" + OutputId + ".y + tangentToWorld[2] * tangentNormal" + OutputId + ".z);");
                    return(GetOutputVectorItem(0, outputId, "modWorldNormal" + OutputId));
                }
                else
                {
                    string result = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId);
                    return(GetOutputVectorItem(0, outputId, result));
                }
            }
        }
예제 #11
0
        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.GetWorldNormal(m_inputPorts[0].GeneratePortInstructions(ref dataCollector));
                    RegisterLocalVariable(0, value, ref dataCollector, "worldNormal" + OutputId);
                    return(m_outputPorts[0].LocalValue);
                }
                else
                {
                    return(GetOutputVectorItem(0, outputId, dataCollector.TemplateDataCollectorInstance.GetWorldNormal()));
                }
            }

            if (dataCollector.PortCategory == MasterNodePortCategory.Fragment || dataCollector.PortCategory == MasterNodePortCategory.Debug)
            {
                dataCollector.AddToInput(UniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_NORMAL), true);
                //dataCollector.AddToInput( m_uniqueId, Constants.InternalData, false );
                string result = string.Empty;
                if (m_inputPorts[0].IsConnected)
                {
                    dataCollector.AddToInput(UniqueId, Constants.InternalData, false);
                    dataCollector.ForceNormal = true;

                    result = "WorldNormalVector( " + Constants.InputVarStr + " , " + m_inputPorts[0].GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT3, ignoreLocalvar) + " )";

                    if (m_outputPorts[0].ConnectionCount > 1)
                    {
                        dataCollector.AddToLocalVariables(UniqueId, string.Format(NormalVecDecStr, NormalVecValStr + UniqueId, result));
                        return(GetOutputVectorItem(0, outputId, NormalVecValStr + UniqueId));
                    }
                }
                else
                {
                    if (!dataCollector.DirtyNormal)
                    {
                        result = Constants.InputVarStr + ".worldNormal";
                    }
                    else
                    {
                        dataCollector.AddToInput(UniqueId, Constants.InternalData, false);
                        result = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId);
                    }
                }

                return(GetOutputVectorItem(0, outputId, result));
            }
            else
            {
                if (m_inputPorts[0].IsConnected)
                {
                    string inputTangent = m_inputPorts[0].GeneratePortInstructions(ref dataCollector);

                    dataCollector.AddToVertexLocalVariables(UniqueId, "float3 normalWorld = UnityObjectToWorldNormal( " + Constants.VertexShaderInputStr + ".normal );");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "float4 tangentWorld = float4( UnityObjectToWorldDir( " + Constants.VertexShaderInputStr + ".tangent.xyz ), " + Constants.VertexShaderInputStr + ".tangent.w );");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "float3x3 tangentToWorld = CreateTangentToWorldPerVertex( normalWorld, tangentWorld.xyz, tangentWorld.w );");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "float3 tangentNormal" + OutputId + " = " + inputTangent + ";");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "float3 modWorldtangent" + OutputId + " = (tangentToWorld[0] * tangentNormal" + OutputId + ".x + tangentToWorld[1] * tangentNormal" + OutputId + ".y + tangentToWorld[2] * tangentNormal" + OutputId + ".z);");
                    return(GetOutputVectorItem(0, outputId, "modWorldtangent" + OutputId));
                }
                else
                {
                    dataCollector.AddToVertexLocalVariables(UniqueId, "float3 normalWorld = UnityObjectToWorldNormal( " + Constants.VertexShaderInputStr + ".normal );");
                    return(GetOutputVectorItem(0, outputId, "normalWorld"));
                    //if ( m_outputPorts[ 0 ].IsLocalValue )
                    //	return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue );

                    //RegisterLocalVariable( 0, string.Format( "UnityObjectToWorldNormal( {0}.normal )", Constants.VertexShaderInputStr ), ref dataCollector, "normalWorld" );

                    //return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue );
                }
                //half3 worldNormal = UnityObjectToWorldNormal( v.normal );
            }
        }
예제 #12
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (dataCollector.IsTemplate && dataCollector.IsFragmentCategory)
            {
                dataCollector.AddToIncludes(UniqueId, Constants.UnityLightingLib);

                string texcoord1 = string.Empty;
                string texcoord2 = string.Empty;

                if (dataCollector.TemplateDataCollectorInstance.HasInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES1, false, MasterNodePortCategory.Vertex))
                {
                    texcoord1 = dataCollector.TemplateDataCollectorInstance.GetInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES1, false, MasterNodePortCategory.Vertex).VarName;
                }
                else
                {
                    texcoord1 = dataCollector.TemplateDataCollectorInstance.RegisterInfoOnSemantic(MasterNodePortCategory.Vertex, TemplateInfoOnSematics.TEXTURE_COORDINATES1, TemplateSemantics.TEXCOORD1, "texcoord1", WirePortDataType.FLOAT4, PrecisionType.Float, false);
                }

                if (dataCollector.TemplateDataCollectorInstance.HasInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES2, false, MasterNodePortCategory.Vertex))
                {
                    texcoord2 = dataCollector.TemplateDataCollectorInstance.GetInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES2, false, MasterNodePortCategory.Vertex).VarName;
                }
                else
                {
                    texcoord2 = dataCollector.TemplateDataCollectorInstance.RegisterInfoOnSemantic(MasterNodePortCategory.Vertex, TemplateInfoOnSematics.TEXTURE_COORDINATES2, TemplateSemantics.TEXCOORD2, "texcoord2", WirePortDataType.FLOAT4, PrecisionType.Float, false);
                }

                string             vOutName = dataCollector.TemplateDataCollectorInstance.CurrentTemplateData.VertexFunctionData.OutVarName;
                string             fInName  = dataCollector.TemplateDataCollectorInstance.CurrentTemplateData.FragmentFunctionData.InVarName;
                TemplateVertexData data     = dataCollector.TemplateDataCollectorInstance.RequestNewInterpolator(WirePortDataType.FLOAT4, false, "ase_lmap");

                string varName = "ase_lmap";
                if (data != null)
                {
                    varName = data.VarName;
                }

                dataCollector.AddToVertexLocalVariables(UniqueId, "#ifdef DYNAMICLIGHTMAP_ON //dynlm");
                dataCollector.AddToVertexLocalVariables(UniqueId, vOutName + "." + varName + ".zw = " + texcoord2 + ".xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;");
                dataCollector.AddToVertexLocalVariables(UniqueId, "#endif //dynlm");
                dataCollector.AddToVertexLocalVariables(UniqueId, "#ifdef LIGHTMAP_ON //stalm");
                dataCollector.AddToVertexLocalVariables(UniqueId, vOutName + "." + varName + ".xy = " + texcoord1 + ".xy * unity_LightmapST.xy + unity_LightmapST.zw;");
                dataCollector.AddToVertexLocalVariables(UniqueId, "#endif //stalm");

                TemplateVertexData shdata      = dataCollector.TemplateDataCollectorInstance.RequestNewInterpolator(WirePortDataType.FLOAT3, false, "ase_sh");
                string             worldPos    = dataCollector.TemplateDataCollectorInstance.GetWorldPos(false, MasterNodePortCategory.Vertex);
                string             worldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(PrecisionType.Float, false, MasterNodePortCategory.Vertex);
                //Debug.Log( shdata );
                string shVarName = "ase_sh";
                if (shdata != null)
                {
                    shVarName = shdata.VarName;
                }
                string outSH = vOutName + "." + shVarName + ".xyz";
                dataCollector.AddToVertexLocalVariables(UniqueId, "#ifndef LIGHTMAP_ON //nstalm");
                dataCollector.AddToVertexLocalVariables(UniqueId, "#if UNITY_SHOULD_SAMPLE_SH //sh");
                dataCollector.AddToVertexLocalVariables(UniqueId, outSH + " = 0;");
                dataCollector.AddToVertexLocalVariables(UniqueId, "#ifdef VERTEXLIGHT_ON //vl");
                dataCollector.AddToVertexLocalVariables(UniqueId, outSH + " += Shade4PointLights (");
                dataCollector.AddToVertexLocalVariables(UniqueId, "unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,");
                dataCollector.AddToVertexLocalVariables(UniqueId, "unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,");
                dataCollector.AddToVertexLocalVariables(UniqueId, "unity_4LightAtten0, " + worldPos + ", " + worldNormal + ");");
                dataCollector.AddToVertexLocalVariables(UniqueId, "#endif //vl");
                dataCollector.AddToVertexLocalVariables(UniqueId, outSH + " = ShadeSHPerVertex (" + worldNormal + ", " + outSH + ");");
                dataCollector.AddToVertexLocalVariables(UniqueId, "#endif //sh");
                dataCollector.AddToVertexLocalVariables(UniqueId, "#endif //nstalm");

                string fragWorldNormal = string.Empty;
                if (m_inputPorts[0].IsConnected)
                {
                    if (m_normalSpace == ViewSpace.Tangent)
                    {
                        fragWorldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(UniqueId, m_currentPrecisionType, m_inputPorts[0].GeneratePortInstructions(ref dataCollector), OutputId);
                    }
                    else
                    {
                        fragWorldNormal = m_inputPorts[0].GeneratePortInstructions(ref dataCollector);
                    }
                }
                else
                {
                    fragWorldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(PrecisionType.Float, false, MasterNodePortCategory.Fragment);
                }

                dataCollector.AddLocalVariable(UniqueId, "UnityGIInput data" + OutputId + ";");
                dataCollector.AddLocalVariable(UniqueId, "UNITY_INITIALIZE_OUTPUT( UnityGIInput, data" + OutputId + " );");

                dataCollector.AddLocalVariable(UniqueId, "#if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON) //dylm" + OutputId);
                dataCollector.AddLocalVariable(UniqueId, "data" + OutputId + ".lightmapUV = " + fInName + "." + varName + ";");
                dataCollector.AddLocalVariable(UniqueId, "#endif //dylm" + OutputId);

                dataCollector.AddLocalVariable(UniqueId, "#if UNITY_SHOULD_SAMPLE_SH //fsh" + OutputId);
                dataCollector.AddLocalVariable(UniqueId, "data" + OutputId + ".ambient = " + fInName + "." + shVarName + ";");
                dataCollector.AddLocalVariable(UniqueId, "#endif //fsh" + OutputId);

                dataCollector.AddToLocalVariables(UniqueId, "UnityGI gi" + OutputId + " = UnityGI_Base(data" + OutputId + ", 1, " + fragWorldNormal + ");");

                return("gi" + OutputId + ".indirect.diffuse");
            }

            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 + " )";
                }
            }
            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);
            }


            if (dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation)
            {
                dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, "indirectDiffuse" + OutputId, "ShadeSH9( float4( " + normal + ", 1 ) )");
            }
            else
            {
                dataCollector.AddLocalVariable(UniqueId, "UnityGI gi" + OutputId + " = gi;");
                dataCollector.AddLocalVariable(UniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, "diffNorm" + OutputId, normal);
                dataCollector.AddLocalVariable(UniqueId, "gi" + OutputId + " = UnityGI_Base( data, 1, diffNorm" + OutputId + " );");
                dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, "indirectDiffuse" + OutputId, "gi" + OutputId + ".indirect.diffuse + diffNorm" + OutputId + " * 0.0001");
            }

            return("indirectDiffuse" + OutputId);
        }
        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));
            }
            string finalValue = string.Empty;

            if (dataCollector.IsTemplate && dataCollector.IsFragmentCategory)
            {
                if (!dataCollector.IsSRP)
                {
                    dataCollector.AddToIncludes(UniqueId, Constants.UnityLightingLib);

                    string texcoord1 = string.Empty;
                    string texcoord2 = string.Empty;

                    if (dataCollector.TemplateDataCollectorInstance.HasInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES1, false, MasterNodePortCategory.Vertex))
                    {
                        texcoord1 = dataCollector.TemplateDataCollectorInstance.GetInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES1, false, MasterNodePortCategory.Vertex).VarName;
                    }
                    else
                    {
                        texcoord1 = dataCollector.TemplateDataCollectorInstance.RegisterInfoOnSemantic(MasterNodePortCategory.Vertex, TemplateInfoOnSematics.TEXTURE_COORDINATES1, TemplateSemantics.TEXCOORD1, "texcoord1", WirePortDataType.FLOAT4, PrecisionType.Float, false);
                    }

                    if (dataCollector.TemplateDataCollectorInstance.HasInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES2, false, MasterNodePortCategory.Vertex))
                    {
                        texcoord2 = dataCollector.TemplateDataCollectorInstance.GetInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES2, false, MasterNodePortCategory.Vertex).VarName;
                    }
                    else
                    {
                        texcoord2 = dataCollector.TemplateDataCollectorInstance.RegisterInfoOnSemantic(MasterNodePortCategory.Vertex, TemplateInfoOnSematics.TEXTURE_COORDINATES2, TemplateSemantics.TEXCOORD2, "texcoord2", WirePortDataType.FLOAT4, PrecisionType.Float, false);
                    }

                    string             vOutName = dataCollector.TemplateDataCollectorInstance.CurrentTemplateData.VertexFunctionData.OutVarName;
                    string             fInName  = dataCollector.TemplateDataCollectorInstance.CurrentTemplateData.FragmentFunctionData.InVarName;
                    TemplateVertexData data     = dataCollector.TemplateDataCollectorInstance.RequestNewInterpolator(WirePortDataType.FLOAT4, false, "ase_lmap");

                    string varName = "ase_lmap";
                    if (data != null)
                    {
                        varName = data.VarName;
                    }

                    dataCollector.AddToVertexLocalVariables(UniqueId, "#ifdef DYNAMICLIGHTMAP_ON //dynlm");
                    dataCollector.AddToVertexLocalVariables(UniqueId, vOutName + "." + varName + ".zw = " + texcoord2 + ".xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "#endif //dynlm");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "#ifdef LIGHTMAP_ON //stalm");
                    dataCollector.AddToVertexLocalVariables(UniqueId, vOutName + "." + varName + ".xy = " + texcoord1 + ".xy * unity_LightmapST.xy + unity_LightmapST.zw;");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "#endif //stalm");

                    TemplateVertexData shdata      = dataCollector.TemplateDataCollectorInstance.RequestNewInterpolator(WirePortDataType.FLOAT3, false, "ase_sh");
                    string             worldPos    = dataCollector.TemplateDataCollectorInstance.GetWorldPos(false, MasterNodePortCategory.Vertex);
                    string             worldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(PrecisionType.Float, false, MasterNodePortCategory.Vertex);
                    //Debug.Log( shdata );
                    string shVarName = "ase_sh";
                    if (shdata != null)
                    {
                        shVarName = shdata.VarName;
                    }
                    string outSH = vOutName + "." + shVarName + ".xyz";
                    dataCollector.AddToVertexLocalVariables(UniqueId, "#ifndef LIGHTMAP_ON //nstalm");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "#if UNITY_SHOULD_SAMPLE_SH //sh");
                    dataCollector.AddToVertexLocalVariables(UniqueId, outSH + " = 0;");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "#ifdef VERTEXLIGHT_ON //vl");
                    dataCollector.AddToVertexLocalVariables(UniqueId, outSH + " += Shade4PointLights (");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "unity_4LightAtten0, " + worldPos + ", " + worldNormal + ");");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "#endif //vl");
                    dataCollector.AddToVertexLocalVariables(UniqueId, outSH + " = ShadeSHPerVertex (" + worldNormal + ", " + outSH + ");");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "#endif //sh");
                    dataCollector.AddToVertexLocalVariables(UniqueId, "#endif //nstalm");

                    //dataCollector.AddToPragmas( UniqueId, "multi_compile_fwdbase" );

                    string fragWorldNormal = string.Empty;
                    if (m_inputPorts[0].IsConnected)
                    {
                        if (m_normalSpace == ViewSpace.Tangent)
                        {
                            fragWorldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(UniqueId, CurrentPrecisionType, m_inputPorts[0].GeneratePortInstructions(ref dataCollector), OutputId);
                        }
                        else
                        {
                            fragWorldNormal = m_inputPorts[0].GeneratePortInstructions(ref dataCollector);
                        }
                    }
                    else
                    {
                        fragWorldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(PrecisionType.Float, false, MasterNodePortCategory.Fragment);
                    }

                    dataCollector.AddLocalVariable(UniqueId, "UnityGIInput data" + OutputId + ";");
                    dataCollector.AddLocalVariable(UniqueId, "UNITY_INITIALIZE_OUTPUT( UnityGIInput, data" + OutputId + " );");

                    dataCollector.AddLocalVariable(UniqueId, "#if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON) //dylm" + OutputId);
                    dataCollector.AddLocalVariable(UniqueId, "data" + OutputId + ".lightmapUV = " + fInName + "." + varName + ";");
                    dataCollector.AddLocalVariable(UniqueId, "#endif //dylm" + OutputId);

                    dataCollector.AddLocalVariable(UniqueId, "#if UNITY_SHOULD_SAMPLE_SH //fsh" + OutputId);
                    dataCollector.AddLocalVariable(UniqueId, "data" + OutputId + ".ambient = " + fInName + "." + shVarName + ";");
                    dataCollector.AddLocalVariable(UniqueId, "#endif //fsh" + OutputId);

                    dataCollector.AddToLocalVariables(UniqueId, "UnityGI gi" + OutputId + " = UnityGI_Base(data" + OutputId + ", 1, " + fragWorldNormal + ");");

                    finalValue = "gi" + OutputId + ".indirect.diffuse";
                    m_outputPorts[0].SetLocalValue(finalValue, dataCollector.PortCategory);
                    return(finalValue);
                }
                else
                {
                    if (dataCollector.CurrentSRPType == TemplateSRPType.Lightweight)
                    {
                        string texcoord1 = string.Empty;

                        if (dataCollector.TemplateDataCollectorInstance.HasInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES1, false, MasterNodePortCategory.Vertex))
                        {
                            texcoord1 = dataCollector.TemplateDataCollectorInstance.GetInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES1, false, MasterNodePortCategory.Vertex).VarName;
                        }
                        else
                        {
                            texcoord1 = dataCollector.TemplateDataCollectorInstance.RegisterInfoOnSemantic(MasterNodePortCategory.Vertex, TemplateInfoOnSematics.TEXTURE_COORDINATES1, TemplateSemantics.TEXCOORD1, "texcoord1", WirePortDataType.FLOAT4, PrecisionType.Float, false);
                        }

                        string vOutName = dataCollector.TemplateDataCollectorInstance.CurrentTemplateData.VertexFunctionData.OutVarName;
                        string fInName  = dataCollector.TemplateDataCollectorInstance.CurrentTemplateData.FragmentFunctionData.InVarName;


                        if (!dataCollector.TemplateDataCollectorInstance.HasRawInterpolatorOfName("lightmapUVOrVertexSH"))
                        {
                            string worldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(PrecisionType.Float, false, MasterNodePortCategory.Vertex);
                            dataCollector.TemplateDataCollectorInstance.RequestNewInterpolator(WirePortDataType.FLOAT4, false, "lightmapUVOrVertexSH");

                            dataCollector.AddToVertexLocalVariables(UniqueId, "OUTPUT_LIGHTMAP_UV( " + texcoord1 + ", unity_LightmapST, " + vOutName + ".lightmapUVOrVertexSH.xy );");
                            dataCollector.AddToVertexLocalVariables(UniqueId, "OUTPUT_SH( " + worldNormal + ", " + vOutName + ".lightmapUVOrVertexSH.xyz );");

                            dataCollector.AddToPragmas(UniqueId, "multi_compile _ DIRLIGHTMAP_COMBINED");
                            dataCollector.AddToPragmas(UniqueId, "multi_compile _ LIGHTMAP_ON");
                        }

                        string fragWorldNormal = string.Empty;
                        if (m_inputPorts[0].IsConnected)
                        {
                            if (m_normalSpace == ViewSpace.Tangent)
                            {
                                fragWorldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(UniqueId, CurrentPrecisionType, m_inputPorts[0].GeneratePortInstructions(ref dataCollector), OutputId);
                            }
                            else
                            {
                                fragWorldNormal = m_inputPorts[0].GeneratePortInstructions(ref dataCollector);
                            }
                        }
                        else
                        {
                            fragWorldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(PrecisionType.Float, false, MasterNodePortCategory.Fragment);
                        }

                        //SAMPLE_GI

                        //This function may not do full pixel and does not behave correctly with given normal thus is commented out
                        //dataCollector.AddLocalVariable( UniqueId, "float3 bakedGI" + OutputId + " = SAMPLE_GI( " + fInName + ".lightmapUVOrVertexSH.xy, " + fInName + ".lightmapUVOrVertexSH.xyz, " + fragWorldNormal + " );" );
                        dataCollector.AddFunction(LWIndirectDiffuseBody[0], LWIndirectDiffuseBody, false);
                        finalValue = "bakedGI" + OutputId;
                        string result = string.Format(LWIndirectDiffuseHeader, fInName + ".lightmapUVOrVertexSH.xy", fragWorldNormal);
                        dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT3, finalValue, result);

                        m_outputPorts[0].SetLocalValue(finalValue, dataCollector.PortCategory);
                        return(finalValue);
                    }
                    else if (dataCollector.CurrentSRPType == TemplateSRPType.HD)
                    {
                        string texcoord1 = string.Empty;
                        string texcoord2 = string.Empty;

                        if (dataCollector.TemplateDataCollectorInstance.HasInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES1, false, MasterNodePortCategory.Vertex))
                        {
                            texcoord1 = dataCollector.TemplateDataCollectorInstance.GetInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES1, false, MasterNodePortCategory.Vertex).VarName;
                        }
                        else
                        {
                            texcoord1 = dataCollector.TemplateDataCollectorInstance.RegisterInfoOnSemantic(MasterNodePortCategory.Vertex, TemplateInfoOnSematics.TEXTURE_COORDINATES1, TemplateSemantics.TEXCOORD1, "texcoord1", WirePortDataType.FLOAT4, PrecisionType.Float, false);
                        }

                        if (dataCollector.TemplateDataCollectorInstance.HasInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES2, false, MasterNodePortCategory.Vertex))
                        {
                            texcoord2 = dataCollector.TemplateDataCollectorInstance.GetInfo(TemplateInfoOnSematics.TEXTURE_COORDINATES2, false, MasterNodePortCategory.Vertex).VarName;
                        }
                        else
                        {
                            texcoord2 = dataCollector.TemplateDataCollectorInstance.RegisterInfoOnSemantic(MasterNodePortCategory.Vertex, TemplateInfoOnSematics.TEXTURE_COORDINATES2, TemplateSemantics.TEXCOORD2, "texcoord2", WirePortDataType.FLOAT4, PrecisionType.Float, false);
                        }

                        dataCollector.TemplateDataCollectorInstance.RequestNewInterpolator(WirePortDataType.FLOAT4, false, "ase_lightmapUVs");

                        string vOutName = dataCollector.TemplateDataCollectorInstance.CurrentTemplateData.VertexFunctionData.OutVarName;
                        string fInName  = dataCollector.TemplateDataCollectorInstance.CurrentTemplateData.FragmentFunctionData.InVarName;

                        dataCollector.AddToVertexLocalVariables(UniqueId, vOutName + ".ase_lightmapUVs.xy = " + texcoord1 + ".xy * unity_LightmapST.xy + unity_LightmapST.zw;");
                        dataCollector.AddToVertexLocalVariables(UniqueId, vOutName + ".ase_lightmapUVs.zw = " + texcoord2 + ".xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;");

                        string worldPos = dataCollector.TemplateDataCollectorInstance.GetWorldPos(false, MasterNodePortCategory.Fragment);

                        dataCollector.AddToPragmas(UniqueId, "multi_compile _ LIGHTMAP_ON");
                        dataCollector.AddToPragmas(UniqueId, "multi_compile _ DIRLIGHTMAP_COMBINED");
                        dataCollector.AddToPragmas(UniqueId, "multi_compile _ DYNAMICLIGHTMAP_ON");

                        string fragWorldNormal = string.Empty;
                        if (m_inputPorts[0].IsConnected)
                        {
                            if (m_normalSpace == ViewSpace.Tangent)
                            {
                                fragWorldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(UniqueId, CurrentPrecisionType, m_inputPorts[0].GeneratePortInstructions(ref dataCollector), OutputId);
                            }
                            else
                            {
                                fragWorldNormal = m_inputPorts[0].GeneratePortInstructions(ref dataCollector);
                            }
                        }
                        else
                        {
                            fragWorldNormal = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(PrecisionType.Float, false, MasterNodePortCategory.Fragment);
                        }

                        //SAMPLE_GI
                        dataCollector.AddLocalVariable(UniqueId, "float3 bakedGI" + OutputId + " = SampleBakedGI( " + worldPos + ", " + fragWorldNormal + ", " + fInName + ".ase_lightmapUVs.xy, " + fInName + ".ase_lightmapUVs.zw );");
                        finalValue = "bakedGI" + OutputId;
                        m_outputPorts[0].SetLocalValue(finalValue, dataCollector.PortCategory);
                        return(finalValue);
                    }
                }
            }
            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, 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 + " )";
                }
            }
            else
            {
                if (dataCollector.IsFragmentCategory)
                {
                    dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, CurrentPrecisionType);
                    if (dataCollector.DirtyNormal)
                    {
                        dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false);
                        dataCollector.ForceNormal = true;
                    }
                }

                normal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId);
            }


            if (dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation)
            {
                dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT3, "indirectDiffuse" + OutputId, "ShadeSH9( float4( " + normal + ", 1 ) )");
            }
            else
            {
                dataCollector.AddLocalVariable(UniqueId, "UnityGI gi" + OutputId + " = gi;");
                dataCollector.AddLocalVariable(UniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, "diffNorm" + OutputId, normal);
                dataCollector.AddLocalVariable(UniqueId, "gi" + OutputId + " = UnityGI_Base( data, 1, diffNorm" + OutputId + " );");
                dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT3, "indirectDiffuse" + OutputId, "gi" + OutputId + ".indirect.diffuse + diffNorm" + OutputId + " * 0.0001");
            }

            finalValue = "indirectDiffuse" + OutputId;
            m_outputPorts[0].SetLocalValue(finalValue, dataCollector.PortCategory);
            return(finalValue);
        }
        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, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_REFL), true);

                string result = string.Empty;
                if (m_inputPorts[0].IsConnected)
                {
                    dataCollector.AddToInput(UniqueId, Constants.InternalData, 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, Constants.InternalData, 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 );
            }
        }
예제 #15
0
        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);
        }
예제 #16
0
        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_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));
        }
예제 #18
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar)
        {
            if (dataCollector.IsTemplate)
            {
                if (m_inputPorts[0].IsConnected)
                {
                    if (m_outputPorts[0].IsLocalValue(dataCollector.PortCategory))
                    {
                        return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
                    }


                    string value = dataCollector.TemplateDataCollectorInstance.GetWorldReflection(CurrentPrecisionType, m_inputPorts[0].GeneratePortInstructions(ref dataCollector));
                    if (m_normalize)
                    {
                        value = string.Format(NormalizeFunc, value);
                    }
                    RegisterLocalVariable(0, value, ref dataCollector, "worldRefl" + OutputId);
                    return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
                }
                else
                {
                    string name;
                    string value = dataCollector.TemplateDataCollectorInstance.GetWorldReflection(CurrentPrecisionType);
                    if (m_normalize)
                    {
                        name  = "normalizedWorldRefl";
                        value = string.Format(NormalizeFunc, value);
                        RegisterLocalVariable(0, value, ref dataCollector, name);
                    }
                    else
                    {
                        name = value;
                    }
                    return(GetOutputVectorItem(0, outputId, name));
                }
            }

            bool isVertex = (dataCollector.PortCategory == MasterNodePortCategory.Tessellation || dataCollector.PortCategory == MasterNodePortCategory.Vertex);

            if (isVertex)
            {
                if (m_inputPorts[0].IsConnected)
                {
                    if (m_outputPorts[0].IsLocalValue(dataCollector.PortCategory))
                    {
                        return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
                    }

                    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);");

                    string value = "reflect( -" + viewDir + ", modWorldNormal" + OutputId + " )";
                    if (m_normalize)
                    {
                        value = string.Format(NormalizeFunc, value);
                    }

                    RegisterLocalVariable(0, value, ref dataCollector, "modReflection" + OutputId);
                    return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
                }
                else
                {
                    if (m_outputPorts[0].IsLocalValue(dataCollector.PortCategory))
                    {
                        return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
                    }

                    string worldNormal = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId);
                    string viewDir     = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId);

                    string value = "reflect( -" + viewDir + ", " + worldNormal + " )";
                    if (m_normalize)
                    {
                        value = string.Format(NormalizeFunc, value);
                    }
                    RegisterLocalVariable(0, value, ref dataCollector, ReflectionVecValStr + OutputId);
                    return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
                }
            }
            else
            {
                if (m_outputPorts[0].IsLocalValue(dataCollector.PortCategory))
                {
                    return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
                }

                dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_REFL, 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].GeneratePortInstructions(ref dataCollector) + " )";
                    if (m_normalize)
                    {
                        result = String.Format(NormalizeFunc, result);
                    }
                    int connCount = 0;
                    for (int i = 0; i < m_outputPorts.Count; i++)
                    {
                        connCount += m_outputPorts[i].ConnectionCount;
                    }

                    if (connCount > 1)
                    {
                        string precisionType = UIUtils.PrecisionWirePortToCgType(UIUtils.CurrentWindow.CurrentGraph.CurrentPrecision, WirePortDataType.FLOAT3);

                        dataCollector.AddToFragmentLocalVariables(UniqueId, string.Format(ReflectionVecDecStr, precisionType, ReflectionVecValStr + OutputId, result));
                        RegisterLocalVariable(0, result, ref dataCollector, ReflectionVecValStr + OutputId);
                        return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
                    }
                }
                else
                {
                    dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false);
                    result = GeneratorUtils.GenerateWorldReflection(ref dataCollector, UniqueId, m_normalize);
                    if (dataCollector.DirtyNormal)
                    {
                        dataCollector.ForceNormal = true;
                    }
                }

                return(GetOutputVectorItem(0, outputId, result));
                //RegisterLocalVariable( 0, result, ref dataCollector, "worldrefVec" + OutputId );
                //return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue );
            }
        }
예제 #19
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            //ConfigureFunctions();
            dataCollector.AddPropertyNode(m_topTexture);
            dataCollector.AddPropertyNode(m_midTexture);
            dataCollector.AddPropertyNode(m_botTexture);

            bool isVertex = (dataCollector.PortCategory == MasterNodePortCategory.Tessellation || dataCollector.PortCategory == MasterNodePortCategory.Vertex);

            string texTop = string.Empty;
            string texMid = string.Empty;
            string texBot = string.Empty;

            if (m_inputPorts[0].IsConnected)
            {
                texTop = m_inputPorts[0].GeneratePortInstructions(ref dataCollector);
            }
            else
            {
                dataCollector.AddToUniforms(m_uniqueId, m_topTexture.GetTexture2DUniformValue());
                dataCollector.AddToProperties(m_uniqueId, m_topTexture.GetTexture2DPropertyValue(), m_topTexture.OrderIndex);
                texTop = m_topTexture.PropertyName;
            }

            if (m_selectedTriplanarType == TriplanarType.Spherical)
            {
                texMid = texTop;
                texBot = texTop;
            }
            else
            {
                if (m_inputPorts[1].IsConnected)
                {
                    texMid = m_inputPorts[1].GeneratePortInstructions(ref dataCollector);
                }
                else
                {
                    dataCollector.AddToUniforms(m_uniqueId, m_midTexture.GetTexture2DUniformValue());
                    dataCollector.AddToProperties(m_uniqueId, m_midTexture.GetTexture2DPropertyValue(), m_midTexture.OrderIndex);
                    texMid = m_midTexture.PropertyName;
                }

                if (m_inputPorts[2].IsConnected)
                {
                    texBot = m_inputPorts[2].GeneratePortInstructions(ref dataCollector);
                }
                else
                {
                    dataCollector.AddToUniforms(m_uniqueId, m_botTexture.GetTexture2DUniformValue());
                    dataCollector.AddToProperties(m_uniqueId, m_botTexture.GetTexture2DPropertyValue(), m_botTexture.OrderIndex);
                    texBot = m_botTexture.PropertyName;
                }
            }

            dataCollector.AddToInput(m_uniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_POS), true);
            dataCollector.AddToInput(m_uniqueId, UIUtils.GetInputDeclarationFromType(m_currentPrecisionType, AvailableSurfaceInputs.WORLD_NORMAL), true);
            string tilling = m_inputPorts[3].GeneratePortInstructions(ref dataCollector);
            string falloff = m_inputPorts[4].GeneratePortInstructions(ref dataCollector);

            dataCollector.ForceNormal = true;

            dataCollector.AddToInput(m_uniqueId, Constants.InternalData, false);

            if (m_normalCorrection)
            {
                string worldToTangent = GeneratorUtils.GenerateWorldToTangentMatrix(ref dataCollector, m_uniqueId, m_currentPrecisionType);

                string pos  = GeneratorUtils.GenerateWorldPosition(ref dataCollector, m_uniqueId);
                string norm = GeneratorUtils.GenerateWorldNormal(ref dataCollector, m_uniqueId);
                if (m_selectedTriplanarSpace == TriplanarSpace.Object)
                {
                    dataCollector.AddLocalVariable(m_uniqueId, "float3 localTangent = mul( unity_WorldToObject, float4( " + GeneratorUtils.WorldTangentStr + ", 0 ) );");
                    dataCollector.AddLocalVariable(m_uniqueId, "float3 localBitangent = mul( unity_WorldToObject, float4( " + GeneratorUtils.WorldBitangentStr + ", 0 ) );");
                    dataCollector.AddLocalVariable(m_uniqueId, "float3 localNormal = mul( unity_WorldToObject, float4( " + GeneratorUtils.WorldNormalStr + ", 0 ) );");
                    norm = "localNormal";
                    dataCollector.AddLocalVariable(m_uniqueId, "float3x3 objectToTangent = float3x3(localTangent, localBitangent, localNormal);");
                    dataCollector.AddLocalVariable(m_uniqueId, "float3 localPos = mul( unity_WorldToObject, float4( " + pos + ", 1 ) );");
                    pos            = "localPos";
                    worldToTangent = "objectToTangent";
                }

                string normalTriplanar = string.Empty;
                IOUtils.AddFunctionHeader(ref normalTriplanar, m_functionNormalHeader);
                if (m_selectedTriplanarType == TriplanarType.Spherical)
                {
                    for (int i = 0; i < m_functionNormalBody.Length; i++)
                    {
                        IOUtils.AddFunctionLine(ref normalTriplanar, m_functionNormalBody[i]);
                    }
                }
                else
                {
                    for (int i = 0; i < m_functionNormalBodyTMB.Length; i++)
                    {
                        IOUtils.AddFunctionLine(ref normalTriplanar, m_functionNormalBodyTMB[i]);
                    }
                }
                IOUtils.CloseFunctionBody(ref normalTriplanar);

                string call = dataCollector.AddFunctions(m_functionNormalCall, normalTriplanar, texTop, texMid, texBot, pos, norm, falloff, tilling, (isVertex ? "1" : "0"));
                dataCollector.AddToLocalVariables(dataCollector.PortCategory, m_uniqueId, "float3 worldTriplanarNormal" + m_uniqueId + " = " + call + ";");
                dataCollector.AddToLocalVariables(dataCollector.PortCategory, m_uniqueId, "float3 tanTriplanarNormal" + m_uniqueId + " = mul( " + worldToTangent + ", worldTriplanarNormal" + m_uniqueId + " );");
                return(GetOutputVectorItem(0, outputId, "tanTriplanarNormal" + m_uniqueId));
            }
            else
            {
                string samplingTriplanar = string.Empty;
                IOUtils.AddFunctionHeader(ref samplingTriplanar, m_functionSamplingHeader);
                if (m_selectedTriplanarType == TriplanarType.Spherical)
                {
                    for (int i = 0; i < m_functionSamplingBody.Length; i++)
                    {
                        IOUtils.AddFunctionLine(ref samplingTriplanar, m_functionSamplingBody[i]);
                    }
                }
                else
                {
                    for (int i = 0; i < m_functionSamplingBodyTMB.Length; i++)
                    {
                        IOUtils.AddFunctionLine(ref samplingTriplanar, m_functionSamplingBodyTMB[i]);
                    }
                }
                IOUtils.CloseFunctionBody(ref samplingTriplanar);

                string pos  = GeneratorUtils.GenerateWorldPosition(ref dataCollector, m_uniqueId);
                string norm = GeneratorUtils.GenerateWorldNormal(ref dataCollector, m_uniqueId);
                if (m_selectedTriplanarSpace == TriplanarSpace.Object)
                {
                    dataCollector.AddToLocalVariables(dataCollector.PortCategory, m_uniqueId, "float3 localPos = mul( unity_WorldToObject, float4( " + pos + ", 1 ) );");
                    pos = "localPos";
                    dataCollector.AddToLocalVariables(dataCollector.PortCategory, m_uniqueId, "float3 localNormal = mul( unity_WorldToObject, float4( " + norm + ", 0 ) );");
                    norm = "localNormal";
                }

                string call = dataCollector.AddFunctions(m_functionSamplingCall, samplingTriplanar, texTop, texMid, texBot, pos, norm, falloff, tilling, (isVertex ? "1" : "0"));
                dataCollector.AddToLocalVariables(dataCollector.PortCategory, m_uniqueId, "float4 triplanar" + m_uniqueId + " = " + call + ";");
                return(GetOutputVectorItem(0, outputId, "triplanar" + m_uniqueId));
            }
        }
        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));
        }