public override string GenerateShaderForOutput(int outputId, WirePortDataType inputPortType, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar) { base.GenerateShaderForOutput(outputId, inputPortType, ref dataCollector, ignoreLocalvar); if (outputId < m_outputData.Count) { Dictionary <string, InputPort> inputs = InputPortsDict; string value = m_outputData[outputId].expression; for (int i = 0; i < m_outputData[outputId].inputData.Count; i++) { if (inputs.ContainsKey(m_outputData[outputId].inputData[i].name)) { InputPort inputPort = inputs[m_outputData[outputId].inputData[i].name]; if (inputPort != null) { string inputValue = inputPort.GenerateShaderForOutput(ref dataCollector, WirePortDataType.OBJECT, ignoreLocalvar); value = value.Replace(m_outputData[outputId].inputData[i].name, inputValue); } else { UIUtils.ShowMessage(m_outputData[outputId].inputData[i].name + " invalid on the inputs list", MessageSeverity.Error); return(string.Empty); } } else { UIUtils.ShowMessage(m_outputData[outputId].inputData[i].name + " Not found on the inputs list", MessageSeverity.Error); return(string.Empty); } } return(value); } return(string.Empty); }
public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar) { if (!m_texPort.IsConnected) { UIUtils.ShowMessage(UniqueId, "Parallax Occlusion Mapping node only works if a Texture Object is connected to its Tex (R) port"); return("0"); } base.GenerateShaderForOutput(outputId, ref dataCollector, ignoreLocalvar); WirePortDataType texType = (m_pomTexType == POMTexTypes.Texture3D)?WirePortDataType.SAMPLER3D: WirePortDataType.SAMPLER2D; GeneratePOMfunction(); string arrayIndex = m_arrayIndexPort.Visible?m_arrayIndexPort.GeneratePortInstructions(ref dataCollector):"0"; string textcoords = m_uvPort.GeneratePortInstructions(ref dataCollector); if (m_pomTexType == POMTexTypes.Texture3D) { string texName = "pomTexCoord" + OutputId; dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT3, texName, string.Format("float3({0},{1})", textcoords, arrayIndex)); textcoords = texName; } string texture = m_texPort.GenerateShaderForOutput(ref dataCollector, texType, false, true); string scale = m_defaultScale.ToString(); if (m_scalePort.IsConnected) { scale = m_scalePort.GeneratePortInstructions(ref dataCollector); } string viewDirTan = ""; if (!m_viewdirTanPort.IsConnected) { if (!dataCollector.DirtyNormal) { dataCollector.ForceNormal = true; } if (dataCollector.IsTemplate) { viewDirTan = dataCollector.TemplateDataCollectorInstance.GetTangentViewDir(CurrentPrecisionType); } else { viewDirTan = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, ViewSpace.Tangent); //dataCollector.AddToInput( UniqueId, SurfaceInputs.VIEW_DIR, m_currentPrecisionType ); //viewDirTan = Constants.InputVarStr + "." + UIUtils.GetInputValueFromType( SurfaceInputs.VIEW_DIR ); } } else { viewDirTan = m_viewdirTanPort.GeneratePortInstructions(ref dataCollector); } //generate world normal string normalWorld = string.Empty; if (dataCollector.IsTemplate) { normalWorld = dataCollector.TemplateDataCollectorInstance.GetWorldNormal(CurrentPrecisionType); } else { dataCollector.AddToInput(UniqueId, SurfaceInputs.WORLD_NORMAL, CurrentPrecisionType); dataCollector.AddToInput(UniqueId, SurfaceInputs.INTERNALDATA, addSemiColon: false); normalWorld = GeneratorUtils.GenerateWorldNormal(ref dataCollector, UniqueId); } //string normalWorld = "WorldNormalVector( " + Constants.InputVarStr + ", float3( 0, 0, 1 ) )"; //generate viewDir in world space //string worldPos = string.Empty; //if( dataCollector.IsTemplate ) //{ // worldPos = dataCollector.TemplateDataCollectorInstance.GetWorldPos(); //} //else //{ // dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_POS ); // worldPos = Constants.InputVarStr + ".worldPos"; //} //if( !dataCollector.IsTemplate ) // dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_POS ); string worldViewDir = GeneratorUtils.GenerateViewDirection(ref dataCollector, UniqueId, ViewSpace.World); //dataCollector.AddToLocalVariables( UniqueId, m_currentPrecisionType, WirePortDataType.FLOAT3, WorldDirVarStr, TemplateHelperFunctions.WorldSpaceViewDir( dataCollector, worldPos, true ) ); string dx = "ddx(" + textcoords + ")"; string dy = "ddy(" + textcoords + ")"; string refPlane = m_defaultRefPlane.ToString(); if (m_refPlanePort.IsConnected) { refPlane = m_refPlanePort.GeneratePortInstructions(ref dataCollector); } string curvature = "float2(" + m_CurvatureVector.x + "," + m_CurvatureVector.y + ")"; if (m_useCurvature) { dataCollector.AddToProperties(UniqueId, "[Header(Parallax Occlusion Mapping)]", 300); dataCollector.AddToProperties(UniqueId, "_CurvFix(\"Curvature Bias\", Range( 0 , 1)) = 1", 301); dataCollector.AddToUniforms(UniqueId, "uniform float _CurvFix;"); if (m_curvaturePort.IsConnected) { curvature = m_curvaturePort.GeneratePortInstructions(ref dataCollector); } } string localVarName = "OffsetPOM" + OutputId; string textCoordsST = string.Empty; //string textureSTType = dataCollector.IsSRP ? "float4 " : "uniform float4 "; //dataCollector.AddToUniforms( UniqueId, textureSTType + texture +"_ST;"); if (m_texCoordsHelper == null) { m_texCoordsHelper = CreateInstance <Vector4Node>(); m_texCoordsHelper.ContainerGraph = ContainerGraph; m_texCoordsHelper.SetBaseUniqueId(UniqueId, true); m_texCoordsHelper.RegisterPropertyOnInstancing = false; m_texCoordsHelper.AddGlobalToSRPBatcher = true; } if (UIUtils.CurrentWindow.OutsideGraph.IsInstancedShader) { m_texCoordsHelper.CurrentParameterType = PropertyType.InstancedProperty; } else { m_texCoordsHelper.CurrentParameterType = PropertyType.Global; } m_texCoordsHelper.ResetOutputLocals(); m_texCoordsHelper.SetRawPropertyName(texture + "_ST"); textCoordsST = m_texCoordsHelper.GenerateShaderForOutput(0, ref dataCollector, false); ////// if (m_pomTexType == POMTexTypes.TextureArray) { dataCollector.UsingArrayDerivatives = true; } string textureArgs = string.Empty; if (m_pomTexType == POMTexTypes.TextureArray) { if (UIUtils.CurrentWindow.OutsideGraph.IsSRP) { textureArgs = "TEXTURE2D_ARRAY_PARAM(" + texture + " , " + "sampler##" + texture + ")"; } else { textureArgs = "UNITY_PASS_TEX2DARRAY(" + texture + ")"; } } else { bool sampleThroughMacros = UIUtils.CurrentWindow.OutsideGraph.SamplingThroughMacros; if (sampleThroughMacros) { dataCollector.AddToUniforms(UniqueId, string.Format(Constants.SamplerDeclarationSRPMacros[TextureType.Texture2D], texture)); textureArgs = string.Format("{0},sampler{0}", texture); } else { textureArgs = texture; } } //string functionResult = dataCollector.AddFunctions( m_functionHeader, m_functionBody, ( (m_pomTexType == POMTexTypes.TextureArray) ? "UNITY_PASS_TEX2DARRAY(" + texture + ")": texture), textcoords, dx, dy, normalWorld, worldViewDir, viewDirTan, m_minSamples, m_maxSamples, scale, refPlane, texture+"_ST.xy", curvature, arrayIndex ); string functionResult = dataCollector.AddFunctions(m_functionHeader, m_functionBody, textureArgs, textcoords, dx, dy, normalWorld, worldViewDir, viewDirTan, m_inlineMinSamples.GetValueOrProperty(false), m_inlineMinSamples.GetValueOrProperty(false), scale, refPlane, textCoordsST + ".xy", curvature, arrayIndex); dataCollector.AddLocalVariable(UniqueId, CurrentPrecisionType, m_pomUVPort.DataType, localVarName, functionResult); return(GetOutputVectorItem(0, outputId, localVarName)); }
public override string GenerateShaderForOutput(int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar) { if (dataCollector.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) { 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)); }
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); 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)); }
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)); }