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 (m_referenceType == TexReferenceType.Instance)
            {
                return(GenerateInstancedShaderForOutput(outputId, ref dataCollector, ignoreLocalvar));
            }

            string dataType = UIUtils.FinalPrecisionWirePortToCgType(m_currentPrecisionType, AvailableTypesValues[m_type]);

            InputPort indexXPort       = GetInputPortByUniqueId(0);
            InputPort arrayLengthXPort = GetInputPortByUniqueId(1);
            string    result           = string.Empty;

            if (m_isJagged)
            {
                InputPort indexYPort       = GetInputPortByUniqueId(2);
                InputPort arrayLengthYPort = GetInputPortByUniqueId(3);

                string arrayIndexX  = indexXPort.IsConnected ? indexXPort.GeneratePortInstructions(ref dataCollector) : m_indexX.ToString();
                string arrayLengthX = arrayLengthXPort.IsConnected ? arrayLengthXPort.GeneratePortInstructions(ref dataCollector) : m_arrayLengthX.ToString();

                string arrayIndexY  = indexYPort.IsConnected ? indexYPort.GeneratePortInstructions(ref dataCollector) : m_indexY.ToString();
                string arrayLengthY = arrayLengthYPort.IsConnected ? arrayLengthYPort.GeneratePortInstructions(ref dataCollector) : m_arrayLengthY.ToString();

                dataCollector.AddToUniforms(UniqueId, dataType, string.Format(JaggedArrayFormatStr, m_name, arrayLengthX, arrayLengthY));
                if (m_autoRangeCheck)
                {
                    arrayIndexX = string.Format("clamp({0},0,({1} - 1))", arrayIndexX, arrayLengthX);
                    arrayIndexY = string.Format("clamp({0},0,({1} - 1))", arrayIndexY, arrayLengthY);
                }
                result = string.Format(JaggedArrayFormatStr, m_name, arrayIndexX, arrayIndexY);
            }
            else
            {
                string arrayIndex  = indexXPort.IsConnected ? indexXPort.GeneratePortInstructions(ref dataCollector) : m_indexX.ToString();
                string arrayLength = arrayLengthXPort.IsConnected ? arrayLengthXPort.GeneratePortInstructions(ref dataCollector) : m_arrayLengthX.ToString();


                dataCollector.AddToUniforms(UniqueId, dataType, string.Format(ArrayFormatStr, m_name, arrayLength));

                if (m_autoRangeCheck)
                {
                    arrayIndex = string.Format("clamp({0},0,({1} - 1))", arrayIndex, arrayLength);
                }

                result = string.Format(ArrayFormatStr, m_name, arrayIndex);
            }

            m_outputPorts[0].SetLocalValue(result, dataCollector.PortCategory);
            return(m_outputPorts[0].LocalValue(dataCollector.PortCategory));
        }
Exemple #2
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (!m_outputPorts[0].IsLocalValue(dataCollector.PortCategory))
            {
                string propertyOrOptions = string.Empty;
                if (m_texPort.IsConnected)
                {
                    propertyOrOptions = m_texPort.GeneratePortInstructions(ref dataCollector);
                }
                else if (m_referenceNode != null)
                {
                    m_referenceNode.BaseGenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar);
                    propertyOrOptions = m_referenceNode.PropertyName;
                }
                else
                {
                    propertyOrOptions = GenerateSamplerAttributes();
                }

                string sampler = GeneratorUtils.GenerateSamplerState(ref dataCollector, UniqueId, propertyOrOptions);

                m_outputPorts[0].SetLocalValue(sampler, dataCollector.PortCategory);
            }

            return(m_outputPorts[outputId].LocalValue(dataCollector.PortCategory));
        }
Exemple #3
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar);
            switch (dataCollector.CurrentSRPType)
            {
            case TemplateSRPType.BuiltIn:
            {
                InputPort port = UIUtils.CurrentWindow.OutsideGraph.IsStandardSurface ? GetInputPortByUniqueId(0) : GetInputPortByUniqueId(3);
                return(port.GeneratePortInstructions(ref dataCollector));
            }

            case TemplateSRPType.Lightweight:
            {
                InputPort port = GetInputPortByUniqueId(1);
                return(port.GeneratePortInstructions(ref dataCollector));
            }

            case TemplateSRPType.HD:
            {
                InputPort port = GetInputPortByUniqueId(2);
                return(port.GeneratePortInstructions(ref dataCollector));
            }
            }

            return("0");
        }
        public string GenerateInstancedShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            string result = string.Empty;

            if (m_referenceNode != null)
            {
                InputPort indexXPort = GetInputPortByUniqueId(0);
                if (m_referenceNode.IsJagged)
                {
                    InputPort indexYPort  = GetInputPortByUniqueId(2);
                    string    arrayIndexX = indexXPort.IsConnected ? indexXPort.GeneratePortInstructions(ref dataCollector) : m_indexX.ToString();
                    string    arrayIndexY = indexYPort.IsConnected ? indexYPort.GeneratePortInstructions(ref dataCollector) : m_indexY.ToString();
                    result = m_referenceNode.GetArrayValue(arrayIndexX, arrayIndexY);
                }
                else
                {
                    string arrayIndexX = indexXPort.IsConnected ? indexXPort.GeneratePortInstructions(ref dataCollector) : m_indexX.ToString();
                    result = m_referenceNode.GetArrayValue(arrayIndexX);
                }
            }
            m_outputPorts[0].SetLocalValue(result, dataCollector.PortCategory);
            return(result);
        }
Exemple #5
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (m_outputPorts[0].IsLocalValue)
            {
                return(m_outputPorts[0].LocalValue);
            }

            InputPort vertexPort = GetInputPortByUniqueId(2);
            InputPort lengthPort = GetInputPortByUniqueId(0);
            InputPort offsetPort = GetInputPortByUniqueId(1);

            string distance = lengthPort.GeneratePortInstructions(ref dataCollector);
            string offset   = offsetPort.GeneratePortInstructions(ref dataCollector);

            string value    = string.Empty;
            string eyeDepth = string.Empty;

            if (dataCollector.IsTemplate)
            {
                if (vertexPort.IsConnected)
                {
                    string varName = "customSurfaceDepth" + OutputId;
                    GenerateInputInVertex(ref dataCollector, 2, varName, false);

                    string formatStr = string.Empty;
                    if (dataCollector.TemplateDataCollectorInstance.CurrentSRPType == TemplateSRPType.Lightweight)
                    {
                        formatStr = "-TransformWorldToView(TransformObjectToWorld({0})).z";
                    }
                    else
                    {
                        formatStr = "-UnityObjectToViewPos({0}).z";
                    }

                    string eyeInstruction = string.Format(formatStr, varName);
                    eyeDepth = "customEye" + OutputId;
                    dataCollector.TemplateDataCollectorInstance.RegisterCustomInterpolatedData(eyeDepth, WirePortDataType.FLOAT, m_currentPrecisionType, eyeInstruction);
                }
                else
                {
                    eyeDepth = dataCollector.TemplateDataCollectorInstance.GetEyeDepth(m_currentPrecisionType);
                }

                value = string.Format(CameraDepthFadeFormat, eyeDepth, offset, distance);
                RegisterLocalVariable(0, value, ref dataCollector, "cameraDepthFade" + OutputId);
                return(m_outputPorts[0].LocalValue);
            }

            if (dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation)
            {
                string vertexVarName = string.Empty;
                if (vertexPort.IsConnected)
                {
                    vertexVarName = vertexPort.GeneratePortInstructions(ref dataCollector);
                }
                else
                {
                    vertexVarName = Constants.VertexShaderInputStr + ".vertex.xyz";
                }

                //dataCollector.AddVertexInstruction( "float cameraDepthFade" + UniqueId + " = (( -UnityObjectToViewPos( " + Constants.VertexShaderInputStr + ".vertex.xyz ).z -_ProjectionParams.y - " + offset + " ) / " + distance + ");", UniqueId );
                value = string.Format(CameraDepthFadeFormat, "-UnityObjectToViewPos( " + vertexVarName + " ).z", offset, distance);
                RegisterLocalVariable(0, value, ref dataCollector, "cameraDepthFade" + OutputId);
                return(m_outputPorts[0].LocalValue);
            }

            dataCollector.AddToIncludes(UniqueId, Constants.UnityShaderVariables);

            if (dataCollector.TesselationActive)
            {
                if (vertexPort.IsConnected)
                {
                    string vertexValue = vertexPort.GeneratePortInstructions(ref dataCollector);
                    eyeDepth = "customSurfaceDepth" + OutputId;
                    RegisterLocalVariable(0, string.Format("-UnityObjectToViewPos( {0} ).z", vertexValue), ref dataCollector, eyeDepth);
                }
                else
                {
                    eyeDepth = GeneratorUtils.GenerateScreenDepthOnFrag(ref dataCollector, UniqueId, m_currentPrecisionType);
                }
            }
            else
            {
                if (vertexPort.IsConnected)
                {
                    string varName = "customSurfaceDepth" + OutputId;
                    GenerateInputInVertex(ref dataCollector, 2, varName, false);
                    dataCollector.AddToInput(UniqueId, varName, WirePortDataType.FLOAT);
                    string vertexInstruction = "-UnityObjectToViewPos( " + varName + " ).z";
                    dataCollector.AddVertexInstruction(Constants.VertexShaderOutputStr + "." + varName + " = " + vertexInstruction, UniqueId);
                    eyeDepth = Constants.InputVarStr + "." + varName;
                }
                else
                {
                    dataCollector.AddToInput(UniqueId, "eyeDepth", WirePortDataType.FLOAT);
                    string instruction = "-UnityObjectToViewPos( " + Constants.VertexShaderInputStr + ".vertex.xyz ).z";
                    dataCollector.AddVertexInstruction(Constants.VertexShaderOutputStr + ".eyeDepth = " + instruction, UniqueId);
                    eyeDepth = Constants.InputVarStr + ".eyeDepth";
                }
            }

            value = string.Format(CameraDepthFadeFormat, eyeDepth, offset, distance);
            RegisterLocalVariable(0, value, ref dataCollector, "cameraDepthFade" + OutputId);
            //dataCollector.AddToLocalVariables( UniqueId, "float cameraDepthFade" + UniqueId + " = (( " + Constants.InputVarStr + ".eyeDepth -_ProjectionParams.y - "+ offset + " ) / " + distance + ");" );

            return(m_outputPorts[0].LocalValue);
        }
Exemple #6
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));
        }
Exemple #7
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (dataCollector.IsTemplate)
            {
                return("0");
            }

            if (dataCollector.IsFragmentCategory)
            {
                UIUtils.ShowMessage(ErrorMessage);
                return("0");
            }
            if (m_outputPorts[0].IsLocalValue(dataCollector.PortCategory))
            {
                return("0");
            }

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

            StandardSurfaceOutputNode masterNode = m_containerGraph.CurrentMasterNode as StandardSurfaceOutputNode;

            MasterNodeDataCollector outlineDataCollector = new MasterNodeDataCollector();

            outlineDataCollector.IsOutlineDataCollector = true;
            outlineDataCollector.DirtyNormal            = true;
            InputPort colorPort  = GetInputPortByUniqueId(0);
            InputPort alphaPort  = GetInputPortByUniqueId(2);
            InputPort vertexPort = GetInputPortByUniqueId(1);

            if (vertexPort.IsConnected)
            {
                outlineDataCollector.PortCategory = MasterNodePortCategory.Vertex;
                string outlineWidth = vertexPort.GenerateShaderForOutput(ref outlineDataCollector, vertexPort.DataType, true, true);
                outlineDataCollector.AddToVertexLocalVariables(UniqueId, PrecisionType.Float, vertexPort.DataType, "outlineVar", outlineWidth);

                outlineDataCollector.AddVertexInstruction(outlineDataCollector.SpecialLocalVariables, UniqueId, false);
                outlineDataCollector.ClearSpecialLocalVariables();

                outlineDataCollector.AddVertexInstruction(outlineDataCollector.VertexLocalVariables, UniqueId, false);
                outlineDataCollector.ClearVertexLocalVariables();

                // need to check whether this breaks other outputs or not
                ContainerGraph.ResetNodesLocalVariables();
            }

            outlineDataCollector.PortCategory = MasterNodePortCategory.Fragment;
            string outlineColor = colorPort.GeneratePortInstructions(ref outlineDataCollector);              // "\to.Emission = " + colorPort.GeneratePortInstructions( ref outlineDataCollector ) + ";";
            string alphaValue   = alphaPort.Visible ? alphaPort.GeneratePortInstructions(ref outlineDataCollector) : string.Empty;

            bool addTabs = outlineDataCollector.DirtySpecialLocalVariables || alphaPort.Available;

            outlineDataCollector.AddInstructions("\t" + outlineDataCollector.SpecialLocalVariables.TrimStart('\t'));
            outlineDataCollector.ClearSpecialLocalVariables();
            outlineDataCollector.AddInstructions((addTabs ? "\t\t\t" : "") + "o.Emission = " + outlineColor + ";");
            if (alphaPort.Visible)
            {
                if (m_currentAlphaMode == OutlineAlphaModes.Masked)
                {
                    float maskClipValue = 0.5f;

                    if (masterNode != null)
                    {
                        maskClipValue = masterNode.OpacityMaskClipValue;
                    }

                    if (masterNode.InlineOpacityMaskClipValue.IsValid)
                    {
                        RangedFloatNode fnode = UIUtils.GetNode(masterNode.InlineOpacityMaskClipValue.NodeId) as RangedFloatNode;
                        if (fnode != null)
                        {
                            outlineDataCollector.AddToProperties(fnode.UniqueId, fnode.GetPropertyValue(), fnode.OrderIndex);
                            outlineDataCollector.AddToUniforms(fnode.UniqueId, fnode.GetUniformValue());
                        }
                        else
                        {
                            IntNode inode = UIUtils.GetNode(masterNode.InlineOpacityMaskClipValue.NodeId) as IntNode;
                            outlineDataCollector.AddToProperties(inode.UniqueId, inode.GetPropertyValue(), inode.OrderIndex);
                            outlineDataCollector.AddToUniforms(inode.UniqueId, inode.GetUniformValue());
                        }
                    }
                    else
                    {
                        outlineDataCollector.AddToProperties(-1, string.Format(IOUtils.MaskClipValueProperty, OpacityMaskClipValueStr, maskClipValue), -1);
                        outlineDataCollector.AddToUniforms(-1, string.Format(IOUtils.MaskClipValueUniform, maskClipValue));
                    }

                    outlineDataCollector.AddInstructions((addTabs ? "\n\t\t\t" : "") + "clip( " + alphaValue + " - " + masterNode.InlineOpacityMaskClipValue.GetValueOrProperty(IOUtils.MaskClipValueName, false) + " );");
                }
                else
                {
                    outlineDataCollector.AddInstructions((addTabs ? "\n\t\t\t" : "") + "o.Alpha = " + alphaValue + ";");
                }
            }

            if (outlineDataCollector.UsingWorldNormal)
            {
                outlineDataCollector.AddInstructions((addTabs ? "\n\t\t\t" : "") + "o.Normal = float3(0,0,-1);");
            }

            if (masterNode != null)
            {
                //masterNode.AdditionalIncludes.AddToDataCollector( ref outlineDataCollector );
                //masterNode.AdditionalPragmas.AddToDataCollector( ref outlineDataCollector );
                //masterNode.AdditionalDefines.AddToDataCollector( ref outlineDataCollector );
                masterNode.AdditionalDirectives.AddAllToDataCollector(ref outlineDataCollector);
            }

            ContainerGraph.CurrentStandardSurface.OutlineHelper.InputList       = outlineDataCollector.InputList;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.Inputs          = outlineDataCollector.Inputs;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.DirtyInput      = outlineDataCollector.DirtyInputs;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.Includes        = outlineDataCollector.Includes;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.Pragmas         = outlineDataCollector.Pragmas;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.Defines         = outlineDataCollector.Defines;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.Uniforms        = outlineDataCollector.Uniforms;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.UniformList     = outlineDataCollector.UniformsList;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.VertexData      = outlineDataCollector.VertexData;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.Instructions    = outlineDataCollector.Instructions;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.Functions       = outlineDataCollector.Functions;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.LocalFunctions  = outlineDataCollector.LocalFunctions;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.OutlineCullMode = m_cullMode;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.ZTestMode       = m_zTestMode;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.ZWriteMode      = m_zWriteMode;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.OffsetMode      = m_currentSelectedMode;
            ContainerGraph.CurrentStandardSurface.OutlineHelper.CustomNoFog     = m_noFog;
            dataCollector.CustomOutlineSelectedAlpha = (int)m_currentAlphaMode;

            for (int i = 0; i < outlineDataCollector.PropertiesList.Count; i++)
            {
                dataCollector.AddToProperties(UniqueId, outlineDataCollector.PropertiesList[i].PropertyName, outlineDataCollector.PropertiesList[i].OrderIndex);
            }

            ContainerGraph.ResetNodesLocalVariablesIfNot(MasterNodePortCategory.Vertex);
            return("0");
        }
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar)
        {
            if (dataCollector.PortCategory == MasterNodePortCategory.Tessellation)
            {
                UIUtils.ShowMessage(m_nodeAttribs.Name + " cannot be used on Master Node Tessellation port");
                return("-1");
            }

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

            string tiling = string.Empty;
            string offset = string.Empty;

            string portProperty = string.Empty;

            if (m_texPort.IsConnected)
            {
                portProperty = m_texPort.GeneratePortInstructions(ref dataCollector);
            }

            if (m_referenceArrayId > -1)
            {
                TexturePropertyNode temp = UIUtils.GetTexturePropertyNode(m_referenceArrayId);
                if (temp != null)
                {
                    portProperty = temp.BaseGenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalVar);
                }
            }

            //TEMPLATES
            if (dataCollector.MasterNodeCategory == AvailableShaderTypes.Template)
            {
                if (m_outputPorts[0].IsLocalValue(dataCollector.PortCategory))
                {
                    return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
                }

                string uvName = string.Empty;
                if (dataCollector.TemplateDataCollectorInstance.HasUV(m_textureCoordChannel))
                {
                    uvName = dataCollector.TemplateDataCollectorInstance.GetUVName(m_textureCoordChannel, m_outputPorts[0].DataType);
                }
                else
                {
                    uvName = dataCollector.TemplateDataCollectorInstance.RegisterUV(m_textureCoordChannel, m_outputPorts[0].DataType);
                }
                string currPropertyName = GetValidPropertyName();
                if (!string.IsNullOrEmpty(portProperty) && portProperty != "0.0")
                {
                    currPropertyName = portProperty;
                }
                if (!string.IsNullOrEmpty(currPropertyName))
                {
                    string finalTexCoordName      = "uv" + currPropertyName;
                    string dummyPropertyTexcoords = currPropertyName + "_ST";
                    dataCollector.AddToUniforms(UniqueId, "float4", dummyPropertyTexcoords);

                    if (m_texcoordSize > 2)
                    {
                        dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_outputPorts[0].DataType, finalTexCoordName, uvName);
                        dataCollector.AddLocalVariable(UniqueId, finalTexCoordName + ".xy", string.Format(Constants.TilingOffsetFormat, uvName + ".xy", dummyPropertyTexcoords + ".xy", dummyPropertyTexcoords + ".zw") + ";");
                        m_outputPorts[0].SetLocalValue(finalTexCoordName, dataCollector.PortCategory);
                    }
                    else
                    {
                        RegisterLocalVariable(0, string.Format(Constants.TilingOffsetFormat, uvName, dummyPropertyTexcoords + ".xy", dummyPropertyTexcoords + ".zw"), ref dataCollector, finalTexCoordName);
                    }
                    //RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, dummyPropertyTexcoords+".xy", dummyPropertyTexcoords+".zw" ), ref dataCollector, finalTexCoordName );
                }
                else
                {
                    string finalTexCoordName = "uv" + OutputId;
                    tiling = m_tilingPort.GeneratePortInstructions(ref dataCollector);
                    offset = m_offsetPort.GeneratePortInstructions(ref dataCollector);

                    if (m_texcoordSize > 2)
                    {
                        dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_outputPorts[0].DataType, finalTexCoordName, uvName);
                        dataCollector.AddLocalVariable(UniqueId, finalTexCoordName + ".xy", string.Format(Constants.TilingOffsetFormat, uvName + ".xy", tiling, offset) + ";");
                        m_outputPorts[0].SetLocalValue(finalTexCoordName, dataCollector.PortCategory);
                    }
                    else
                    {
                        RegisterLocalVariable(0, string.Format(Constants.TilingOffsetFormat, uvName, tiling, offset), ref dataCollector, finalTexCoordName);
                    }
                    //RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, tiling, offset ), ref dataCollector, finalTexCoordName );
                }
                return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
            }

            //SURFACE
            string propertyName = GetValidPropertyName();

            if (!string.IsNullOrEmpty(portProperty) && portProperty != "0.0")
            {
                propertyName = portProperty;
            }

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

            if (!m_tilingPort.IsConnected && m_tilingPort.Vector2InternalData == Vector2.one)
            {
                tiling = null;
            }
            else
            {
                tiling = m_tilingPort.GeneratePortInstructions(ref dataCollector);
            }

            if (!m_offsetPort.IsConnected && m_offsetPort.Vector2InternalData == Vector2.zero)
            {
                offset = null;
            }
            else
            {
                offset = m_offsetPort.GeneratePortInstructions(ref dataCollector);
            }

            if (!string.IsNullOrEmpty(propertyName) /*m_referenceArrayId > -1*/)
            {
                m_surfaceTexcoordName = GeneratorUtils.GenerateAutoUVs(ref dataCollector, UniqueId, m_textureCoordChannel, propertyName, m_outputPorts[0].DataType, tiling, offset, OutputId);
            }
            else
            {
                m_surfaceTexcoordName = GeneratorUtils.GenerateAutoUVs(ref dataCollector, UniqueId, m_textureCoordChannel, null, m_outputPorts[0].DataType, tiling, offset, OutputId);
            }

            m_outputPorts[0].SetLocalValue(m_surfaceTexcoordName, dataCollector.PortCategory);
            return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
        }
        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));
        }
