1+ /*
2+ This program is free software; you can redistribute it and/or
3+ modify it under the terms of the GNU General Public License
4+ as published by the Free Software Foundation; either version 2
5+ of the License, or (at your option) any later version.
6+
7+ This program is distributed in the hope that it will be useful,
8+ but WITHOUT ANY WARRANTY; without even the implied warranty of
9+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+ GNU General Public License for more details.
11+
12+ You should have received a copy of the GNU General Public License
13+ along with this program; if not, write to the Free Software Foundation,
14+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15+
16+ The Original Code is Copyright (C) 2020 Voxell Technologies.
17+ All rights reserved.
18+ */
19+
20+ using UnityEngine ;
21+
22+ namespace Voxell . Audio
23+ {
24+ public partial class AudioCore
25+ {
26+ [ Header ( "Audio Visualizer Interaction" ) ]
27+ [ Tooltip ( "Velocity of audio visualizer when there is no interaction" ) ]
28+ public Vector2 idleVelocity = new Vector2 ( 1.0f , - 1.0f ) ;
29+
30+ [ Tooltip ( "Minimum turbulence for VFX graph when there is no interaction or extra force" ) ]
31+ public float idleNoiseIntensity = 0.1f ;
32+ public float maxNoiseIntensity = 0.3f ;
33+
34+ [ Tooltip ( "Sensitivity of the audio visualizer on mouse drag" ) ]
35+ public float rotationMultiplier = 500 ;
36+
37+ [ Range ( 0.8f , 0.99f ) , Tooltip ( "A number that multiplies the velocity of the audio visualizer on each update" ) ]
38+ public float velocityDamping = 0.9f ;
39+
40+ [ Tooltip ( "Multiplier of noise intensity before sending it to the VFX graph" ) ]
41+ public float intensityCoefficient = 0.1f ;
42+
43+ private Vector2 _rotationVelocity ;
44+
45+ private void InitAgentInteraction ( ) => _rotationVelocity = Vector2 . zero ;
46+
47+ private void UpdateAgentInteraction ( )
48+ {
49+ // if (Input.GetMouseButton(0)) OnMouseDrag();
50+
51+ if ( Vector3 . Dot ( transform . up , Vector3 . up ) >= 0 )
52+ transform . Rotate ( Camera . main . transform . up , - Vector3 . Dot ( _rotationVelocity , Camera . main . transform . right ) , Space . World ) ;
53+ else
54+ transform . Rotate ( Camera . main . transform . up , - Vector3 . Dot ( _rotationVelocity , Camera . main . transform . right ) , Space . World ) ;
55+
56+ transform . Rotate ( Camera . main . transform . right , Vector3 . Dot ( _rotationVelocity , Camera . main . transform . up ) , Space . World ) ;
57+ _rotationVelocity *= velocityDamping ;
58+ _rotationVelocity += idleVelocity * Time . deltaTime ;
59+
60+ if ( _rotationVelocity . magnitude <= EPSILON ) _rotationVelocity = Vector2 . zero ;
61+
62+ float intensity = Mathf . Clamp ( _rotationVelocity . magnitude * intensityCoefficient , idleNoiseIntensity , maxNoiseIntensity ) ;
63+ audioVFX . SetFloat ( VFXPropertyId . float_noiseIntensity , intensity ) ;
64+ }
65+
66+ /// <summary>
67+ /// Calcualte mouse drag force and apply rotational force accordingly
68+ /// </summary>
69+ private void OnMouseDrag ( )
70+ {
71+ float rotationX = Input . GetAxis ( "Mouse X" ) * rotationMultiplier * Mathf . Deg2Rad ;
72+ float rotationY = Input . GetAxis ( "Mouse Y" ) * rotationMultiplier * Mathf . Deg2Rad ;
73+
74+ _rotationVelocity = new Vector2 ( rotationX , rotationY ) ;
75+ }
76+ }
77+ }
0 commit comments