Jump to content

Models randomly share the same material?


Lunarovich
 Share

Recommended Posts

Hello! I'm puzzled by the SetMaterial() behavior.

 

I have a scene with randomly disposed cubes made in the editor. When I do the mouse pick on a cube, I check its material, and switch/toggle the material of the cube. I have two separate materials, one with and the other without the emission texture/shader. Now, when I pick the closest cube and toggle its material, some of the other cubes (but not all) get the same material. Sometimes only one cube switches material, sometimes two or more. Everytime the same group of cubes react to the single cube picking/material setting, so apparently that's not something random.

 

For the sake of completeness, I have tried to make individual cubes both with brushes and CTRL-axis drag & drop. The result is similar.

 

Now, I suppose that this behavior has something to do with the Entity:Instance() VS Entity:Copy(). But could be totally wrong smile.png

 

N.B. I use the closest entity option in the Pick routine.

 

Here is my pick routine:

 

local window = Window:GetCurrent()
local context = Context:GetCurrent()
if (window:MouseHit(1)) then
local pickinfo = PickInfo()
if (self.camera:Pick(context:GetWidth() / 2, context:GetHeight() / 2,pickinfo,0,true)) then
 if (pickinfo.entity:GetMaterial() == self.blokMatNormal) then
  pickinfo.entity:SetMaterial(self.blokMatEmissive)
 else
  pickinfo.entity:SetMaterial(self.blokMatNormal)
 end
end
end

Link to comment
Share on other sites

hmmm thats weird... it does seem to be random on which ones it picks - but it consistently picks the same ones to change. At first i thought it had something to do with which material was used during its creation but that doesn't seem to be the case.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

I've tried to release a cube upon picking it, and the engine released every other connected cube. It probably has to do something with the way brushes work. I'm testing the behavior on the models right now...

 

Just tested it. I get the same behavior with models and with prefabs.

Link to comment
Share on other sites

It probably has to do something with the way brushes work.

yeah that seems to be the issue. I added mass to some of the csg primitives and now they only change material by themselves. I assume adding an empty script will do the same.

 

Edit-- yes, adding an empty script or mass will prevent this issue with CSG objects.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Yeah, adding scripts effectively solved the issue with models (EDIT: with prefabs made out of brushes) as well as with brushes. Thanks!

 

Is there some logical reason why adding scripts would help in this case? Does adding script do some under the hood "individualization" of the object? If yes, in what exactly consists this individualization?

 

P.S. Sorry for philosophical terms. I'm a philosopher by profession smile.png

Link to comment
Share on other sites

Brushes are collapsed when loaded, unless they have mass or a script, so what you think is a single object is actually a big cluster of merged objects.

 

In the case of models, if you change the material of one instance, all other models of the same file will get the same material (actually, their surface will, since the surface is shared across them all).

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

In the case of models, if you change the material of one instance, all other models of the same file will get the same material (actually, their surface will, since the surface is shared across them all).

Why would adding a script effect this - at least based on OP's last post? havent tried it yet myself.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Here, I have cubes made out of brushes with scripts attached to every cube:

 

GqtPX4c.png

 

It works really neat in the game. Just as expected. I can turn on/off individual cubes with camera mouse picking.

 

If I make a prefab out of the single cube (made using CSG, ie. brushes), it still works as intended.

 

However, it does not work with models (even if I attach scripts to them). This agrees with what Josh has said:

In the case of models, if you change the material of one instance, all other models of the same file will get the same material (actually, their surface will, since the surface is shared across them all).

 

But why does it work then with prefabs (with scripts attached)? Aren't prefabs made out of brushes internally turned into models? And finally, why does it work with individual pieces of CSGs with scripts attached?

 

P.S. Josh, thanks for this nice framework. Really enjoying it! Haven't found a 3D framework so transparent and so easy to use, but still not simplistic a bit... And I've tried a lot of them smile.png

Link to comment
Share on other sites

Why would adding a script effect this - at least based on OP's last post? havent tried it yet myself.

I don't think it does, I think he is probably confused in his description.

 

Nice glasses, BTW.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I don't think it does, I think he is probably confused in his description.

 

I've already corrected/edited the post #5 and said in post #9 that adding scripts to same instances of models do not make models have separate materials. So, their behavior is expected and explained (which puts my mind at ease).

 

However, the question remains in respect to CSG. Why does adding scripts works in the case of objects made using brushes? And why random group of these objects react in the same manner - get / do not get the assigned material - on material change (via mouse picking, which is inessential here)?

 

Does it stem from the fact that Leadwerks converts CSG objects with scripts, under the hood and in the runtime / on the map creation, into separate models, ie. models with separate surfaces?

Link to comment
Share on other sites

In order to improve performance, brushes are normally assumed to be static, so they are collapsed into big models in the scene. The clusters are defined by the scene octree, so instead of having hundreds of boxes, you just have a few large models in the scene, which renders much faster.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I was going through this

and he says that you need to give non-zero mass or attach a script to the CSG object in order to turn it into entity. Concerning what Josh have said, maybe he should have added, an entity with a separate surface, since large models made out of CSG are geometries turned into entities silently by Leadwerks, as I believe.

 

P.S. Maybe I'm overdoing it, but it's just my cartesian need to have a clear and distinct understanding of the subject matter smile.png

Link to comment
Share on other sites

It's very simple. If CSG meshes have no scripts and have 0 mass then they get merged into static scene geometry (upon loading). In cases when mass is specified or scripts attached they are turned into individual entities. You can try this with a sphere. Create a CSG sphere, add mass and make it rigid body. Run the game and it will bounce around the scene.

Intel Core i7 Quad 2.3 Ghz, 8GB RAM, GeForce GT 630M 2GB, Windows 10 (x64)

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...