Exemple #10
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (m_outputPorts[0].IsLocalValue)
            {
                return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue));
            }


            OnPropertyNameChanged();

            CheckReference();

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

            bool instanced = false;

            if (m_referenceType == TexReferenceType.Instance && m_referenceSampler != null)
            {
                instanced = true;
            }

            if (!instanced)
            {
                base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar);
            }

            string level = string.Empty;

            if (m_lodPort.Visible)
            {
                level = m_lodPort.GeneratePortInstructions(ref dataCollector);
            }

            if (isVertex && !m_lodPort.Visible)
            {
                level = "0";
            }


            string uvs = string.Empty;

            if (m_uvPort.IsConnected)
            {
                uvs = m_uvPort.GeneratePortInstructions(ref dataCollector);
            }
            else
            {
                if (isVertex)
                {
                    uvs = TexCoordVertexDataNode.GenerateVertexUVs(ref dataCollector, UniqueId, m_uvSet, (instanced ? m_referenceSampler.PropertyName : PropertyName));
                }
                else
                {
                    uvs = TexCoordVertexDataNode.GenerateFragUVs(ref dataCollector, UniqueId, m_uvSet, (instanced ? m_referenceSampler.PropertyName : PropertyName));
                }
            }
            string index = m_indexPort.GeneratePortInstructions(ref dataCollector);

            int connectionNumber = 0;

            for (int i = 0; i < m_outputPorts.Count; i++)
            {
                connectionNumber += m_outputPorts[i].ConnectionCount;
            }

            string propertyName = string.Empty;

            if (!instanced)
            {
                propertyName = PropertyName;
            }
            else
            {
                propertyName = m_referenceSampler.PropertyName;
            }


            string m_normalMapUnpackMode = "";

            if (m_autoUnpackNormals)
            {
                bool isScaledNormal = false;
                if (m_normalPort.IsConnected)
                {
                    isScaledNormal = true;
                }
                else
                {
                    if (m_normalPort.FloatInternalData != 1)
                    {
                        isScaledNormal = true;
                    }
                }
                if (isScaledNormal)
                {
                    string scaleValue = m_normalPort.GeneratePortInstructions(ref dataCollector);
                    dataCollector.AddToIncludes(UniqueId, Constants.UnityStandardUtilsLibFuncs);
                    m_normalMapUnpackMode = "UnpackScaleNormal( {0} ," + scaleValue + " )";
                }
                else
                {
                    m_normalMapUnpackMode = "UnpackNormal( {0} )";
                }
            }

            string result = "UNITY_SAMPLE_TEX2DARRAY" + (m_lodPort.Visible || isVertex ? "_LOD" : "") + "(" + propertyName + ", float3(" + uvs + ", " + index + ") " + (m_lodPort.Visible || isVertex ? ", " + level : "") + " )";

            if (m_autoUnpackNormals)
            {
                result = string.Format(m_normalMapUnpackMode, result);
            }

            //if ( connectionNumber > 1 )
            //{
            RegisterLocalVariable(0, result, ref dataCollector, "texArray" + OutputId);
            //dataCollector.AddToLocalVariables( UniqueId, "float" + ( m_autoUnpackNormals ? "3" : "4" ) + " texArray" + m_uniqueId + " = " + result + ";" );
            return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue));
            //}
            //else
            //{

            //	return GetOutputVectorItem( 0, outputId, result );
            //}
        }
        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));
            }

            GeneratePattern(ref dataCollector);

            if (!(dataCollector.IsTemplate && dataCollector.IsSRP))
            {
                dataCollector.AddToIncludes(UniqueId, Constants.UnityShaderVariables);
            }
            string varName    = string.Empty;
            bool   isFragment = dataCollector.IsFragmentCategory;

            if (m_customScreenPos && m_inputPorts[2].IsConnected)
            {
                varName = "ditherCustomScreenPos" + OutputId;
                string customScreenPosVal = m_inputPorts[2].GeneratePortInstructions(ref dataCollector);
                dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT4, varName, customScreenPosVal);
            }
            else
            {
                if (dataCollector.TesselationActive && isFragment)
                {
                    varName = GeneratorUtils.GenerateClipPositionOnFrag(ref dataCollector, UniqueId, CurrentPrecisionType);
                }
                else
                {
                    if (dataCollector.IsTemplate)
                    {
                        varName = dataCollector.TemplateDataCollectorInstance.GetScreenPosNormalized(CurrentPrecisionType);
                    }
                    else
                    {
                        varName = GeneratorUtils.GenerateScreenPositionNormalized(ref dataCollector, UniqueId, CurrentPrecisionType, !dataCollector.UsingCustomScreenPos);
                    }
                }
            }
            string surfInstruction = varName + ".xy * _ScreenParams.xy";

            m_showErrorMessage = false;
            string functionResult = "";
            string noiseTex       = string.Empty;

            switch (m_selectedPatternInt)
            {
            default:
            case 0:
                dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT2, "clipScreen" + OutputId, surfInstruction);
                functionResult = dataCollector.AddFunctions(m_functionHeader, m_functionBody, "fmod(" + "clipScreen" + OutputId + ".x, 4)", "fmod(" + "clipScreen" + OutputId + ".y, 4)");
                break;

            case 1:
                dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT2, "clipScreen" + OutputId, surfInstruction);
                functionResult = dataCollector.AddFunctions(m_functionHeader, m_functionBody, "fmod(" + "clipScreen" + OutputId + ".x, 8)", "fmod(" + "clipScreen" + OutputId + ".y, 8)");
                break;

            case 2:
            {
                if (!m_texPort.IsConnected)
                {
                    m_showErrorMessage        = true;
                    m_errorMessageTypeIsError = NodeMessageType.Warning;
                    m_errorMessageTooltip     = "Please connect a texture object to the Pattern input port to generate a proper dithered pattern";
                    return("0");
                }
                else
                {
                    ParentGraph outsideGraph = UIUtils.CurrentWindow.OutsideGraph;
                    noiseTex = m_texPort.GeneratePortInstructions(ref dataCollector);
                    //GeneratePattern( ref dataCollector );
                    dataCollector.AddToUniforms(UniqueId, "float4 " + noiseTex + "_TexelSize;", dataCollector.IsSRP);
#if UNITY_2018_1_OR_NEWER
                    if (outsideGraph.SamplingMacros)
#else
                    if (outsideGraph.SamplingMacros && !outsideGraph.IsStandardSurface)
#endif
                    {
                        string sampler = string.Empty;
                        if (m_ssPort.IsConnected)
                        {
                            sampler = m_ssPort.GeneratePortInstructions(ref dataCollector);
                        }
                        else
                        {
                            sampler = GeneratorUtils.GenerateSamplerState(ref dataCollector, UniqueId, noiseTex);
                        }
                        //if( outsideGraph.IsSRP )
                        //	functionResult = dataCollector.AddFunctions( m_functionHeader, m_functionBody, varName, noiseTex + ", " + sampler, noiseTex + "_TexelSize" );
                        //else
                        functionResult = dataCollector.AddFunctions(m_functionHeader, m_functionBody, varName, noiseTex + ", " + sampler, noiseTex + "_TexelSize");
                    }
                    else
                    {
                        functionResult = dataCollector.AddFunctions(m_functionHeader, m_functionBody, varName, noiseTex, noiseTex + "_TexelSize");
                    }
                }
            }
            break;
            }

            dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT, "dither" + OutputId, functionResult);

            if (m_inputPorts[0].IsConnected)
            {
                string driver = m_inputPorts[0].GeneratePortInstructions(ref dataCollector);
                dataCollector.AddLocalVariable(UniqueId, "dither" + OutputId + " = step( dither" + OutputId + ", " + driver + " );");
            }

            //RegisterLocalVariable( 0, functionResult, ref dataCollector, "dither" + OutputId );
            m_outputPorts[0].SetLocalValue("dither" + OutputId, dataCollector.PortCategory);

            return(m_outputPorts[0].LocalValue(dataCollector.PortCategory));
        }
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar)
        {
            if (dataCollector.PortCategory == MasterNodePortCategory.Tessellation)
            {
                UIUtils.ShowMessage(m_nodeAttribs.Name + " cannot be used on Master Node Tessellation port");
                return("-1");
            }

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

            if (m_texPort.IsConnected)
            {
                m_texPort.GeneratePortInstructions(ref dataCollector);
            }

            string propertyName = GetValidPropertyName();

            if (!string.IsNullOrEmpty(propertyName) /*m_referenceArrayId > -1*/)
            {
                //m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceArrayId );
                //if ( m_referenceNode != null )
                //{
                //string propertyName = m_referenceNode.PropertyName;
                int    coordSet = ((m_textureCoordChannel < 0) ? 0 : m_textureCoordChannel);
                string uvName   = string.Empty;

                string dummyPropUV = "_tex" + (m_texcoordSize > 2 ? "" + m_texcoordSize : "") + "coord" + (coordSet > 0 ? (coordSet + 1).ToString() : "");
                string dummyUV     = "uv" + (coordSet > 0 ? (coordSet + 1).ToString() : "") + dummyPropUV;

                if (isVertex)
                {
                    uvName = IOUtils.GetUVChannelName(propertyName, coordSet);
                    string vertexInput = Constants.VertexShaderInputStr + ".texcoord";
                    if (coordSet > 0)
                    {
                        vertexInput += coordSet.ToString();
                    }
                    dataCollector.AddToVertexLocalVariables(UniqueId, "float" + m_texcoordSize + " " + uvName + " = " + vertexInput + ";");
                    dataCollector.AddToVertexLocalVariables(UniqueId, uvName + ".xy = " + vertexInput + ".xy * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw;");
                }
                else
                {
                    uvName = IOUtils.GetUVChannelName(propertyName, coordSet);
                    if (m_texcoordSize > 2)
                    {
                        uvName += m_texcoordSize;
                        dataCollector.UsingHigherSizeTexcoords = true;
                        dataCollector.AddToLocalVariables(UniqueId, "float" + m_texcoordSize + " " + uvName + " = " + Constants.InputVarStr + "." + dummyUV + ";");
                        dataCollector.AddToLocalVariables(UniqueId, uvName + ".xy = " + Constants.InputVarStr + "." + dummyUV + ".xy * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw;");
                    }
                    else
                    {
                        dataCollector.AddToLocalVariables(UniqueId, PrecisionType.Float, WirePortDataType.FLOAT2, uvName, Constants.InputVarStr + "." + dummyUV + " * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw");
                    }
                }

                dataCollector.AddToUniforms(UniqueId, "uniform float4 " + propertyName + "_ST;");
                dataCollector.AddToProperties(UniqueId, "[HideInInspector] " + dummyPropUV + "( \"\", 2D ) = \"white\" {}", 100);
                dataCollector.AddToInput(UniqueId, "float" + m_texcoordSize + " " + dummyUV, true);

                return(GetOutputVectorItem(0, outputId, uvName));
                //}
            }

            if (m_texcoordId < 0)
            {
                if (!isVertex && dataCollector.TesselationActive)
                {
                    return(GenerateFragShaderForOutput(outputId, ref dataCollector, ignoreLocalVar));
                }

                bool tessVertexMode = isVertex && dataCollector.TesselationActive;

                m_texcoordId = dataCollector.AvailableUvIndex;
                string texcoordName = TexCoordStr + m_texcoordId;



                string uvChannel = m_textureCoordChannel == 0 ? ".xy" : m_textureCoordChannel + ".xy";

                MasterNodePortCategory portCategory = dataCollector.PortCategory;
                if (dataCollector.PortCategory != MasterNodePortCategory.Vertex && dataCollector.PortCategory != MasterNodePortCategory.Tessellation)
                {
                    dataCollector.PortCategory = MasterNodePortCategory.Vertex;
                }

                // We need to reset local variables if there are already created to force them to be created in the vertex function
                int buffer = m_texcoordId;
                ContainerGraph.ResetNodesLocalVariables(this);

                bool dirtySpecialVarsBefore = dataCollector.DirtySpecialLocalVariables;
                bool dirtyVertexVarsBefore  = dataCollector.DirtyVertexVariables;

                string tiling = m_tilingPort.GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT2, false, true);
                string offset = m_offsetPort.GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT2, false, true);
                dataCollector.PortCategory = portCategory;

                string vertexUV = Constants.VertexShaderInputStr + ".texcoord" + uvChannel;

                if (!tessVertexMode)
                {
                    dataCollector.AddToInput(UniqueId, "float" + m_texcoordSize + " " + texcoordName, true);
                }

                bool resetLocals = false;
                // new texture coordinates are calculated on the vertex shader so we need to register its local vars
                if (!dirtySpecialVarsBefore && dataCollector.DirtySpecialLocalVariables)
                {
                    dataCollector.AddVertexInstruction(UIUtils.CurrentDataCollector.SpecialLocalVariables, UniqueId, false);
                    UIUtils.CurrentDataCollector.ClearSpecialLocalVariables();
                    resetLocals = true;
                }

                if (!dirtyVertexVarsBefore && dataCollector.DirtyVertexVariables)
                {
                    dataCollector.AddVertexInstruction(UIUtils.CurrentDataCollector.VertexLocalVariables, UniqueId, false);
                    UIUtils.CurrentDataCollector.ClearVertexLocalVariables();
                    resetLocals = true;
                }

                //Reset local variables again so they wont be caught on the fragment shader
                if (resetLocals)
                {
                    ContainerGraph.ResetNodesLocalVariables(this);
                }

                if (tessVertexMode)
                {
                    dataCollector.AddVertexInstruction(vertexUV + " = " + vertexUV + " * " + tiling + " + " + offset, UniqueId);
                    m_surfaceTexcoordName = Constants.VertexShaderInputStr + "." + IOUtils.GetVertexUVChannelName(m_textureCoordChannel) + ".xy";
                }
                else
                {
                    if (dataCollector.TesselationActive)
                    {
                        if (isVertex)
                        {
                            dataCollector.AddVertexInstruction(vertexUV + " = " + vertexUV + " * " + tiling + " + " + offset, UniqueId);
                            m_surfaceTexcoordName = Constants.VertexShaderOutputStr + "." + texcoordName;
                        }
                        else
                        {
                            dataCollector.AddToProperties(UniqueId, DummyPropertyDec + " {}", -1);
                            dataCollector.AddToUniforms(UniqueId, DummyUniformDec);
                            string texCoordPrefix = (m_textureCoordChannel == 0) ? string.Empty : (m_textureCoordChannel + 1).ToString();
                            dataCollector.AddToInput(UniqueId, "float2 " + string.Format(DummyTexCoordDef, texCoordPrefix), true);
                            dataCollector.AddToSpecialLocalVariables(UniqueId, string.Format(DummyTexCoordSurfDef, OutputId, Constants.InputVarStr, texCoordPrefix, tiling, offset));
                            m_surfaceTexcoordName = string.Format(DummyTexCoordSurfVar, UniqueId);
                        }
                    }
                    else
                    {
                        dataCollector.AddVertexInstruction(Constants.VertexShaderOutputStr + "." + texcoordName + ".xy = " + vertexUV + " * " + tiling + " + " + offset, UniqueId);
                        m_surfaceTexcoordName = (isVertex ? Constants.VertexShaderOutputStr : Constants.InputVarStr) + "." + texcoordName;
                    }
                }

                m_texcoordId = buffer;
            }

            return(GetOutputVectorItem(0, outputId, m_surfaceTexcoordName));
        }
