12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399 |
- // Amplify Shader Editor - Visual Shader Editing Tool
- // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
- using UnityEngine;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using System;
- namespace AmplifyShaderEditor
- {
- public enum NormalizeType
- {
- Off,
- Regular,
- Safe
- }
- public enum DepthMode
- {
- DepthLinearEye,
- DepthLinear01,
- DepthEye,
- Depth01,
- }
- public class InterpDataHelper
- {
- public string VarName;
- public WirePortDataType VarType;
- public bool IsSingleComponent;
- public bool SetAtCompileTime;
- public InterpDataHelper( WirePortDataType varType, string varName, bool isSingleComponent = true , bool setAtCompileTime = false )
- {
- VarName = varName;
- VarType = varType;
- IsSingleComponent = isSingleComponent;
- SetAtCompileTime = setAtCompileTime;
- }
- }
- public class TemplateCustomData
- {
- public WirePortDataType DataType;
- public string Name;
- public bool IsVertex;
- public bool IsFragment;
- public TemplateCustomData( string name, WirePortDataType dataType )
- {
- name = Name;
- DataType = dataType;
- IsVertex = false;
- IsFragment = false;
- }
- }
- public class TemplateInputParameters
- {
- public WirePortDataType Type;
- public string Name;
- public string Declaration;
- public TemplateSemantics Semantic;
- public TemplateInputParameters( WirePortDataType type, PrecisionType precision, string name, TemplateSemantics semantic, string custom = null )
- {
- Type = type;
- Name = name;
- Semantic = semantic;
- Declaration = string.Format( "{0} {1} : {2}", UIUtils.PrecisionWirePortToCgType( precision, type ), Name, Semantic );
- if( !string.IsNullOrEmpty( custom ) )
- Declaration = custom;
- }
- }
- public class TemplateDataCollector
- {
- private const int MaxUV = 8;
- private int[] m_UVUsage = { 0, 0, 0, 0, 0, 0, 0, 0 };
- private int m_multipassSubshaderIdx = 0;
- private int m_multipassPassIdx = 0;
- private TemplateMultiPass m_currentTemplate;
- private TemplateSRPType m_currentSRPType = TemplateSRPType.BiRP;
- private Dictionary<string, TemplateCustomData> m_customInterpolatedData;
- private Dictionary<string, TemplateVertexData> m_registeredVertexData;
- private Dictionary<TemplateInfoOnSematics, InterpDataHelper> m_availableFragData;
- private Dictionary<TemplateInfoOnSematics, InterpDataHelper> m_availableVertData;
- private TemplateInterpData m_interpolatorData;
- private Dictionary<TemplateSemantics, TemplateVertexData> m_vertexDataDict;
- private TemplateData m_currentTemplateData;
- private MasterNodeDataCollector m_currentDataCollector;
- public Dictionary<TemplateSemantics, TemplateInputParameters> m_vertexInputParams;
- public Dictionary<TemplateSemantics, TemplateInputParameters> m_fragmentInputParams;
- private Dictionary<TemplateInfoOnSematics, TemplateLocalVarData> m_specialVertexLocalVars;
- private Dictionary<TemplateInfoOnSematics, TemplateLocalVarData> m_specialFragmentLocalVars;
- private List<PropertyDataCollector> m_lateDirectivesList = new List<PropertyDataCollector>();
- private Dictionary<string, PropertyDataCollector> m_lateDirectivesDict = new Dictionary<string, PropertyDataCollector>();
- private List<PropertyDataCollector> m_srpBatcherPropertiesList = new List<PropertyDataCollector>();
- private List<PropertyDataCollector> m_fullSrpBatcherPropertiesList = new List<PropertyDataCollector>();
- private Dictionary<string, PropertyDataCollector> m_srpBatcherPropertiesDict = new Dictionary<string, PropertyDataCollector>();
- public void CopySRPPropertiesFromDataCollector( int nodeId, TemplateDataCollector dataCollector )
- {
- for( int i = 0; i < dataCollector.SrpBatcherPropertiesList.Count; i++ )
- {
- AddSRPBatcherProperty( nodeId, dataCollector.SrpBatcherPropertiesList[ i ].PropertyName );
- }
- }
- public void AddSRPBatcherProperty( int nodeID, string property )
- {
- if( !m_srpBatcherPropertiesDict.ContainsKey( property ) )
- {
- PropertyDataCollector newValue = new PropertyDataCollector( nodeID, property );
- m_srpBatcherPropertiesDict.Add( property, newValue );
- m_srpBatcherPropertiesList.Add( newValue );
- }
- }
- public void SetUVUsage( int uv, WirePortDataType type )
- {
- if( uv >= 0 && uv < MaxUV )
- {
- m_UVUsage[ uv ] = Mathf.Max( m_UVUsage[ uv ], TemplateHelperFunctions.DataTypeChannelUsage[ type ] );
- }
- }
- public void SetUVUsage( int uv, int size )
- {
- if( uv >= 0 && uv < MaxUV )
- {
- m_UVUsage[ uv ] = Mathf.Max( m_UVUsage[ uv ], size );
- }
- }
- public void CloseLateDirectives()
- {
- if( m_lateDirectivesList.Count > 0 )
- {
- m_lateDirectivesList.Add( new PropertyDataCollector( -1, string.Empty ) );
- }
- }
- public void AddHDLightInfo()
- {
- }
- public void AddLateDirective( AdditionalLineType type, string value )
- {
- if( !m_lateDirectivesDict.ContainsKey( value ) )
- {
- string formattedValue = string.Empty;
- switch( type )
- {
- case AdditionalLineType.Include: formattedValue = string.Format( Constants.IncludeFormat, value ); break;
- case AdditionalLineType.Define: formattedValue = string.Format( Constants.DefineFormat, value ); break;
- case AdditionalLineType.Pragma: formattedValue = string.Format( Constants.PragmaFormat, value ); break;
- case AdditionalLineType.Custom: formattedValue = value; break;
- }
- PropertyDataCollector property = new PropertyDataCollector( -1, formattedValue );
- m_lateDirectivesDict.Add( value, property );
- m_lateDirectivesList.Add( property );
- }
- }
- public void SetMultipassInfo( TemplateMultiPass currentTemplate, int subShaderIdx, int passIdx, TemplateSRPType currentSRPType )
- {
- m_currentTemplate = currentTemplate;
- m_multipassSubshaderIdx = subShaderIdx;
- m_multipassPassIdx = passIdx;
- m_currentSRPType = currentSRPType;
- }
- public bool HasDirective( AdditionalLineType type, string value )
- {
- switch( type )
- {
- case AdditionalLineType.Include:
- {
- return m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Modules.IncludePragmaContainer.HasInclude( value ) ||
- m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Passes[ m_multipassPassIdx ].Modules.IncludePragmaContainer.HasInclude( value );
- }
- case AdditionalLineType.Define:
- {
- return m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Modules.IncludePragmaContainer.HasDefine( value ) ||
- m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Passes[ m_multipassPassIdx ].Modules.IncludePragmaContainer.HasDefine( value );
- }
- case AdditionalLineType.Pragma:
- {
- return m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Modules.IncludePragmaContainer.HasPragma( value ) ||
- m_currentTemplate.SubShaders[ m_multipassSubshaderIdx ].Passes[ m_multipassPassIdx ].Modules.IncludePragmaContainer.HasPragma( value );
- }
- }
- return false;
- }
- public void FillSpecialVariables( TemplatePass currentPass )
- {
- m_specialVertexLocalVars = new Dictionary<TemplateInfoOnSematics, TemplateLocalVarData>();
- m_specialFragmentLocalVars = new Dictionary<TemplateInfoOnSematics, TemplateLocalVarData>();
- int localVarAmount = currentPass.LocalVarsList.Count;
- for( int i = 0; i < localVarAmount; i++ )
- {
- if( currentPass.LocalVarsList[ i ].IsSpecialVar )
- {
- if( currentPass.LocalVarsList[ i ].Category == MasterNodePortCategory.Vertex )
- {
- m_specialVertexLocalVars.Add( currentPass.LocalVarsList[ i ].SpecialVarType, currentPass.LocalVarsList[ i ] );
- }
- else
- {
- m_specialFragmentLocalVars.Add( currentPass.LocalVarsList[ i ].SpecialVarType, currentPass.LocalVarsList[ i ] );
- }
- }
- }
- }
- public void BuildFromTemplateData( MasterNodeDataCollector dataCollector, TemplateData templateData )
- {
- m_registeredVertexData = new Dictionary<string, TemplateVertexData>();
- m_customInterpolatedData = new Dictionary<string, TemplateCustomData>();
- m_currentDataCollector = dataCollector;
- m_currentTemplateData = templateData;
- m_vertexDataDict = new Dictionary<TemplateSemantics, TemplateVertexData>();
- if( templateData.VertexDataList != null )
- {
- for( int i = 0; i < templateData.VertexDataList.Count; i++ )
- {
- m_vertexDataDict.Add( templateData.VertexDataList[ i ].Semantics, new TemplateVertexData( templateData.VertexDataList[ i ] ) );
- }
- }
- m_availableFragData = new Dictionary<TemplateInfoOnSematics, InterpDataHelper>();
- if( templateData.InterpolatorData != null && templateData.FragmentFunctionData != null )
- {
- m_interpolatorData = new TemplateInterpData( templateData.InterpolatorData );
- int fragCount = templateData.InterpolatorData.Interpolators.Count;
- for( int i = 0; i < fragCount; i++ )
- {
- string varName = string.Empty;
- if( templateData.InterpolatorData.Interpolators[ i ].ExcludeStructPrefix )
- {
- varName = templateData.InterpolatorData.Interpolators[ i ].VarName;
- }
- else if( templateData.InterpolatorData.Interpolators[ i ].IsSingleComponent )
- {
- varName = string.Format( TemplateHelperFunctions.TemplateVarFormat,
- templateData.FragmentFunctionData.InVarName,
- templateData.InterpolatorData.Interpolators[ i ].VarNameWithSwizzle );
- }
- else
- {
- varName = string.Format( templateData.InterpolatorData.Interpolators[ i ].VarNameWithSwizzle, templateData.FragmentFunctionData.InVarName );
- }
- m_availableFragData.Add( templateData.InterpolatorData.Interpolators[ i ].DataInfo,
- new InterpDataHelper( templateData.InterpolatorData.Interpolators[ i ].SwizzleType,
- varName,
- templateData.InterpolatorData.Interpolators[ i ].IsSingleComponent ) );
- }
- }
- m_availableVertData = new Dictionary<TemplateInfoOnSematics, InterpDataHelper>();
- if( templateData.VertexFunctionData != null && templateData.VertexDataList != null )
- {
- int vertCount = templateData.VertexDataList.Count;
- for( int i = 0; i < vertCount; i++ )
- {
- string varName = string.Empty;
- if( templateData.VertexDataList[ i ].ExcludeStructPrefix )
- {
- varName = templateData.VertexDataList[ i ].VarName;
- }
- else
- {
- varName = string.Format( TemplateHelperFunctions.TemplateVarFormat, templateData.VertexFunctionData.InVarName, templateData.VertexDataList[ i ].VarNameWithSwizzle );
- }
- m_availableVertData.Add( templateData.VertexDataList[ i ].DataInfo,
- new InterpDataHelper( templateData.VertexDataList[ i ].SwizzleType,
- varName,
- templateData.VertexDataList[ i ].IsSingleComponent ) );
- }
- }
- }
- public void RegisterFragInputParams( WirePortDataType type, PrecisionType precision, string name, TemplateSemantics semantic, string custom )
- {
- if( m_fragmentInputParams == null )
- m_fragmentInputParams = new Dictionary<TemplateSemantics, TemplateInputParameters>();
- m_fragmentInputParams.Add( semantic, new TemplateInputParameters( type, precision, name, semantic, custom ) );
- }
- public void RegisterFragInputParams( WirePortDataType type, PrecisionType precision, string name, TemplateSemantics semantic )
- {
- if( m_fragmentInputParams == null )
- m_fragmentInputParams = new Dictionary<TemplateSemantics, TemplateInputParameters>();
- m_fragmentInputParams.Add( semantic, new TemplateInputParameters( type, precision, name, semantic ) );
- }
- public void RegisterVertexInputParams( WirePortDataType type , PrecisionType precision , string name , TemplateSemantics semantic, string custom )
- {
- if( m_vertexInputParams == null )
- m_vertexInputParams = new Dictionary<TemplateSemantics , TemplateInputParameters>();
- m_vertexInputParams.Add( semantic , new TemplateInputParameters( type , precision , name , semantic, custom ) );
- }
- public void RegisterVertexInputParams( WirePortDataType type, PrecisionType precision, string name, TemplateSemantics semantic )
- {
- if( m_vertexInputParams == null )
- m_vertexInputParams = new Dictionary<TemplateSemantics, TemplateInputParameters>();
- m_vertexInputParams.Add( semantic, new TemplateInputParameters( type, precision, name, semantic ) );
- }
- public string GetInstanceId()
- {
- var precision = PrecisionType.Float;
- bool useMasterNodeCategory = true;
- MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment;
- m_currentDataCollector.AddToDirectives( "#define INSTANCEID_SEMANTIC SV_InstanceID" );
- if ( HasInfo( TemplateInfoOnSematics.INSTANCEID, useMasterNodeCategory, customCategory ) )
- {
- InterpDataHelper info = GetInfo( TemplateInfoOnSematics.INSTANCEID, useMasterNodeCategory, customCategory );
- return info.VarName;
- }
- else
- {
- MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- string name = TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_InstanceID ];
- string varName = RegisterInfoOnSemantic( category, TemplateInfoOnSematics.INSTANCEID, TemplateSemantics.INSTANCEID_SEMANTIC, name, WirePortDataType.UINT, precision, true );
- return varName;
- }
- }
- public string GetVertexId()
- {
- var precision = PrecisionType.Float;
- bool useMasterNodeCategory = true;
- MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment;
- m_currentDataCollector.AddToDirectives( "#define VERTEXID_SEMANTIC SV_VertexID" );
- WirePortDataType type = WirePortDataType.UINT;
- if( HasInfo( TemplateInfoOnSematics.VERTEXID, useMasterNodeCategory, customCategory ) )
- {
- InterpDataHelper info = GetInfo( TemplateInfoOnSematics.VERTEXID, useMasterNodeCategory, customCategory );
- return TemplateHelperFunctions.AutoSwizzleData( info.VarName, info.VarType, type, true );
- }
- else
- {
- MasterNodePortCategory portCategory = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- string name = TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_VertexID ];
- return RegisterInfoOnSemantic( portCategory, TemplateInfoOnSematics.VERTEXID, TemplateSemantics.VERTEXID_SEMANTIC, name, WirePortDataType.UINT, precision, true );
- }
- }
- public string GetPrimitiveId()
- {
- if ( m_fragmentInputParams != null && m_fragmentInputParams.ContainsKey( TemplateSemantics.SV_PrimitiveID ) )
- return m_fragmentInputParams[ TemplateSemantics.SV_PrimitiveID ].Name;
- string name = TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_PrimitiveID ];
- RegisterFragInputParams( WirePortDataType.UINT, PrecisionType.Half, name, TemplateSemantics.SV_PrimitiveID );
- return m_fragmentInputParams[ TemplateSemantics.SV_PrimitiveID ].Name;
- }
- public string GetURPMainLight( int uniqueId, string shadowCoords = null )
- {
- if( string.IsNullOrEmpty( shadowCoords ) )
- {
- shadowCoords = GetShadowCoords( uniqueId );
- }
- m_currentDataCollector.AddLocalVariable( uniqueId , string.Format( "Light ase_mainLight = GetMainLight( {0} );",shadowCoords) );
- return "ase_mainLight";
- }
- public string GetVFace( int uniqueId )
- {
- if( IsSRP )
- {
- string result = string.Empty;
- if( GetCustomInterpolatedData( TemplateInfoOnSematics.VFACE, WirePortDataType.FLOAT, PrecisionType.Float, ref result, true, MasterNodePortCategory.Fragment ) )
- {
- m_currentDataCollector.AddToDirectives( "#if !defined(ASE_NEED_CULLFACE)" );
- m_currentDataCollector.AddToDirectives( "#define ASE_NEED_CULLFACE 1" );
- m_currentDataCollector.AddToDirectives( "#endif //ASE_NEED_CULLFACE" );
- return result;
- }
- else
- {
- if( m_fragmentInputParams != null && m_fragmentInputParams.ContainsKey( TemplateSemantics.SV_IsFrontFacing ) )
- return m_fragmentInputParams[ TemplateSemantics.SV_IsFrontFacing ].Name;
- string custom = "bool "+ TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_IsFrontFacing ] + " : SV_IsFrontFace";
- RegisterFragInputParams( WirePortDataType.FLOAT, PrecisionType.Half, TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_IsFrontFacing ], TemplateSemantics.SV_IsFrontFacing, custom );
- return m_fragmentInputParams[ TemplateSemantics.SV_IsFrontFacing ].Name;
- }
- }
- else
- {
- //if( m_fragmentInputParams != null && m_fragmentInputParams.ContainsKey( TemplateSemantics.VFACE ) )
- // return m_fragmentInputParams[ TemplateSemantics.VFACE ].Name;
- //RegisterFragInputParams( WirePortDataType.FLOAT, PrecisionType.Half, TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.VFACE ], TemplateSemantics.VFACE );
- //return m_fragmentInputParams[ TemplateSemantics.VFACE ].Name;
- if( m_fragmentInputParams != null && m_fragmentInputParams.ContainsKey( TemplateSemantics.SV_IsFrontFacing ) )
- return m_fragmentInputParams[ TemplateSemantics.SV_IsFrontFacing ].Name;
- string custom = "bool " + TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_IsFrontFacing ] + " : SV_IsFrontFace";
- RegisterFragInputParams( WirePortDataType.FLOAT , PrecisionType.Half , TemplateHelperFunctions.SemanticsDefaultName[ TemplateSemantics.SV_IsFrontFacing ] , TemplateSemantics.SV_IsFrontFacing , custom );
- return m_fragmentInputParams[ TemplateSemantics.SV_IsFrontFacing ].Name;
- }
- }
- public string GetShadowCoords( int uniqueId, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- // overriding precision
- var precision = PrecisionType.Float;
- string worldPos = GetWorldPos( false, m_currentDataCollector.PortCategory );
- string result = string.Empty;
- if( GetCustomInterpolatedData( TemplateInfoOnSematics.SHADOWCOORDS, WirePortDataType.FLOAT4, precision, ref result, useMasterNodeCategory, customCategory ) )
- {
- return result;
- }
- string varName = GeneratorUtils.ShadowCoordsStr;
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string shadowCoordsValue = string.Format( "TransformWorldToShadowCoord( {0} )", worldPos );
- if( m_currentDataCollector.PortCategory == MasterNodePortCategory.Fragment )
- {
- worldPos = GetWorldPos( false, MasterNodePortCategory.Vertex );
- m_currentDataCollector.AddLocalVariable( uniqueId, "#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) //la" );
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT4, precision, string.Format( "TransformWorldToShadowCoord( {0} )", worldPos ), false, MasterNodePortCategory.Fragment );
- m_currentDataCollector.AddLocalVariable( uniqueId, "#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS) //la" );
- m_currentDataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, varName, shadowCoordsValue );
- m_currentDataCollector.AddLocalVariable( uniqueId, "#else //la" );
- m_currentDataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, varName, "0" );
- m_currentDataCollector.AddLocalVariable( uniqueId, "#endif //la" );
- } else
- {
- m_currentDataCollector.AddLocalVariable( uniqueId, precision, WirePortDataType.FLOAT4, varName, shadowCoordsValue );
- }
- return varName;
- }
- public bool HasUV( int uvChannel )
- {
- return ( m_currentDataCollector.PortCategory == MasterNodePortCategory.Fragment ) ? m_availableFragData.ContainsKey( TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ) : m_availableVertData.ContainsKey( TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] );
- }
- public string GetUVName( int uvChannel, WirePortDataType dataType = WirePortDataType.FLOAT2 )
- {
- InterpDataHelper info = ( m_currentDataCollector.PortCategory == MasterNodePortCategory.Fragment ) ? m_availableFragData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ] : m_availableVertData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ];
- if( dataType != info.VarType )
- return info.VarName + UIUtils.GetAutoSwizzle( dataType );
- else
- return info.VarName;
- }
- public string GetTextureCoord( int uvChannel, string propertyName, int uniqueId, PrecisionType precisionType )
- {
- bool isVertex = ( m_currentDataCollector.PortCategory == MasterNodePortCategory.Vertex || m_currentDataCollector.PortCategory == MasterNodePortCategory.Tessellation );
- string uvChannelName = string.Empty;
- string propertyHelperVar = propertyName + "_ST";
- m_currentDataCollector.AddToUniforms( uniqueId, "float4", propertyHelperVar, IsSRP );
- string uvName = string.Empty;
- string result = string.Empty;
- if( GetCustomInterpolatedData( TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ], WirePortDataType.FLOAT2, PrecisionType.Float, ref result, false, m_currentDataCollector.PortCategory ) )
- {
- uvName = result;
- }
- else
- if( m_currentDataCollector.TemplateDataCollectorInstance.HasUV( uvChannel ) )
- {
- uvName = m_currentDataCollector.TemplateDataCollectorInstance.GetUVName( uvChannel );
- }
- else
- {
- uvName = m_currentDataCollector.TemplateDataCollectorInstance.RegisterUV( uvChannel );
- }
- uvChannelName = "uv" + propertyName;
- if( isVertex )
- {
- string value = string.Format( Constants.TilingOffsetFormat, uvName, propertyHelperVar + ".xy", propertyHelperVar + ".zw" );
- string lodLevel = "0";
- value = "float4( " + value + ", 0 , " + lodLevel + " )";
- m_currentDataCollector.AddLocalVariable( uniqueId, precisionType, WirePortDataType.FLOAT4, uvChannelName, value );
- }
- else
- {
- m_currentDataCollector.AddLocalVariable( uniqueId, precisionType, WirePortDataType.FLOAT2, uvChannelName, string.Format( Constants.TilingOffsetFormat, uvName, propertyHelperVar + ".xy", propertyHelperVar + ".zw" ) );
- }
- return uvChannelName;
- }
- public string GenerateAutoUVs( int uvChannel, WirePortDataType size = WirePortDataType.FLOAT2 )
- {
- string uvName = string.Empty;
- string result = string.Empty;
- if( GetCustomInterpolatedData( TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ], size, PrecisionType.Float, ref result, false, m_currentDataCollector.PortCategory ) )
- {
- uvName = result;
- }
- else
- if( HasUV( uvChannel ) )
- {
- uvName = GetUVName( uvChannel, size );
- }
- else
- {
- uvName = RegisterUV( uvChannel, size );
- }
- return uvName;
- }
- public string GetUV( int uvChannel, MasterNodePortCategory category = MasterNodePortCategory.Fragment, WirePortDataType size = WirePortDataType.FLOAT4 )
- {
- if( !HasUV( uvChannel ) )
- {
- RegisterUV( uvChannel, size );
- }
- InterpDataHelper info = ( category == MasterNodePortCategory.Fragment ) ? m_availableFragData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ] : m_availableVertData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ];
- return info.VarName;
- }
- public InterpDataHelper GetUVInfo( int uvChannel )
- {
- return ( m_currentDataCollector.PortCategory == MasterNodePortCategory.Fragment ) ? m_availableFragData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ] : m_availableVertData[ TemplateHelperFunctions.IntToUVChannelInfo[ uvChannel ] ];
- }
- public string RegisterUV( int UVChannel, WirePortDataType size = WirePortDataType.FLOAT2 )
- {
- int channelsSize = TemplateHelperFunctions.DataTypeChannelUsage[ size ];
- WirePortDataType originalSize = size;
- if( m_UVUsage[ UVChannel ] > channelsSize )
- {
- size = TemplateHelperFunctions.ChannelToDataType[ m_UVUsage[ UVChannel ] ];
- }
- if( m_currentDataCollector.PortCategory == MasterNodePortCategory.Vertex )
- {
- TemplateSemantics semantic = TemplateHelperFunctions.IntToSemantic[ UVChannel ];
- if( m_vertexDataDict.ContainsKey( semantic ) )
- {
- return m_vertexDataDict[ semantic ].VarName;
- }
- string varName = TemplateHelperFunctions.BaseInterpolatorName + ( ( UVChannel > 0 ) ? UVChannel.ToString() : string.Empty );
- m_availableVertData.Add( TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ],
- new InterpDataHelper( WirePortDataType.FLOAT4,
- string.Format( TemplateHelperFunctions.TemplateVarFormat,
- m_currentTemplateData.VertexFunctionData.InVarName,
- varName ) ) );
- m_currentDataCollector.AddToVertexInput(
- string.Format( TemplateHelperFunctions.TexFullSemantic,
- varName,
- semantic ) );
- RegisterOnVertexData( semantic, size, varName );
- string finalVarName = m_availableVertData[ TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ] ].VarName;
- switch( size )
- {
- case WirePortDataType.FLOAT:
- case WirePortDataType.INT:
- case WirePortDataType.UINT:
- finalVarName += ".x";
- break;
- case WirePortDataType.FLOAT2:
- finalVarName += ".xy";
- break;
- case WirePortDataType.FLOAT3:
- finalVarName += ".xyz";
- break;
- case WirePortDataType.UINT4:
- case WirePortDataType.FLOAT4:
- case WirePortDataType.COLOR:
- case WirePortDataType.FLOAT3x3:
- case WirePortDataType.FLOAT4x4:
- case WirePortDataType.SAMPLER1D:
- case WirePortDataType.SAMPLER2D:
- case WirePortDataType.SAMPLER3D:
- case WirePortDataType.SAMPLERCUBE:
- case WirePortDataType.SAMPLER2DARRAY:
- case WirePortDataType.SAMPLERSTATE:
- case WirePortDataType.OBJECT:
- default:
- break;
- }
- return finalVarName;
- }
- else
- {
- //search if the correct vertex data is set ...
- TemplateInfoOnSematics info = TemplateHelperFunctions.IntToInfo[ UVChannel ];
- TemplateSemantics vertexSemantics = TemplateSemantics.NONE;
- foreach( KeyValuePair<TemplateSemantics, TemplateVertexData> kvp in m_vertexDataDict )
- {
- if( kvp.Value.DataInfo == info )
- {
- vertexSemantics = kvp.Key;
- break;
- }
- }
- // if not, add vertex data and create interpolator
- if( vertexSemantics == TemplateSemantics.NONE )
- {
- vertexSemantics = TemplateHelperFunctions.IntToSemantic[ UVChannel ];
- if( !m_vertexDataDict.ContainsKey( vertexSemantics ) )
- {
- string varName = TemplateHelperFunctions.BaseInterpolatorName + ( ( UVChannel > 0 ) ? UVChannel.ToString() : string.Empty );
- m_availableVertData.Add( TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ],
- new InterpDataHelper( WirePortDataType.FLOAT4,
- string.Format( TemplateHelperFunctions.TemplateVarFormat,
- m_currentTemplateData.VertexFunctionData.InVarName,
- varName ) ) );
- m_currentDataCollector.AddToVertexInput(
- string.Format( TemplateHelperFunctions.TexFullSemantic,
- varName,
- vertexSemantics ) );
- RegisterOnVertexData( vertexSemantics, size, varName );
- }
- }
- // either way create interpolator
- TemplateVertexData availableInterp = RequestNewInterpolator( size, false );
- if( availableInterp != null )
- {
- bool isPosition = vertexSemantics == TemplateSemantics.POSITION || vertexSemantics == TemplateSemantics.POSITION;
- string interpVarName = m_currentTemplateData.VertexFunctionData.OutVarName + "." + availableInterp.VarNameWithSwizzle;
- InterpDataHelper vertInfo = m_availableVertData[ TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ] ];
- string interpDecl = string.Format( TemplateHelperFunctions.TemplateVariableDecl, interpVarName, TemplateHelperFunctions.AutoSwizzleData( vertInfo.VarName, vertInfo.VarType, size , isPosition ) );
- m_currentDataCollector.AddToVertexInterpolatorsDecl( interpDecl );
- string finalVarName = m_currentTemplateData.FragmentFunctionData.InVarName + "." + availableInterp.VarNameWithSwizzle;
- m_availableFragData.Add( TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ], new InterpDataHelper( size, finalVarName ) );
- if( size != originalSize )
- {
- //finalVarName = m_currentTemplateData.FragmentFunctionData.InVarName + "." + availableInterp.VarName + UIUtils.GetAutoSwizzle( originalSize );
- finalVarName = m_availableFragData[ TemplateHelperFunctions.IntToUVChannelInfo[ UVChannel ] ].VarName + UIUtils.GetAutoSwizzle( originalSize );
- }
- return finalVarName;
- }
- }
- return string.Empty;
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- bool IsSemanticUsedOnInterpolator( TemplateSemantics semantics )
- {
- for( int i = 0; i < m_interpolatorData.Interpolators.Count; i++ )
- {
- if( m_interpolatorData.Interpolators[ i ].Semantics == semantics )
- {
- return true;
- }
- }
- return false;
- }
- public bool HasInfo( TemplateInfoOnSematics info, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- return ( category == MasterNodePortCategory.Fragment ) ? m_availableFragData.ContainsKey( info ) : m_availableVertData.ContainsKey( info );
- }
- public InterpDataHelper GetInfo( TemplateInfoOnSematics info, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- if( category == MasterNodePortCategory.Fragment )
- {
- if( !m_availableFragData[ info ].SetAtCompileTime )
- {
- string defineValue = string.Empty;
- if( TemplateHelperFunctions.InfoToDefineFrag.TryGetValue( info, out defineValue ) )
- m_currentDataCollector.AddToDefines( -1, defineValue );
- }
- return m_availableFragData[ info ];
- }
- else
- {
- if( !m_availableVertData[ info ].SetAtCompileTime )
- {
- string defineValue = string.Empty;
- if( TemplateHelperFunctions.InfoToDefineVertex.TryGetValue( info, out defineValue ) )
- m_currentDataCollector.AddToDefines( -1, defineValue );
- }
- return m_availableVertData[ info ];
- }
- }
- public string RegisterInfoOnSemantic( TemplateInfoOnSematics info, TemplateSemantics semantic, string name, WirePortDataType dataType, PrecisionType precisionType, bool requestNewInterpolator, string dataName = null )
- {
- return RegisterInfoOnSemantic( m_currentDataCollector.PortCategory, info, semantic, name, dataType, precisionType, requestNewInterpolator, dataName );
- }
- // This should only be used to semantics outside the text coord set
- public string RegisterInfoOnSemantic( MasterNodePortCategory portCategory, TemplateInfoOnSematics info, TemplateSemantics semantic, string name, WirePortDataType dataType, PrecisionType precisionType, bool requestNewInterpolator, string dataName = null )
- {
- if( portCategory == MasterNodePortCategory.Vertex )
- {
- if( m_vertexDataDict.ContainsKey( semantic ) )
- {
- return m_vertexDataDict[ semantic ].VarName;
- }
- m_availableVertData.Add( info,
- new InterpDataHelper( dataType,
- string.Format( TemplateHelperFunctions.TemplateVarFormat,
- m_currentTemplateData.VertexFunctionData.InVarName,
- name ),true,true ) );
- string vertInputVarType = UIUtils.PrecisionWirePortToCgType( precisionType, dataType );
- m_currentDataCollector.AddToVertexInput(
- string.Format( TemplateHelperFunctions.InterpFullSemantic,
- vertInputVarType,
- name,
- semantic ) );
- RegisterOnVertexData( semantic, dataType, name );
- return m_availableVertData[ info ].VarName;
- }
- else
- {
- //search if the correct vertex data is set ...
- TemplateSemantics vertexSemantics = TemplateSemantics.NONE;
- foreach( KeyValuePair<TemplateSemantics, TemplateVertexData> kvp in m_vertexDataDict )
- {
- if( kvp.Value.DataInfo == info )
- {
- vertexSemantics = kvp.Key;
- break;
- }
- }
- // if not, add vertex data and create interpolator
- if( vertexSemantics == TemplateSemantics.NONE )
- {
- vertexSemantics = semantic;
- if( !m_vertexDataDict.ContainsKey( vertexSemantics ) )
- {
- m_availableVertData.Add( info,
- new InterpDataHelper( dataType,
- string.Format( TemplateHelperFunctions.TemplateVarFormat,
- m_currentTemplateData.VertexFunctionData.InVarName,
- name ),true,true ) );
- string vertInputVarType = UIUtils.PrecisionWirePortToCgType( precisionType, dataType );
- m_currentDataCollector.AddToVertexInput(
- string.Format( TemplateHelperFunctions.InterpFullSemantic,
- vertInputVarType,
- name,
- vertexSemantics ) );
- RegisterOnVertexData( vertexSemantics, dataType, name );
- }
- }
- // either way create interpolator
- TemplateVertexData availableInterp = null;
- if( requestNewInterpolator || IsSemanticUsedOnInterpolator( semantic ) )
- {
- availableInterp = RequestNewInterpolator( dataType, false, dataName );
- }
- else
- {
- availableInterp = RegisterOnInterpolator( semantic, dataType, dataName );
- }
- if( availableInterp != null )
- {
- bool isPosition = vertexSemantics == TemplateSemantics.POSITION || vertexSemantics == TemplateSemantics.POSITION;
- string interpVarName = m_currentTemplateData.VertexFunctionData.OutVarName + "." + availableInterp.VarNameWithSwizzle;
- string interpDecl = string.Format( TemplateHelperFunctions.TemplateVariableDecl, interpVarName, TemplateHelperFunctions.AutoSwizzleData( m_availableVertData[ info ].VarName, m_availableVertData[ info ].VarType, dataType, isPosition ) );
- m_currentDataCollector.AddToVertexInterpolatorsDecl( interpDecl );
- string finalVarName = m_currentTemplateData.FragmentFunctionData.InVarName + "." + availableInterp.VarNameWithSwizzle;
- m_availableFragData.Add( info, new InterpDataHelper( dataType, finalVarName ) );
- return finalVarName;
- }
- }
- return string.Empty;
- }
- TemplateVertexData RegisterOnInterpolator( TemplateSemantics semantics, WirePortDataType dataType, string vertexDataName = null )
- {
- if( vertexDataName == null )
- {
- if( TemplateHelperFunctions.SemanticsDefaultName.ContainsKey( semantics ) )
- {
- vertexDataName = TemplateHelperFunctions.SemanticsDefaultName[ semantics ];
- }
- else
- {
- vertexDataName = string.Empty;
- Debug.LogError( "No valid name given to vertex data" );
- }
- }
- TemplateVertexData data = new TemplateVertexData( semantics, dataType, vertexDataName );
- m_interpolatorData.Interpolators.Add( data );
- string interpolator = string.Format( TemplateHelperFunctions.InterpFullSemantic, UIUtils.WirePortToCgType( dataType ), data.VarName, data.Semantics );
- m_currentDataCollector.AddToInterpolators( interpolator );
- return data;
- }
- public void RegisterOnVertexData( TemplateSemantics semantics, WirePortDataType dataType, string varName )
- {
- m_vertexDataDict.Add( semantics, new TemplateVertexData( semantics, dataType, varName ) );
- }
- public TemplateVertexData RequestMacroInterpolator( string varName )
- {
- if( varName != null && m_registeredVertexData.ContainsKey( varName ) )
- {
- return m_registeredVertexData[ varName ];
- }
- for( int i = 0; i < m_interpolatorData.AvailableInterpolators.Count; i++ )
- {
- if( !m_interpolatorData.AvailableInterpolators[ i ].IsFull )
- {
- TemplateVertexData data = m_interpolatorData.AvailableInterpolators[ i ].RequestChannels( WirePortDataType.FLOAT4, false, varName );
- if( data != null )
- {
- if( !m_registeredVertexData.ContainsKey( data.VarName ) )
- {
- m_registeredVertexData.Add( data.VarName, data );
- }
- if( m_interpolatorData.AvailableInterpolators[ i ].Usage == 1 )
- {
- string interpolator = string.Format( TemplateHelperFunctions.InterpMacro, varName, TemplateHelperFunctions.SemanticToInt[ data.Semantics ] );
- m_currentDataCollector.AddToInterpolators( interpolator );
- }
- return data;
- }
- }
- }
- return null;
- }
- public bool HasRawInterpolatorOfName( string name )
- {
- return m_interpolatorData.HasRawInterpolatorOfName( name );
- }
- public TemplateVertexData RequestNewInterpolator( WirePortDataType dataType, bool isColor, string varName = null , bool noInterpolationFlag = false, bool sampleFlag = false )
- {
- if( varName != null && m_registeredVertexData.ContainsKey( varName ) )
- {
- return m_registeredVertexData[ varName ];
- }
- for( int i = 0; i < m_interpolatorData.AvailableInterpolators.Count; i++ )
- {
- if( !m_interpolatorData.AvailableInterpolators[ i ].IsFull )
- {
- if( m_interpolatorData.AvailableInterpolators[ i ].Usage != 0 &&
- (m_interpolatorData.AvailableInterpolators[ i ].NoInterpolation != noInterpolationFlag ||
- m_interpolatorData.AvailableInterpolators[ i ].Sample != sampleFlag ))
- continue;
- TemplateVertexData data = m_interpolatorData.AvailableInterpolators[ i ].RequestChannels( dataType, isColor, varName );
- if( data != null )
- {
- if( !m_registeredVertexData.ContainsKey( data.VarName ) )
- {
- m_registeredVertexData.Add( data.VarName, data );
- }
- if( m_interpolatorData.AvailableInterpolators[ i ].Usage == 1 )
- {
- m_interpolatorData.AvailableInterpolators[ i ].NoInterpolation = noInterpolationFlag;
- m_interpolatorData.AvailableInterpolators[ i ].Sample = sampleFlag;
- // First time using this interpolator, so we need to register it
- string interpolator = string.Format( TemplateHelperFunctions.TexFullSemantic,
- data.VarName, data.Semantics );
- if( noInterpolationFlag )
- interpolator = "nointerpolation " + interpolator;
- if( sampleFlag)
- interpolator = "sample " + interpolator;
- m_currentDataCollector.AddToInterpolators( interpolator );
- }
- return data;
- }
- }
- }
- // This area is reached if max available interpolators from shader model is reached
- // Nevertheless, we register all new interpolators to that list so no imediate compilation errors are thrown
- // A warning message is then thrown to warn the user about this
- int newInterpId = 1 + TemplateHelperFunctions.SemanticToInt[ m_interpolatorData.AvailableInterpolators[ m_interpolatorData.AvailableInterpolators.Count - 1 ].Semantic ];
- if( TemplateHelperFunctions.IntToSemantic.ContainsKey( newInterpId ) )
- {
- TemplateInterpElement item = new TemplateInterpElement( TemplateHelperFunctions.IntToSemantic[ newInterpId ] );
- m_interpolatorData.AvailableInterpolators.Add( item );
- TemplateVertexData data = item.RequestChannels( dataType, isColor, varName );
- if( data != null )
- {
- if( !m_registeredVertexData.ContainsKey( data.VarName ) )
- {
- m_registeredVertexData.Add( data.VarName, data );
- }
- if( item.Usage == 1 )
- {
- string interpolator = string.Format( TemplateHelperFunctions.TexFullSemantic, data.VarName, data.Semantics );
- m_currentDataCollector.AddToInterpolators( interpolator );
- }
- return data;
- }
- }
- UIUtils.ShowMessage( "Maximum amount of interpolators exceeded", MessageSeverity.Error );
- return null;
- }
- // Unused channels in interpolators must be set to something so the compiler doesn't generate warnings
- public List<string> GetInterpUnusedChannels()
- {
- List<string> resetInstrucctions = new List<string>();
- if( m_interpolatorData != null )
- {
- for( int i = 0; i < m_interpolatorData.AvailableInterpolators.Count; i++ )
- {
- if( m_interpolatorData.AvailableInterpolators[ i ].Usage > 0 && !m_interpolatorData.AvailableInterpolators[ i ].IsFull )
- {
- string channels = string.Empty;
- bool[] availableChannels = m_interpolatorData.AvailableInterpolators[ i ].AvailableChannels;
- for( int j = 0; j < availableChannels.Length; j++ )
- {
- if( availableChannels[ j ] )
- {
- channels += TemplateHelperFunctions.VectorSwizzle[ j ];
- }
- }
- resetInstrucctions.Add( string.Format( "{0}.{1}.{2} = 0;", m_currentTemplateData.VertexFunctionData.OutVarName, m_interpolatorData.AvailableInterpolators[ i ].Name, channels ) );
- }
- }
- }
- if( resetInstrucctions.Count > 0 )
- {
- resetInstrucctions.Insert( 0, "\n//setting value to unused interpolator channels and avoid initialization warnings" );
- }
- return resetInstrucctions;
- }
- public bool ContainsSpecialLocalFragVar( TemplateInfoOnSematics info, WirePortDataType type, ref string result )
- {
- if( m_specialFragmentLocalVars.ContainsKey( info ) )
- {
- result = m_specialFragmentLocalVars[ info ].LocalVarName;
- if( m_specialFragmentLocalVars[ info ].DataType != type )
- {
- result = TemplateHelperFunctions.AutoSwizzleData( result, m_specialFragmentLocalVars[ info ].DataType, type, false );
- }
- return true;
- }
- return false;
- }
- public bool GetCustomInterpolatedData( TemplateInfoOnSematics info, WirePortDataType type, PrecisionType precisionType, ref string result, bool useMasterNodeCategory, MasterNodePortCategory customCategory )
- {
- bool isPosition = info == TemplateInfoOnSematics.POSITION ||
- info == TemplateInfoOnSematics.CLIP_POS ||
- info == TemplateInfoOnSematics.SCREEN_POSITION ||
- info == TemplateInfoOnSematics.SCREEN_POSITION_NORMALIZED ||
- info == TemplateInfoOnSematics.WORLD_POSITION ||
- info == TemplateInfoOnSematics.RELATIVE_WORLD_POS ||
- info == TemplateInfoOnSematics.OBJECT_POSITION ||
- info == TemplateInfoOnSematics.VIEW_POSITION;
- MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- if( category == MasterNodePortCategory.Vertex )
- {
- if( m_specialVertexLocalVars.ContainsKey( info ) )
- {
- result = m_specialVertexLocalVars[ info ].LocalVarName;
- if( m_specialVertexLocalVars[ info ].DataType != type )
- {
- result = TemplateHelperFunctions.AutoSwizzleData( result, m_specialVertexLocalVars[ info ].DataType, type , isPosition );
- }
- string defineValue = string.Empty;
- if( TemplateHelperFunctions.InfoToDefineVertex.TryGetValue( info, out defineValue ) )
- m_currentDataCollector.AddToDefines( -1, defineValue );
- return true;
- }
- }
- if( category == MasterNodePortCategory.Fragment )
- {
- if( m_specialFragmentLocalVars.ContainsKey( info ) )
- {
- result = m_specialFragmentLocalVars[ info ].LocalVarName;
- if( m_specialFragmentLocalVars[ info ].DataType != type )
- {
- result = TemplateHelperFunctions.AutoSwizzleData( result, m_specialFragmentLocalVars[ info ].DataType, type, isPosition );
- }
- string defineValue = string.Empty;
- if( TemplateHelperFunctions.InfoToDefineFrag.TryGetValue( info, out defineValue ))
- m_currentDataCollector.AddToDefines( -1, defineValue );
- return true;
- }
- if( m_availableFragData.ContainsKey( info ) )
- {
- if( m_availableFragData[ info ].IsSingleComponent )
- {
- result = m_availableFragData[ info ].VarName;
- if( m_availableFragData[ info ].VarType != type )
- {
- result = TemplateHelperFunctions.AutoSwizzleData( result, m_availableFragData[ info ].VarType, type, isPosition );
- }
- return true;
- }
- else if( TemplateHelperFunctions.InfoToLocalVar.ContainsKey( info ) && TemplateHelperFunctions.InfoToWirePortType.ContainsKey( info ) )
- {
- result = TemplateHelperFunctions.InfoToLocalVar[ info ];
- m_currentDataCollector.AddLocalVariable( -1, precisionType, TemplateHelperFunctions.InfoToWirePortType[ info ], result, m_availableFragData[ info ].VarName );
- return true;
- }
- }
- }
- return false;
- }
- public WirePortDataType GetVertexPositionDataType()
- {
- InterpDataHelper info = GetInfo( TemplateInfoOnSematics.POSITION , false, MasterNodePortCategory.Vertex);
- return info.VarType;
- }
- public string GetVertexPosition( WirePortDataType type, PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- PrecisionType precision = PrecisionType.Float;
- string result = string.Empty;
- if ( GetCustomInterpolatedData( TemplateInfoOnSematics.OBJECT_POSITION, WirePortDataType.FLOAT3, precision, ref result, useMasterNodeCategory, customCategory ) )
- {
- return result;
- }
- if ( HasInfo( TemplateInfoOnSematics.POSITION, useMasterNodeCategory, customCategory ) )
- {
- InterpDataHelper info = GetInfo( TemplateInfoOnSematics.POSITION, useMasterNodeCategory, customCategory );
- if( type != WirePortDataType.OBJECT && type != info.VarType )
- return TemplateHelperFunctions.AutoSwizzleData( info.VarName, info.VarType, type,true );
- else
- return info.VarName;
- }
- else
- {
- MasterNodePortCategory portCategory = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- string name = "ase_vertex_pos";
- string varName = RegisterInfoOnSemantic( portCategory, TemplateInfoOnSematics.POSITION, TemplateSemantics.POSITION, name, WirePortDataType.FLOAT4, precisionType, true );
- if( type != WirePortDataType.OBJECT && type != WirePortDataType.FLOAT4 )
- return TemplateHelperFunctions.AutoSwizzleData( varName, WirePortDataType.FLOAT4, type,true );
- else
- return varName;
- }
- }
- private const string InstancingLibStandard = "UnityInstancing.cginc";
- private const string InstancingLibSRP = "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl";
- public void SetupInstancing()
- {
- if( !HasInfo( TemplateInfoOnSematics.INSTANCE_ID ) )
- {
- m_currentDataCollector.AddToPragmas( -1, IOUtils.InstancedPropertiesHeader );
- m_currentDataCollector.AddToIncludes( -1, IsSRP ? InstancingLibSRP : InstancingLibStandard );
- m_currentDataCollector.AddToVertexInput( Constants.InstanceIdMacro );
- m_currentDataCollector.AddToInterpolators( Constants.InstanceIdMacro );
- m_currentDataCollector.AddToLocalVariables( MasterNodePortCategory.Vertex, -1, string.Format( "UNITY_SETUP_INSTANCE_ID( {0} );", m_currentTemplateData.VertexFunctionData.InVarName ) );
- m_currentDataCollector.AddToLocalVariables( MasterNodePortCategory.Vertex, -1, string.Format( "UNITY_TRANSFER_INSTANCE_ID( {0}, {1} );", m_currentTemplateData.VertexFunctionData.InVarName, m_currentTemplateData.VertexFunctionData.OutVarName ) );
- m_currentDataCollector.AddToLocalVariables( MasterNodePortCategory.Fragment, -1, string.Format( "UNITY_SETUP_INSTANCE_ID( {0} );", m_currentTemplateData.FragmentFunctionData.InVarName ) );
- }
- }
- public string GetVertexColor( PrecisionType precisionType )
- {
- if( HasInfo( TemplateInfoOnSematics.COLOR ) )
- {
- return GetInfo( TemplateInfoOnSematics.COLOR ).VarName;
- }
- else
- {
- string name = "ase_color";
- return RegisterInfoOnSemantic( TemplateInfoOnSematics.COLOR, TemplateSemantics.COLOR, name, WirePortDataType.FLOAT4, precisionType, false );
- }
- }
- public string GetVertexNormal( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- if( HasInfo( TemplateInfoOnSematics.NORMAL, useMasterNodeCategory, customCategory ) )
- {
- InterpDataHelper info = GetInfo( TemplateInfoOnSematics.NORMAL, useMasterNodeCategory, customCategory );
- return TemplateHelperFunctions.AutoSwizzleData( info.VarName, info.VarType, WirePortDataType.FLOAT3 , false);
- }
- else
- {
- MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- string name = "ase_normal";
- return RegisterInfoOnSemantic( category, TemplateInfoOnSematics.NORMAL, TemplateSemantics.NORMAL, name, WirePortDataType.FLOAT3, precisionType, false );
- }
- }
- public string GetWorldNormal( PrecisionType precisionType = PrecisionType.Float, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, bool normalize = false )
- {
- string result = string.Empty;
- if( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_NORMAL, WirePortDataType.FLOAT3, precisionType, ref result, useMasterNodeCategory, customCategory ) )
- {
- if( normalize )
- return string.Format( "normalize( {0} )", result );
- else
- return result;
- }
- string varName = normalize ? "normalizeWorldNormal" : GeneratorUtils.WorldNormalStr;
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string worldNormalValue = string.Empty;
- if( !GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_NORMAL, WirePortDataType.FLOAT3, precisionType, ref worldNormalValue, false, MasterNodePortCategory.Vertex ) )
- {
- string vertexNormal = GetVertexNormal( precisionType, false, MasterNodePortCategory.Vertex );
- string formatStr = string.Empty;
- if( IsSRP )
- formatStr = "TransformObjectToWorldNormal( {0} )";
- else
- formatStr = "UnityObjectToWorldNormal( {0} )";
- worldNormalValue = string.Format( formatStr, vertexNormal );
- }
- if( normalize )
- worldNormalValue = string.Format( "normalize( {0} )", worldNormalValue );
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precisionType, worldNormalValue, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetWorldNormal( int uniqueId, PrecisionType precisionType, string normal, string outputId )
- {
- string tanToWorld0 = string.Empty;
- string tanToWorld1 = string.Empty;
- string tanToWorld2 = string.Empty;
- GetWorldTangentTf( precisionType, out tanToWorld0, out tanToWorld1, out tanToWorld2, true );
- string tanNormal = "tanNormal" + outputId;
- m_currentDataCollector.AddLocalVariable( uniqueId, "float3 " + tanNormal + " = " + normal + ";" );
- return string.Format( "float3( dot( {1}, {0} ), dot( {2}, {0} ), dot( {3}, {0} ) )", tanNormal, tanToWorld0, tanToWorld1, tanToWorld2 );
- }
- public string GetVertexTangent( WirePortDataType type, PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- if( HasInfo( TemplateInfoOnSematics.TANGENT, useMasterNodeCategory, customCategory ) )
- {
- InterpDataHelper info = GetInfo( TemplateInfoOnSematics.TANGENT, useMasterNodeCategory, customCategory );
- if( type != WirePortDataType.OBJECT && type != info.VarType )
- return TemplateHelperFunctions.AutoSwizzleData( info.VarName, info.VarType, type , false);
- else
- return info.VarName;
- }
- else
- {
- MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- string name = "ase_tangent";
- string varName = RegisterInfoOnSemantic( category, TemplateInfoOnSematics.TANGENT, TemplateSemantics.TANGENT, name, WirePortDataType.FLOAT4, precisionType, false );
- if( type != WirePortDataType.OBJECT && type != WirePortDataType.FLOAT4 )
- return TemplateHelperFunctions.AutoSwizzleData( varName, WirePortDataType.FLOAT4, type , false );
- else
- return varName;
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public string GetBlendWeights( bool useMasterNodeCategory = true , MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- if( HasInfo( TemplateInfoOnSematics.BLENDWEIGHTS , useMasterNodeCategory , customCategory ) )
- {
- InterpDataHelper info = GetInfo( TemplateInfoOnSematics.BLENDWEIGHTS , useMasterNodeCategory , customCategory );
- return info.VarName;
- }
- else
- {
- MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- string name = GeneratorUtils.VertexBlendWeightsStr;
- string varName = RegisterInfoOnSemantic( category , TemplateInfoOnSematics.BLENDWEIGHTS , TemplateSemantics.BLENDWEIGHTS , name , WirePortDataType.FLOAT4 ,PrecisionType.Float , false , name );
- return varName;
- }
- }
- public string GetBlendIndices( bool useMasterNodeCategory = true , MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- if( HasInfo( TemplateInfoOnSematics.BLENDINDICES , useMasterNodeCategory , customCategory ) )
- {
- InterpDataHelper info = GetInfo( TemplateInfoOnSematics.BLENDINDICES , useMasterNodeCategory , customCategory );
- return info.VarName;
- }
- else
- {
- MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- string name = GeneratorUtils.VertexBlendIndicesStr;
- string varName = RegisterInfoOnSemantic( category , TemplateInfoOnSematics.BLENDINDICES , TemplateSemantics.BLENDINDICES , name , WirePortDataType.UINT4 , PrecisionType.Float , false , name );
- return varName;
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- public string GetVertexBitangent( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string varName = GeneratorUtils.VertexBitangentStr;
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string tangentValue = GetVertexTangent( WirePortDataType.FLOAT4, precisionType, false, MasterNodePortCategory.Vertex );
- string normalValue = GetVertexNormal( precisionType, false, MasterNodePortCategory.Vertex );
- string bitangentValue = string.Format( "cross( {0}, {1}.xyz ) * {1}.w * ( unity_WorldTransformParams.w >= 0.0 ? 1.0 : -1.0 )", normalValue, tangentValue );
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precisionType, bitangentValue, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetWorldTangent( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string result = string.Empty;
- if( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_TANGENT, WirePortDataType.FLOAT3, precisionType, ref result, useMasterNodeCategory, customCategory ) )
- {
- return result;
- }
- string varName = GeneratorUtils.WorldTangentStr;
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string worldTangentValue = string.Empty;
- if( !GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_TANGENT, WirePortDataType.FLOAT3, precisionType, ref worldTangentValue, false, MasterNodePortCategory.Vertex ) )
- {
- string vertexTangent = GetVertexTangent( WirePortDataType.FLOAT4, precisionType, false, MasterNodePortCategory.Vertex );
- string formatStr = string.Empty;
- if( IsSRP )
- formatStr = "TransformObjectToWorldDir( {0}.xyz )";
- else
- formatStr = "UnityObjectToWorldDir( {0} )";
- worldTangentValue = string.Format( formatStr, vertexTangent );
- }
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precisionType, worldTangentValue, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetTangentSign( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string varName = GeneratorUtils.VertexTangentSignStr;
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string tangentValue = GetVertexTangent( WirePortDataType.FLOAT4, precisionType, false, MasterNodePortCategory.Vertex );
- string tangentSignValue = string.Format( "{0}.w * ( unity_WorldTransformParams.w >= 0.0 ? 1.0 : -1.0 )", tangentValue );
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT, precisionType, tangentSignValue, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetWorldBinormal( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string result = string.Empty;
- if( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_BITANGENT, WirePortDataType.FLOAT3, precisionType, ref result, useMasterNodeCategory, customCategory ) )
- {
- return result;
- }
- string varName = GeneratorUtils.WorldBitangentStr;
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string worldBinormal = string.Empty;
- if( !GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_BITANGENT, WirePortDataType.FLOAT3, precisionType, ref worldBinormal, false, MasterNodePortCategory.Vertex ) )
- {
- string worldNormal = GetWorldNormal( precisionType, false, MasterNodePortCategory.Vertex );
- string worldtangent = GetWorldTangent( precisionType, false, MasterNodePortCategory.Vertex );
- string tangentSign = GetTangentSign( precisionType, false, MasterNodePortCategory.Vertex );
- worldBinormal = string.Format( "cross( {0}, {1} ) * {2}", worldNormal, worldtangent, tangentSign );
- }
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, PrecisionType.Float, worldBinormal, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetWorldReflection( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, bool normalize = false )
- {
- string varName = GeneratorUtils.WorldReflectionStr;//UIUtils.GetInputValueFromType( SurfaceInputs.WORLD_REFL );
- if( normalize )
- varName = "normalized" + varName;
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string worldNormal = GetWorldNormal( precisionType );
- string worldViewDir = GetViewDir();
- string worldRefl = string.Format( "reflect( -{0}, {1} )", worldViewDir, worldNormal );
- if( normalize )
- worldRefl = string.Format( "normalize( {0} )", worldRefl );
- m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, varName, worldRefl );
- return varName;
- }
- public string GetWorldReflection( PrecisionType precisionType, string normal )
- {
- string tanToWorld0 = string.Empty;
- string tanToWorld1 = string.Empty;
- string tanToWorld2 = string.Empty;
- GetWorldTangentTf( precisionType, out tanToWorld0, out tanToWorld1, out tanToWorld2 );
- string worldRefl = GetViewDir();
- return string.Format( "reflect( -{0}, float3( dot( {2}, {1} ), dot( {3}, {1} ), dot( {4}, {1} ) ) )", worldRefl, normal, tanToWorld0, tanToWorld1, tanToWorld2 );
- }
- public string GetLightAtten( int uniqueId, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- //string result = string.Empty;
- //if( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_POSITION, PrecisionType.Float, ref result, useMasterNodeCategory, customCategory ) )
- //{
- // return result;
- //}
- //string varName = GeneratorUtils.WorldPositionStr;//UIUtils.GetInputValueFromType( SurfaceInputs.WORLD_POS );
- //if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- // return varName;
- //if( !m_availableVertData.ContainsKey( TemplateInfoOnSematics.POSITION ) )
- //{
- // UIUtils.ShowMessage( "Attempting to access inexisting vertex position to calculate world pos" );
- // return "fixed3(0,0,0)";
- //}
- //string vertexPos = m_availableVertData[ TemplateInfoOnSematics.POSITION ].VarName;
- //string worldPosConversion = string.Format( "mul(unity_ObjectToWorld, {0}).xyz", vertexPos );
- //RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, PrecisionType.Float, worldPosConversion, useMasterNodeCategory, customCategory );
- //return varName;
- m_currentDataCollector.AddToIncludes( uniqueId, Constants.UnityAutoLightLib );
- m_currentDataCollector.AddToDefines( uniqueId, "ASE_SHADOWS 1" );
- RequestMacroInterpolator( "UNITY_SHADOW_COORDS" );
- //string vOutName = CurrentTemplateData.VertexFunctionData.OutVarName;
- string fInName = CurrentTemplateData.FragmentFunctionData.InVarName;
- string worldPos = GetWorldPos();
- m_currentDataCollector.AddLocalVariable( uniqueId, "UNITY_LIGHT_ATTENUATION( ase_atten, " + fInName + ", " + worldPos + " )" );
- return "ase_atten";
- }
- public string GenerateRotationIndependentObjectScale( ref MasterNodeDataCollector dataCollector, int uniqueId )
- {
- string worldToObjectMatrix = ( m_currentSRPType == TemplateSRPType.BiRP ) ? "unity_WorldToObject" : "GetWorldToObjectMatrix()";
- string value = string.Format( "( 1.0 / float3( length( {0}[ 0 ].xyz ), length( {0}[ 1 ].xyz ), length( {0}[ 2 ].xyz ) ) )", worldToObjectMatrix );
- dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ParentObjectScaleStr, value );
- return GeneratorUtils.ParentObjectScaleStr;
- }
- public string GenerateObjectScale( ref MasterNodeDataCollector dataCollector, int uniqueId )
- {
- string worldToObjectMatrix = ( m_currentSRPType == TemplateSRPType.BiRP ) ? "unity_ObjectToWorld" : "GetObjectToWorldMatrix()";
- string value = string.Format( "float3( length( {0}[ 0 ].xyz ), length( {0}[ 1 ].xyz ), length( {0}[ 2 ].xyz ) )", worldToObjectMatrix );
- dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ObjectScaleStr, value );
- return GeneratorUtils.ObjectScaleStr;
- }
- public string GenerateObjectPosition( ref MasterNodeDataCollector dataCollector, int uniqueId )
- {
- string value = string.Empty;
- if ( m_currentSRPType != TemplateSRPType.BiRP )
- {
- value = "GetAbsolutePositionWS( UNITY_MATRIX_M._m03_m13_m23 )";
- }
- else
- {
- value = "UNITY_MATRIX_M._m03_m13_m23";
- }
- dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ObjectPositionStr, value );
- return GeneratorUtils.ObjectPositionStr;
- }
- public string GenerateObjectBoundsMin( ref MasterNodeDataCollector dataCollector, int uniqueId )
- {
- string value = string.Empty;
- if ( m_currentSRPType != TemplateSRPType.BiRP && ( ASEPackageManagerHelper.CurrentHDRPBaseline >= ASESRPBaseline.ASE_SRP_14 ||
- ASEPackageManagerHelper.CurrentURPBaseline >= ASESRPBaseline.ASE_SRP_14 ) )
- {
- value = "unity_RendererBounds_Min.xyz";
- }
- else
- {
- value = "0"; // not supported
- }
- dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ObjectBoundsMinStr, value );
- return GeneratorUtils.ObjectBoundsMinStr;
- }
- public string GenerateObjectBoundsMax( ref MasterNodeDataCollector dataCollector, int uniqueId )
- {
- string value = string.Empty;
- if ( m_currentSRPType != TemplateSRPType.BiRP && ( ASEPackageManagerHelper.CurrentHDRPBaseline >= ASESRPBaseline.ASE_SRP_14 ||
- ASEPackageManagerHelper.CurrentURPBaseline >= ASESRPBaseline.ASE_SRP_14 ) )
- {
- value = "unity_RendererBounds_Max.xyz";
- }
- else
- {
- value = "0"; // not supported
- }
- dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ObjectBoundsMaxStr, value );
- return GeneratorUtils.ObjectBoundsMaxStr;
- }
- public string GenerateObjectBoundsSize( ref MasterNodeDataCollector dataCollector, int uniqueId )
- {
- string value = string.Empty;
- if ( m_currentSRPType != TemplateSRPType.BiRP && ( ASEPackageManagerHelper.CurrentHDRPBaseline >= ASESRPBaseline.ASE_SRP_14 ||
- ASEPackageManagerHelper.CurrentURPBaseline >= ASESRPBaseline.ASE_SRP_14 ) )
- {
- value = "( unity_RendererBounds_Max.xyz - unity_RendererBounds_Min.xyz )";
- }
- else
- {
- value = "0"; // not supported
- }
- dataCollector.AddLocalVariable( uniqueId, PrecisionType.Float, WirePortDataType.FLOAT3, GeneratorUtils.ObjectBoundsSizeStr, value );
- return GeneratorUtils.ObjectBoundsSizeStr;
- }
- public string GetPosition( PositionNode.Space space, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- // overriding precision
- var precision = PrecisionType.Float;
- string result;
- if ( space == PositionNode.Space.Object )
- {
- result = GetVertexPosition( WirePortDataType.FLOAT3, precision, useMasterNodeCategory, customCategory );
- }
- else if ( space == PositionNode.Space.RelativeWorld )
- {
- result = GetRelativeWorldPos( useMasterNodeCategory, customCategory );
- }
- else if ( space == PositionNode.Space.View )
- {
- result = GetViewPos( useMasterNodeCategory, customCategory );
- }
- else // PositionNode.Space.World
- {
- result = GetWorldPos( useMasterNodeCategory, customCategory );
- }
- return result;
- }
- public string GetWorldPos( bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- // overriding precision
- var precision = PrecisionType.Float;
- string result = string.Empty;
- if( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_POSITION, WirePortDataType.FLOAT3, precision, ref result, useMasterNodeCategory, customCategory ) )
- {
- return result;
- }
- else if( m_currentSRPType == TemplateSRPType.HDRP )
- {
- if( GetCustomInterpolatedData( TemplateInfoOnSematics.RELATIVE_WORLD_POS, WirePortDataType.FLOAT3, precision, ref result, useMasterNodeCategory, customCategory ) )
- {
- string worldPosVarName = GeneratorUtils.WorldPositionStr;
- string relWorldPosConversion = string.Format( "GetAbsolutePositionWS( {0} )", result );
- m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT3, worldPosVarName, relWorldPosConversion );
- return worldPosVarName;
- }
- }
- string varName = GeneratorUtils.WorldPositionStr;//UIUtils.GetInputValueFromType( SurfaceInputs.WORLD_POS );
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- if( !m_availableVertData.ContainsKey( TemplateInfoOnSematics.POSITION ) )
- {
- UIUtils.ShowMessage( "Attempting to access inexisting vertex position to calculate world pos" );
- return "half3(0,0,0)";
- }
- string vertexPos = m_availableVertData[ TemplateInfoOnSematics.POSITION ].VarName;
- string worldPosConversion = string.Empty;
- //Check if world pos already defined in the vertex body
- if( !GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_POSITION, WirePortDataType.FLOAT3, precision, ref worldPosConversion, false, MasterNodePortCategory.Vertex ) )
- {
- if( m_currentSRPType == TemplateSRPType.HDRP )
- {
- worldPosConversion = string.Format( "GetAbsolutePositionWS( TransformObjectToWorld( ( {0} ).xyz ) )", vertexPos );
- }
- else if( m_currentSRPType == TemplateSRPType.URP )
- {
- worldPosConversion = string.Format( "TransformObjectToWorld( ( {0} ).xyz )", vertexPos );
- }
- else
- {
- worldPosConversion = string.Format( "mul( unity_ObjectToWorld, float4( ( {0} ).xyz, 1 ) ).xyz", vertexPos );
- }
- }
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precision, worldPosConversion, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetRelativeWorldPos( bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- PrecisionType precision = PrecisionType.Float;
- string result = string.Empty;
- if ( GetCustomInterpolatedData( TemplateInfoOnSematics.RELATIVE_WORLD_POS, WirePortDataType.FLOAT3, precision, ref result, useMasterNodeCategory, customCategory ) )
- {
- return result;
- }
- if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.RelativeWorldPositionStr ) )
- {
- string positionOS = GetVertexPosition( WirePortDataType.FLOAT4, precision, false, MasterNodePortCategory.Vertex );
- string positionCS = GetClipPos( false, MasterNodePortCategory.Vertex );
- string invProjMatrix = GeneratorUtils.GenerateInverseProjection( ref m_currentDataCollector, -1, precision, false, MasterNodePortCategory.Vertex );
- if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ViewHPositionStr ) )
- {
- m_currentDataCollector.AddToVertexLocalVariables( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ViewHPositionStr,
- string.Format( "mul( {0}, {1} )", invProjMatrix, GeneratorUtils.ClipPositionStr ) );
- }
- m_currentDataCollector.AddToVertexLocalVariables( -1, precision, WirePortDataType.FLOAT3, GeneratorUtils.RelativeWorldPositionStr,
- string.Format( "mul( ( float3x3 )UNITY_MATRIX_I_V, {0}.xyz / {0}.w )", GeneratorUtils.ViewHPositionStr ) );
- RegisterCustomInterpolatedData( GeneratorUtils.RelativeWorldPositionStr, WirePortDataType.FLOAT3, precision, GeneratorUtils.RelativeWorldPositionStr, useMasterNodeCategory, customCategory );
- }
- return GeneratorUtils.RelativeWorldPositionStr;
- }
- public string GetViewPos( bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- PrecisionType precision = PrecisionType.Float;
- string result = string.Empty;
- if ( GetCustomInterpolatedData( TemplateInfoOnSematics.VIEW_POSITION, WirePortDataType.FLOAT3, precision, ref result, useMasterNodeCategory, customCategory ) )
- {
- return result;
- }
- MasterNodePortCategory portCategory = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ViewPositionStr ) )
- {
- if ( portCategory == MasterNodePortCategory.Vertex || portCategory == MasterNodePortCategory.Tessellation )
- {
- string positionOS = GetVertexPosition( WirePortDataType.FLOAT3, precision, useMasterNodeCategory, customCategory );
- result = string.Format( IsSRP ? "TransformWorldToView( TransformObjectToWorld( {0} ) )" : "UnityObjectToViewPos( {0} )", positionOS );
- }
- else
- {
- string positionWS = GetWorldPos( useMasterNodeCategory, customCategory );
- result = string.Format( IsSRP ? "TransformWorldToView( {0} )" : "UnityWorldToViewPos( {0} )", positionWS );
- }
- m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT3, GeneratorUtils.ViewPositionStr, result );
- }
- return GeneratorUtils.ViewPositionStr;
- }
- public string GetClipPosForValue( string customVertexPos, string outputId, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string varName = GeneratorUtils.ClipPositionStr + outputId;
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- if( !m_availableVertData.ContainsKey( TemplateInfoOnSematics.POSITION ) )
- {
- UIUtils.ShowMessage( "Attempting to access inexisting vertex position to calculate clip pos" );
- return "half4(0,0,0,0)";
- }
- string formatStr = string.Empty;
- switch( m_currentSRPType )
- {
- default:
- case TemplateSRPType.BiRP:
- formatStr = "UnityObjectToClipPos( {0} )";
- break;
- case TemplateSRPType.HDRP:
- formatStr = "TransformWorldToHClip( TransformObjectToWorld( ( {0} ).xyz ) )";
- break;
- case TemplateSRPType.URP:
- formatStr = "TransformObjectToHClip( ( {0} ).xyz )";
- break;
- }
- string clipSpaceConversion = string.Format( formatStr, customVertexPos );
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT4, PrecisionType.Float, clipSpaceConversion, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetClipPos( bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string varName = GeneratorUtils.ClipPositionStr;// "clipPos";
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- if( !m_availableVertData.ContainsKey( TemplateInfoOnSematics.POSITION ) )
- {
- UIUtils.ShowMessage( "Attempting to access inexisting vertex position to calculate clip pos" );
- return "half4(0,0,0,0)";
- }
- string vertexPos = m_availableVertData[ TemplateInfoOnSematics.POSITION ].VarName;
- string formatStr = string.Empty;
- switch( m_currentSRPType )
- {
- default:
- case TemplateSRPType.BiRP:
- formatStr = "UnityObjectToClipPos( {0} )";
- break;
- case TemplateSRPType.HDRP:
- formatStr = "TransformWorldToHClip( TransformObjectToWorld( ( {0} ).xyz ) )";
- break;
- case TemplateSRPType.URP:
- formatStr = "TransformObjectToHClip( ( {0} ).xyz )";
- break;
- }
- string clipSpaceConversion = string.Format( formatStr, vertexPos );
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT4, PrecisionType.Float, clipSpaceConversion, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetScreenPosForValue( PrecisionType precision, string customVertexPos, string outputId, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- // overriding precision
- precision = PrecisionType.Float;
- string varName = UIUtils.GetInputValueFromType( SurfaceInputs.SCREEN_POS ) + outputId;
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string clipSpacePos = GetClipPosForValue( customVertexPos, outputId, false, MasterNodePortCategory.Vertex );
- string screenPosConversion = string.Empty;
- if( m_currentSRPType == TemplateSRPType.HDRP )
- {
- screenPosConversion = string.Format( "ComputeScreenPos( {0}, _ProjectionParams.x )", clipSpacePos );
- }
- else
- {
- screenPosConversion = string.Format( "ComputeScreenPos( {0} )", clipSpacePos );
- }
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT4, precision, screenPosConversion, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetScreenPosRaw( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- // overriding precision
- precision = PrecisionType.Float;
- string result = string.Empty;
- if ( GetCustomInterpolatedData( TemplateInfoOnSematics.SCREEN_POSITION, WirePortDataType.FLOAT4, precision, ref result, useMasterNodeCategory, customCategory ) )
- {
- return result;
- }
- string varName = UIUtils.GetInputValueFromType( SurfaceInputs.SCREEN_POS );
- if ( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- {
- return varName;
- }
- string clipSpacePos = GetClipPos( false, MasterNodePortCategory.Vertex );
- string screenPosConversion = string.Empty;
- if ( m_currentSRPType == TemplateSRPType.HDRP )
- {
- screenPosConversion = string.Format( "ComputeScreenPos( {0}, _ProjectionParams.x )", clipSpacePos );
- }
- else
- {
- screenPosConversion = string.Format( "ComputeScreenPos( {0} )", clipSpacePos );
- }
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT4, precision, screenPosConversion, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetScreenPosNormalized( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string result = string.Empty;
- if( GetCustomInterpolatedData( TemplateInfoOnSematics.SCREEN_POSITION_NORMALIZED, WirePortDataType.FLOAT4, precision, ref result, useMasterNodeCategory, customCategory ) )
- {
- return result;
- }
- if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosNormStr ) )
- {
- string screenPos = GetScreenPosRaw( precision, useMasterNodeCategory, customCategory );
- string clipPlaneTestOp = string.Format( "{0}.z = ( UNITY_NEAR_CLIP_VALUE >= 0 ) ? {0}.z : {0}.z * 0.5 + 0.5;", GeneratorUtils.ScreenPosNormStr );
- m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosNormStr, string.Format( GeneratorUtils.NormalizedScreenPosFormat, screenPos ) );
- m_currentDataCollector.AddLocalVariable( -1, clipPlaneTestOp );
- }
- return GeneratorUtils.ScreenPosNormStr;
- }
- public string GetScreenPosCenter( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string screenPosNorm = GetScreenPosNormalized( precision, useMasterNodeCategory, customCategory );
- if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosCenterStr ) )
- {
- string value = string.Format( "float4( {0}.xy * 2 - 1, 0, 0 )", screenPosNorm );
- m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosCenterStr, value );
- }
- return GeneratorUtils.ScreenPosCenterStr;
- }
- public string GetScreenPosTiled( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosTiledStr ) )
- {
- string screenPosNorm = GetScreenPosNormalized( precision, useMasterNodeCategory, customCategory );
- string value = string.Format( "frac( float4( ( {0}.x * 2 - 1 ) * _ScreenParams.x / _ScreenParams.y, {0}.y * 2 - 1, 0, 0 ) )", screenPosNorm );
- m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosTiledStr, value );
- }
- return GeneratorUtils.ScreenPosTiledStr;
- }
- public string GetScreenPosPixel( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- if ( !m_currentDataCollector.HasLocalVariableByName( GeneratorUtils.ScreenPosPixelStr ) )
- {
- string screenPosNorm = GetScreenPosNormalized( precision, useMasterNodeCategory, customCategory );
- GeneratorUtils.GenerateScreenPosNormToPixelFunction( ref m_currentDataCollector );
- m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT4, GeneratorUtils.ScreenPosPixelStr, GeneratorUtils.GenerateScreenPosNormToPixelFunctionCall( screenPosNorm ) );
- }
- return GeneratorUtils.ScreenPosPixelStr;
- }
- public string GetSurfaceDepth( DepthMode depthMode, PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string screenPos = GetScreenPosRaw( precision, useMasterNodeCategory, customCategory );
- string instruction = string.Format( "{0}.z / {0}.w", screenPos );
- string varName;
- switch ( depthMode )
- {
- case DepthMode.DepthLinearEye:
- {
- instruction = GeneratorUtils.ApplyLinearDepthModifier( ref m_currentDataCollector, instruction, depthMode );
- varName = GeneratorUtils.DepthLinearEyeStr;
- break;
- }
- case DepthMode.DepthLinear01:
- {
- instruction = GeneratorUtils.ApplyLinearDepthModifier( ref m_currentDataCollector, instruction, depthMode );
- varName = GeneratorUtils.DepthLinear01Str;
- break;
- }
- case DepthMode.DepthEye:
- {
- instruction = string.Format( "( {0} ) * ( _ProjectionParams.z - _ProjectionParams.y )", instruction );
- varName = GeneratorUtils.DepthEyeStr;
- break;
- }
- case DepthMode.Depth01:
- default:
- {
- varName = GeneratorUtils.DepthRawStr;
- break;
- }
- }
- m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT, varName, instruction );
- return varName;
- }
- public string GetViewVector( PrecisionType precisionType = PrecisionType.Float, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, ViewSpace space = ViewSpace.World )
- {
- string varName;
- switch ( space )
- {
- case ViewSpace.Tangent: varName = GeneratorUtils.TangentViewVectorStr; break;
- case ViewSpace.Object: varName = GeneratorUtils.ObjectViewVectorStr; break;
- case ViewSpace.View: varName = GeneratorUtils.ViewViewVectorStr; break;
- case ViewSpace.World:
- default: varName = GeneratorUtils.WorldViewVectorStr; break;
- }
- if ( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- {
- return varName;
- }
- string worldPos = GetWorldPos();
- string viewVectorWS = "( _WorldSpaceCameraPos.xyz - " + worldPos + " )";
- string viewVector;
- if ( space == ViewSpace.Tangent )
- {
- string tanToWorld0, tanToWorld1, tanToWorld2;
- GetWorldTangentTf( precisionType, out tanToWorld0, out tanToWorld1, out tanToWorld2 );
- viewVector = string.Format( " {0} * {3}.x + {1} * {3}.y + {2} * {3}.z", tanToWorld0, tanToWorld1, tanToWorld2, viewVectorWS );
- }
- else if ( space == ViewSpace.Object )
- {
- string worldToObjectMatrix = ( m_currentSRPType == TemplateSRPType.BiRP ) ? "unity_WorldToObject" : "GetWorldToObjectMatrix()";
- viewVector = string.Format( "mul( ( float3x3 ){0}, {1} )", worldToObjectMatrix, viewVectorWS );
- }
- else if ( space == ViewSpace.View )
- {
- viewVector = string.Format( "mul( ( float3x3 )UNITY_MATRIX_V, {0} )", viewVectorWS );
- }
- else
- {
- viewVector = viewVectorWS;
- }
- m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, varName, viewVector );
- return varName;
- }
- public string GetViewDir( PrecisionType precisionType = PrecisionType.Float, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, NormalizeType normalizeType = NormalizeType.Regular, ViewSpace space = ViewSpace.World )
- {
- if ( space == ViewSpace.World )
- {
- string result = string.Empty;
- if ( GetCustomInterpolatedData( TemplateInfoOnSematics.WORLD_VIEW_DIR, WirePortDataType.FLOAT3, precisionType, ref result, useMasterNodeCategory, customCategory ) )
- {
- return result;
- }
- }
- string varName;
- switch ( space )
- {
- case ViewSpace.Tangent: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.TangentViewDirectionStr : GeneratorUtils.TangentViewDirectionSafeStr; break;
- case ViewSpace.Object: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.ObjectViewDirectionStr : GeneratorUtils.ObjectViewDirectionSafeStr; break;
- case ViewSpace.View: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.ViewViewDirectionStr : GeneratorUtils.ViewViewDirectionSafeStr; break;
- case ViewSpace.World:
- default: varName = ( normalizeType == NormalizeType.Regular ) ? GeneratorUtils.WorldViewDirectionStr : GeneratorUtils.WorldViewDirectionSafeStr; break;
- }
- if ( !m_currentDataCollector.HasLocalVariableByName( varName ) )
- {
- string viewVector = GetViewVector( precisionType, useMasterNodeCategory, customCategory, space );
- if ( normalizeType == NormalizeType.Regular )
- {
- viewVector = "normalize( " + viewVector + " )";
- }
- else if ( normalizeType == NormalizeType.Safe )
- {
- viewVector = TemplateHelperFunctions.SafeNormalize( m_currentDataCollector, viewVector );
- }
- m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, varName, viewVector );
- }
- return varName;
- }
- public void GetWorldTangentTf( PrecisionType precisionType, out string tanToWorld0, out string tanToWorld1, out string tanToWorld2, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- tanToWorld0 = "tanToWorld0";
- tanToWorld1 = "tanToWorld1";
- tanToWorld2 = "tanToWorld2";
- if( HasCustomInterpolatedData( tanToWorld0, useMasterNodeCategory, customCategory ) ||
- HasCustomInterpolatedData( tanToWorld1, useMasterNodeCategory, customCategory ) ||
- HasCustomInterpolatedData( tanToWorld2, useMasterNodeCategory, customCategory ) )
- return;
- string worldTangent = GetWorldTangent( precisionType, useMasterNodeCategory, customCategory );
- string worldNormal = GetWorldNormal( precisionType, useMasterNodeCategory, customCategory );
- string worldBinormal = GetWorldBinormal( precisionType, useMasterNodeCategory, customCategory );
- string tanToWorldVar0 = string.Format( "float3( {0}.x, {1}.x, {2}.x )", worldTangent, worldBinormal, worldNormal );
- string tanToWorldVar1 = string.Format( "float3( {0}.y, {1}.y, {2}.y )", worldTangent, worldBinormal, worldNormal );
- string tanToWorldVar2 = string.Format( "float3( {0}.z, {1}.z, {2}.z )", worldTangent, worldBinormal, worldNormal );
- if( customCategory == MasterNodePortCategory.Vertex )
- {
- RegisterCustomInterpolatedData( tanToWorld0, WirePortDataType.FLOAT3, precisionType, tanToWorldVar0, useMasterNodeCategory, customCategory );
- RegisterCustomInterpolatedData( tanToWorld1, WirePortDataType.FLOAT3, precisionType, tanToWorldVar1, useMasterNodeCategory, customCategory );
- RegisterCustomInterpolatedData( tanToWorld2, WirePortDataType.FLOAT3, precisionType, tanToWorldVar2, useMasterNodeCategory, customCategory );
- }
- else
- {
- m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, tanToWorld0, tanToWorldVar0 );
- m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, tanToWorld1, tanToWorldVar1 );
- m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3, tanToWorld2, tanToWorldVar2 );
- }
- }
- public string GetTangentToWorldMatrixFast( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string worldTangent = GetWorldTangent( precisionType );
- string worldNormal = GetWorldNormal( precisionType );
- string worldBinormal = GetWorldBinormal( precisionType );
- string varName = GeneratorUtils.TangentToWorldFastStr;
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string result = string.Format( "float3x3( {0}.x, {1}.x, {2}.x, {0}.y, {1}.y, {2}.y, {0}.z, {1}.z, {2}.z )", worldTangent, worldBinormal, worldNormal );
- m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3x3, GeneratorUtils.TangentToWorldFastStr, result );
- return GeneratorUtils.TangentToWorldFastStr;
- }
- public string GetTangentToWorldMatrixPrecise( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string worldToTangent = GetWorldToTangentMatrix( precisionType, useMasterNodeCategory, customCategory );
- GeneratorUtils.Add3x3InverseFunction( ref m_currentDataCollector, UIUtils.PrecisionWirePortToCgType( precisionType, WirePortDataType.FLOAT ) );
- m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3x3, GeneratorUtils.TangentToWorldPreciseStr, string.Format( GeneratorUtils.Inverse3x3Header, worldToTangent ) );
- return GeneratorUtils.TangentToWorldPreciseStr;
- }
- public string GetWorldToTangentMatrix( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- string worldTangent = GetWorldTangent( precisionType );
- string worldNormal = GetWorldNormal( precisionType );
- string worldBinormal = GetWorldBinormal( precisionType );
- string varName = GeneratorUtils.WorldToTangentStr;// "worldToTanMat";
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string worldTanMat = string.Format( "float3x3( {0}, {1}, {2} )", worldTangent, worldBinormal, worldNormal );
- m_currentDataCollector.AddLocalVariable( -1, precisionType, WirePortDataType.FLOAT3x3, varName, worldTanMat );
- return varName;
- }
- public string GetObjectToViewPos( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- // overriding precision
- precision = PrecisionType.Float;
- string varName = "objectToViewPos";
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string vertexPos = GetVertexPosition( WirePortDataType.FLOAT3, precision, false, MasterNodePortCategory.Vertex );
- string formatStr = string.Empty;
- if( IsSRP )
- formatStr = "TransformWorldToView( TransformObjectToWorld( {0} ) )";
- else
- formatStr = "UnityObjectToViewPos( {0} )";
- string objectToViewPosValue = string.Format( formatStr, vertexPos );
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precision, objectToViewPosValue, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetEyeDepth( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, bool linear01 = false )
- {
- // overriding precision
- precision = PrecisionType.Float;
- string varName = "eyeDepth";
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string objectToView = GetObjectToViewPos( precision, false, MasterNodePortCategory.Vertex );
- string eyeDepthValue = string.Format( "-{0}.z", objectToView );
- if( linear01 )
- {
- eyeDepthValue += " * _ProjectionParams.w";
- }
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT, precision, eyeDepthValue, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetObjectSpaceLightDir( PrecisionType precisionType, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- if( !IsSRP )
- {
- m_currentDataCollector.AddToIncludes( -1, Constants.UnityLightingLib );
- m_currentDataCollector.AddToIncludes( -1, Constants.UnityAutoLightLib );
- }
- string varName = "objectSpaceLightDir";
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string vertexPos = GetVertexPosition( WirePortDataType.FLOAT4, precisionType, false, MasterNodePortCategory.Vertex );
- string objectSpaceLightDir = string.Empty;
- switch( m_currentSRPType )
- {
- default:
- case TemplateSRPType.BiRP:
- objectSpaceLightDir = string.Format( "ObjSpaceLightDir( {0} )", vertexPos );
- break;
- case TemplateSRPType.HDRP:
- string worldSpaceLightDir = GetWorldSpaceLightDir( precisionType, useMasterNodeCategory, customCategory );
- objectSpaceLightDir = string.Format( "mul( GetWorldToObjectMatrix(), {0} ).xyz", worldSpaceLightDir );
- break;
- case TemplateSRPType.URP:
- objectSpaceLightDir = "mul( GetWorldToObjectMatrix(), _MainLightPosition ).xyz";
- break;
- }
- RegisterCustomInterpolatedData( varName, WirePortDataType.FLOAT3, precisionType, objectSpaceLightDir, useMasterNodeCategory, customCategory );
- return varName;
- }
- public string GetWorldSpaceLightDir( PrecisionType precision, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- if( !IsSRP )
- {
- m_currentDataCollector.AddToIncludes( -1, Constants.UnityLightingLib );
- m_currentDataCollector.AddToIncludes( -1, Constants.UnityAutoLightLib );
- AddLateDirective( AdditionalLineType.Custom, "//This is a late directive" );
- }
- else
- {
- string lightVar;
- if( m_currentSRPType == TemplateSRPType.HDRP )
- {
- AddHDLightInfo();
- lightVar = "-" + string.Format( TemplateHelperFunctions.HDLightInfoFormat, "0", "forward" );
- }
- else
- {
- lightVar = "_MainLightPosition.xyz";
- }
- return m_currentDataCollector.SafeNormalizeLightDir ? string.Format( "SafeNormalize( {0} )", lightVar ) : lightVar;
- }
- string varName = "worldSpaceLightDir";
- if( HasCustomInterpolatedData( varName, useMasterNodeCategory, customCategory ) )
- return varName;
- string worldPos = GetWorldPos( useMasterNodeCategory, customCategory );
- string worldSpaceLightDir = string.Format( "UnityWorldSpaceLightDir( {0} )", worldPos );
- if( m_currentDataCollector.SafeNormalizeLightDir )
- {
- if( IsSRP )
- {
- worldSpaceLightDir = string.Format( "SafeNormalize( {0} )", worldSpaceLightDir );
- }
- else
- {
- m_currentDataCollector.AddToIncludes( -1, Constants.UnityBRDFLib );
- worldSpaceLightDir = string.Format( "Unity_SafeNormalize( {0} )", worldSpaceLightDir );
- }
- }
- m_currentDataCollector.AddLocalVariable( -1, precision, WirePortDataType.FLOAT3, varName, worldSpaceLightDir );
- return varName;
- }
- public void RegisterCustomInterpolatedData( string name, WirePortDataType dataType, PrecisionType precision, string vertexInstruction, bool useMasterNodeCategory = true,
- MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment, bool noInterpolationFlag = false, bool sampleFlag = false )
- {
- bool addLocalVariable = !name.Equals( vertexInstruction );
- MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- if( !m_customInterpolatedData.ContainsKey( name ) )
- {
- m_customInterpolatedData.Add( name, new TemplateCustomData( name, dataType ) );
- }
- if( !m_customInterpolatedData[ name ].IsVertex )
- {
- m_customInterpolatedData[ name ].IsVertex = true;
- if( addLocalVariable )
- m_currentDataCollector.AddToVertexLocalVariables( -1, precision, dataType, name, vertexInstruction );
- }
- if( category == MasterNodePortCategory.Fragment )
- {
- if( !m_customInterpolatedData[ name ].IsFragment )
- {
- m_customInterpolatedData[ name ].IsFragment = true;
- TemplateVertexData interpData = RequestNewInterpolator( dataType, false,null, noInterpolationFlag,sampleFlag );
- if( interpData == null )
- {
- Debug.LogErrorFormat( "Could not assign interpolator of type {0} to variable {1}", dataType, name );
- return;
- }
- m_currentDataCollector.AddToVertexLocalVariables( -1, m_currentTemplateData.VertexFunctionData.OutVarName + "." + interpData.VarNameWithSwizzle, name );
- m_currentDataCollector.AddToLocalVariables( -1, precision, dataType, name, m_currentTemplateData.FragmentFunctionData.InVarName + "." + interpData.VarNameWithSwizzle );
- }
- }
- }
- public bool HasCustomInterpolatedData( string name, bool useMasterNodeCategory = true, MasterNodePortCategory customCategory = MasterNodePortCategory.Fragment )
- {
- if( m_customInterpolatedData.ContainsKey( name ) )
- {
- MasterNodePortCategory category = useMasterNodeCategory ? m_currentDataCollector.PortCategory : customCategory;
- return ( category == MasterNodePortCategory.Fragment ) ? m_customInterpolatedData[ name ].IsFragment : m_customInterpolatedData[ name ].IsVertex;
- }
- return false;
- }
- public bool HasFragmentInputParams
- {
- get
- {
- if( m_fragmentInputParams != null )
- return m_fragmentInputParams.Count > 0;
- return false;
- }
- }
- public string FragInputParamsStr
- {
- get
- {
- string value = string.Empty;
- if( m_fragmentInputParams != null && m_fragmentInputParams.Count > 0 )
- {
- int count = m_fragmentInputParams.Count;
- if( count > 0 )
- {
- value = ", ";
- foreach( KeyValuePair<TemplateSemantics, TemplateInputParameters> kvp in m_fragmentInputParams )
- {
- value += kvp.Value.Declaration;
- if( --count > 0 )
- {
- value += " , ";
- }
- }
- }
- }
- return value;
- }
- }
- public string VertexInputParamsStr
- {
- get
- {
- string value = string.Empty;
- if( m_vertexInputParams != null && m_vertexInputParams.Count > 0 )
- {
- int count = m_vertexInputParams.Count;
- if( count > 0 )
- {
- value = ", ";
- foreach( KeyValuePair<TemplateSemantics, TemplateInputParameters> kvp in m_vertexInputParams )
- {
- value += kvp.Value.Declaration;
- if( --count > 0 )
- {
- value += " , ";
- }
- }
- }
- }
- return value;
- }
- }
- public void Destroy()
- {
- m_currentTemplate = null;
- m_currentTemplateData = null;
- m_currentDataCollector = null;
- if( m_fullSrpBatcherPropertiesList != null )
- {
- m_fullSrpBatcherPropertiesList.Clear();
- m_fullSrpBatcherPropertiesList = null;
- }
- if( m_srpBatcherPropertiesList != null )
- {
- m_srpBatcherPropertiesList.Clear();
- m_srpBatcherPropertiesList = null;
- }
- if( m_srpBatcherPropertiesDict != null )
- {
- m_srpBatcherPropertiesDict.Clear();
- m_srpBatcherPropertiesDict = null;
- }
- if( m_lateDirectivesList != null )
- {
- m_lateDirectivesList.Clear();
- m_lateDirectivesList = null;
- }
- if( m_lateDirectivesDict != null )
- {
- m_lateDirectivesDict.Clear();
- m_lateDirectivesDict = null;
- }
- if( m_registeredVertexData != null )
- {
- m_registeredVertexData.Clear();
- m_registeredVertexData = null;
- }
- if( m_vertexInputParams != null )
- {
- m_vertexInputParams.Clear();
- m_vertexInputParams = null;
- }
- if( m_fragmentInputParams != null )
- {
- m_fragmentInputParams.Clear();
- m_fragmentInputParams = null;
- }
- if( m_vertexDataDict != null )
- {
- m_vertexDataDict.Clear();
- m_vertexDataDict = null;
- }
- if( m_interpolatorData != null )
- {
- m_interpolatorData.Destroy();
- m_interpolatorData = null;
- }
- if( m_availableFragData != null )
- {
- m_availableFragData.Clear();
- m_availableFragData = null;
- }
- if( m_availableVertData != null )
- {
- m_availableVertData.Clear();
- m_availableVertData = null;
- }
- if( m_customInterpolatedData != null )
- {
- m_customInterpolatedData.Clear();
- m_customInterpolatedData = null;
- }
- if( m_specialVertexLocalVars != null )
- {
- m_specialVertexLocalVars.Clear();
- m_specialVertexLocalVars = null;
- }
- if( m_specialFragmentLocalVars != null )
- {
- m_specialFragmentLocalVars.Clear();
- m_specialFragmentLocalVars = null;
- }
- }
- public void BuildCBuffer( int nodeId )
- {
- m_fullSrpBatcherPropertiesList.Clear();
- if( m_srpBatcherPropertiesList.Count > 0 )
- {
- var regex = new Regex( @"(\d)\s+\b" );
- m_srpBatcherPropertiesList.Sort( ( a, b ) =>
- {
- var matchA = regex.Match( a.PropertyName );
- int sizeA = 0;
- if( matchA.Groups.Count > 1 && matchA.Groups[ 1 ].Value.Length > 0 )
- sizeA = Convert.ToInt32( matchA.Groups[ 1 ].Value, System.Globalization.CultureInfo.InvariantCulture );
- var matchB = regex.Match( b.PropertyName );
- int sizeB = 0;
- if( matchB.Groups.Count > 1 && matchB.Groups[ 1 ].Value.Length > 0 )
- sizeB = Convert.ToInt32( matchB.Groups[ 1 ].Value, System.Globalization.CultureInfo.InvariantCulture );
- return sizeB.CompareTo( sizeA );
- } );
- m_fullSrpBatcherPropertiesList.Insert(0, new PropertyDataCollector( nodeId, IOUtils.SRPCBufferPropertiesBegin ));
- m_fullSrpBatcherPropertiesList.AddRange( m_srpBatcherPropertiesList );
- m_fullSrpBatcherPropertiesList.Add( new PropertyDataCollector( nodeId, IOUtils.SRPCBufferPropertiesEnd ) );
- }
- }
- public void DumpSRPBatcher()
- {
- for( int i = 0; i < m_srpBatcherPropertiesList.Count; i++ )
- {
- Debug.Log( i + "::" + m_srpBatcherPropertiesList[ i ].PropertyName );
- }
- }
- public const string GlobalMaxInterpolatorReachedMsg = "Maximum amount of interpolators reached!\nPlease consider optmizing your shader!";
- public const string MaxInterpolatorSMReachedMsg = "Maximum amount of interpolators reached for current shader model on pass {0}! Please consider increasing the shader model to {1}!";
- public void CheckInterpolatorOverflow( string currShaderModel, string passName )
- {
- int maxInterpolatorAmount = TemplateHelperFunctions.AvailableInterpolators[ currShaderModel ];
- int currInterpolatorAmount = 1 + TemplateHelperFunctions.SemanticToInt[ InterpData.AvailableInterpolators[ InterpData.AvailableInterpolators.Count - 1 ].Semantic ];
- if( currInterpolatorAmount > maxInterpolatorAmount )
- {
- string shaderModel = string.Empty;
- if( TemplateHelperFunctions.GetShaderModelForInterpolatorAmount( currInterpolatorAmount, ref shaderModel ) )
- {
- UIUtils.ShowMessage( string.Format( MaxInterpolatorSMReachedMsg, passName, shaderModel ), MessageSeverity.Error );
- }
- else
- {
- UIUtils.ShowMessage( GlobalMaxInterpolatorReachedMsg, MessageSeverity.Error );
- }
- }
- }
- public Dictionary<TemplateSemantics, TemplateInputParameters> FragInputParameters { get { return m_fragmentInputParams; } }
- public bool HasVertexInputParams
- {
- get
- {
- if( m_vertexInputParams != null )
- return m_vertexInputParams.Count > 0;
- return false;
- }
- }
- public Dictionary<TemplateSemantics, TemplateInputParameters> VertexInputParameters { get { return m_vertexInputParams; } }
- public TemplateData CurrentTemplateData { get { return m_currentTemplateData; } }
- public int MultipassSubshaderIdx { get { return m_multipassSubshaderIdx; } }
- public int MultipassPassIdx { get { return m_multipassPassIdx; } }
- public TemplateSRPType CurrentSRPType { get { return m_currentSRPType; } set { m_currentSRPType = value; } }
- public bool IsHDRP { get { return m_currentSRPType == TemplateSRPType.HDRP; } }
- public bool IsLWRP { get { return m_currentSRPType == TemplateSRPType.URP; } }
- public bool IsSRP { get { return ( m_currentSRPType == TemplateSRPType.URP || m_currentSRPType == TemplateSRPType.HDRP ); } }
- public TemplateInterpData InterpData { get { return m_interpolatorData; } }
- public List<PropertyDataCollector> LateDirectivesList { get { return m_lateDirectivesList; } }
- public List<PropertyDataCollector> SrpBatcherPropertiesList { get { return m_srpBatcherPropertiesList; } }
- public List<PropertyDataCollector> FullSrpBatcherPropertiesList { get { return m_fullSrpBatcherPropertiesList; } }
- public Dictionary<TemplateSemantics, TemplateVertexData> VertexDataDict { get { return m_vertexDataDict; } }
- }
- }
|