--track0:揺れ角,0,360,30
--track1:揺れ周期,0.01,100,2,0.01
--track2:揺れズレ,-360,360,90
--track3:センター,-180,180,0
--dialog:分割,local N=10;上固定長％,local dL=10;下固定長％,local dL2=10;下を基準/chk,UB=0;ランダム揺れ量/chk,local rndF=0;ランダム揺れﾊﾟﾀｰﾝ,local seed=0;時間ずれ,local sft=0.1;横に繰り返す/chk,local rep=0;繰り返し個数,local repN=3;間隔,local stepX=50;

local w,h = obj.getpixel()
local w2,h2=w/2,h/2
local t=obj.time
if UB==1 then
	dL,dL2=dL2,dL
end
if dL<0 then dL=0 elseif dL>100 then dL=100 end
if dL2<0 then dL2=0 elseif dL2>100-dL then dL2=100-dL end
dL=dL*0.01*h
dL2=dL2*0.01*h
local L=h-dL-dL2			--長さ
local F=math.pi*obj.track0/180		--揺れ幅
local dt=obj.track1
local c=2*math.pi/dt			--揺れ速度
local d=2*obj.track2*math.pi/180	--揺れズレ
local CNT=obj.track3*math.pi/180	--センター
seed=10+math.abs(seed)

WindShakeT=function(n)
	local t=t-n*sft
	local F=F
	if rndF==1 then
		local s=t/dt
		local n1=math.floor(s)
		local n0=n1-1
		local n2=n1+1
		local n3=n1+2
		local m0=n0+1
		local m1=n1+1
		local m2=n2+1
		local m3=n3+1

		if n0<0 then m0=10000-n0 end
		if n1<0 then m1=10000-n1 end
		if n2<0 then m2=10000-n2 end
		if n3<0 then m3=10000-n3 end

		s=s-n1
		local f0=obj.rand(0,1000,-seed,m0)*F*0.001
		local f1=obj.rand(0,1000,-seed,m1)*F*0.001
		local f2=obj.rand(0,1000,-seed,m2)*F*0.001
		local f3=obj.rand(0,1000,-seed,m3)*F*0.001
		F=obj.interpolation(s,f0,f1,f2,f3)
	end
	local X={}
	local Y={}
	local V={}
	X[0]=0
	Y[0]=0
	for i=1,N do
		local Si=(F*math.sin((c*t-i*d/N))+CNT)*(1-(1-i/N)^4)
		X[i]=X[i-1]+math.sin(Si)
		Y[i]=Y[i-1]+math.cos(Si)
	end
	local a=L/N
	for i=0,N do
		X[i]=X[i]*a
		Y[i]=Y[i]*a+dL-h2
		V[i]=a*i+dL
	end
	X[-1]=0
	Y[-1]=-h2
	V[-1]=0
	local X1={}
	local X2={}
	local Y1={}
	local Y2={}
	X1[-1]=-w2
	X2[-1]= w2
	Y1[-1]=-h2
	Y2[-1]=-h2

	for i=0,N do
		local dx=X[i]-X[i-1]
		local dy=Y[i]-Y[i-1]
		dx,dy=-dy,dx
		local dr=math.sqrt(dx*dx+dy*dy)
		if dr~=0 then 
			dx,dy=dx/dr*w2,dy/dr*w2
			X1[i]=X[i]+dx
			X2[i]=X[i]-dx
			Y1[i]=Y[i]+dy
			Y2[i]=Y[i]-dy
		else
			X1[i]=X1[i-1]
			X2[i]=X2[i-1]
			Y1[i]=Y1[i-1]
			Y2[i]=Y2[i-1]
		end
	end

	if dL2>0 then
		local dx=X[N]-X[N-1]
		local dy=Y[N]-Y[N-1]
		local dr=math.sqrt(dx*dx+dy*dy)
		dx,dy=dx/dr*dL2,dy/dr*dL2
		X1[N+1]=X1[N]+dx
		X2[N+1]=X2[N]+dx
		Y1[N+1]=Y1[N]+dy
		Y2[N+1]=Y2[N]+dy
		V[N+1]=h
		N=N+1
	end

	local minX=-w2
	local maxX= w2
	local minY=-h2
	local maxY=-h2
	for i=0,N do
		if minX>X1[i] then minX=X1[i] elseif maxX<X1[i] then maxX=X1[i] end
		if minX>X2[i] then minX=X2[i] elseif maxX<X2[i] then maxX=X2[i] end
		if minY>Y1[i] then minY=Y1[i] elseif maxY<Y1[i] then maxY=Y1[i] end
		if minY>Y2[i] then minY=Y2[i] elseif maxY<Y2[i] then maxY=Y2[i] end
	end
	local WW=maxX-minX
	local HH=maxY-minY
	local CX=(maxX+minX)*0.5
	local CY=(maxY+minY)*0.5
	for i=-1,N do
		X1[i]=X1[i]-CX
		X2[i]=X2[i]-CX
		Y1[i]=Y1[i]-CY
		Y2[i]=Y2[i]-CY
	end
	obj.setoption("drawtarget","tempbuffer",WW,HH)
	obj.setoption("blend","alpha_add2")
	for i=0,N do
		obj.drawpoly(X1[i-1],Y1[i-1],0, X2[i-1],Y2[i-1],0, X2[i],Y2[i],0, X1[i],Y1[i],0, 0,V[i-1], w,V[i-1], w,V[i], 0,V[i])
	end
	return WW,HH,CX,CY
end

if UB==1 then
	obj.effect("反転","上下反転",1)
end
if rep==0 then
	local WW,HH,CX,CY=WindShakeT(1)
	obj.load("tempbuffer")
	obj.cx=-CX
	if UB==1 then
		obj.effect("反転","上下反転",1)
		obj.cy=CY
	else
		obj.cy=-CY
	end
else

	obj.copybuffer("cache:Ori","obj")
	for i=1,repN do
		obj.copybuffer("obj","cache:Ori")
		local WW,HH,CX,CY=WindShakeT(i)
		CX=(i-(repN+1)/2)*stepX+CX
		obj.load("tempbuffer")
		obj.setoption("drawtarget","framebuffer")

		if UB==1 then
			obj.effect("反転","上下反転",1)
			obj.draw(CX,-CY)
		else
			obj.draw(CX,CY)
		end


	end

end