Kanzi学习之路(6):属性绑定

在设计中我们会经常遇到要让节点的属性跟随其他节点属性的值一起变化,这个时候我们可以使用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







  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值