unity3d 层 Layers

Layers are most commonly used by Cameras to render only a part of the scene, and by Lights to illuminate only parts of the scene. But they can also used by raycasting to selectively ignore colliders or to create collisions.

经常使用层的是相机和灯光,相机用它来渲染场景中的一部分物体,而灯光依靠层来仅仅照亮场景中的一部分物体。但是也经常利用层,通过光线投射来选择性地忽略碰撞器,或者添加碰撞功能。

Creating Layers 创建层

The first step is to create a new layer, which we can then assign to a GameObject. To create a new layer, open the Edit menu and select Project Settings->Tags.

首先是创建一个新的层,随后将它指派给一个GameObject(即与该GameObject关联)。创建一个新的层只需要,打开编辑菜单,选择工程设置->标签。

We create a new layer in one of the empty User Layers. We choose layer 8.

在空用户层里创建一个新的层,我们选曾第8层。

Assigning Layers 分配层

Now that you have created a new layer, you have to assign the layer to one of the game objects.

当你创建了一个新的层后,必须把它和一个游戏对象关联起来。

In the tag manager we assigned the Player layer to be in layer 8.

在标签管理器中,把玩家层放到层8中。

Drawing only a part of the scene with the camera's culling mask
使用相机剔除掩膜来对场景部分绘制

Using the camera's culling mask, you can selectively render objects which are in one particular layer. To do this, select the camera that should selectively render objects.

通过相机剔除掩膜,你可以有选择性地渲染那些在特定层中的物体。为了达到这一目的,首先选择目标相机。

Modify the culling mask by checking or unchecking layers in the culling mask property.

设置剔除掩膜,只需要在剔除掩膜属性中选择或删除相应层。

Casting Rays Selectively 选择性地投射光线

Using layers you can cast rays and ignore colliders in specific layers. For example you might want to cast a ray only against the player layer and ignore all other colliders.

通过层你可以在特定的层里投射光线、忽略碰撞器。比如说,你只想在玩家层中投射光线(即光线只跟玩家层中的碰撞体作用),进而忽略其他所有的碰撞器。

The Physics.Raycast function takes a bitmask, where each bit determines if a layer will be ignored or not. If all bits in the layerMask are on, we will collide against all colliders. If the layerMask = 0, we will never find any collisions with the ray.

Physics.Raycast函数里操作一个掩膜位,该掩膜位的每一位决定相应的层是否被忽略。如果layerMask中的每一位都为1,则将会与所有的碰撞器作用。相反如果layerMask=0,那么将会光线将不会与任何碰撞器作用。

// bit shift the index of the layer to get a bit mask
var layerMask = 1 << 8;
// Does the ray intersect any objects which are in the player layer.
if (Physics.Raycast (transform.position, Vector3.forward, Mathf.Infinity, layerMask))
    print ("The ray hit the player"); 

In the real world you want to do the inverse of that however. We want to cast a ray against all colliders except those in the Player layer.

在现实生活中,会遇到完全相反的情况。如我们只想排除玩家层(Player layer),即投射出的光线跟除玩家层中的碰撞器外的所有碰撞器作用。

function Update () {
  // Bit shift the index of the layer (8) to get a bit mask
  var layerMask = 1 << 8;
  // This would cast rays only against colliders in layer 8.
  // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask.
  layerMask = ~layerMask;

  var hit : RaycastHit;
  // Does the ray intersect any objects excluding the player layer
  if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit, Mathf.Infinity, layerMask)) {
    Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.yellow);
    print ("Did Hit");
  } else {
    Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) *1000, Color.white);
    print ("Did not Hit");
  }
}

When you don't pass a layerMask to the Raycast function, it will only ignore colliders that use the IgnoreRaycast layer. This is the easiest way to ignore some colliders when casting a ray.

当没有给Raycast函数传layerMask参数时,仅仅只忽略使用IgnoreRaycast层的碰撞器。这是投射光线时忽略某些碰撞器的最简单方法。


http://game.ceeger.com/Components/Layers.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当您调用 `circle3D.layers.enable(1)` 时,它启用了圆形网格对象的第一个(即第一个二进制位为 1 的)。 默认情况下,`THREE.Scene` 和 `THREE.Object3D` 对象的都是禁用的,它们不会对渲染器产生任何影响。但是,您可以选择启用它们的某些,以便在渲染时只渲染特定中的对象。 例如,如果您有多个圆形网格对象,并且您希望只在特定中渲染其中的一些对象,您可以将这些对象分配到不同的中,并在渲染时仅启用相应的。 以下是一个将圆形网格对象分配到不同中的示例代码: ``` // 创建两个圆形网格对象 const circle1 = new THREE.Mesh( geometry, material ); const circle2 = new THREE.Mesh( geometry, material ); // 将一个圆形网格对象分配到第一个中 circle1.layers.enable( 1 ); // 将另一个圆形网格对象分配到第二个中 circle2.layers.enable( 2 ); // 将这两个圆形网格对象添加到场景中 scene.add( circle1 ); scene.add( circle2 ); ``` 在上面的示例代码中,我们将第一个圆形网格对象分配到第一个中,将第二个圆形网格对象分配到第二个中。然后,在渲染时,您可以选择启用特定的,以便只渲染在该中的对象: ``` // 仅启用第一个,渲染第一个圆形网格对象 renderer.render( scene, camera ); renderer.autoClear = false; renderer.clearDepth(); circle1.visible = false; renderer.render( scene, camera ); ``` 请注意,为了仅渲染第一个圆形网格对象,我们禁用了第二个圆形网格对象,并在渲染之前调用了 `renderer.clearDepth()` 方法,以确保在渲染第一个圆形网格对象时不会遮挡第二个圆形网格对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值