コード例 #1
0
ファイル: FunctionNode.cs プロジェクト: EGSP/Flappy-Bird
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            OutputPort     outputPort     = GetOutputPortByUniqueId(outputId);
            FunctionOutput functionOutput = GetFunctionOutputByUniqueId(outputId);

            if (outputPort.IsLocalValue(dataCollector.PortCategory))
            {
                return(outputPort.LocalValue(dataCollector.PortCategory));
            }

            m_functionGraph.CurrentPrecision = ContainerGraph.ParentWindow.CurrentGraph.CurrentPrecision;
            ParentGraph cachedGraph = ContainerGraph.ParentWindow.CustomGraph;

            m_outsideGraph = cachedGraph;
            ContainerGraph.ParentWindow.CustomGraph = m_functionGraph;
#if ADD_SHADER_FUNCTION_HEADERS
            if (m_reordenator != null && m_reordenator.RecursiveCount() > 0 && m_reordenator.HasTitle)
            {
                dataCollector.AddToProperties(UniqueId, "[Header(" + m_reordenator.HeaderTitle.Replace("-", " ") + ")]", m_reordenator.OrderIndex);
            }
#endif
            string result = string.Empty;
            for (int i = 0; i < m_allFunctionInputs.Count; i++)
            {
                if (!m_allFunctionInputs[i].InputPorts[0].IsConnected || m_inputPorts[i].IsConnected)
                {
                    m_allFunctionInputs[i].OnPortGeneration += FunctionNodeOnPortGeneration;
                }
            }

            result += functionOutput.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar);

            for (int i = 0; i < m_allFunctionInputs.Count; i++)
            {
                if (!m_allFunctionInputs[i].InputPorts[0].IsConnected || m_inputPorts[i].IsConnected)
                {
                    m_allFunctionInputs[i].OnPortGeneration -= FunctionNodeOnPortGeneration;
                }
            }

            ContainerGraph.ParentWindow.CustomGraph = cachedGraph;

            if (outputPort.ConnectionCount > 1)
            {
                RegisterLocalVariable(outputId, result, ref dataCollector);
            }
            else
            {
                outputPort.SetLocalValue(result, dataCollector.PortCategory);
            }

            return(outputPort.LocalValue(dataCollector.PortCategory));
        }