Exemple #13
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (m_outputPorts[0].IsLocalValue(dataCollector.PortCategory))
            {
                return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
            }


            OnPropertyNameChanged();

            if (CheckReference())
            {
                OrderIndex       = m_referenceSampler.RawOrderIndex;
                OrderIndexOffset = m_referenceSampler.OrderIndexOffset;
            }

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

            bool instanced = false;

            if (m_referenceType == TexReferenceType.Instance && m_referenceSampler != null)
            {
                instanced = true;
            }

            if (instanced)
            {
                if (!m_referenceSampler.TexPort.IsConnected)
                {
                    base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar);
                }
            }
            else if (!m_texPort.IsConnected)
            {
                base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar);
            }

            string level = string.Empty;

            if (m_lodPort.Visible)
            {
                level = m_lodPort.GeneratePortInstructions(ref dataCollector);
            }

            if (isVertex && !m_lodPort.Visible)
            {
                level = "0";
            }

            string propertyName = string.Empty;

            if (instanced)
            {
                if (m_referenceSampler.TexPort.IsConnected)
                {
                    propertyName = m_referenceSampler.TexPort.GeneratePortInstructions(ref dataCollector);
                }
                else
                {
                    propertyName = m_referenceSampler.PropertyName;
                }
            }
            else if (m_texPort.IsConnected)
            {
                propertyName = m_texPort.GeneratePortInstructions(ref dataCollector);
            }
            else
            {
                propertyName = PropertyName;
            }

            string uvs = string.Empty;

            if (m_uvPort.IsConnected)
            {
                uvs = m_uvPort.GeneratePortInstructions(ref dataCollector);
            }
            else
            {
                if (dataCollector.IsTemplate)
                {
                    uvs = dataCollector.TemplateDataCollectorInstance.GetTextureCoord(m_uvSet, propertyName /*( instanced ? m_referenceSampler.PropertyName : PropertyName )*/, UniqueId, CurrentPrecisionType);
                }
                else
                {
                    if (isVertex)
                    {
                        uvs = TexCoordVertexDataNode.GenerateVertexUVs(ref dataCollector, UniqueId, m_uvSet, propertyName);
                    }
                    else
                    {
                        uvs = TexCoordVertexDataNode.GenerateFragUVs(ref dataCollector, UniqueId, m_uvSet, propertyName);
                    }
                }
            }
            string index = m_indexPort.GeneratePortInstructions(ref dataCollector);

            string result = string.Empty;

            if (dataCollector.IsTemplate && dataCollector.IsSRP)
            {
                //CAREFUL mipbias here means derivative (this needs index changes)
                //TODO: unity now supports bias as well
                if (m_mipMode == MipType.MipBias)
                {
                    dataCollector.UsingArrayDerivatives = true;
                    result = propertyName + ".SampleGrad(sampler" + propertyName + ", float3(" + uvs + ", " + index + "), " + m_ddxPort.GeneratePortInstructions(ref dataCollector) + ", " + m_ddyPort.GeneratePortInstructions(ref dataCollector) + ");";
                }
                else if (m_lodPort.Visible || isVertex)
                {
                    result = "SAMPLE_TEXTURE2D_ARRAY_LOD(" + propertyName + ", sampler" + propertyName + ", " + uvs + ", " + index + ", " + level + " )";
                }
                else
                {
                    result = "SAMPLE_TEXTURE2D_ARRAY(" + propertyName + ", sampler" + propertyName + ", " + uvs + ", " + index + " )";
                }
            }
            else
            {
                //CAREFUL mipbias here means derivative (this needs index changes)
                if (m_mipMode == MipType.MipBias)
                {
                    dataCollector.UsingArrayDerivatives = true;
                    result = "ASE_SAMPLE_TEX2DARRAY_GRAD(" + propertyName + ", float3(" + uvs + ", " + index + "), " + m_ddxPort.GeneratePortInstructions(ref dataCollector) + ", " + m_ddyPort.GeneratePortInstructions(ref dataCollector) + " )";
                }
                else if (m_lodPort.Visible || isVertex)
                {
                    result = "UNITY_SAMPLE_TEX2DARRAY_LOD(" + propertyName + ", float3(" + uvs + ", " + index + "), " + level + " )";
                }
                else
                {
                    result = "UNITY_SAMPLE_TEX2DARRAY" + (m_lodPort.Visible || isVertex ? "_LOD" : "") + "(" + propertyName + ", float3(" + uvs + ", " + index + ") " + (m_lodPort.Visible || isVertex ? ", " + level : "") + " )";
                }
            }

            if (m_autoUnpackNormals)
            {
                bool isScaledNormal = false;
                if (m_normalPort.IsConnected)
                {
                    isScaledNormal = true;
                }
                else
                {
                    if (m_normalPort.FloatInternalData != 1)
                    {
                        isScaledNormal = true;
                    }
                }

                string scaleValue = isScaledNormal ? m_normalPort.GeneratePortInstructions(ref dataCollector) : "1.0";
                result = GeneratorUtils.GenerateUnpackNormalStr(ref dataCollector, CurrentPrecisionType, UniqueId, OutputId, result, isScaledNormal, scaleValue);
                if (isScaledNormal && (!dataCollector.IsTemplate || !dataCollector.IsSRP))
                {
                    dataCollector.AddToIncludes(UniqueId, Constants.UnityStandardUtilsLibFuncs);
                }
            }

            RegisterLocalVariable(0, result, ref dataCollector, "texArray" + OutputId);
            return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
        }
		public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
		{
			if( dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
			{
				UIUtils.ShowMessage( m_nodeAttribs.Name + " cannot be used on Master Node Tessellation port" ):
				return "-1":
			}

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

			string tiling = string.Empty:
			string offset = string.Empty:

			string portProperty = string.Empty:
			if( m_texPort.IsConnected )
				portProperty = m_texPort.GeneratePortInstructions( ref dataCollector ):

			if( m_referenceArrayId > -1 )
			{
				TexturePropertyNode temp = UIUtils.GetTexturePropertyNode( m_referenceArrayId ):
				if( temp != null )
				{
					portProperty = temp.BaseGenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalVar ):
				}
			}

			//TEMPLATES
			if( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
			{
				if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
					return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) ):

				string uvName = string.Empty:
				if( dataCollector.TemplateDataCollectorInstance.HasUV( m_textureCoordChannel ) )
				{
					uvName = dataCollector.TemplateDataCollectorInstance.GetUVName( m_textureCoordChannel, m_outputPorts[ 0 ].DataType ):
				}
				else
				{
					uvName = dataCollector.TemplateDataCollectorInstance.RegisterUV( m_textureCoordChannel, m_outputPorts[ 0 ].DataType ):
				}
				string currPropertyName = GetValidPropertyName():
				if( !string.IsNullOrEmpty( portProperty ) && portProperty != "0.0" )
				{
					currPropertyName = portProperty:
				}
				if( !string.IsNullOrEmpty( currPropertyName ) )
				{
					string finalTexCoordName = "uv" + m_textureCoordChannel + currPropertyName:
					string dummyPropertyTexcoords = currPropertyName + "_ST":

					if( m_texCoordsHelper == null )
					{
						m_texCoordsHelper = CreateInstance<Vector4Node>():
						m_texCoordsHelper.ContainerGraph = ContainerGraph:
						m_texCoordsHelper.SetBaseUniqueId( UniqueId, true ):
						m_texCoordsHelper.RegisterPropertyOnInstancing = false:
					}

					if( UIUtils.CurrentWindow.OutsideGraph.IsInstancedShader )
					{
						m_texCoordsHelper.CurrentParameterType = PropertyType.InstancedProperty:
					}
					else
					{
						m_texCoordsHelper.CurrentParameterType = PropertyType.Global:
					}
					m_texCoordsHelper.ResetOutputLocals():
					m_texCoordsHelper.SetRawPropertyName( dummyPropertyTexcoords ):
					dummyPropertyTexcoords = m_texCoordsHelper.GenerateShaderForOutput( 0, ref dataCollector, false ):

					if( m_texcoordSize > 2 )
					{
						dataCollector.AddLocalVariable( UniqueId, m_currentPrecisionType, m_outputPorts[ 0 ].DataType, finalTexCoordName, uvName ):
						dataCollector.AddLocalVariable( UniqueId, finalTexCoordName + ".xy", string.Format( Constants.TilingOffsetFormat, uvName + ".xy", dummyPropertyTexcoords + ".xy", dummyPropertyTexcoords + ".zw" ) + ":" ):
						m_outputPorts[ 0 ].SetLocalValue( finalTexCoordName, dataCollector.PortCategory ):
					}
					else
					{
						RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, dummyPropertyTexcoords + ".xy", dummyPropertyTexcoords + ".zw" ), ref dataCollector, finalTexCoordName ):
					}
					//RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, dummyPropertyTexcoords+".xy", dummyPropertyTexcoords+".zw" ), ref dataCollector, finalTexCoordName ):
				}
				else
				{
					string finalTexCoordName = "uv" + m_textureCoordChannel + OutputId:
					tiling = m_tilingPort.GeneratePortInstructions( ref dataCollector ):
					offset = m_offsetPort.GeneratePortInstructions( ref dataCollector ):

					if( m_texcoordSize > 2 )
					{
						dataCollector.AddLocalVariable( UniqueId, m_currentPrecisionType, m_outputPorts[ 0 ].DataType, finalTexCoordName, uvName ):
						dataCollector.AddLocalVariable( UniqueId, finalTexCoordName + ".xy", string.Format( Constants.TilingOffsetFormat, uvName + ".xy", tiling, offset ) + ":" ):
						m_outputPorts[ 0 ].SetLocalValue( finalTexCoordName, dataCollector.PortCategory ):
					}
					else
					{
						RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, tiling, offset ), ref dataCollector, finalTexCoordName ):
					}
					//RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, tiling, offset ), ref dataCollector, finalTexCoordName ):
				}
				return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) ):
			}

			//SURFACE
			string propertyName = GetValidPropertyName():
			if( !string.IsNullOrEmpty( portProperty ) && portProperty != "0.0" )
			{
				propertyName = portProperty:
			}

			if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
				return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) ):

			if( !m_tilingPort.IsConnected && m_tilingPort.Vector2InternalData == Vector2.one )
				tiling = null:
			else
				tiling = m_tilingPort.GeneratePortInstructions( ref dataCollector ):

			if( !m_offsetPort.IsConnected && m_offsetPort.Vector2InternalData == Vector2.zero )
				offset = null:
			else
				offset = m_offsetPort.GeneratePortInstructions( ref dataCollector ):

			if( !string.IsNullOrEmpty( propertyName ) /*m_referenceArrayId > -1*/ )
			{
				m_surfaceTexcoordName = GeneratorUtils.GenerateAutoUVs( ref dataCollector, UniqueId, m_textureCoordChannel, propertyName, m_outputPorts[ 0 ].DataType, tiling, offset, OutputId ):
			}
			else
			{
				m_surfaceTexcoordName = GeneratorUtils.GenerateAutoUVs( ref dataCollector, UniqueId, m_textureCoordChannel, null, m_outputPorts[ 0 ].DataType, tiling, offset, OutputId ):
			}

			m_outputPorts[ 0 ].SetLocalValue( m_surfaceTexcoordName, dataCollector.PortCategory ):
			return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) ):
		}
