1. 程式人生 > >【Unity 3D】學習筆記四十三:布料

【Unity 3D】學習筆記四十三:布料

布料

布料是特殊的元件,它可以變化成任意形狀,比如說:隨風飄的旗子,窗簾等

建立布料的方法有兩種:建立布料物件,在遊戲物件中添加布料元件。前者通過hierarchy檢視中選擇create——cloth即可,建立後,系統會自動將互動布料元件(interactive clothe)與布料渲染元件(cloth renderer)新增值該物件中。後者是在導航選單中選component——physics——interactive cloth選單項即可。

互動布料元件是由網格組成的布料,只要用於布料的邏輯判斷,應用於摩擦,密度,氣壓等影響布料的效果,影響到布料的具體物理數值之間的判斷。

布料渲染是給布料繪製一張貼圖使指更加美觀。

布料面板(skinned cloth)是用來模擬人物模型面板的布料,比如說衣服,褲子等。這些布料會根據角色骨骼動畫的運動而發生改變。


bending stiffness:硬度,取值0-1。

stretching stiffness:韌度,取值0-1。

damping:阻力,取值0-1。

thickness:厚度,直接影響布料的質量大小。

user gravity:只用重力。

self collision:自身碰撞。

external acceleration:作用於布料的一個外力,影響布料的預設行為。

random acceleration:隨機外力。

mesh:網格面,決定布料的形狀。

friction:摩擦力,取值0-1.

density:密度,數值越大布料的質量越高。

pressure:氣壓。

collision response:與其他模型碰撞後的反饋。

attachment tear factor:附帶撕破係數。

attachment response:附帶反饋。

tear factor:撕破係數,值越大越不容易撕破。

attached colliders:附帶碰撞器。

本文將總結回顧一個布料例項,點選左側的按鈕可以控制平面物件的移動方向,由於會和正方體發生碰撞,所以布料會變形。

using UnityEngine;
using System.Collections;

public class Script_06_12 : MonoBehaviour 
{

	//布料物件
	Cloth cloth = null;
	
	void Start()
	{
		//獲取布料物件
		cloth = (Cloth)GetComponent<InteractiveCloth>();
	}
	
	void OnGUI()
	{
		//移動布料
		if(GUILayout.RepeatButton("向上"))
		{
			cloth.externalAcceleration =  new Vector3(0,1,0);
		}
		if(GUILayout.RepeatButton("向下"))
		{
			cloth.externalAcceleration =  new Vector3(0,-1,0);
	
		}
		if(GUILayout.RepeatButton("向左"))
		{
			cloth.externalAcceleration =  new Vector3(1,0,0);
		}
		if(GUILayout.RepeatButton("向右"))
		{
			cloth.externalAcceleration =  new Vector3(-1,0,0);
	
		}
	}

}

執行:


點選按鈕後: