- 最后登录
- 2017-9-18
- 注册时间
- 2011-1-12
- 阅读权限
- 90
- 积分
- 12276
  
- 纳金币
- 5568
- 精华
- 0
|
现在我们要写一个behavior用于3D精灵,首先创建一个3D成员,然后把它放在score中。这个behavior开始的时候要重置3D成员,然后通过调用自定义的句柄来创建每一个原型。
property pscene, pSphere, pBox, pCylinder, pPlane
on beginSprite me
pScene = sprite (me.spriteNum).member
-- reset the world when done
pScene.resetWorld ()
-- create box, cylinder and plane
createBox (me)
createCylinder (me)
createPlane (me)
end
句柄createbox创造一个立方体的模型,和我们上周创建球体的过程相同。立方体没有球体的radius(半径)属性,但它有width、height,还有length属性。我们创建的这个立方体各个边长都是20像素。
on createBox me
-- create a box resource
boxResource = pScene.newModelResource ("BoxResource", #box)
boxResource.width = 20
boxResource.height = 20
boxResource.length = 20
现在,原型创建好了,我们要利用这个原型创建一个模型。我们用属性变量pBox来储存这个模型。
-- create new model
pBox = pScene.newModel ("Box", boxResource)
为这个模型来上材质有些麻烦,因为立方体有6个面。我们通过成员“textture-blue”来创建一个材质,然后为这个材质创建一个名字为“box shader”的shader。
然后我们我们用一个循环来使立方体shaderlist中的所有项都等于这个shader。这样就会创建一个不错的蓝色立方体。
-- assign a texture to all 6 faces
pScene.newtexture ("box texture", #fromCastMember, member ("texture-blue"))
pScene.newshader ("box shader", #standard)
pScene.shader ("box shader").texture = pScene.texture ("box texture")
repeat with j = 1 to pBox.shaderList.count
pBox.shaderList[j] = pScene.shader ("box shader")
end repeat
既然我们还要创建其他的模型,那么我们最好将这个已经创建好的模型移开一端距离,以便腾出空间创建其他的模型。我们用translate命令来将立方体在x方向上移动 -30像素。
-- move into position
pBox.translate (-30, 0, 0)
end
创建一个圆柱同样简单,圆柱的属性有height、topRadius、bottomRadius。他们有3个面需要我们上shader:顶面、底面和侧面。创建之后我们将他右移30像素。
on createCylinder me
-- create a cylinder resource
cylinderResource = pScene.newModelResource ("CylinderResource",#cylinder)
cylinderResource.height = 20
cylinderResource.topRadius = 5
cylinderResource.bottomRadius = 5
-- create new model
pCylinder = pScene.newModel ("Cylinder", cylinderResource)
-- assign a texture to all 3 faces
pScene.newtexture ("cylinder texture",#fromCastMember, member ("texture-green"))
pScene.newshader ("cylinder shader", #standard)
pScene.shader ("cylinder shader").texture = pScene.texture ("cylinder texture")
repeat with j = 1 to pCylinder.shaderList.count
pCylinder.shaderList[j] = pScene.shader ("cylinder shader")
end repeat
-- move into position
pCylinder.translate (30, 0, 0)
end
既然圆柱体有topRadius和bottomRadius属性,那么你就可以轻松地创建出圆锥形。试试改变topRadius的值。
最后我们要创建一个平面的原型。
平面应该是很奇怪的,因为他没有厚度。他只是一个完全的面,有前面和后面的两个表面。你可以设置width和length,但是没有height。
下面的句柄将创建一个100×100的平面,平面只有一个面需要上shader,因为我们不可能同时看到两个面。
平面刚创建出来的时候是面向camera(镜头)的。为了把这个平面弄的像地板一样的效果,我们需要将它在x轴方向上旋转90度,这样他就和屏幕垂直了,因为他没有厚度,所以你看不见他,因此我们把他向下移动40像素,这样他就轻微的斜那么一点,我们就可以看到他了(视觉效果好像相对于camera)。
on createPlane me
-- create a plane resource
planeResource = pScene.newModelResource (" laneResource", #plane)
planeResource.length = 100
planeResource.width = 100
-- create new model
pPlane = pScene.newModel (" lane", planeResource)
-- assign a texture to the sphere
pScene.newtexture ("plane texture",#fromCastMember,member ("texture-purple"))
pScene.newshader ("plane shader", #standard)
pScene.shader ("plane shader").texture = pScene.texture ("plane texture")
pPlane.shaderList[1] = pScene.shader ("plane shader")
-- move plane into position
pPlane.translate (0, -40, 0)
-- rotate plane so it is parallel to ground
pPlane.rotate (90, 0, 0)
end
上周我们能让球体旋转起来,今天我们同样可以让这些模型作相同的动作。我们可以让立方体和圆柱体在两个方向上旋转。我们只让平面在一个轴上旋转。
on exitFrame me
-- make box and cylinder tumble
pBox.rotate (1, 1, 0)
pCylinder.rotate (1, 1, 0)
-- make plane spin
pPlane.rotate (0, 0, 1) |
|