BlendCharacterGui.js 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
/**
 * @author Michael Guerrero / http://realitymeltdown.com
 */

function BlendCharacterGui(animations) {

	var controls = {

		gui: null,
10 11
		"Show Model": true,
		"Show Skeleton": false,
12 13 14 15 16 17 18 19 20 21 22
		"Time Scale": 1.0,
		"Step Size": 0.016,
		"Crossfade Time": 3.5,
		"idle": 0.33,
		"walk": 0.33,
		"run": 0.33

	};

	var animations = animations;

23 24 25 26 27 28
	this.showModel = function() {

		return controls['Show Model'];

	};

29
	this.showSkeleton = function() {
30

31
		return controls['Show Skeleton'];
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

	};

	this.getTimeScale = function() {

		return controls['Time Scale'];

	};

	this.update = function() {

		controls[ 'idle'] = animations[ 'idle' ].weight;
		controls[ 'walk'] = animations[ 'walk' ].weight;
		controls[ 'run'] = animations[ 'run' ].weight;

	};

	var init = function() {

		controls.gui = new dat.GUI();

		var settings = controls.gui.addFolder( 'Settings' );
		var playback = controls.gui.addFolder( 'Playback' );
		var blending = controls.gui.addFolder( 'Blend Tuning' );

57
		settings.add( controls, "Show Model" ).onChange( controls.showModelChanged );
58
		settings.add( controls, "Show Skeleton" ).onChange( controls.showSkeletonChanged );
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
		settings.add( controls, "Time Scale", 0, 1, 0.01 );
		settings.add( controls, "Step Size", 0.01, 0.1, 0.01 );
		settings.add( controls, "Crossfade Time", 0.1, 6.0, 0.05 );

		// These controls execute functions
		playback.add( controls, "start" );
		playback.add( controls, "pause" );
		playback.add( controls, "step" );
		playback.add( controls, "idle to walk" );
		playback.add( controls, "walk to run" );
		playback.add( controls, "warp walk to run" );

		blending.add( controls, "idle", 0, 1, 0.01).listen().onChange( controls.weight );
		blending.add( controls, "walk", 0, 1, 0.01).listen().onChange( controls.weight );
		blending.add( controls, "run", 0, 1, 0.01).listen().onChange( controls.weight );

		settings.open();
		playback.open();
		blending.open();

B
brason 已提交
79
	};
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

	var getAnimationData = function() {

		return {

			detail: {

				anims: [ "idle", "walk", "run" ],

				weights: [ controls['idle'],
						   controls['walk'],
						   controls['run'] ]
			}

		};
B
brason 已提交
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

	controls.start = function() {

		var startEvent = new CustomEvent( 'start-animation', getAnimationData() );
		window.dispatchEvent(startEvent);

	};

	controls.stop = function() {

		var stopEvent = new CustomEvent( 'stop-animation' );
		window.dispatchEvent( stopEvent );

	};

	controls.pause = function() {

		var pauseEvent = new CustomEvent( 'pause-animation' );
		window.dispatchEvent( pauseEvent );

	};

	controls.step = function() {

		var stepData = { detail: { stepSize: controls['Step Size'] } };
		window.dispatchEvent( new CustomEvent('step-animation', stepData ));

	};

	controls.weight = function() {

		// renormalize
		var sum = controls['idle'] + controls['walk'] + controls['run'];
		controls['idle'] /= sum;
		controls['walk'] /= sum;
		controls['run'] /= sum;

		var weightEvent = new CustomEvent( 'weight-animation', getAnimationData() );
		window.dispatchEvent(weightEvent);
	};

	controls.crossfade = function( from, to ) {

		var fadeData = getAnimationData();
		fadeData.detail.from = from;
		fadeData.detail.to = to;
		fadeData.detail.time = controls[ "Crossfade Time" ];

		window.dispatchEvent( new CustomEvent( 'crossfade', fadeData ) );
B
brason 已提交
145
	};
146 147 148 149 150 151 152 153 154

	controls.warp = function( from, to ) {

		var warpData = getAnimationData();
		warpData.detail.from = 'walk';
		warpData.detail.to = 'run';
		warpData.detail.time = controls[ "Crossfade Time" ];

		window.dispatchEvent( new CustomEvent( 'warp', warpData ) );
B
brason 已提交
155
	};
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

	controls['idle to walk'] = function() {

		controls.crossfade( 'idle', 'walk' );

	};

	controls['walk to run'] = function() {

		controls.crossfade( 'walk', 'run' );

	};

	controls['warp walk to run'] = function() {

		controls.warp( 'walk', 'run' );

	};

175
	controls.showSkeletonChanged = function() {
176 177 178

		var data = {
			detail: {
179
				shouldShow: controls['Show Skeleton']
180
			}
B
brason 已提交
181
		};
182

183
		window.dispatchEvent( new CustomEvent( 'toggle-show-skeleton', data ) );
B
brason 已提交
184
	};
185 186


187 188 189 190 191 192
	controls.showModelChanged = function() {

		var data = {
			detail: {
				shouldShow: controls['Show Model']
			}
B
brason 已提交
193
		};
194 195

		window.dispatchEvent( new CustomEvent( 'toggle-show-model', data ) );
B
brason 已提交
196
	};
197 198


199 200
	init.call(this);

201
}