Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / The advantages of InstanceObject

Author
Message
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 17th Jan 2013 03:57
Hi folks.
I'm just wondering if one of the more experienced devs here could tell me what the advantage is for instancing an object vs. cloning.
I understand the instance is unaffected by any transformations done to the original other than deletion, but why use it?
Is it a big memory saver?

Thanks!!

Nexus 7 (Jelly Bean 4.1.2) -- HTC Droid Incredible 2 (Gingerbread 2.3.?) -- Motorola Droid X (Gingerbread 2.3.4)
Willing to test for you!
tornado
18
Years of Service
User Offline
Joined: 21st Jul 2005
Location:
Posted: 17th Jan 2013 12:51 Edited at: 17th Jan 2013 12:52
Not being experienced in agk 3d yet, but suggest Instance saves memory by sharing sourceobject 3d data, like 1 piece of memory to create lots of objects. In that case changing this source memory area will affect 3d data for all object instances. Copying creates new memory allocations for each new object and therefore consumes a lot more memory.

MikeMax
AGK Academic Backer
12
Years of Service
User Offline
Joined: 13th Dec 2011
Location: Paris
Posted: 17th Jan 2013 13:38 Edited at: 17th Jan 2013 13:47
tornado is right.

Naphier, InstanceObject shares vertex data with original object (so .. only present one time in memory).

Imagine a forest with 300 trees (OBJ Format for example) ... Cloning will prevent your code from reloading each tree from the original OBJ file but will create 300 vertex data arrays in memory (each one with same datas).

Instancing instead of cloning will permit your app to use only the original tree's vertex data array (so .. only one array of one tree in memory) ... all instanced objects will point to original memory array (from original object) to display them... That's why modifying vertex datas (original coords, UV, normals,...) of the original object will affect all instanced objects ( not true for textures which can be different for each object .. and obviously Positions and rotations are linked to instanced object and doesn't modify original vertex datas

so yes ... it can save a lot of memory (instancing) and reduce the loading duration (instancing or cloning) of your app.

If you have the choice (dépending how you need to manage your objects (if you need to delete them or not etc ..) it's better to Instance them to save loading time and memory (not always possible !)
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 17th Jan 2013 18:31
Makes sense, I was wondering if there was any more to it. MikeMax;s example with the trees is perfect.

Here's the responses from Lee and Paul courtesy of Alistair at Digital Skills:

Quote: "
Here's the text I got from Lee about clones and instances:

The difference between CLONE and INSTANCE is that clone is a complete byte for byte copy of the original 3D object used as the source. Instance does not copy the data, and instant 'borrows' the data from the original by reference only. That means, when the original is deleted, the instanced object has no parent to borrow the data from and also disappears. In programming parlance, it's similar to pass by object vs pass by reference.
"


Quote: "
Here's the text I got from Paul (who writes all the code for AGK) about clones and instance objects:

Instances share vertex and collision data with the original object, everything else like images and shaders are separate. There is a caveat on shaders, if the instance object uses a different shader from the original object then it must have exactly the same vertex attribute fields as the shader on the original object.

As far as I can see, the only reason to use instance is to save some space, especially if the model is a complex one with many vertices."



This leaves me to wonder:
Is it possible to modify the location of individual vertices in an instanced object without affecting the original or the other instances? I don't see any 3D commands to manipulate the individual vertices in a 3D object yet, but I believe something similar was available in DBPro via VertexData commands. Something like this would be extremely useful in generating instances of something, like trees, where the instances have random alterations to locations of individual vertices in the object. For example, it could be used as a way to make the height of all of the instances slightly random.
I don't see a way to do this yet, but it would certainly be quite handy!

Nexus 7 (Jelly Bean 4.1.2) -- HTC Droid Incredible 2 (Gingerbread 2.3.?) -- Motorola Droid X (Gingerbread 2.3.4)
Willing to test for you!
Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 17th Jan 2013 19:22
Quote: "Is it possible to modify the location of individual vertices in an instanced object without affecting the original or the other instances?"


No since the instance doesn't contain any vertices, every time a request is made to that object to use its vertices it gets redirected to the original object that has the data.

Quote: "For example, it could be used as a way to make the height of all of the instances slightly random."


You could do this with the SetObjectScale command instead as it doesn't modify the vertices themselves, just the way they are displayed.
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 17th Jan 2013 19:25
Bummer, but makes sense. Probably best to just have various models with randomization and then further randomize with object scaling.
Just dreaming I suppose.

Thanks for the quick response, Paul.

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 17th Jan 2013 21:36
Quote: "You could do this with the SetObjectScale command instead as it doesn't modify the vertices themselves, just the way they are displayed."

Is setObjectScale working yet Paul? I have a feeling not...


this.mess = abs(sin(times#))
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 17th Jan 2013 22:06
Worked for me in 1088 on a sphere with CreateObjectSphere()
I've not tried it with a loaded OBJ though.

Paul Johnston
TGC Developer
21
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 18th Jan 2013 01:12
Quote: "Is setObjectScale working yet Paul?"


Should be working on all objects
MikeMax
AGK Academic Backer
12
Years of Service
User Offline
Joined: 13th Dec 2011
Location: Paris
Posted: 18th Jan 2013 03:25 Edited at: 18th Jan 2013 03:26
it's working very well for me bax (with OBJ files)

Login to post a reply

Server time is: 2024-05-03 23:11:39
Your offset time is: 2024-05-03 23:11:39