在設計中我們會經常遇到要讓節點的屬性跟隨其他節點屬性的值一起變化,這個時候我們可以使用kanzi裏的綁定實現。
通過綁定,我們可以實現根據其他節點的屬性和屬性分量來刷新一個節點的屬性和屬性分量。綁定能夠讓在其他節點屬性改變或者外部事件發生的情況下,自動更新屬性的值。
最簡單的綁定就是將一個節點的屬性綁定到一個常量或者它自己的一個屬性,複雜些的比如將一個節點的屬性和屬性分量綁定到幾個其他不同節點。在kanzi studio中,可以在節點的屬性窗口裏Bingdings屬性下添加綁定,而且,kanzi studio會用藍色標註有綁定的屬性,如下圖:
當創建綁定時要注意以下幾點:
1、只有綁定到相似的數據類型纔是有效的。比如,你只能將顏色屬性綁定顏色屬性上,是二維向量的屬性綁定到二維向量的屬性上等。
2、綁定只會算綁定表達式的最終值,不論是一元、二元操作,還是一個固定值或者變量。
3、在綁定中,四種基本類型(integer,float,boolean和string)是都可以相互轉換。在integer,float,boolean這三種類型之間轉換,會根據屬性的類型隱式轉換的。但無論是轉到string類型,還是從string類型轉到其他,都是要指明的。
下面我們重點來看看屬性綁定表達式的使用,我們首先來看看它的語法:
1、註釋
語法:#(comments)
#後面可以跟着我們的註釋。如下:
#後面就可以跟着註釋
#計算A的值
A = (4 + 5)
2、基本加減乘除、賦值:
A=4
B=2
#Return 6
A + B
#Reurn 2
A - B
#Return 8
A * B
#Return 2
A / B
3、類型轉換:
1)轉換成整形
語法:INT(value)
2)轉換成浮點類型
語法:FLOAT(value)
3)轉換成布爾類型
語法:BOOL(valued)
4)轉換成字符串類型
語法:STRING(value)
4、內置函數:
1)絕對值
語法:ABS(value)
2) 動畫
語法:Animation(property,“animationDataResourceID”)
3)計算大於等於的最小的整數
CEIL(value)
4)將值限定到一定範圍(MIN(MAX(value,low),high))
語法:CLAMP(low,high,value)
5)計算小於等於的最大整數
語法:FLOOR(value)
6)Linear step(return the same value as CLAMP(0,1,(value-low)/(high-low))
語法:LINEARSTEP(low,high,value)
7)Round(計算最接近的整數)
語法:ROUND(value)
8)指數
語法:POW(n,e)
9)求模
語法:MOD(value1,value2)
10)求平方根
語法:SQRT(n)
11)比較閾值
語法:STEP(threshold,value)
返回值:如果大於等於threshold,返回1,否則返回0.
12)顏色分量
語法:Color4(r,g,b,a)
# Sets the color to white and opaque.
Color4(1, 1, 1, 1)
# Same as above, but with alternative syntax.
Color(1, 1, 1, 1)
# Sets the color to red with 50% transparency.
Color4(1, 0, 0, 0.5)
# Invalid expression, one argument is missing.
Color4(0.1, 1, 0.4)
# Use variables as attributes of the Color4() to assign the
# attribute values of the whole Color property.
#
# Assigns custom properties Red, Green, and Blue to variables
# you use to control the color of an object.
red = {@./Red}
green = {@./Green}
blue = {@./Blue}
color = Color4(0, 0, 0, 1)
# Assigns the red, green, and blue variables to each color channel attribute.
color.ColorR = red
color.ColorG = green
color.ColorB = blue
color
13)轉換分量
語法:MatrixSRT(ScaleX,ScaleY,ScaleZ,RotationX,RotationY,RotationZ,TranslationX,TranslationY,TranslationZ)
# Scales the object on the x, y, and z axes to 1 unit,
# rotates it around the x axis by 30 degrees
# and moves it on the z axis by 2 units.
MatrixSRT(1, 1, 1, 30, 0, 0, 0, 0, 2)
# Use variables as attributes of the MatrixSRT() to assign the
# attribute values of the whole Render Transformation property.
# Assigns custom property Rotation to a variable you use to control
# the rotation of an object.
rotate = {@./Rotation}
position = MatrixSRT(1, 1, 1, 0, 0, 0, 0, 0, 0)
# Assigns the rotation variable to each rotation attribute.
position.RotationX = rotate
position.RotationY = rotate
position.RotationZ = rotate
position
接下里看看綁定的表達式:
1、屬性綁定:
語法:{[path]/[property]}
當你在路徑前使用@符號,當資源和目標對象之間位置發生了改變,kanzi sudio會自動幫你更新綁定的表達式,否則,如果沒有用@符號,節點相對位置發生了改變,表達式不會更新,綁定就會失效。但是要注意使用@符號用於屬性綁定只能用於同一個模板,不能用於不同模板之間。
# Binds to property Layout Width of the current object.
{@./LayoutWidth}
# Binds to the property FOV of the Camera object.
{../Camera/Fov}
2、別名(alias)綁定
語法:{#[aliasName]/[property]}
# Binds to the Layout Width property of the target object of the alias named Sphere.
{#Sphere/LayoutWidth}
# Multiplies the FOV property of the target object of the alias named MainCamera
# with the Render Transformation property attribute Scale X.
{#MainCamera/Fov} * {../Box/RenderTransformation}.ScaleX
3、分量(attribute)綁定
語法:{[path]/[property].[attribute]}
# Binds to the attribute Scale X (value of the Scale X attribute)
# of the Box node's Layout Transformation property.
{../Box/LayoutTransformation}.ScaleX
# Multiply property FOV with Render Transformation property attribute Scale X.
{../Camera/Fov} * {../Box/RenderTransformation}.ScaleX