summaryrefslogtreecommitdiff
path: root/Assets/AmplifyShaderEditor/Plugins/Editor/Nodes/Misc/RotateAboutAxisNode.cs
blob: b9b0a6c2d5d4fb6823807c18c3a69756412711e8 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Amplify Shader Editor - Visual Shader Editing Tool
// Copyright (c) Amplify Creations, Lda <info@amplify.pt>
using UnityEditor;
using System;

namespace AmplifyShaderEditor
{
    [Serializable]
    [NodeAttributes( "Rotate About Axis", "Vector Operators", "Rotates a vector around a normalized axis" )]
    public class RotateAboutAxisNode : ParentNode
    {
        private const string FunctionHeader = "float3 RotateAroundAxis( float3 center, float3 original, float3 u, float angle )";
        private const string FunctionCall = "RotateAroundAxis( {0}, {1}, {2}, {3} )";
        private readonly string[] FunctionBody =
        {
            "float3 RotateAroundAxis( float3 center, float3 original, float3 u, float angle )\n",
            "{\n",
            "\toriginal -= center;\n",
            "\tfloat C = cos( angle );\n",
            "\tfloat S = sin( angle );\n",
            "\tfloat t = 1 - C;\n",
            "\tfloat m00 = t * u.x * u.x + C;\n",
            "\tfloat m01 = t * u.x * u.y - S * u.z;\n",
            "\tfloat m02 = t * u.x * u.z + S * u.y;\n",
            "\tfloat m10 = t * u.x * u.y + S * u.z;\n",
            "\tfloat m11 = t * u.y * u.y + C;\n",
            "\tfloat m12 = t * u.y * u.z - S * u.x;\n",
            "\tfloat m20 = t * u.x * u.z - S * u.y;\n",
            "\tfloat m21 = t * u.y * u.z + S * u.x;\n",
            "\tfloat m22 = t * u.z * u.z + C;\n",
            "\tfloat3x3 finalMatrix = float3x3( m00, m01, m02, m10, m11, m12, m20, m21, m22 );\n",
            "\treturn mul( finalMatrix, original ) + center;\n",
            "}\n"
        };

        private const string NormalizeAxisLabel = "Rotation Axis";
        private const string NonNormalizeAxisLabel = "Normalized Rotation Axis";
        private const string NormalizeAxisStr = "Normalize Axis";

        [UnityEngine.SerializeField]
        private bool m_normalizeAxis = false;

        protected override void CommonInit( int uniqueId )
        {
            base.CommonInit( uniqueId );
            AddInputPort( WirePortDataType.FLOAT3, false, m_normalizeAxis? NormalizeAxisLabel: NonNormalizeAxisLabel );
            AddInputPort( WirePortDataType.FLOAT, false, "Rotation Angle" );
            AddInputPort( WirePortDataType.FLOAT3, false, "Pivot Point" );
            AddInputPort( WirePortDataType.FLOAT3, false, "Position" );
            AddOutputPort( WirePortDataType.FLOAT3, Constants.EmptyPortValue );
            m_useInternalPortData = true;
			m_autoWrapProperties = true;
        }

        public override void DrawProperties()
        {
            base.DrawProperties();
            EditorGUI.BeginChangeCheck();
            m_normalizeAxis = EditorGUILayoutToggle( NormalizeAxisStr, m_normalizeAxis );
            if( EditorGUI.EndChangeCheck() )
            {
                m_inputPorts[ 0 ].Name = (m_normalizeAxis ? NormalizeAxisLabel : NonNormalizeAxisLabel);
            }
        }

        public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
        {
            if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
                return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );

            string normalizeRotAxis = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
            if( m_normalizeAxis )
            {
                normalizeRotAxis = string.Format( "normalize( {0} )", normalizeRotAxis );
            }
            string rotationAngle = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
            string pivotPoint = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
            string position = m_inputPorts[ 3 ].GeneratePortInstructions( ref dataCollector );
            dataCollector.AddFunction( FunctionHeader, FunctionBody, false );
            RegisterLocalVariable( 0, string.Format( FunctionCall, pivotPoint, position, normalizeRotAxis, rotationAngle ), ref dataCollector, "rotatedValue" + OutputId );
            return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
        }
        public override void ReadFromString( ref string[] nodeParams )
        {
            base.ReadFromString( ref nodeParams );
            m_normalizeAxis = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
        }

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