summaryrefslogtreecommitdiff
path: root/Assets/AmplifyShaderEditor/Plugins/Editor/Nodes/Vertex/ObjectScaleNode.cs
blob: 6dd1b8d3a81a93d4f919d40e986a06574e5713fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>

using System;
using UnityEngine;
using UnityEditor;
namespace AmplifyShaderEditor
{
	[Serializable]
	[NodeAttributes( "Object Scale", "Vertex Data", "Object Scale extracted directly from its transform matrix" )]
	public class ObjectScaleNode : ParentNode
	{
		private const string RotationIndependentScaleStr = "Rotation Independent Scale";

		[SerializeField]
		private bool m_rotationIndependentScale = false;
		
		protected override void CommonInit( int uniqueId )
		{
			base.CommonInit( uniqueId );
			AddOutputVectorPorts( WirePortDataType.FLOAT3, "XYZ" );
			m_drawPreviewAsSphere = true;
			m_previewShaderGUID = "5540033c6c52f51468938c1a42bd2730";
			m_textLabelWidth = 180;
			UpdateMaterialPass();
			m_autoWrapProperties = true;
		}

		public override void DrawProperties()
		{
			base.DrawProperties();
			EditorGUI.BeginChangeCheck();
			m_rotationIndependentScale = EditorGUILayoutToggle( RotationIndependentScaleStr, m_rotationIndependentScale );
			if( EditorGUI.EndChangeCheck() )
			{
				UpdateMaterialPass();
			}
		}

		public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
		{
			string objectScale = m_rotationIndependentScale ?	GeneratorUtils.GenerateRotationIndependentObjectScale( ref dataCollector, UniqueId ):
														GeneratorUtils.GenerateObjectScale( ref dataCollector, UniqueId );

			return GetOutputVectorItem( 0, outputId, objectScale );
		}

		public override void ReadFromString( ref string[] nodeParams )
		{
			base.ReadFromString( ref nodeParams );
			if( UIUtils.CurrentShaderVersion() < 17402 )
			{
				m_rotationIndependentScale = false;
			}
			else
			{
				m_rotationIndependentScale = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
			}
			UpdateMaterialPass();
		}

		public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
		{
			base.WriteToString( ref nodeInfo, ref connectionsInfo );
			IOUtils.AddFieldValueToString( ref nodeInfo, m_rotationIndependentScale );
		}

		void UpdateMaterialPass()
		{
			m_previewMaterialPassId = m_rotationIndependentScale ? 1 : 0;
			PreviewIsDirty = true;
		}

	}
}