MaxScript Matrix學習總結

環境:MAX2018

源於MaxScript的API和網絡資料

 

NOTE:max的矩陣是3x4的矩陣

創建一個矩陣

matrix3 <row1_point3> <row2_point3> <row3_point3> <row4_point3>

例子:

matrix3 [0,0,0] [0,0,0] [0,0,0] [0,0,0]

如果只想創建一個0矩陣的話:matrix3 0     (matrix3 [0,0,0] [0,0,0] [0,0,0] [0,0,0])

如果只想創建一個單位矩陣的話: matrix3 1     (matrix3 [1,0,0] [0,1,0] [0,0,1] [0,0,0])

將quat,angleaxis,eulerangles轉換爲旋轉矩陣的方法

<quat> as matrix3

<angleaxis> as matrix3

<eulerangles> as matrix3

如果想獲得一個繞某一個軸旋轉的矩陣的話:

rotateXMatrix <number>

rotateYMatriy <number>

rotateZMatriz <number>

這裏寫個🌰測試一下上面說的吧

---------------------------------------------

rotateXMatrix 10

(matrix3 [1,0,0] [0,0.984808,0.173648] [0,-0.173648,0.984808] [0,0,0])

這裏的意思是得到一個繞着X軸選擇10度的選擇矩陣。怎麼驗證呢?

這裏修改一下上面的測試:

tmp = rotateXMatrix 10

這樣tmp就拿到了返回值

tmp.rotationpart

返回:(quat -0.0871556 0 0 0.996195)

tmp.translationpart

返回:[0,0,0]

tmp.scalepart

返回:[1,1,1]

這樣就看出來這個矩陣裏只有rotation部分是變化的。

rotation部分返回了一個四元數。

tmp.rotationpart as angleaxis

返回:(angleAxis 9.99999 [-1,0,0]),這裏看的出來是繞着x軸旋轉了9.9999999999999999999度

tmp.rotationpart as  eulerangles

返回:(eulerAngles 9.99999 0 0)  這裏也看的出來x軸的值是9.999999999999999999999度

---------------------------------------------

上面說了旋轉矩陣的介紹,接下來說一下位移矩陣的介紹

transMatrix <point3>

transMatrix [10,0,0]

返回:(matrix3 [1,0,0] [0,1,0] [0,0,1] [10,0,0])

淺顯易懂,就不必多言了

再說一下縮放矩陣

scaleMatrix <point3>

scaleMatrix  [2,3,4]
返回 : (matrix3 [2,0,0] [0,3,0] [0,0,4] [0,0,0])

上圖來自:http://www.chrobotics.com/library/understanding-euler-angles

如果想根據yaw  pitch  roll去做一個矩陣呢:

下面下個方法要注意左右手座標系。上圖只是個演示圖,不要用上圖去考慮。

rotateYPRMatrix <yaw_number> <pitch_number> <roll_number>

rotateYPRMatrix 0 10 0

如果想要根據法線去new一個矩陣的:

matrixFromNormal <point3>

-----------------------------------------------------------------

簡單個做一個測試:

normalMatrix = matrixFromNormal [1,0,0]

返回:(matrix3 [0,1,0] [0,0,1] [1,0,0] [0,0,0])

(我的理解是:這個api會構建了一個法線空間下的轉換矩陣)

-----------------------------------------------------------------

矩陣的基本(部分)操作

<matrix3> + <matrix3>

<matrix3> - <matrix3>

<matrix3> * <matrix3>

<matrix3> as <class>

獲取矩陣的某一行

<matrix3>[<integer>]

或者

<matrix3>.row1: Point3

<matrix3>.row2: Point3

<matrix3>.row3: Point3

<matrix3>.row4: Point3

<matrix3>.translation: Point3

其中row4跟translation是一樣的。

拆分矩陣:

<matrix3>.rotationpart: Quat, read-only

<matrix3>.translationpart: Point3, read-only

<matrix3>.scalerotationpart: Quat, read-only

<matrix3>.scalepart: Point3, read-only

獲得矩陣的行列式的符號:

<matrix3>.determinantsign: Integer, read-only

複製一個矩陣:

copy <matrix3>

如果直接賦值的話,是引用。

判斷矩陣是否是單位矩陣

isIdentity <matrix3>

求矩陣的逆

inverse <matrix3>

將矩陣轉換到另一個空間

xformMat <transform_matrix3> <space_matrix3>

其實上面這個方法的執行命令就是:space_matrix3 * transform_matrix3 * inverse(space_matrix3).

單位化矩陣:identity <matrix3>

零化矩陣:zero <matrix3>

正交化矩陣:orthogonalize <matrix3>

修改矩陣的位移旋轉縮放部分
translate <matrix3> <point3>

rotateX <matrix3> <number>

rotateY <matrix3> <number>

rotateZ <matrix3> <number>

rotate <matrix3> <quat>

scale <matrix3> <point3> [ <boolean> ]   只說一下這個的最後一個boolean參數,如果爲True,位移部分會被縮放;false,不縮放位移部分。

上面這一串都是右乘,下面這一串都是左乘

preTranslate <matrix3> <point3>

preRotateX<matrix3> <number>

preRotateY<matrix3> <number>

preRotateZ<matrix3> <number>

preRotate<matrix3> <quat>

preScale <matrix3> <point3> [ <boolean> ]

 

還有幾個是GetEulerMatAngleRatio,等有時間寫歐拉的時候在一起把

-----------------------------------------------------------------------------------------------------也是基本都是API-------------------------

####################################以上基本都不需要看####################################

放一個源碼,調整物體的pivot

思路:把物體的transform用目標的transform代替,然後再去做objectoffsetXXX,矯正回原來的位置

fn SetPivotOnly obj target = 
(
	with redraw off
	(
		ResetPivot obj
		local preTrans = copy obj.transform
		local curTrans = target
		if classof target != matrix3 then curTrans = target.transform
		
		--排除目標物體(目標矩陣)中的scale
		local row1_new = normalize(curTrans.row1)
		local row2_new = normalize(curTrans.row2)
		local row3_new = normalize(curTrans.row3)
		
		--添加自身矩陣的scale
		local preScalePart = preTrans.scalePart
		
		curTrans.row1 = row1_new * preScalePart[1]
		curTrans.row2 = row2_new * preScalePart[2]
		curTrans.row3 = row3_new * preScalePart[3]
		
		
		obj.transform = curTrans
		
		--計算旋轉變換矩陣
		local deltaMat = preTrans * (inverse curTrans)
		
		obj.objectOffsetRot = deltaMat as quat
		
		local pos_world = preTrans.row4 - curTrans.row4
		
		local pos_new = (xformMat (transMatrix pos_world) curTrans).row4
		obj.objectOffsetPos = pos_new
	)
)

--------------------------------------------------------------下面要來源於網絡---------------------------------------------------------------------------

max  or  maya Transform Matrix

 

將max的矩陣轉到maya的空間中

mayaMatrix = (matrix3 [1,0,0] [0,0,1] [0,-1,0] [0,0,0])
mat3 = $.transform
resultmat3 = xformmat mat3 mayaMatrix 

獲得物體的local矩陣

$.transform * (inverse $.parent.transform)
$.transform.controller.value
---上面兩種方式都可以

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章