Exemple #15
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar)
        {
            if (dataCollector.PortCategory == MasterNodePortCategory.Tessellation)
            {
                UIUtils.ShowMessage(m_nodeAttribs.Name + " cannot be used on Master Node Tessellation port");
                return("-1");
            }

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

            string tiling = string.Empty;
            string offset = string.Empty;

            string portProperty = string.Empty;

            if (m_texPort.IsConnected)
            {
                portProperty = m_texPort.GenerateShaderForOutput(ref dataCollector, m_texPort.DataType, ignoreLocalVar);
            }

            if (m_referenceArrayId > -1)
            {
                TexturePropertyNode temp = UIUtils.GetTexturePropertyNode(m_referenceArrayId);
                if (temp != null)
                {
                    portProperty = temp.BaseGenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalVar);
                }
            }

            //TEMPLATES
            if (dataCollector.MasterNodeCategory == AvailableShaderTypes.Template)
            {
                if (m_outputPorts[0].IsLocalValue)
                {
                    return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue));
                }

                string uvName = string.Empty;
                if (dataCollector.TemplateDataCollectorInstance.HasUV(m_textureCoordChannel))
                {
                    uvName = dataCollector.TemplateDataCollectorInstance.GetUVName(m_textureCoordChannel);
                }
                else
                {
                    uvName = dataCollector.TemplateDataCollectorInstance.RegisterUV(m_textureCoordChannel);
                    //string uvName = TemplateHelperFunctions.GenerateTextureSemantic( ref dataCollector, m_textureCoordChannel );
                    //uvName = ( dataCollector.IsFragmentCategory ? Constants.InputVarStr : Constants.VertexShaderInputStr ) + "." + uvName;
                }
                string currPropertyName = GetValidPropertyName();
                if (!string.IsNullOrEmpty(portProperty) && portProperty != "0.0")
                {
                    currPropertyName = portProperty;
                }
                if (!string.IsNullOrEmpty(currPropertyName))
                {
                    string finalTexCoordName      = "uv" + currPropertyName;
                    string dummyPropertyTexcoords = currPropertyName + "_ST";
                    dataCollector.AddToUniforms(UniqueId, "float4", dummyPropertyTexcoords);

                    if (m_texcoordSize > 2)
                    {
                        dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_outputPorts[0].DataType, finalTexCoordName, uvName);
                        dataCollector.AddLocalVariable(UniqueId, finalTexCoordName + ".xy", string.Format(Constants.TilingOffsetFormat, uvName + ".xy", dummyPropertyTexcoords + ".xy", dummyPropertyTexcoords + ".zw") + ";");
                        m_outputPorts[0].SetLocalValue(finalTexCoordName);
                    }
                    else
                    {
                        RegisterLocalVariable(0, string.Format(Constants.TilingOffsetFormat, uvName, dummyPropertyTexcoords + ".xy", dummyPropertyTexcoords + ".zw"), ref dataCollector, finalTexCoordName);
                    }
                    //RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, dummyPropertyTexcoords+".xy", dummyPropertyTexcoords+".zw" ), ref dataCollector, finalTexCoordName );
                }
                else
                {
                    string finalTexCoordName = "uv" + OutputId;
                    tiling = m_tilingPort.GeneratePortInstructions(ref dataCollector);
                    offset = m_offsetPort.GeneratePortInstructions(ref dataCollector);

                    if (m_texcoordSize > 2)
                    {
                        dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_outputPorts[0].DataType, finalTexCoordName, uvName);
                        dataCollector.AddLocalVariable(UniqueId, finalTexCoordName + ".xy", string.Format(Constants.TilingOffsetFormat, uvName + ".xy", tiling, offset) + ";");
                        m_outputPorts[0].SetLocalValue(finalTexCoordName);
                    }
                    else
                    {
                        RegisterLocalVariable(0, string.Format(Constants.TilingOffsetFormat, uvName, tiling, offset), ref dataCollector, finalTexCoordName);
                    }
                    //RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, tiling, offset ), ref dataCollector, finalTexCoordName );
                }
                return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue));
            }

            //SURFACE
            string propertyName = GetValidPropertyName();

            if (!string.IsNullOrEmpty(portProperty) && portProperty != "0.0")
            {
                propertyName = portProperty;
            }

            if (m_outputPorts[0].IsLocalValue)
            {
                return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue));
            }

            tiling = m_tilingPort.GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT2, false, true);
            offset = m_offsetPort.GenerateShaderForOutput(ref dataCollector, WirePortDataType.FLOAT2, false, true);
            if (!string.IsNullOrEmpty(propertyName) /*m_referenceArrayId > -1*/)
            {
                ////m_referenceNode = UIUtils.GetTexturePropertyNode( m_referenceArrayId );
                ////if ( m_referenceNode != null )
                ////{
                ////string propertyName = m_referenceNode.PropertyName;
                //int coordSet = ( ( m_textureCoordChannel < 0 ) ? 0 : m_textureCoordChannel );
                //string uvName = string.Empty;

                //string dummyPropUV = "_tex" + ( m_texcoordSize > 2 ? "" + m_texcoordSize : "" ) + "coord" + ( coordSet > 0 ? ( coordSet + 1 ).ToString() : "" );
                //string dummyUV = "uv" + ( coordSet > 0 ? ( coordSet + 1 ).ToString() : "" ) + dummyPropUV;

                //if ( isVertex )
                //{
                //	uvName = IOUtils.GetUVChannelName( propertyName, coordSet ) + m_texcoordSize.ToString() + coordSet.ToString();
                //	string vertexInput = Constants.VertexShaderInputStr + ".texcoord";
                //	if ( coordSet > 0 )
                //	{
                //		vertexInput += coordSet.ToString();
                //	}

                //	dataCollector.AddToVertexLocalVariables( UniqueId, "float" + m_texcoordSize + " " + uvName + " = " + vertexInput + ";" );
                //	dataCollector.AddToVertexLocalVariables( UniqueId, uvName + ".xy = " + vertexInput + ".xy * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw;" );
                //}
                //else
                //{
                //	uvName = IOUtils.GetUVChannelName( propertyName, coordSet );
                //	if ( m_texcoordSize > 2 )
                //	{
                //		uvName += m_texcoordSize;
                //		dataCollector.UsingHigherSizeTexcoords = true;
                //		dataCollector.AddToLocalVariables( UniqueId, "float" + m_texcoordSize + " " + uvName + " = " + Constants.InputVarStr + "." + dummyUV + ";" );
                //		dataCollector.AddToLocalVariables( UniqueId, uvName + ".xy = " + Constants.InputVarStr + "." + dummyUV + ".xy * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw;" );
                //	}
                //	else
                //	{
                //		dataCollector.AddToLocalVariables( UniqueId, PrecisionType.Float, WirePortDataType.FLOAT2, uvName, Constants.InputVarStr + "." + dummyUV + " * " + propertyName + "_ST.xy + " + propertyName + "_ST.zw" );
                //	}
                //}

                //dataCollector.AddToUniforms( UniqueId, "uniform float4 " + propertyName + "_ST;" );
                //dataCollector.AddToProperties( UniqueId, "[HideInInspector] " + dummyPropUV + "( \"\", 2D ) = \"white\" {}", 100 );
                //dataCollector.AddToInput( UniqueId, "float" + m_texcoordSize + " " + dummyUV, true );

                //return GetOutputVectorItem( 0, outputId, uvName );
                ////}

                m_surfaceTexcoordName = GeneratorUtils.GenerateAutoUVs(ref dataCollector, UniqueId, m_textureCoordChannel, propertyName, m_outputPorts[0].DataType, tiling, offset, OutputId);
            }
            else
            {
                m_surfaceTexcoordName = GeneratorUtils.GenerateAutoUVs(ref dataCollector, UniqueId, m_textureCoordChannel, null, m_outputPorts[0].DataType, tiling, offset, OutputId);
            }
            //{
            //	if ( !isVertex && dataCollector.TesselationActive )
            //	{
            //		return GenerateFragShaderForOutput( outputId, ref dataCollector, ignoreLocalVar );
            //	}

            //	bool tessVertexMode = isVertex && dataCollector.TesselationActive;

            //	string texcoordName = TexCoordStr + OutputId;

            //	string uvChannel = m_textureCoordChannel == 0 ? ".xy" : m_textureCoordChannel + ".xy";

            //	MasterNodePortCategory portCategory = dataCollector.PortCategory;
            //	if ( dataCollector.PortCategory != MasterNodePortCategory.Vertex && dataCollector.PortCategory != MasterNodePortCategory.Tessellation )
            //		dataCollector.PortCategory = MasterNodePortCategory.Vertex;

            //	// We need to reset local variables if there are already created to force them to be created in the vertex function
            //	ContainerGraph.ResetNodesLocalVariablesIfNot( this, MasterNodePortCategory.Vertex );

            //	bool dirtySpecialVarsBefore = dataCollector.DirtySpecialLocalVariables;
            //	bool dirtyVertexVarsBefore = dataCollector.DirtyVertexVariables;

            //	string tiling = m_tilingPort.GenerateShaderForOutput( ref dataCollector, WirePortDataType.FLOAT2, false, true );
            //	string offset = m_offsetPort.GenerateShaderForOutput( ref dataCollector, WirePortDataType.FLOAT2, false, true );
            //	dataCollector.PortCategory = portCategory;

            //	string vertexUV = Constants.VertexShaderInputStr + ".texcoord" + uvChannel;

            //	if ( !tessVertexMode )
            //		dataCollector.AddToInput( UniqueId, "float" + m_texcoordSize + " " + texcoordName, true );

            //	bool resetLocals = false;
            //	// new texture coordinates are calculated on the vertex shader so we need to register its local vars
            //	if ( !dirtySpecialVarsBefore && dataCollector.DirtySpecialLocalVariables )
            //	{
            //		dataCollector.AddVertexInstruction( dataCollector.SpecialLocalVariables, UniqueId, false );
            //		dataCollector.ClearSpecialLocalVariables();
            //		resetLocals = true;
            //	}

            //	if ( !dirtyVertexVarsBefore && dataCollector.DirtyVertexVariables )
            //	{
            //		dataCollector.AddVertexInstruction( dataCollector.VertexLocalVariables, UniqueId, false );
            //		dataCollector.ClearVertexLocalVariables();
            //		resetLocals = true;
            //	}

            //	//Reset local variables again so they wont be caught on the fragment shader
            //	if ( resetLocals )
            //		ContainerGraph.ResetNodesLocalVariablesIfNot( this, MasterNodePortCategory.Vertex );

            //	if ( tessVertexMode )
            //	{
            //		dataCollector.AddToVertexLocalVariables(UniqueId, vertexUV + " = " + vertexUV + " * " + tiling + " + " + offset+";");
            //		m_surfaceTexcoordName = Constants.VertexShaderInputStr + "." + IOUtils.GetVertexUVChannelName( m_textureCoordChannel ) + ".xy";
            //	}
            //	else if( dataCollector.TesselationActive )
            //	{
            //		string texCoordPrefix = ( m_textureCoordChannel == 0 ) ? string.Empty : ( m_textureCoordChannel + 1 ).ToString();

            //		dataCollector.AddToProperties( UniqueId, string.Format( DummyPropertyDec, texCoordPrefix ) + " {}", -1 );
            //		dataCollector.AddToUniforms( UniqueId, string.Format( DummyUniformDec, texCoordPrefix ) );

            //		dataCollector.AddToInput( UniqueId, "float2 " + string.Format( DummyTexCoordDef, texCoordPrefix ), true );
            //		dataCollector.AddToSpecialLocalVariables( UniqueId, string.Format( DummyTexCoordSurfDef, OutputId, Constants.InputVarStr, texCoordPrefix, tiling, offset ) );
            //		m_surfaceTexcoordName = string.Format( DummyTexCoordSurfVar, OutputId );
            //	}
            //	else
            //	{
            //		//m_surfaceTexcoordName = GeneratorUtils.GenerateAutoUVs( ref dataCollector, UniqueId, m_textureCoordChannel, null, m_texcoordSize == 2 ? WirePortDataType.FLOAT2 : WirePortDataType.FLOAT3, tiling, offset, OutputId );
            //		dataCollector.AddToVertexLocalVariables( UniqueId, Constants.VertexShaderOutputStr + "." + texcoordName + ".xy = " + vertexUV + " * " + tiling + " + " + offset + ";" );
            //		m_surfaceTexcoordName = ( isVertex ? Constants.VertexShaderOutputStr : Constants.InputVarStr ) + "." + texcoordName;
            //	}
            //}

            m_outputPorts[0].SetLocalValue(m_surfaceTexcoordName, dataCollector.PortCategory);
            return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue));
        }
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (m_outputPorts[0].IsLocalValue)
            {
                return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue));
            }


            OnPropertyNameChanged();

            CheckReference();

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

            bool instanced = false;

            if (m_referenceType == TexReferenceType.Instance && m_referenceSampler != null)
            {
                instanced = true;
            }

            if (instanced)
            {
                if (!m_referenceSampler.TexPort.IsConnected)
                {
                    base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar);
                }
            }
            else if (!m_texPort.IsConnected)
            {
                base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar);
            }

            string level = string.Empty;

            if (m_lodPort.Visible)
            {
                level = m_lodPort.GeneratePortInstructions(ref dataCollector);
            }

            if (isVertex && !m_lodPort.Visible)
            {
                level = "0";
            }

            string propertyName = string.Empty;

            if (instanced)
            {
                if (m_referenceSampler.TexPort.IsConnected)
                {
                    propertyName = m_referenceSampler.TexPort.GeneratePortInstructions(ref dataCollector);
                }
                else
                {
                    propertyName = m_referenceSampler.PropertyName;
                }
            }
            else if (m_texPort.IsConnected)
            {
                propertyName = m_texPort.GeneratePortInstructions(ref dataCollector);
            }
            else
            {
                propertyName = PropertyName;
            }

            string uvs = string.Empty;

            if (m_uvPort.IsConnected)
            {
                uvs = m_uvPort.GeneratePortInstructions(ref dataCollector);
            }
            else
            {
                if (dataCollector.IsTemplate)
                {
                    uvs = dataCollector.TemplateDataCollectorInstance.GetTextureCoord(m_uvSet, (instanced ? m_referenceSampler.PropertyName : PropertyName), UniqueId, m_currentPrecisionType);
                }
                else
                {
                    if (isVertex)
                    {
                        uvs = TexCoordVertexDataNode.GenerateVertexUVs(ref dataCollector, UniqueId, m_uvSet, propertyName);
                    }
                    else
                    {
                        uvs = TexCoordVertexDataNode.GenerateFragUVs(ref dataCollector, UniqueId, m_uvSet, propertyName);
                    }
                }
            }
            string index = m_indexPort.GeneratePortInstructions(ref dataCollector);

            string m_normalMapUnpackMode = "";

            if (m_autoUnpackNormals)
            {
                bool isScaledNormal = false;
                if (m_normalPort.IsConnected)
                {
                    isScaledNormal = true;
                }
                else
                {
                    if (m_normalPort.FloatInternalData != 1)
                    {
                        isScaledNormal = true;
                    }
                }
                if (isScaledNormal)
                {
                    string scaleValue = m_normalPort.GeneratePortInstructions(ref dataCollector);
                    dataCollector.AddToIncludes(UniqueId, Constants.UnityStandardUtilsLibFuncs);
                    m_normalMapUnpackMode = "UnpackScaleNormal( {0} ," + scaleValue + " )";
                }
                else
                {
                    m_normalMapUnpackMode = "UnpackNormal( {0} )";
                }
            }

            string result = string.Empty;

            //CAREFUL mipbias here means derivative (this needs index changes)
            if (m_mipMode == MipType.MipBias)
            {
                dataCollector.UsingArrayDerivatives = true;
                result = "ASE_SAMPLE_TEX2DARRAY_GRAD(" + propertyName + ", float3(" + uvs + ", " + index + "), " + m_ddxPort.GeneratePortInstructions(ref dataCollector) + ", " + m_ddyPort.GeneratePortInstructions(ref dataCollector) + " )";
            }
            else if (m_lodPort.Visible || isVertex)
            {
                result = "UNITY_SAMPLE_TEX2DARRAY_LOD(" + propertyName + ", float3(" + uvs + ", " + index + "), " + level + " )";
            }
            else
            {
                result = "UNITY_SAMPLE_TEX2DARRAY" + (m_lodPort.Visible || isVertex ? "_LOD" : "") + "(" + propertyName + ", float3(" + uvs + ", " + index + ") " + (m_lodPort.Visible || isVertex ? ", " + level : "") + " )";
            }

            if (m_autoUnpackNormals)
            {
                result = string.Format(m_normalMapUnpackMode, result);
            }

            RegisterLocalVariable(0, result, ref dataCollector, "texArray" + OutputId);
            return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue));
        }
Exemple #17
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));
        }
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar)
        {
            if (dataCollector.PortCategory == MasterNodePortCategory.Tessellation)
            {
                UIUtils.ShowMessage(UniqueId, m_nodeAttribs.Name + " cannot be used on Master Node Tessellation port");
                return("-1");
            }

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

            string tiling = string.Empty;
            string offset = string.Empty;

            string portProperty = string.Empty;

            if (m_texPort.IsConnected)
            {
                portProperty = m_texPort.GeneratePortInstructions(ref dataCollector);
            }
            else if (m_referenceArrayId > -1)
            {
                TexturePropertyNode temp = UIUtils.GetTexturePropertyNode(m_referenceArrayId);
                if (temp != null)
                {
                    portProperty = temp.BaseGenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalVar);
                }
            }

            //TEMPLATES
            if (dataCollector.MasterNodeCategory == AvailableShaderTypes.Template)
            {
                if (m_outputPorts[0].IsLocalValue(dataCollector.PortCategory))
                {
                    return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
                }

                string uvName   = string.Empty;
                string result   = string.Empty;
                string indexStr = m_textureCoordChannel > 0 ? (m_textureCoordChannel + 1).ToString() : "";
                string sizeDif  = string.Empty;
                if (m_texcoordSize == 3)
                {
                    sizeDif = "3";
                }
                else if (m_texcoordSize == 4)
                {
                    sizeDif = "4";
                }

                if (dataCollector.TemplateDataCollectorInstance.GetCustomInterpolatedData(TemplateHelperFunctions.IntToUVChannelInfo[m_textureCoordChannel], m_outputPorts[0].DataType, PrecisionType.Float, ref result, false, dataCollector.PortCategory))
                {
                    uvName = result;
                }
                else if (dataCollector.TemplateDataCollectorInstance.HasUV(m_textureCoordChannel))
                {
                    uvName = dataCollector.TemplateDataCollectorInstance.GetUVName(m_textureCoordChannel, m_outputPorts[0].DataType);
                }
                else
                {
                    uvName = dataCollector.TemplateDataCollectorInstance.RegisterUV(m_textureCoordChannel, m_outputPorts[0].DataType);
                }
                string currPropertyName = GetValidPropertyName();
                if (!string.IsNullOrEmpty(portProperty) && portProperty != "0.0")
                {
                    currPropertyName = portProperty;
                }
                if (!string.IsNullOrEmpty(currPropertyName))
                {
                    string finalTexCoordName      = "uv" + indexStr + (m_texcoordSize > 2 ? "s" + sizeDif : "") + currPropertyName;
                    string dummyPropertyTexcoords = currPropertyName + "_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(dummyPropertyTexcoords);
                    dummyPropertyTexcoords = m_texCoordsHelper.GenerateShaderForOutput(0, ref dataCollector, false);

                    if (m_texcoordSize > 2)
                    {
                        dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, m_outputPorts[0].DataType, finalTexCoordName, uvName);
                        dataCollector.AddLocalVariable(UniqueId, finalTexCoordName + ".xy", string.Format(Constants.TilingOffsetFormat, uvName + ".xy", dummyPropertyTexcoords + ".xy", dummyPropertyTexcoords + ".zw") + ";");
                        m_outputPorts[0].SetLocalValue(finalTexCoordName, dataCollector.PortCategory);
                    }
                    else
                    {
                        RegisterLocalVariable(0, string.Format(Constants.TilingOffsetFormat, uvName, dummyPropertyTexcoords + ".xy", dummyPropertyTexcoords + ".zw"), ref dataCollector, finalTexCoordName);
                    }
                    //RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, dummyPropertyTexcoords+".xy", dummyPropertyTexcoords+".zw" ), ref dataCollector, finalTexCoordName );
                }
                else
                {
                    string finalTexCoordName = "texCoord" + OutputId;
                    tiling = m_tilingPort.GeneratePortInstructions(ref dataCollector);
                    offset = m_offsetPort.GeneratePortInstructions(ref dataCollector);

                    if (m_texcoordSize > 2)
                    {
                        dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, m_outputPorts[0].DataType, finalTexCoordName, uvName);
                        dataCollector.AddLocalVariable(UniqueId, finalTexCoordName + ".xy", string.Format(Constants.TilingOffsetFormat, uvName + ".xy", tiling, offset) + ";");
                        m_outputPorts[0].SetLocalValue(finalTexCoordName, dataCollector.PortCategory);
                    }
                    else
                    {
                        RegisterLocalVariable(0, string.Format(Constants.TilingOffsetFormat, uvName, tiling, offset), ref dataCollector, finalTexCoordName);
                    }
                    //RegisterLocalVariable( 0, string.Format( Constants.TilingOffsetFormat, uvName, tiling, offset ), ref dataCollector, finalTexCoordName );
                }
                return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
            }

            //SURFACE
            string propertyName = GetValidPropertyName();

            if (!string.IsNullOrEmpty(portProperty) && portProperty != "0.0")
            {
                propertyName = portProperty;
            }

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

            if (!m_tilingPort.IsConnected && m_tilingPort.Vector2InternalData == Vector2.one)
            {
                tiling = null;
            }
            else
            {
                tiling = m_tilingPort.GeneratePortInstructions(ref dataCollector);
            }

            if (!m_offsetPort.IsConnected && m_offsetPort.Vector2InternalData == Vector2.zero)
            {
                offset = null;
            }
            else
            {
                offset = m_offsetPort.GeneratePortInstructions(ref dataCollector);
            }

            if (!string.IsNullOrEmpty(propertyName) /*m_referenceArrayId > -1*/)
            {
                m_surfaceTexcoordName = GeneratorUtils.GenerateAutoUVs(ref dataCollector, UniqueId, m_textureCoordChannel, propertyName, m_outputPorts[0].DataType, tiling, offset, OutputId);
            }
            else
            {
                m_surfaceTexcoordName = GeneratorUtils.GenerateAutoUVs(ref dataCollector, UniqueId, m_textureCoordChannel, null, m_outputPorts[0].DataType, tiling, offset, OutputId);
            }

            m_outputPorts[0].SetLocalValue(m_surfaceTexcoordName, dataCollector.PortCategory);
            return(GetOutputVectorItem(0, outputId, m_outputPorts[0].LocalValue(dataCollector.PortCategory)));
        }
        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));
        }