【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 2250|回复: 0

【Godot 3.2】4.Instancing 实例

[复制链接]
发表于 2020-8-6 21:28:51 | 显示全部楼层 |阅读模式
Introduction介绍

Creating a single scene and adding nodes into it might work for small projects, but as a project grows in size and complexity, the number of nodes can quickly become unmanageable. To address this, Godot allows a project to be separated into any number of scenes. This provides you with a powerful tool that helps you organize the different components of your game.
创建单个场景并在其中添加节点可能适用于小型项目,但是随着项目规模和复杂性的增长,节点的数量可能很快变得难以管理。为了解决这个问题,Godot允许将一个项目分为多个场景。这为您提供了功能强大的工具,可帮助您组织游戏的不同组成部分。

In Scenes and nodes you learned that a scene is a collection of nodes organized in a tree structure, with a single node as the tree root.
在“ 场景和节点”中,您了解到场景是以树结构组织的节点的集合,其中单个节点为树的根。

You can create as many scenes as you like and save them to disk. Scenes saved in this manner are called "Packed Scenes" and have a .tscn filename extension.
您可以创建任意数量的场景并将其保存到磁盘。以这种方式保存的场景称为“打包场景”,并具有.tscn文件扩展名。

Once a scene has been saved, it can be instanced into another scene as if it were any other node.
保存场景后,就可以将其实例化为另一个场景,就好像它是任何其他节点一样。

In the above picture, Scene B was added to Scene A as an instance.
在上图中,将场景B作为实例添加到了场景A。


Instancing by example
实例化

To learn how instancing works, let's start by downloading a sample project: instancing.zip.
要了解实例化的工作原理,让我们从下载示例项目开始:instancing.zip

Unzip this project anywhere you like. Then open Godot and add this project to the project manager using the 'Import' button:
将该项目解压缩到您喜欢的任何位置。然后打开Godot并使用“导入”按钮将此项目添加到项目经理中:

Browse to the folder you extracted and open the "project.godot" file you can find inside it. After doing this, the new project will appear on the list of projects. Edit the project by pressing the 'Edit' button.
浏览到您提取的文件夹,然后打开其中的“ project.godot”文件。完成此操作后,新项目将出现在项目列表中。按下“编辑”按钮来编辑项目。

This project contains two scenes: "Ball.tscn" and "Main.tscn". The ball scene uses a RigidBody2D to provide physics behavior while the main scene has a set of obstacles for the ball to collide with (using StaticBody2D).
该项目包含两个场景:“ Ball.tscn”和“ Main.tscn”。球场景使用RigidBody2D提供物理行为,而主场景具有一组球碰撞的障碍物(使用StaticBody2D)。

Open the Main scene, and then select the root node:
打开Main场景,然后选择根节点:

We want to add an instance of the Ball scene as a child of Main. Click the "link"-shaped button (its hover-text says "Instance a scene file as a Node.") and select the Ball.tscn file.
我们想将Ball场景的实例添加为的子级Main。单击“链接”形的按钮(其悬停文本为“将场景文件作为节点实例”。)并选择Ball.tscn文件。

The ball will be placed at the top-left corner of the screen area (this is (0, 0) in screen coordinates). Click and drag the ball somewhere near the top-center of the scene:
球将放置在屏幕区域的左上角( 在屏幕坐标中)。单击并将球拖动到场景顶部中心附近的某个位置:(0, 0)

Press "Play" and watch the ball fall to the bottom of the screen:
按下“播放”按钮,观察球落到屏幕底部:


Multiple instances
多个实例

You can add as many instances as you like to a scene, either by using the "Instance" button again, or by clicking on the ball instance and pressing Ctrl + D (Cmd + D on macOS) to duplicate it:
您可以根据需要向场景中添加任意多个实例,方法是再次使用“实例”按钮,或者单击球形实例并按 Ctrl + DCmd + D在macOS上)复制它:

Run the scene again and all of the balls will fall.
再次运行场景,所有球都会掉落。


Editing instances
编辑实例

Open the Ball scene, expand the PhysicsMaterial by clicking on it, and set the Bounce property to 1.
打开Ball场景,PhysicsMaterial通过单击扩展它,并将Bounce属性设置为1

Press "Play" and notice that all of the instanced balls are now much more bouncy. Because the instanced balls are based on the saved scene, changes to that scene will affect all instances.
按“播放”,然后注意到所有实例化的球现在的弹力都大得多。由于实例化的球基于保存的场景,因此对该场景的更改将影响所有实例。

You can also adjust individual instances. Set the bounce value back to 0 and then in the Main scene, select one of the instanced balls. Resources like PhysicsMaterial are shared between instances by default, so we need to make it unique. Click on the down arrow and select "Make Unique". Set its Bounce to 1 and press "Play".
您还可以调整单个实例。将反弹值设置为0 ,然后在Main场景中选择一个实例球。PhysicsMaterial默认情况下,类似资源在实例之间共享,因此我们需要使其唯一。单击向下箭头,然后选择“设为唯一”。将其设置 Bounce1,然后按“播放”。


Notice that a grey "revert" button appears next to the adjusted property. When this button is present, it means you modified a property in the instanced scene to override its value in the saved scene. Even if that property is modified in the original scene, the custom value will remain. Pressing the revert button will restore the property to the value in the saved scene.
注意,灰色的“还原”按钮出现在调整后的属性旁边。如果存在此按钮,则意味着您已修改实例场景中的属性以覆盖其在已保存场景中的值。即使在原始场景中修改了该属性,自定义值也将保留。按下还原按钮会将属性恢复为已保存场景中的值。


Conclusion

Instancing can be useful when you want to create many copies of the same object. It is also possible to create instances in code by using GDScript, see Instancing (continued).



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-11-23 12:10

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表