python 2D相机抖动扩展

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 2D相机抖动扩展相关的知识,希望对你有一定的参考价值。

extends Camera2D

var _duration = 0.0
var _period_in_ms = 0.0
var _amplitude = 0.0
var _timer = 0.0
var _last_shook_timer = 0
var _previous_x = 0.0
var _previous_y = 0.0
var _last_offset = Vector2(0, 0)

func _ready():
  set_process(true)

# Shake with decreasing intensity while there's time remaining.
func _process(delta):
  # Only shake when there's shake time remaining.
  if _timer == 0:
    return
  # Only shake on certain frames.
  _last_shook_timer = _last_shook_timer + delta
  # Be mathematically correct in the face of lag; usually only happens once.
  while _last_shook_timer >= _period_in_ms:
    _last_shook_timer = _last_shook_timer - _period_in_ms
    # Lerp between [amplitude] and 0.0 intensity based on remaining shake time.
    var intensity = _amplitude * (1 - ((_duration - _timer) / _duration))
    # Noise calculation logic from http://jonny.morrill.me/blog/view/14
    var new_x = rand_range(-1.0, 1.0)
    var x_component = intensity * (_previous_x + (delta * (new_x - _previous_x)))
    var new_y = rand_range(-1.0, 1.0)
    var y_component = intensity * (_previous_y + (delta * (new_y - _previous_y)))
    _previous_x = new_x
    _previous_y = new_y
    # Track how much we've moved the offset, as opposed to other effects.
    var new_offset = Vector2(x_component, y_component)
    set_offset(get_offset() - _last_offset + new_offset)
    _last_offset = new_offset
    # Reset the offset when we're done shaking.
    _timer = _timer - delta
    if _timer <= 0:
      _timer = 0
      set_offset(get_offset() - _last_offset)

# Kick off a new screenshake effect.
func shake(duration, frequency, amplitude):
  # Initialize variables.
  _duration = duration
  _timer = duration
  _period_in_ms = 1.0 / frequency
  _amplitude = amplitude
  _previous_x = rand_range(-1.0, 1.0)
  _previous_y = rand_range(-1.0, 1.0)
  # Reset previous offset, if any.
  set_offset(get_offset() - _last_offset)
  _last_offset = Vector2(0, 0)

以上是关于python 2D相机抖动扩展的主要内容,如果未能解决你的问题,请参考以下文章

相机跟随刚体抖动每隔几秒与背景对象

由于 Time.deltaTime,C#/Unity 相机跟随抖动

Unity 2D 中 Pixel Perfect Camera 和 Cinemachine 的问题

SpriteKit 的相机抖动效果

Opencv算法在移动/抖动相机中检测移动车辆

CinemachineFreelook相机鬼畜抖动的解决方法