How can I get and set 3D object points from the code?

Good day!
Finally I decided to add some noise effect to the locations generation. I think will do it with self-made basic Perlin noise.
I don’t see big problems with sprites position. But for the work with terrain surface I must to go over all points of the ground plane and set them Y positions with algorithm.

At first I need to create Plane with about hundred triangles. I just will make it in Blender.
But, after that, I must get all of this plane points from code and set them positions from the cycle. How can I do it? And maybe will better to make ground plane from code too?

I know about the TCastleTerrainNoise, but in my case, I think, more easy set all plane points and game objects positions by hands.

Thanks

  1. I would advise to explore what existing TCastleTerrain and TCastleTerrainNoise give you :slight_smile:, as from your description it seems they should give you all you need. They give you a mesh that is generated using a noise with a few typical features.

    We also plan a number of extensions to the terrain system (see roadmap about terrains).

    Explore examples/terrain to see them in action.

    That said, naturally they don’t provide “everything possible”, so if you really want to do this yourself, read on :slight_smile:

  2. The points of a mesh are represented by TCoordinateNode inside a TCastleScene. You can create and/or update them from code as you wish.

    See the example viewport_and_scenes/mesh_update. It does a bit more than you need – it shows how to modify the mesh coordinates every frame (e.g. for waves), and shows 2 ways to do this (with or without shaders). To create a terrain, you don’t need shaders, and you just set the coordinates once.

As for the question “should you create flat grid in code, or in Blender”:

It indeed can be done both ways. You can create the grid in code, or create it in Blender and only modify positions from code.

Without knowing any other requirements, I would advise to create it from code. It is simpler, and then your code controls 100% the resolution of your grid, and you don’t need to search for a grid inside the glTF file – instead you create it, so you will “have it in your hands”.

The example viewport_and_scenes/mesh_update shows creating such grid, see castle-engine/examples/viewport_and_scenes/mesh_update/code/gameviewmain.pas at f201b1938c9b5e2328f5079c02b462f3554e99a6 · castle-engine/castle-engine · GitHub .

2 Likes

I modify terrain and maintain a copy of the noise generated to maintain the data so it can be modified and saved. This way I can dig holes and shelves for roads and buildings. You can do everything in code pretty easily.

1 Like

I have question about this procedure from the example:
procedure BuildMainScene;
In the string MainScene.Load(RootNode, true); component TCastleScene used without constructor and without interface loading.
I created TCastleScene into the viewport Items too, but get problem in the next CGE procedure (with RestoreProcessEvents := ProcessEvents;):

procedure TCastleSceneCore.LoadCore(const ARootNode: TX3DRootNode;
  const ARootNodeCacheOrigin: TX3DRootNode;
  const AOwnsRootNode: boolean;
  const AOptions: TSceneLoadOptions);
var
  RestoreProcessEvents: boolean;
begin
  { ARootNodeCacheOrigin can be non-nil only if ARootNode is non-nil. }
  Assert(not ((ARootNodeCacheOrigin <> nil) and (ARootNode = nil)));

  { temporarily turn off events, to later initialize and turn them on }
  RestoreProcessEvents := ProcessEvents;
...

I can use constructor TCastleScene for the TX3DRootNode before it will loaded, but in this case I don’t see prepared terrain grid.

In the example castle-engine/examples/viewport_and_scenes/mesh_update/code/gameviewmain.pas at f201b1938c9b5e2328f5079c02b462f3554e99a6 · castle-engine/castle-engine · GitHub , the MainScene is not explicitly created because it is a published component and it exists within the design (gameviewmain.castle-user-interface). That is, in the editor, I added an (empty) MainScene instance. It is automatically created when the view starts.

As the comments at castle-engine/examples/viewport_and_scenes/mesh_update/code/gameviewmain.pas at f201b1938c9b5e2328f5079c02b462f3554e99a6 · castle-engine/castle-engine · GitHub say, the published components are designed using editor:

published
    { Components designed using CGE editor.
      These fields will be automatically initialized at Start. }
    ...
    MainScene: TCastleScene;

In your own code, you can do it like me (place MainScene in editor, and make sure the name matches) or not (you can create your TCastleScene instance explicitly from code and add it to viewport, as you wish).

I’m not certain do I understand this issue. If you create your own TCastleScene, be sure to add it to the viewport (like MyViewport.Items.Add(MyScene)). Following the docs Writing code to modify scenes and transformations | Manual | Castle Game Engine :slight_smile: Let me know if there are still problems with it – please submit an exact source code that fails, I can help more.

1 Like

I haven’t enough time for feedback in recent weeks, but for now I rewrote grid algorithm more accurate and it works fine, thanks!
:blush:

1 Like

Hi @Noscow, happy to hear your grid is working well now! Doing it in code gives you a lot of control. If you run into any issues with TCastleScene or setting up the terrain, share your code, and I’ll try to help

1 Like

Thanks, @shital !
I think return to this game component some later.
I hope I will finish all main game parts, clear code and open it from GitHub in the foreseeable future for demonstration of the game process essence.
It is didn’t done yet only because my current code not enough good formatted and can cause eye-bleeding :sweat_smile: