#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["SampleRC03"]
#Text["SampleRC03: Particle Rendering (Custom Shader, 3D)"]

@Initialize {
	SetFogParam(256, 1000, 0, 0, 0);

	TSample();
}

@MainLoop {
	yield;
}

task TSample() {
	let dir = GetCurrentScriptDirectory();

	let obj = ObjParticleList_Create(OBJ_PARTICLE_LIST_3D);
	
	Obj_SetRenderPriorityI(obj, 21);
	ObjPrim_SetTexture(obj, dir ~ "Effect01.png");
	
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	
	//You may treat a 3D particle list object as either a 3D primitive object or a 3D sprite object.
	ObjSprite3D_SetSourceRect(obj, 140, 39, 186, 86);
	ObjSprite3D_SetDestRect(obj, -46 / 2, -47 / 2, 46 / 2, 47 / 2);
	
	//This is absolutely necessary, rendering will not be performed otherwise.
	ObjPrim_SetVertexIndex(obj, [0, 1, 2, 3]);
	
	ObjShader_SetShaderF(obj, dir ~ "SampleRC03_HLSL.txt");
	ObjShader_SetTechnique(obj, "Render");
	
	loop {
		loop (7) CreateParticle();
		yield;
	}
	
	task CreateParticle() {
		let x = rand(-700, 700);
		let y = rand(-700, 700);
		let z = rand(-700, 700);
		
		let x_speed = rand(-2, 2);
		let y_speed = rand(-2, 2);
		let z_speed = rand(-2, 2);
		
		let x_angle = rand(0, 360);
		let y_angle = rand(0, 360);
		let z_angle = rand(0, 360);
		let x_add = rand(-5, 5);
		let y_add = rand(-5, 5);
		let z_add = rand(-5, 5);
		
		let scale = rand(0.3, 0.7);
		
		let timer_begin = floor(rand(10, 30 + 1));
		ascent (i in 0..timer_begin) Update(Interpolate_Smooth(0, 1, i / (timer_begin - 1)));
		
		loop (rand(30, 90)) Update(1);
		
		let timer_end = floor(rand(10, 30 + 1));
		descent (i in 0..timer_end) Update(Interpolate_Smooth(0, 1, i / (timer_end - 1)));
		
		function Update(mul_scale) {
			//Sets up instance data.
			let tmp_sc = scale * mul_scale;
			ObjParticleList_SetScale(obj, tmp_sc, tmp_sc, tmp_sc);
			ObjParticleList_SetAngle(obj, x_angle, y_angle, z_angle);
			ObjParticleList_SetPosition(obj, x, y, z);
			
			//Submits the current data to an instance, cleared every frame.
			ObjParticleList_AddInstance(obj);
		
			x += x_speed;
			y += y_speed;
			z += z_speed;
			x_angle += x_add;
			y_angle += y_add;
			z_angle += z_add;
			yield;
		}
	}
}

