1- using System . Reflection ;
21using UnityEngine ;
32using UnityEditor . Graphing ;
43using UnityEditor . ShaderGraph . Drawing . Controls ;
4+ using UnityEditor . ShaderGraph . Internal ;
55
66namespace UnityEditor . ShaderGraph
77{
88 [ FormerName ( "UnityEditor.ShaderGraph.BakedGAbstractMaterialNode" ) ]
99 [ FormerName ( "UnityEditor.ShaderGraph.LightProbeNode" ) ]
1010 [ Title ( "Input" , "Lighting" , "Baked GI" ) ]
11- class BakedGINode : CodeFunctionNode
11+ class BakedGINode : AbstractMaterialNode , IGeneratesBodyCode , IMayRequirePixelPosition , IMayRequirePosition , IMayRequireNormal , IMayRequireMeshUV
1212 {
1313 public override bool hasPreview { get { return false ; } }
1414
@@ -18,14 +18,6 @@ public BakedGINode()
1818 synonyms = new string [ ] { "global illumination" } ;
1919 }
2020
21- protected override MethodInfo GetFunctionToConvert ( )
22- {
23- if ( applyScaling . isOn )
24- return GetType ( ) . GetMethod ( "Unity_BakedGIScale" , BindingFlags . Static | BindingFlags . NonPublic ) ;
25- else
26- return GetType ( ) . GetMethod ( "Unity_BakedGI" , BindingFlags . Static | BindingFlags . NonPublic ) ;
27- }
28-
2921 [ SerializeField ]
3022 private bool m_ApplyScaling = true ;
3123
@@ -42,36 +34,85 @@ public ToggleData applyScaling
4234 }
4335 }
4436
45- static string Unity_BakedGI (
46- [ Slot ( 2 , Binding . WorldSpacePosition ) ] Vector3 Position ,
47- [ Slot ( 0 , Binding . WorldSpaceNormal ) ] Vector3 Normal ,
48- [ Slot ( 3 , Binding . MeshUV1 ) ] Vector2 StaticUV ,
49- [ Slot ( 4 , Binding . MeshUV2 ) ] Vector2 DynamicUV ,
50- [ Slot ( 1 , Binding . None ) ] out Vector3 Out )
37+ const int kNormalWSInputSlotId = 0 ;
38+ const string kNormalWSInputSlotName = "NormalWS" ;
39+
40+ const int kOutputSlotId = 1 ;
41+ const string kOutputSlotName = "Out" ;
42+
43+ const int kPositionWSInputSlotId = 2 ;
44+ const string kPositionWSInputSlotName = "PositionWS" ;
45+
46+ const int kStaticUVInputSlotId = 3 ;
47+ const string kStaticUVInputSlotName = "StaticUV" ;
48+
49+ const int kDynamicUVInputSlotId = 4 ;
50+ const string kDynamicUVInputSlotName = "DynamicUV" ;
51+
52+ public sealed override void UpdateNodeAfterDeserialization ( )
5153 {
52- Out = Vector3 . one ;
53- return
54- @"
55- {
56- Out = SHADERGRAPH_BAKED_GI(Position, Normal, StaticUV, DynamicUV, false);
57- }
58- " ;
54+ // Input
55+ AddSlot ( new NormalMaterialSlot ( kNormalWSInputSlotId , kNormalWSInputSlotName , kNormalWSInputSlotName , CoordinateSpace . World ) ) ;
56+ AddSlot ( new PositionMaterialSlot ( kPositionWSInputSlotId , kPositionWSInputSlotName , kPositionWSInputSlotName , CoordinateSpace . World ) ) ;
57+ AddSlot ( new UVMaterialSlot ( kStaticUVInputSlotId , kStaticUVInputSlotName , kStaticUVInputSlotName , UVChannel . UV1 ) ) ;
58+ AddSlot ( new UVMaterialSlot ( kDynamicUVInputSlotId , kDynamicUVInputSlotName , kDynamicUVInputSlotName , UVChannel . UV2 ) ) ;
59+
60+ // Output
61+ AddSlot ( new Vector3MaterialSlot ( kOutputSlotId , kOutputSlotName , kOutputSlotName , SlotType . Output , Vector3 . zero ) ) ;
62+
63+ RemoveSlotsNameNotMatching ( new [ ]
64+ {
65+ // Input
66+ kNormalWSInputSlotId ,
67+ kPositionWSInputSlotId ,
68+ kStaticUVInputSlotId ,
69+ kDynamicUVInputSlotId ,
70+
71+ // Output
72+ kOutputSlotId ,
73+ } ) ;
5974 }
6075
61- static string Unity_BakedGIScale (
62- [ Slot ( 2 , Binding . WorldSpacePosition ) ] Vector3 Position ,
63- [ Slot ( 0 , Binding . WorldSpaceNormal ) ] Vector3 Normal ,
64- [ Slot ( 3 , Binding . MeshUV1 ) ] Vector2 StaticUV ,
65- [ Slot ( 4 , Binding . MeshUV2 ) ] Vector2 DynamicUV ,
66- [ Slot ( 1 , Binding . None ) ] out Vector3 Out )
76+ public void GenerateNodeCode ( ShaderStringBuilder sb , GenerationMode generationMode )
6777 {
68- Out = Vector3 . one ;
69- return
70- @"
71- {
72- Out = SHADERGRAPH_BAKED_GI(Position, Normal, StaticUV, DynamicUV, true);
73- }
74- " ;
78+ if ( generationMode == GenerationMode . ForReals )
79+ {
80+ sb . AppendLine ( "$precision3 {6} = SHADERGRAPH_BAKED_GI({0}, {1}, IN.{2}.xy, {3}, {4}, {5});" ,
81+ GetSlotValue ( kPositionWSInputSlotId , generationMode ) ,
82+ GetSlotValue ( kNormalWSInputSlotId , generationMode ) ,
83+ ShaderGeneratorNames . PixelPosition ,
84+ GetSlotValue ( kStaticUVInputSlotId , generationMode ) ,
85+ GetSlotValue ( kDynamicUVInputSlotId , generationMode ) ,
86+ applyScaling . isOn ? "true" : "false" ,
87+ GetVariableNameForSlot ( kOutputSlotId ) ) ;
88+ }
89+ else
90+ {
91+ // Output zeros
92+ sb . AppendLine ( "$precision3 {0} = 0.0;" ,
93+ GetVariableNameForSlot ( kOutputSlotId ) ) ;
94+ }
95+ }
96+
97+ public bool RequiresPixelPosition ( ShaderStageCapability stageCapability = ShaderStageCapability . All )
98+ {
99+ return true ; // needed for APV sampling noise when TAA is used
100+ }
101+
102+ public NeededCoordinateSpace RequiresPosition ( ShaderStageCapability stageCapability = ShaderStageCapability . All )
103+ {
104+ return FindSlot < PositionMaterialSlot > ( kPositionWSInputSlotId ) . RequiresPosition ( ) ;
105+ }
106+
107+ public NeededCoordinateSpace RequiresNormal ( ShaderStageCapability stageCapability = ShaderStageCapability . All )
108+ {
109+ return FindSlot < NormalMaterialSlot > ( kNormalWSInputSlotId ) . RequiresNormal ( ) ;
110+ }
111+
112+ public bool RequiresMeshUV ( UVChannel channel , ShaderStageCapability stageCapability = ShaderStageCapability . All )
113+ {
114+ return FindSlot < UVMaterialSlot > ( kStaticUVInputSlotId ) . RequiresMeshUV ( channel ) ||
115+ FindSlot < UVMaterialSlot > ( kDynamicUVInputSlotId ) . RequiresMeshUV ( channel ) ;
75116 }
76117 }
77118}
0 commit comments