How Can I acces an object of the imported mesh?

Blitz3D Forums/Blitz3D Programming/How Can I acces an object of the imported mesh?

vinnicius(Posted 2017) [#1]
If I export a mesh with 5 cubes with different names.

Is it possible to check the distance between the camera and objects(1,2,3,4,5) of the mesh?


RemiD(Posted 2017) [#2]
if you add pivots/joints in the mesh, then yes : you can load the mesh and retrieve each "children" either manually with :
Mesh = loadanimmesh("mesh.b3d")
Pivot = findchild(Mesh,"pivotname") ;it can be a pivot or a joint...

or automatically with something like :
http://www.blitzbasic.com/codearcs/codearcs.php?code=3264
also take a look at :
http://www.blitzbasic.com/b3ddocs/command.php?name=FindChild

I suppose that if your modeling tool allows you to have several meshes in the same file, the pivots/origins of each mesh are added automatically so you probably don't need to add them manually. (not sure, test it!)


Bobysait(Posted 2017) [#3]
For this special case, you can access them procedurally if all the cubes use the same prefix in their name (like "Cube 1", "Cube 2" etc ...) and the findchild function

For the example, let's say they all start with "Cube " and you have 5 cubes (starting from "Cube 1" and ending to "Cube 5") :
MyMesh = LoadAnimMesh("Your file")
For n = 1 To 5
  cub = FindChild(MyMesh, "Cube "+n)
  Dist# = EntityDistance (camera, cub)
  ; [...]
  ; do whatever you want with the distance here
Next



K(Posted 2017) [#4]
There is also "Getchild(entity,index)" which allows you to procedurally cycle through them. Index counts from 1.


vinnicius(Posted 2017) [#5]
Thank you guys. Everybody here are so "professional"! I like this forum because I feel that people here love blitz3d. :D

I am trying to talk with my best english. I speak brazilian portuguese. :D


vinnicius(Posted 2017) [#6]
Bobysait: I am getting "entity does not exist".

I am trying to print the distance just for testing.
The variables are global and the name of the object "I think" is correct...

:(



EDIT:

When i print it appears:
(( CountChildren = 3 ))
(( FindChild = 8496224))

But when i try to calculate the distance:
"entity does not exist".


vinnicius(Posted 2017) [#7]
I did it!


Now I can hide and show the grass one by one deppending on the distance.
:D:D:D


vinnicius(Posted 2017) [#8]
How i did it:



	For i = 0 To total_world_pieces_x
		For j = 0 To total_world_pieces_z
			If world(i,j)\grass_layer > 0
				For k = 1 To CountChildren(world(i,j)\grass_layer)
					If EntityDistance (cam,GetChild(world(i,j)\grass_layer,k)) > distance_to_hide
						HideEntity GetChild(world(i,j)\grass_layer,k)
					Else
						ShowEntity GetChild(world(i,j)\grass_layer,k)
					EndIf
				Next
			EndIf
		Next
	Next






gpete(Posted 2017) [#9]
this looks very useful...it's nice to have the forum- great place to explain Blitz methods!


vinnicius(Posted 2017) [#10]
I had problems with performance.

So I have changed to this one:
"value% = 16"
"r% = Rand(1,value%)"
"CountChildren(world(i,j)\grass_layer)/r%"



        value% = 16 <<<<<<<<<<<<<<<<<<

	For i = 0 To total_world_pieces_x
		For j = 0 To total_world_pieces_z
			If world(i,j)\grass_layer > 0

				r% = Rand(1,value%) <<<<<<<<<<<<<<<<<<<<<

				For k = 1 To CountChildren(world(i,j)\grass_layer)/r% <<<<<<<<<<<<<<<<<<<<<
					If EntityDistance (cam,GetChild(world(i,j)\grass_layer,k)) > distance_to_hide
						HideEntity GetChild(world(i,j)\grass_layer,k)
					Else
						ShowEntity GetChild(world(i,j)\grass_layer,k)
					EndIf
				Next
			EndIf
		Next
	Next


And this function is called not once by frame/cycle.

And it is working smoothly now :D


RemiD(Posted 2017) [#11]
Maybe store the reference of each grass mesh in an arraylist/customtypelist, then browse in this list, this will take less time...


vinnicius(Posted 2017) [#12]
RemiD, yes!

Thank you. This is something basic but i have forgotten.


Cocopino(Posted 2017) [#13]
While this could a good idea to limit the polycount, the implementation will be very slow I think when having a lot of grass because of looping through many objects.

To speed up performance you could separate the grass into groups based on location on the map then only check the groups that are close to the camera.

Also, you won't need to check the visibility every single frame, so you could have a global tick counter (every frame ticks=ticks+1), then check groups with a function like this
CheckVisibilityOfGroup(ticks mod groupcount)


This way your code is only checking a small amount of objects each time freeing up your cpu for more important things.


RemiD(Posted 2017) [#14]
or you could use a grid system, when by default all entities are hidden, and you only show the entities around player (using the player cell coordinate and minx,minz maxx,maxz area of cells around player, to determine which entities are around player (are inside the cells around player) very fast ! (from my tests, 20 times faster than checking a 2d distance)


Imperium(Posted 2017) [#15]
How much CPU time does hiding the entities really save? For example if you have a city with a bunch of buildings. Every mesh outside of the cameraview is not being drawn, but will still be involved in collision checks.

The assets are stored in memory so what I'm not following is how many cycles this hiding/unhiding of entities really saves when they are already not drawn past a certain point?


RemiD(Posted 2017) [#16]
if the entities are outside the camera fov, you don't need to hide them because they will be automatically hidden (not rendered)


vinnicius(Posted 2017) [#17]
RemiD: I use a gridsystem to show and hide the terrain. Maybe I can implement it to the grass too.

Cocopino: Yes, I am not checking every single frame.

My computer (laptop): Intel Core i5 2.6Ghz, 6Gb Ram and Intel Graphics Card 4000.

I have tested with thousands of grass. And the game stills running without big delays.

-- Well, now I faced a new problem. Is it possible to animate the objects (that i have got 'getchild') from the imported mesh individualy? I tested and it does not work. I don't know if I made some basic mistake.


Cocopino(Posted 2017) [#18]
So you want to animate blades of grass individually? Your game will take a serious performance hit from that, this is usually done on the gpu with shaders in new(er) 3D engines.

Will there be more going on in your game world like animated objects, "thinking" enemies, particles, physics/collisions, objects with a high polycount etcetera? I'd focus on getting those in first and see how many frames per second you can still assign to making the grass look better.


vinnicius(Posted 2017) [#19]
Cocopino.
You are right.

I decided to not animate the grass. The environment do not need too much wind because of the context of the game. So no problem.

I think it will be a joy to program the enemies brain. :D