예제 #1
0
        public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar)
        {
            if (dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation)
            {
                UIUtils.ShowNoVertexModeNodeMessage(this);
                return("0");
            }

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

            if (!(dataCollector.IsTemplate && dataCollector.IsSRP))
            {
                dataCollector.AddToIncludes(UniqueId, Constants.UnityCgLibFuncs);
            }

            if (!dataCollector.IsTemplate || dataCollector.TemplateDataCollectorInstance.CurrentSRPType != TemplateSRPType.HD)
            {
                dataCollector.AddToUniforms(UniqueId, "uniform sampler2D _CameraDepthTexture;");
            }

            string    screenPos     = string.Empty;
            string    screenPosNorm = string.Empty;
            InputPort vertexPosPort = GetInputPortByUniqueId(1);

            if (vertexPosPort.IsConnected)
            {
                string vertexPosVar = "vertexPos" + OutputId;
                GenerateInputInVertex(ref dataCollector, 1, vertexPosVar, false);
                screenPos     = GeneratorUtils.GenerateScreenPositionForValue(vertexPosVar, OutputId, ref dataCollector, UniqueId, m_currentPrecisionType, !dataCollector.UsingCustomScreenPos);
                screenPosNorm = GeneratorUtils.GenerateScreenPositionNormalizedForValue(vertexPosVar, OutputId, ref dataCollector, UniqueId, m_currentPrecisionType, !dataCollector.UsingCustomScreenPos);
            }
            else
            {
                screenPos     = GeneratorUtils.GenerateScreenPosition(ref dataCollector, UniqueId, m_currentPrecisionType, !dataCollector.UsingCustomScreenPos);
                screenPosNorm = GeneratorUtils.GenerateScreenPositionNormalized(ref dataCollector, UniqueId, m_currentPrecisionType, !dataCollector.UsingCustomScreenPos);
            }

            string screenDepth = TemplateHelperFunctions.CreateDepthFetch(dataCollector, screenPos);

            if (m_convertToLinear)
            {
                if (dataCollector.IsTemplate && dataCollector.IsSRP)
                {
                    screenDepth = string.Format("LinearEyeDepth({0},_ZBufferParams)", screenDepth);
                }
                else
                {
                    screenDepth = string.Format("LinearEyeDepth({0})", screenDepth);
                }
            }
            else
            {
                screenDepth = string.Format("({0}*( _ProjectionParams.z - _ProjectionParams.y ))", screenDepth);
            }

            string distance = GetInputPortByUniqueId(0).GeneratePortInstructions(ref dataCollector);

            dataCollector.AddLocalVariable(UniqueId, "float screenDepth" + OutputId + " = " + screenDepth + ";");

            string finalVarName  = "distanceDepth" + OutputId;
            string finalVarValue = string.Empty;

            if (dataCollector.IsTemplate && dataCollector.IsSRP)
            {
                finalVarValue = "abs( ( screenDepth" + OutputId + " - LinearEyeDepth( " + screenPosNorm + ".z,_ZBufferParams ) ) / ( " + distance + " ) )";
            }
            else
            {
                finalVarValue = "abs( ( screenDepth" + OutputId + " - LinearEyeDepth( " + screenPosNorm + ".z ) ) / ( " + distance + " ) )";
            }

            if (m_saturate)
            {
                finalVarValue = string.Format("saturate( {0} )", finalVarValue);
            }

            dataCollector.AddLocalVariable(UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT, finalVarName, finalVarValue);
            m_outputPorts[0].SetLocalValue(finalVarName, dataCollector.PortCategory);
            return(GetOutputColorItem(0, outputId, finalVarName));
        }