Subversion Repository Public Repository

Nextrek

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"AutoLipsync.cs"
 * 
 *	This script provides simple lipsyncing for talking characters, "Half Life 1"-style.
 *	The Transform defined in jawBone will rotate according to the sound that the gameObject is emitting.
 * 
 */

using UnityEngine;
using System.Collections.Generic;
using System.Collections;

public class AutoLipSync : MonoBehaviour
{
	
	public Transform jawBone;

	public enum Coord { W, X, Y, Z };
	public Coord coordinateToAffect;
	
	public float rotationFactor = 10f;
	
	private float volume;
	private float bin = 0.04f;
	private int width = 64;
	private float output;

	private float[] array;
	private Quaternion jawRotation;
	
	
	private void Awake ()
	{
		array = new float[width];	
	}
	
	
	private void FixedUpdate ()
	{
		if (audio.isPlaying)
		{
			audio.GetOutputData(array, 0);
			float num3 = 0f;
			for (int i = 0; i < width; i++)
			{
			    float num4 = Mathf.Abs(array[i]);
			    num3 += num4;
			}
			num3 /= (float) width;
			
			// Only record changes big enough
			if (Mathf.Abs (num3 - volume) > bin)
				volume = num3;

			volume = Mathf.Clamp01 (volume * 2);
			volume *= 0.3f;
			
			output = Mathf.Lerp (output, volume, Time.deltaTime * Mathf.Abs (rotationFactor));
			
		}
		else
		{
			output = 0f;
		}
		
	}
	
	
	private void LateUpdate ()
	{
		jawRotation = jawBone.localRotation;
		
		if (coordinateToAffect == Coord.W)
		{
			if (rotationFactor < 0)
			{
				jawRotation.w += output;
			}
			else
			{
				jawRotation.w -= output;
			}
		}
		else if (coordinateToAffect == Coord.X)
		{
			if (rotationFactor < 0)
			{
				jawRotation.x += output;
			}
			else
			{
				jawRotation.x -= output;
			}
		}
		else if (coordinateToAffect == Coord.Y)
		{
			if (rotationFactor < 0)
			{
				jawRotation.y += output;
			}
			else
			{
				jawRotation.y -= output;
			}
		}
		else if (coordinateToAffect == Coord.Z)
		{
			if (rotationFactor < 0)
			{
				jawRotation.z += output;
			}
			else
			{
				jawRotation.z -= output;
			}
		}
		
		jawBone.localRotation = jawRotation;
	}
	
}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/AdventureCreator/Scripts/Speech/AutoLipSync.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000