让pv3d(papervision3D)支持单帧前进、后退(nextFrame)。
下载最新的源码,找到animationController.
修改如下:
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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | package org.papervision3d.core.controller { import flash.events.EventDispatcher; import flash.utils.getTimer; import org.papervision3d.core.animation.IAnimatable; import org.papervision3d.core.animation.channel.Channel3D; import org.papervision3d.core.animation.clip.AnimationClip3D; import org.papervision3d.events.AnimationEvent; /** * The AnimationController class controls an animation. * * @author Tim Knip / floorplanner.com */ public class AnimationController extends EventDispatcher implements IObjectController, IAnimatable { /** * Start time of animation in seconds. */ public var startTime : Number ; /** * End time of animation in seconds. */ public var endTime : Number ; /** * */ private var _channels : Array ; /** * */ private var _isPlaying : Boolean ; /** * */ private var _isPaused : Boolean ; /** * */ private var _isStepping : Boolean = false ; /** * */ private var _currentTime : Number ; /** * */ private var _currentTimeStamp : int ; /** * */ private var _pauseTime : Number ; /** * */ private var _loop : Boolean ; /** */ private var _clip : AnimationClip3D; /** */ private var _clips : Array ; /** */ private var _clipByName : Object ; /** */ private var _dispatchEvents : Boolean ; /** 帧计算 */ private var _frameIndex: int = 0 ; /** * Constructor. */ public function AnimationController(dispatchEvents: Boolean = true ) { super (); _dispatchEvents = dispatchEvents; init(); } /** * */ public function addChannel(channel : Channel3D) : Channel3D { if (_channels.indexOf(channel) == - 1 ) { _channels.push(channel); updateStartAndEndTime(); return channel; } return null ; } /** * */ public function addClip(clip : AnimationClip3D) : AnimationClip3D { if (_clips.indexOf(clip) == - 1 ) { _clips.push(clip); _clipByName[clip.name] = clip; return clip; } return null ; } /** * */ public function clone() : AnimationController { var controller : AnimationController = new AnimationController(); var channel : Channel3D; var cloned : Channel3D; var i : int ; for (i = 0 ; i < _channels.length; i++) { channel = _channels[i]; cloned = channel.clone(); controller.addChannel(cloned); } return controller; } /** * Initialize. */ protected function init() : void { _channels = new Array (); _clips = new Array (); _clipByName = new Object (); _isPlaying = false ; _isPaused = false ; _currentTime = 0 ; _loop = false ; updateStartAndEndTime(); } /** * Pause the animation. */ public function pause() : void { _pauseTime = _currentTime; _isPaused = true ; _isPlaying = false ; if (_dispatchEvents) { var clipName : String = _clip ? _clip.name : "all" ; dispatchEvent( new AnimationEvent(AnimationEvent.PAUSE, _pauseTime, clipName)); } } /** * Plays the animation. * * @param clip Clip to play. Default is "all" * @param loop Whether the animation should loop. Default is true. */ public function play(clip: String = "all" , loop: Boolean = true ) : void { if (clip && clip.length && _clipByName[clip] is AnimationClip3D) { _clip = _clipByName[clip]; } else { _clip = null ; } if (_channels.length) { _loop = loop; _currentTimeStamp = getTimer(); if (_clip) { _currentTimeStamp -= (_clip.startTime * 1000 ); } _isPlaying = true ; _isPaused = false ; } if (_dispatchEvents) { var clipName : String = _clip ? _clip.name : "all" ; var time : Number = _clip ? _clip.startTime : 0 ; dispatchEvent( new AnimationEvent(AnimationEvent.START, time, clipName)); } } /** * 显示下一帧 */ public function next(): void { _frameIndex++; if (_frameIndex >= totalFrames) { _frameIndex = 0 ; } _currentTime = endTime / totalFrames * _frameIndex; this ._isStepping = true ; } public function get totalFrames(): int { var count: int = 0 ; if (! this ._channels) return count; for each ( var _channel:Channel3D in this ._channels) { if (_channel.output == null ) continue ; count = Math.max(_channel.output.length, count); } return count; } /** * */ public function removeAllChannels() : void { while (_channels.length) { _channels.pop(); } updateStartAndEndTime(); } /** * */ public function removeChannel(channel : Channel3D) : Channel3D { var pos : int = _channels.indexOf(channel); if (pos >= 0 ) { _channels.splice(pos, 1 ); updateStartAndEndTime(); return channel; } return null ; } /** * Removes a clip. * * @param clip * * @return The removed clip or null on failure. */ public function removeClip(clip : AnimationClip3D) : AnimationClip3D { var pos : int = _clips.indexOf(clip); if (pos >= 0 ) { _clips.splice(pos, 1 ); _clipByName[clip.name] = null ; return clip; } return null ; } /** * Resumes the animation. * * @param loop Whether the animation should loop. Defaults to true; */ public function resume(loop: Boolean = true ) : void { if (_channels.length) { _loop = loop; _currentTimeStamp = getTimer(); if (_isPaused) { _currentTimeStamp = getTimer() - _pauseTime * 1000 ; } _isPlaying = true ; _isPaused = false ; if (_dispatchEvents) { var clipName : String = _clip ? _clip.name : "all" ; dispatchEvent( new AnimationEvent(AnimationEvent.RESUME, _pauseTime, clipName)); } } } /** * Stops the animation. */ public function stop() : void { _isPlaying = false ; if (_dispatchEvents) { var endTime : Number = _clip ? _clip.endTime : this .endTime; var clipName : String = _clip ? _clip.name : "all" ; dispatchEvent( new AnimationEvent(AnimationEvent.STOP, endTime, clipName)); } } /** * Update. */ public function update() : void { if (_isStepping) { stepping(); _isStepping = false ; return ; } if (!_isPlaying || _isPaused || !_channels.length) { return ; } var t : int = getTimer(); var elapsed : int = t - _currentTimeStamp; var channel : Channel3D; var et : Number = _clip ? _clip.endTime : endTime; var clipName : String = _clip ? _clip.name : "all" ; _currentTime = (elapsed * 0.001 ); if (_currentTime > et) { if (_dispatchEvents) { dispatchEvent( new AnimationEvent(AnimationEvent.COMPLETE, et, clipName)); } if (!_loop) { stop(); return ; } _currentTimeStamp = t; if (_clip) { _currentTimeStamp -= (_clip.startTime * 1000 ); } _currentTime = _clip ? _clip.startTime : startTime; } for each (channel in _channels) { channel.update(_currentTime); } if (_isPlaying && _dispatchEvents) { dispatchEvent( new AnimationEvent(AnimationEvent.NEXT_FRAME, _currentTime, clipName)); } } private function stepping(): void { var channel : Channel3D; var et : Number = _clip ? _clip.endTime : endTime; var clipName : String = _clip ? _clip.name : "all" ; if (_currentTime > et) { if (_dispatchEvents) { dispatchEvent( new AnimationEvent(AnimationEvent.COMPLETE, et, clipName)); } if (!_loop) { stop(); return ; } if (_clip) { _currentTimeStamp -= (_clip.startTime * 1000 ); } _currentTime = _clip ? _clip.startTime : startTime; } for each (channel in _channels) { channel.update(_currentTime); } if (_isPlaying && _dispatchEvents) { dispatchEvent( new AnimationEvent(AnimationEvent.NEXT_FRAME, _currentTime, clipName)); } } /** * Updates the startTime and endTime of this animation controller. */ protected function updateStartAndEndTime() : void { var channel : Channel3D; if (_channels.length == 0 ) { startTime = endTime = 0 ; return ; } startTime = Number .MAX_VALUE; endTime = -startTime; for each (channel in _channels) { startTime = Math.min(startTime, channel.startTime); endTime = Math.max(endTime, channel.endTime); } } /** * */ override public function toString() : String { return "[AnimationController] #channels: " + _channels.length + " startTime: " + startTime + " endTime: " + endTime; } public function set channels(value : Array ) : void { _channels = value; } public function get channels() : Array { return _channels; } public function get currentTime(): Number { return _currentTime; } /** * Gets all defined clip names. This property is read-only. * * @return Array containing clip names. */ public function get clipNames() : Array { var names : Array = new Array (); var clip : AnimationClip3D; for each (clip in _clips) { names.push(clip.name); } return names; } /** * Gets all defined clips. This property is read-only. * * @return Array containing clips. * * @see org.papervision3d.core.animation.clip.AnimationClip3D */ public function get clips() : Array { return _clips; } /** * */ public function get dispatchEvents(): Boolean { return _dispatchEvents; } /** * */ public function set dispatchEvents(value: Boolean ): void { _dispatchEvents = value; } /** * Number of channels. */ public function get numChannels() : uint { return _channels.length; } /** * Whether the animation is playing. This property is read-only. */ public function get playing() : Boolean { return _isPlaying; } } } |
增加了next方法。每次调用,就会进入下一帧。然后调用 onRenderTick即可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· 我干了两个月的大项目,开源了!
· 千万级的大表,如何做性能调优?
· 盘点!HelloGitHub 年度热门开源项目
· Phi小模型开发教程:用C#开发本地部署AI聊天工具,只需CPU,不需要GPU,3G内存就可以运行,
· 你所不知道的 C/C++ 宏知识——基于《C/C++ 宏编程的艺术》