先看: Hello SimpleApplication, 在看: Hello Assets.
我的中文不是呢么好,请你们帮我咻咻他。谢谢。
在这一课,你会学怎么去盖一个3D场景。
-
学这一棵前,你先好知道什么是Scene Graph,
-
如果你不知道他是什么,就看Scene Graph for Dummies.
盖3D游戏时:
-
你要雕塑3D人,房子,ect。
-
你要方静你的模型在游戏场景里
-
你要动,便形,转,图上颜色,和动画他们。
你会学怎么用 Scene Graph 做什么,学为什么 RootNode 呢么重要,学怎么放变量比如活力或钱经一个 Node,学怎么动,转或变你的模型的形状,和你会学 Node 和 Geometry 有什么分别。
例子
----package jme3test.helloworld;
import com.jme3.app.SimpleApplication; import com.jme3.material.Material; import com.jme3.math.Vector3f; import com.jme3.scene.Geometry; import com.jme3.scene.shape.Box; import com.jme3.math.ColorRGBA; import com.jme3.scene.Node;
/* Sample 2 - 怎么用 Nodes 去动或变模型。 *Root Node 很特别,你只能看到放在它上的模型/
public class HelloNode extends SimpleApplication {
public static void main(String[] args){ HelloNode app = new HelloNode(); app.start(); }
@Override public void simpleInitApp() {
/** 改一个新蓝色的立方体在(1,1,1)*/ Box box1 = new Box( Vector3f.ZERO, 1,1,1); Geometry blue = new Geometry("Box", box1); Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat1.setColor("Color", ColorRGBA.Blue); blue.setMaterial(mat1); blue.move(1,-1,1);
/**盖一个新红色的立方体在(1,3,1)*/ Box box2 = new Box( Vector3f.ZERO, 1,1,1); Geometry red = new Geometry("Box", box2); Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat2.setColor("Color", ColorRGBA.Red); red.setMaterial(mat2); red.move(1,3,1);
/** 改一个新 Node 叫 ”pivot“ 在 (0,0,0),放他在 Root Node 上*/ Node pivot = new Node("pivot"); rootNode.attachChild(pivot); // 放 pivot 在 Root Node 上。
/** 放那两个立方体在在 pivot 上 */ pivot.attachChild(blue); pivot.attachChild(red); /** 转 pivot:那两个立方体会跟他转*/ pivot.rotate(.4f,.4f,0f); } }---- 开始 Hello Node 后,你会看到两个一样斜的立方体。
口语到 JME 语
在这一个,你学到了几个东西:
你要做什么 | 怎么说在JME语言 |
---|---|
Lay out the 3D scene |
Populate the scene graph |
Create scene objects |
Create Spatials (e.g. create Geometries) |
Make an object appear in the scene |
Attach a Spatial to the rootNode |
Make an object disappear from the scene |
Detach the Spatial from the rootNode |
Position/move, turn, or resize an object |
Translate, or rotate, or scale an object = transform an object. |
Every JME3 application has a rootNode: Your game automatically inherits the rootNode
object from SimpleApplication. Everything attached to the rootNode is part of the scene graph. The elements of the scene graph are Spatials.
-
A Spatial contains the location, rotation, and scale of an object.
-
A Spatial can be loaded, transformed, and saved.
-
There are two types of Spatials: Nodes and Geometries.
Geometry |
Node |
Visibility: |
A Geometry is a visible scene object. |
A Node is an invisible “handle for scene objects. |
Purpose: |
A Geometry stores an object’s looks. |
A Node groups Geometries and other Nodes together. |
Examples: |
A box, a sphere, a player, a building, a piece of terrain, a vehicle, missiles, NPCs, etc… |
The |
Understanding the Code
What happens in the code snippet? You use the simpleInitApp()
method that was introduced in the first tutorial to initialize the scene.
-
You create the first box Geometry.
-
Create a Box shape with a radius of (1,1,1), that makes the box 2x2x2 world units big.
-
Position the box at (1,-1,1) using the move() method. (Don’t change the Vector3f.ZERO unless you want to change the center of rotation)
-
Wrap the Box shape into a Geometry.
-
Create a blue material.
-
Apply the blue material to the Box Geometry.
-
Box box1 = new Box( Vector3f.ZERO, 1,1,1); Geometry blue = new Geometry("Box", box1); Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat1.setColor("Color", ColorRGBA.Blue); blue.setMaterial(mat1); blue.move(1,-1,1);---- . You create a second box Geometry. ** Create a second Box shape with the same size. ** Position the second box at (1,3,1). This is straight above the first box, with a gap of 2 world units inbetween. ** Wrap the Box shape into a Geometry. ** Create a red material. ** Apply the red material to the Box Geometry. [source,java]
Box box2 = new Box( Vector3f.ZERO, 1,1,1); Geometry red = new Geometry("Box", box2); Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); mat2.setColor("Color", ColorRGBA.Red); red.setMaterial(mat2); red.move(1,3,1);----
-
You create a pivot Node.
-
Name the Node “pivot.
-
By default the Node is positioned at (0,0,0).
-
Attach the Node to the rootNode.
-
The Node has no visible appearance in the scene.
-
Node pivot = new Node("pivot"); rootNode.attachChild(pivot);---- If you run the application with only the code up to here, the scene appears empty. This is because a Node is invisible, and you have not yet attached any visible Geometries to the rootNode. . Attach the two boxes to the pivot node. [source,java]
pivot.attachChild(blue); pivot.attachChild(red);---- If you run the app with only the code up to here, you see two cubes: A red cube straight above a blue cube.
-
Rotate the pivot node.
---- pivot.rotate( 0.4f , 0.4f , 0.0f );---- If you run the app now, you see two boxes on top of each other – both tilted at the same angle.
What is a Pivot Node?
You can transform (e.g. rotate) Geometries around their own center, or around a user defined center point. A user defined center point for one or more Geometries is called pivot.
-
In this example, you have grouped two Geometries by attaching them to one pivot Node. You use the pivot Node as a handle to rotate the two Geometries together around one common center. Rotating the pivot Node rotates all attached Geometries, in one step. The pivot node is the center of the rotation. Before attaching the other Geometries, make certain that the pivot node is at (0,0,0). Transforming a parent Node to transform all attached child Spatials is a common task. You will use this method a lot in your games when you move Spatials around.
Examples: A vehicle and its driver move together; a planet with its moon orbits the sun. -
Contrast this case with the other option: If you don’t create an extra pivot node and transform a Geometry, then every transformation is done relative to the Geometry’s origin (typically the center).
Examples: If you rotate each cube directly (usingred.rotate(0.1f , 0.2f , 0.3f);
andblue.rotate(0.5f , 0.0f , 0.25f);
), then each cube is rotated individually around its center. This is similar to a planet rotating around its own center.
How do I Populate the Scenegraph?
Task…? | Solution! |
---|---|
Create a Spatial |
Create a Mesh shape, wrap it into a Geometry, and give it a Material. For example:
|
Make an object appear in the scene |
Attach the Spatial to the
|
Remove objects from the scene |
Detach the Spatial from the
|
Find a Spatial in the scene by the object’s name, or ID, or by its position in the parent-child hierarchy. |
Look at the node’s children or parent:
|
Specify what should be loaded at the start |
Everything you initialize and attach to the |
How do I Transform Spatials?
There are three types of 3D transformation: Translation, Scaling, and Rotation.
Translation moves Spatials | X-axis | Y-axis | Z-axis |
---|---|---|---|
Specify the new location in three dimensions: How far away is it from the origin going right-up-forward?
|
+right -left |
+up -down |
+forward -backward |
Scaling resizes Spatials | X-axis | Y-axis | Z-axis |
---|---|---|---|
Specify the scaling factor in each dimension: length, height, width.
|
length |
height |
width |
Rotation turns Spatials | X-axis | Y-axis | Z-axis |
---|---|---|---|
3-D rotation is a bit tricky (learn details here). In short: You can rotate around three axes: Pitch, yaw, and roll. You can specify angles in degrees by multiplying the degrees value with
|
pitch = nodding your head |
yaw = shaking your head |
roll = cocking your head |
How do I Troubleshoot Spatials?
If you get unexpected results, check whether you made the following common mistakes:
Problem? | Solution! |
---|---|
A created Geometry does not appear in the scene. |
Have you attached it to (a node that is attached to) the rootNode? |
A Spatial rotates in unexpected ways. |
Did you use radian values, and not degrees? (If you used degrees, multiply them with FastMath.DEG_TO_RAD to convert them to radians) |
A Geometry has an unexpected Color or Material. |
Did you reuse a Material from another Geometry and have inadvertently changed its properties? (If so, consider cloning it: mat2 = mat.clone(); ) |
How do I Add Custom Data to Spatials?
Many Spatials represent game characters or other entities that the player can interact with. The above code that rotates the two boxes around a common center (pivot) could be used for a spacecraft docked to a orbiting space station, for example.
Depending on your game, game entities do not only change their position, rotation, or scale (the transformations that you just learned about). Game entities also have custom properties, such as health, inventory carried, equipment worn for a character, or hull strength and fuel left for a spacecraft. In Java, you represent entity data as class variables, e.g. floats, Strings, or Arrays.
You can add custom data directly to any Node or Geometry. You do not need to extend the Node class to include variables! For example, to add a custom id number to a node, you would use:
----pivot.setUserData( "pivot id", 42 );----
To read this Node's id number elsewhere, you would use:
----int id = pivot.getUserData( "pivot id" ); ----
By using different Strings keys (here the key is `pivot id`), you can get and set several values for whatever data the Spatial needs to carry. When you start writing your game, you might add a fuel value to a car node, speed value to an airplane node, or number of gold coins to a player node, and much more.
Conclusion
You have learned that your 3D scene is a scene graph made up of Spatials: Visible Geometries and invisible Nodes. You can transform Spatials, or attach them to nodes and transform the nodes. You know the easiest way how to add custom entity properties (such as player health or vehicle speed) to Spatials.
Since standard shapes like spheres and boxes get old fast, continue with the next chapter where you learn to load assets such as 3-D models.
<tags><tag target="beginner" /><tag target="rootNode" /><tag target="node" /><tag target="intro" /><tag target="documentation" /><tag target="color" /><tag target="spatial" /><tag target="geometry" /><tag target="scenegraph" /><tag target="mesh" /></tags>