public string GetTexture2DUniformValue() { if (PropertyName == "_CameraDepthTexture") { return(Constants.CameraDepthTextureValue); } else { return(GeneratorUtils.GetPropertyDeclaraction(PropertyName, TextureType.Texture2D, ";")); } }
public override bool GetUniformData(out string dataType, out string dataName, ref bool fullValue) { m_excludeUniform = false; ParentGraph outsideGraph = UIUtils.CurrentWindow.OutsideGraph; #if UNITY_2018_1_OR_NEWER if (outsideGraph.SamplingMacros || m_currentType == TextureType.Texture2DArray) #else if ((outsideGraph.SamplingMacros && !outsideGraph.IsStandardSurface) || m_currentType == TextureType.Texture2DArray) #endif { if (outsideGraph.IsSRP) { if (Constants.TexDeclarationSRPMacros.ContainsKey(m_currentType)) { dataName = GeneratorUtils.GetPropertyDeclaraction(PropertyName, m_currentType, ";"); dataType = string.Empty; fullValue = true; return(true); } } else if (Constants.TexDeclarationStandardMacros.ContainsKey(m_currentType)) { #if !UNITY_2018_1_OR_NEWER if (m_currentType == TextureType.Texture2DArray && outsideGraph.IsStandardSurface) { dataName = string.Format(Constants.TexDeclarationStandardMacros[m_currentType], PropertyName); } else #endif dataName = GeneratorUtils.GetPropertyDeclaraction(PropertyName, m_currentType, ";"); dataType = string.Empty; fullValue = true; return(true); } } //TODO: this is a hack and needs to be properly fixed if (PropertyName == "_CameraDepthTexture") { m_excludeUniform = true; dataType = "UNITY_DECLARE_DEPTH_TEXTURE("; dataName = m_propertyName + " )"; return(true); } dataType = UIUtils.TextureTypeToCgType(m_currentType); dataName = m_propertyName; return(true); }
private void GeneratePOMfunction(ref MasterNodeDataCollector dataCollector) { ParentGraph outsideGraph = UIUtils.CurrentWindow.OutsideGraph; m_functionBody = string.Empty; switch (m_texPort.DataType) { default: case WirePortDataType.SAMPLER2D: { string sampleParam = string.Empty; sampleParam = GeneratorUtils.GetPropertyDeclaraction("heightMap", TextureType.Texture2D, ", ") + GeneratorUtils.GetSamplerDeclaraction("samplerheightMap", TextureType.Texture2D, ", "); IOUtils.AddFunctionHeader(ref m_functionBody, string.Format("inline float2 POM( {0}float2 uvs, float2 dx, float2 dy, float3 normalWorld, float3 viewWorld, float3 viewDirTan, int minSamples, int maxSamples, float parallax, float refPlane, float2 tilling, float2 curv, int index )", sampleParam)); } break; case WirePortDataType.SAMPLER3D: { string sampleParam = string.Empty; sampleParam = GeneratorUtils.GetPropertyDeclaraction("heightMap", TextureType.Texture3D, ", ") + GeneratorUtils.GetSamplerDeclaraction("samplerheightMap", TextureType.Texture3D, ", "); IOUtils.AddFunctionHeader(ref m_functionBody, string.Format("inline float2 POM( {0}float3 uvs, float3 dx, float3 dy, float3 normalWorld, float3 viewWorld, float3 viewDirTan, int minSamples, int maxSamples, float parallax, float refPlane, float2 tilling, float2 curv, int index )", sampleParam)); } break; case WirePortDataType.SAMPLER2DARRAY: if (outsideGraph.IsSRP) { IOUtils.AddFunctionHeader(ref m_functionBody, "inline float2 POM( TEXTURE2D_ARRAY(heightMap), SAMPLER(samplerheightMap), float2 uvs, float2 dx, float2 dy, float3 normalWorld, float3 viewWorld, float3 viewDirTan, int minSamples, int maxSamples, float parallax, float refPlane, float2 tilling, float2 curv, int index )"); } else #if !UNITY_2018_1_OR_NEWER if (outsideGraph.IsStandardSurface) { IOUtils.AddFunctionHeader(ref m_functionBody, "inline float2 POM( UNITY_ARGS_TEX2DARRAY(heightMap), float2 uvs, float2 dx, float2 dy, float3 normalWorld, float3 viewWorld, float3 viewDirTan, int minSamples, int maxSamples, float parallax, float refPlane, float2 tilling, float2 curv, int index )"); } else #endif { IOUtils.AddFunctionHeader(ref m_functionBody, "inline float2 POM( UNITY_DECLARE_TEX2DARRAY_NOSAMPLER(heightMap), SamplerState samplerheightMap, float2 uvs, float2 dx, float2 dy, float3 normalWorld, float3 viewWorld, float3 viewDirTan, int minSamples, int maxSamples, float parallax, float refPlane, float2 tilling, float2 curv, int index )"); } break; } IOUtils.AddFunctionLine(ref m_functionBody, "float3 result = 0;"); IOUtils.AddFunctionLine(ref m_functionBody, "int stepIndex = 0;"); //IOUtils.AddFunctionLine( ref m_functionBody, "int numSteps = ( int )( minSamples + dot( viewWorld, normalWorld ) * ( maxSamples - minSamples ) );" ); //IOUtils.AddFunctionLine( ref m_functionBody, "int numSteps = ( int )lerp( maxSamples, minSamples, length( fwidth( uvs ) ) * 10 );" ); IOUtils.AddFunctionLine(ref m_functionBody, "int numSteps = ( int )lerp( (float)maxSamples, (float)minSamples, saturate( dot( normalWorld, viewWorld ) ) );"); IOUtils.AddFunctionLine(ref m_functionBody, "float layerHeight = 1.0 / numSteps;"); IOUtils.AddFunctionLine(ref m_functionBody, "float2 plane = parallax * ( viewDirTan.xy / viewDirTan.z );"); IOUtils.AddFunctionLine(ref m_functionBody, "uvs.xy += refPlane * plane;"); IOUtils.AddFunctionLine(ref m_functionBody, "float2 deltaTex = -plane * layerHeight;"); IOUtils.AddFunctionLine(ref m_functionBody, "float2 prevTexOffset = 0;"); IOUtils.AddFunctionLine(ref m_functionBody, "float prevRayZ = 1.0f;"); IOUtils.AddFunctionLine(ref m_functionBody, "float prevHeight = 0.0f;"); IOUtils.AddFunctionLine(ref m_functionBody, "float2 currTexOffset = deltaTex;"); IOUtils.AddFunctionLine(ref m_functionBody, "float currRayZ = 1.0f - layerHeight;"); IOUtils.AddFunctionLine(ref m_functionBody, "float currHeight = 0.0f;"); IOUtils.AddFunctionLine(ref m_functionBody, "float intersection = 0;"); IOUtils.AddFunctionLine(ref m_functionBody, "float2 finalTexOffset = 0;"); IOUtils.AddFunctionLine(ref m_functionBody, "while ( stepIndex < numSteps + 1 )"); IOUtils.AddFunctionLine(ref m_functionBody, "{"); string textureProp = "heightMap"; string sampleState = "samplerheightMap"; string uvs = "uvs + currTexOffset"; if (m_texPort.DataType == WirePortDataType.SAMPLER3D) { uvs = "float3(uvs.xy + currTexOffset, uvs.z)"; } else if (m_texPort.DataType == WirePortDataType.SAMPLER2DARRAY) { uvs = outsideGraph.IsSRP ? uvs + ", index" : "float3(" + uvs + ", index)"; } string samplingCall = GeneratorUtils.GenerateSamplingCall(ref dataCollector, m_texPort.DataType, textureProp, sampleState, uvs, MipType.Derivative, "dx", "dy"); if (m_useCurvature) { IOUtils.AddFunctionLine(ref m_functionBody, " \tresult.z = dot( curv, currTexOffset * currTexOffset );"); IOUtils.AddFunctionLine(ref m_functionBody, " \tcurrHeight = " + samplingCall + "." + m_channelTypeVal[m_selectedChannelInt] + " * ( 1 - result.z );"); } else { IOUtils.AddFunctionLine(ref m_functionBody, " \tcurrHeight = " + samplingCall + "." + m_channelTypeVal[m_selectedChannelInt] + ";"); } IOUtils.AddFunctionLine(ref m_functionBody, " \tif ( currHeight > currRayZ )"); IOUtils.AddFunctionLine(ref m_functionBody, " \t{"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tstepIndex = numSteps + 1;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t}"); IOUtils.AddFunctionLine(ref m_functionBody, " \telse"); IOUtils.AddFunctionLine(ref m_functionBody, " \t{"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tstepIndex++;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tprevTexOffset = currTexOffset;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tprevRayZ = currRayZ;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tprevHeight = currHeight;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tcurrTexOffset += deltaTex;"); if (m_useCurvature) { IOUtils.AddFunctionLine(ref m_functionBody, " \t \tcurrRayZ -= layerHeight * ( 1 - result.z ) * (1+_CurvFix);"); } else { IOUtils.AddFunctionLine(ref m_functionBody, " \t \tcurrRayZ -= layerHeight;"); } IOUtils.AddFunctionLine(ref m_functionBody, " \t}"); IOUtils.AddFunctionLine(ref m_functionBody, "}"); if (m_sidewallSteps > 0) { IOUtils.AddFunctionLine(ref m_functionBody, "int sectionSteps = " + m_sidewallSteps + ";"); IOUtils.AddFunctionLine(ref m_functionBody, "int sectionIndex = 0;"); IOUtils.AddFunctionLine(ref m_functionBody, "float newZ = 0;"); IOUtils.AddFunctionLine(ref m_functionBody, "float newHeight = 0;"); IOUtils.AddFunctionLine(ref m_functionBody, "while ( sectionIndex < sectionSteps )"); IOUtils.AddFunctionLine(ref m_functionBody, "{"); IOUtils.AddFunctionLine(ref m_functionBody, " \tintersection = ( prevHeight - prevRayZ ) / ( prevHeight - currHeight + currRayZ - prevRayZ );"); IOUtils.AddFunctionLine(ref m_functionBody, " \tfinalTexOffset = prevTexOffset + intersection * deltaTex;"); IOUtils.AddFunctionLine(ref m_functionBody, " \tnewZ = prevRayZ - intersection * layerHeight;"); string uvs2 = "uvs + finalTexOffset"; if (m_texPort.DataType == WirePortDataType.SAMPLER3D) { uvs2 = "float3(uvs.xy + finalTexOffset, uvs.z)"; } else if (m_texPort.DataType == WirePortDataType.SAMPLER2DARRAY) { uvs2 = outsideGraph.IsSRP ? uvs2 + ", index" : "float3(" + uvs2 + ", index)"; } string samplingCall2 = GeneratorUtils.GenerateSamplingCall(ref dataCollector, m_texPort.DataType, textureProp, sampleState, uvs2, MipType.Derivative, "dx", "dy"); IOUtils.AddFunctionLine(ref m_functionBody, " \tnewHeight = " + samplingCall2 + "." + m_channelTypeVal[m_selectedChannelInt] + ";"); IOUtils.AddFunctionLine(ref m_functionBody, " \tif ( newHeight > newZ )"); IOUtils.AddFunctionLine(ref m_functionBody, " \t{"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tcurrTexOffset = finalTexOffset;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tcurrHeight = newHeight;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tcurrRayZ = newZ;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tdeltaTex = intersection * deltaTex;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tlayerHeight = intersection * layerHeight;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t}"); IOUtils.AddFunctionLine(ref m_functionBody, " \telse"); IOUtils.AddFunctionLine(ref m_functionBody, " \t{"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tprevTexOffset = finalTexOffset;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tprevHeight = newHeight;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tprevRayZ = newZ;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tdeltaTex = ( 1 - intersection ) * deltaTex;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tlayerHeight = ( 1 - intersection ) * layerHeight;"); IOUtils.AddFunctionLine(ref m_functionBody, " \t}"); IOUtils.AddFunctionLine(ref m_functionBody, " \tsectionIndex++;"); IOUtils.AddFunctionLine(ref m_functionBody, "}"); } else { IOUtils.AddFunctionLine(ref m_functionBody, "finalTexOffset = currTexOffset;"); } if (m_useCurvature) { IOUtils.AddFunctionLine(ref m_functionBody, "#ifdef UNITY_PASS_SHADOWCASTER"); IOUtils.AddFunctionLine(ref m_functionBody, "if ( unity_LightShadowBias.z == 0.0 )"); IOUtils.AddFunctionLine(ref m_functionBody, "{"); IOUtils.AddFunctionLine(ref m_functionBody, "#endif"); IOUtils.AddFunctionLine(ref m_functionBody, " \tif ( result.z > 1 )"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tclip( -1 );"); IOUtils.AddFunctionLine(ref m_functionBody, "#ifdef UNITY_PASS_SHADOWCASTER"); IOUtils.AddFunctionLine(ref m_functionBody, "}"); IOUtils.AddFunctionLine(ref m_functionBody, "#endif"); } if (m_clipEnds) { IOUtils.AddFunctionLine(ref m_functionBody, "result.xy = uvs.xy + finalTexOffset;"); IOUtils.AddFunctionLine(ref m_functionBody, "#ifdef UNITY_PASS_SHADOWCASTER"); IOUtils.AddFunctionLine(ref m_functionBody, "if ( unity_LightShadowBias.z == 0.0 )"); IOUtils.AddFunctionLine(ref m_functionBody, "{"); IOUtils.AddFunctionLine(ref m_functionBody, "#endif"); IOUtils.AddFunctionLine(ref m_functionBody, " \tif ( result.x < 0 )"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tclip( -1 );"); IOUtils.AddFunctionLine(ref m_functionBody, " \tif ( result.x > tilling.x )"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tclip( -1 );"); IOUtils.AddFunctionLine(ref m_functionBody, " \tif ( result.y < 0 )"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tclip( -1 );"); IOUtils.AddFunctionLine(ref m_functionBody, " \tif ( result.y > tilling.y )"); IOUtils.AddFunctionLine(ref m_functionBody, " \t \tclip( -1 );"); IOUtils.AddFunctionLine(ref m_functionBody, "#ifdef UNITY_PASS_SHADOWCASTER"); IOUtils.AddFunctionLine(ref m_functionBody, "}"); IOUtils.AddFunctionLine(ref m_functionBody, "#endif"); IOUtils.AddFunctionLine(ref m_functionBody, "return result.xy;"); } else { IOUtils.AddFunctionLine(ref m_functionBody, "return uvs.xy + finalTexOffset;"); } IOUtils.CloseFunctionBody(ref m_functionBody); }
public string GetTexture2DArrayUniformValue() { return(GeneratorUtils.GetPropertyDeclaraction(PropertyName, TextureType.Texture2DArray, ";")); }
public string GetCubeUniformValue() { return(GeneratorUtils.GetPropertyDeclaraction(PropertyName, TextureType.Cube, ";")); }
private void GeneratePattern(ref MasterNodeDataCollector dataCollector) { SetAdditonalTitleText(string.Format(Constants.SubTitleTypeFormatStr, PatternsStr[m_selectedPatternInt])); switch (m_selectedPatternInt) { default: case 0: { m_functionBody = string.Empty; m_functionHeader = "Dither" + PatternsFuncStr[m_selectedPatternInt] + "( {0}, {1} )"; IOUtils.AddFunctionHeader(ref m_functionBody, "inline float Dither" + PatternsFuncStr[m_selectedPatternInt] + "( int x, int y )"); IOUtils.AddFunctionLine(ref m_functionBody, "const float dither[ 16 ] = {"); IOUtils.AddFunctionLine(ref m_functionBody, " 1, 9, 3, 11,"); IOUtils.AddFunctionLine(ref m_functionBody, " 13, 5, 15, 7,"); IOUtils.AddFunctionLine(ref m_functionBody, " 4, 12, 2, 10,"); IOUtils.AddFunctionLine(ref m_functionBody, " 16, 8, 14, 6 };"); IOUtils.AddFunctionLine(ref m_functionBody, "int r = y * 4 + x;"); IOUtils.AddFunctionLine(ref m_functionBody, "return dither[r] / 16; // same # of instructions as pre-dividing due to compiler magic"); IOUtils.CloseFunctionBody(ref m_functionBody); } break; case 1: { m_functionBody = string.Empty; m_functionHeader = "Dither" + PatternsFuncStr[m_selectedPatternInt] + "( {0}, {1} )"; IOUtils.AddFunctionHeader(ref m_functionBody, "inline float Dither" + PatternsFuncStr[m_selectedPatternInt] + "( int x, int y )"); IOUtils.AddFunctionLine(ref m_functionBody, "const float dither[ 64 ] = {"); IOUtils.AddFunctionLine(ref m_functionBody, " 1, 49, 13, 61, 4, 52, 16, 64,"); IOUtils.AddFunctionLine(ref m_functionBody, " 33, 17, 45, 29, 36, 20, 48, 32,"); IOUtils.AddFunctionLine(ref m_functionBody, " 9, 57, 5, 53, 12, 60, 8, 56,"); IOUtils.AddFunctionLine(ref m_functionBody, " 41, 25, 37, 21, 44, 28, 40, 24,"); IOUtils.AddFunctionLine(ref m_functionBody, " 3, 51, 15, 63, 2, 50, 14, 62,"); IOUtils.AddFunctionLine(ref m_functionBody, " 35, 19, 47, 31, 34, 18, 46, 30,"); IOUtils.AddFunctionLine(ref m_functionBody, " 11, 59, 7, 55, 10, 58, 6, 54,"); IOUtils.AddFunctionLine(ref m_functionBody, " 43, 27, 39, 23, 42, 26, 38, 22};"); IOUtils.AddFunctionLine(ref m_functionBody, "int r = y * 8 + x;"); IOUtils.AddFunctionLine(ref m_functionBody, "return dither[r] / 64; // same # of instructions as pre-dividing due to compiler magic"); IOUtils.CloseFunctionBody(ref m_functionBody); } break; case 2: { ParentGraph outsideGraph = UIUtils.CurrentWindow.OutsideGraph; m_functionBody = string.Empty; m_functionHeader = "Dither" + PatternsFuncStr[m_selectedPatternInt] + "({0}, {1}, {2})"; IOUtils.AddFunctionHeader(ref m_functionBody, "inline float Dither" + PatternsFuncStr[m_selectedPatternInt] + "( float4 screenPos, " + GeneratorUtils.GetPropertyDeclaraction("noiseTexture", TextureType.Texture2D, ", ") + GeneratorUtils.GetSamplerDeclaraction("samplernoiseTexture", TextureType.Texture2D, ", ") + "float4 noiseTexelSize )"); string samplingCall = GeneratorUtils.GenerateSamplingCall(ref dataCollector, WirePortDataType.SAMPLER2D, "noiseTexture", "samplernoiseTexture", "screenPos.xy * _ScreenParams.xy * noiseTexelSize.xy", MipType.MipLevel, "0"); IOUtils.AddFunctionLine(ref m_functionBody, "float dither = " + samplingCall + ".g;"); IOUtils.AddFunctionLine(ref m_functionBody, "float ditherRate = noiseTexelSize.x * noiseTexelSize.y;"); IOUtils.AddFunctionLine(ref m_functionBody, "dither = ( 1 - ditherRate ) * dither + ditherRate;"); IOUtils.AddFunctionLine(ref m_functionBody, "return dither;"); IOUtils.CloseFunctionBody(ref m_functionBody); } break; } }