コード例 #2
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (string.IsNullOrEmpty(m_code))
            {
                UIUtils.ShowMessage("Custom Expression need to have code associated", MessageSeverity.Warning);
                return("0");
            }

            m_code = m_code.Replace("\r\n", "\n");

            bool codeContainsReturn = m_code.Contains(ReturnHelper);

            if (!codeContainsReturn && outputId != 0 && m_mode == CustomExpressionMode.Create && !m_voidMode)
            {
                UIUtils.ShowMessage("Attempting to get value from inexisting inout/out variable", MessageSeverity.Warning);
                return("0");
            }

            OutputPort outputPort = GetOutputPortByUniqueId(outputId);

            if (outputPort.IsLocalValue(dataCollector.PortCategory))
            {
                return(outputPort.LocalValue(dataCollector.PortCategory));
            }

            string expressionName = UIUtils.RemoveInvalidCharacters(m_customExpressionName);
            string localVarName   = "local" + expressionName;

            if (m_generateUniqueName)
            {
                expressionName += OutputId;
            }
            localVarName += OutputId;

            int count = m_inputPorts.Count;

            if (count > 0)
            {
                if (m_mode == CustomExpressionMode.Call || m_voidMode)
                {
                    string mainData = m_inputPorts[0].GeneratePortInstructions(ref dataCollector);
                    RegisterLocalVariable(0, string.Format(Constants.CodeWrapper, mainData), ref dataCollector, localVarName);
                }

                if (codeContainsReturn)
                {
                    string function = WrapCodeInFunction(dataCollector.IsTemplate, expressionName, false);

                    string functionCall = expressionName + "( ";
                    for (int i = m_firstAvailablePort; i < count; i++)
                    {
                        string inputPortLocalVar = m_inputPorts[i].Name + OutputId;
                        string result            = m_inputPorts[i].GeneratePortInstructions(ref dataCollector);
                        dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_inputPorts[i].DataType, inputPortLocalVar, result);
                        int idx = i - m_firstAvailablePort;
                        if (m_variableQualifiers[idx] != VariableQualifiers.In)
                        {
                            OutputPort currOutputPort = GetOutputPortByUniqueId(CreateOutputId(m_inputPorts[i].PortId));
                            currOutputPort.SetLocalValue(inputPortLocalVar, dataCollector.PortCategory);
                        }
                        functionCall += inputPortLocalVar;
                        if (i < (count - 1))
                        {
                            functionCall += " , ";
                        }
                    }
                    functionCall += " )";

                    if (m_mode == CustomExpressionMode.Call || m_voidMode)
                    {
                        dataCollector.AddLocalVariable(0, functionCall + ";", true);
                    }
                    else
                    {
                        RegisterLocalVariable(0, functionCall, ref dataCollector, localVarName);
                    }

                    dataCollector.AddFunction(expressionName, function);
                }
                else
                {
                    string localCode = m_code;
                    if (m_mode == CustomExpressionMode.Call || m_voidMode)
                    {
                        for (int i = m_firstAvailablePort; i < count; i++)
                        {
                            string inputPortLocalVar = m_inputPorts[i].Name + OutputId;
                            localCode = localCode.Replace(m_inputPorts[i].Name, inputPortLocalVar);

                            if (m_inputPorts[i].IsConnected)
                            {
                                string result = m_inputPorts[i].GenerateShaderForOutput(ref dataCollector, m_inputPorts[i].DataType, true, true);
                                dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_inputPorts[i].DataType, inputPortLocalVar, result);
                            }
                            else
                            {
                                dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_inputPorts[i].DataType, inputPortLocalVar, m_inputPorts[i].WrappedInternalData);
                            }
                            int idx = i - m_firstAvailablePort;
                            if (m_variableQualifiers[idx] != VariableQualifiers.In)
                            {
                                OutputPort currOutputPort = GetOutputPortByUniqueId(CreateOutputId(m_inputPorts[i].PortId));
                                currOutputPort.SetLocalValue(inputPortLocalVar, dataCollector.PortCategory);
                            }
                        }
                        string[] codeLines = localCode.Split('\n');
                        for (int codeIdx = 0; codeIdx < codeLines.Length; codeIdx++)
                        {
                            dataCollector.AddLocalVariable(0, codeLines[codeIdx], true);
                        }
                    }
                    else
                    {
                        string function = WrapCodeInFunction(dataCollector.IsTemplate, expressionName, true);

                        string functionCall = expressionName + "( ";
                        for (int i = m_firstAvailablePort; i < count; i++)
                        {
                            string inputPortLocalVar = m_inputPorts[i].Name + OutputId;
                            string result            = m_inputPorts[i].GeneratePortInstructions(ref dataCollector);
                            dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, m_inputPorts[i].DataType, inputPortLocalVar, result);
                            int idx = i - m_firstAvailablePort;
                            if (m_variableQualifiers[idx] != VariableQualifiers.In)
                            {
                                OutputPort currOutputPort = GetOutputPortByUniqueId(CreateOutputId(m_inputPorts[i].PortId));
                                currOutputPort.SetLocalValue(inputPortLocalVar, dataCollector.PortCategory);
                            }
                            functionCall += inputPortLocalVar;
                            if (i < (count - 1))
                            {
                                functionCall += " , ";
                            }
                        }
                        functionCall += " )";
                        RegisterLocalVariable(0, functionCall, ref dataCollector, localVarName);
                        dataCollector.AddFunction(expressionName, function);
                    }
                }

                return(outputPort.LocalValue(dataCollector.PortCategory));
            }
            else
            {
                if (m_code.Contains(ReturnHelper))
                {
                    string function = WrapCodeInFunction(dataCollector.IsTemplate, expressionName, false);
                    dataCollector.AddFunction(expressionName, function);
                    string functionCall = expressionName + "()";
                    RegisterLocalVariable(0, functionCall, ref dataCollector, localVarName);
                }
                else
                {
                    RegisterLocalVariable(0, string.Format(Constants.CodeWrapper, m_code), ref dataCollector, localVarName);
                }

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