playMaker

Author Topic: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)  (Read 8504 times)

acriticalstrike

  • Playmaker Newbie
  • *
  • Posts: 24
CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? I'm referring to the number of materials set in the mesh renderer, i have multiple materials that are required for some meshes while others need just one... anyway do i need to write a custom action for this? essentially i just need to change the mesh render components "size" on a game object

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #1 on: July 18, 2014, 12:21:12 AM »
I think the material length value is public, but there isn't an action to modify it.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

acriticalstrike

  • Playmaker Newbie
  • *
  • Posts: 24
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #2 on: July 18, 2014, 01:42:34 AM »
could you give me an example of how I might change the "length" of the material renderer for a specific game object?

Attached is the component I'm referencing. The "Size" of the Mesh Renderer determines how many textures I can manipulate on this game object. Depending on which mesh I have on the game object, I may need one more more textures and so it becomes important for me to set the number of textures available on the game object during run time...

So I suppose It's known as Material Length? and not Material Size? Any help on this subject would be very helpful and any action I write I'll share here so others may use it I figure this is an action that eventually most developers will need at one point or another, I'm using it as part of a "universal element" that I can spawn into the game and morph into whatever object I'd like.


acriticalstrike

  • Playmaker Newbie
  • *
  • Posts: 24
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #3 on: July 18, 2014, 04:15:48 AM »
Here I made an example of an action that I think could work but it seems line 55 is causing an error, maybe someone here can figure this out with me!

Code: [Select]
// Created by Andre First of His Name, King of the Andals and the First Men, Lord of the Seven Kingdoms, and Protector of the Realm

using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Material)]
[Tooltip("Sets the Size of the Mesh Renderer Component, " +
"the number of materials Mesh Renderer array. " +
"Created by Andre First of His Name, King of the Andals and" +
"the First Men, Lord of the Seven Kingdoms, and Protector of the Realm")]
public class setMeshRendererArraySize : FsmStateAction
{
[Tooltip("The GameObject with the Mesh Renderer")]
[CheckForComponent(typeof(MeshRenderer))]
public FsmOwnerDefault gameObject;

[Tooltip("Specify the number of materials.")]
public FsmInt numberOfMaterials;

[Tooltip("Repeat every frame. Useful if the value is animated.")]
public bool everyFrame;

public override void Reset()
{
gameObject = null;
numberOfMaterials = 0;
everyFrame = false;
}

public override void OnEnter()
{
DoSetMeshRendererArraySize();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate ()
{
DoSetMeshRendererArraySize();
}

void DoSetMeshRendererArraySize()
{
//MeshRenderer = new MeshRenderer[numberOfMaterials];
//Material = new Material[numberOfMaterials];
//Renderer.materials = new Material[numberOfMaterials];
//MeshRenderer.materials[numberOfMaterials];
//Renderer.materials[numberOfMaterials];
//Renderer.materials = new Material[numberOfMaterials];
renderer.materials = new Material[numberOfMaterials];
//MeshRenderer = new MeshRenderer.Materials[numberOfMaterials];

return;
}
}
}


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #4 on: July 18, 2014, 08:35:58 AM »
Hi,

 that doesn't make sense.

 The number of materials depends on your mesh and how it was created, it's the fbx mesh model that defines how material it needs and where to apply it.

 Can you give me your use case as to why you want to alter the number of material on a gameobject? Also a gameobject is not really the actual entity to lookat, it's the mesh itself.

 Bye,

 Jean

acriticalstrike

  • Playmaker Newbie
  • *
  • Posts: 24
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #5 on: July 18, 2014, 01:18:18 PM »
When I am running my game I have a game object called universal element. I spawn this element into the game, depending on a multitude of variables, the element will have  a mesh applied to it. Lets say at one moment when it spawns it it's placed in a certain location as a rock. Another moment passes the player is very far away, it'll despawn and respawn into the game near the player as a tree. Well in this case, the tree has a mesh that actually instead of being just a mesh with a single material like the rock, it'll need two materials say for the leafs and the tree trunk. This is when I need to change the material array to 2 instead of 1. Another case, I am further along in the game and the element spawns in as a building. The building has a mesh that requires 4 materials. Now I need to change the array to 4 materials instead of 1. I do all sorts of things to this element, I change its size, the textures it uses, the shaders that are active on its texture, the shader parameters (some of my shaders have methods I can call to change modify the way they behave, the rotation of the object and etc...

This is my use case. Simply put there are times that my element needs to be a rock and times where it needs to be a car, building, tree, bush, water, humanoid, space ship, etc etc all of these things use different meshes and many of those meshes have more than one material. Now If I have an element that has an array with say 10 materials but my current mesh only needs 2, the shaders and the textures do not perform right. For some reason the 8 other shaders and textures even if I do not place a texture on those material "slots" affect the other 2 shaders. In a case where I have a mesh that needs 2 materials and my element has 10 materials in the array, the textures will appear in game while I'm testing but shaders are not applied correctly, it causes errors a reflective shader for instance might not render, it'll appear as one of the shaders applied to one of the other 8 material slots that aren't in use. The same is true for the textures in the other 8 slots, they often will just appear black even if empty. So I might had placed the right texture in slot one and two but the other 8 overwrite it and the end result is black.

To make a long story short, many developers at one point or another are going to need to change the size of the Mesh Renderer component on their game object during runtime. There needs to be an action for this.


Thanks for any help you can give me - Andre
« Last Edit: July 18, 2014, 01:39:26 PM by acriticalstrike »

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #6 on: July 18, 2014, 01:36:42 PM »
Just use Object Pooling, its much better than what you're trying to do with the universal object.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

acriticalstrike

  • Playmaker Newbie
  • *
  • Posts: 24
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #7 on: July 18, 2014, 01:44:13 PM »
I actually do use object pooling :-D

acriticalstrike

  • Playmaker Newbie
  • *
  • Posts: 24
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #8 on: July 18, 2014, 01:45:07 PM »
I pool the universal terrain element, the reason why it changes into different objects is because this way I ONLY POOL ONE OBJECT!

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #9 on: July 18, 2014, 01:48:52 PM »
I pool the universal terrain element, the reason why it changes into different objects is because this way I ONLY POOL ONE OBJECT!

Sure, but the amount of work involved in changing the universal object is silly when you compare it to simply pooling prefabs that are already prepared and ready to go. I'm not really understanding the value of going through all that trouble when the traditional method is perfectly fine.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

acriticalstrike

  • Playmaker Newbie
  • *
  • Posts: 24
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #10 on: July 18, 2014, 01:49:52 PM »
my friend, this game runs on android and I cant use hundreds of pooled objects that are only used some times when i need them, I have a limit of 100 objects lets say and each one MUST be used at all times

acriticalstrike

  • Playmaker Newbie
  • *
  • Posts: 24
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #11 on: July 18, 2014, 01:50:43 PM »
Here is an example of how changing the material Size on the mesh renderer could help me, THE FIRST PICTURE with just 10 materials in the array. THE SECOND I change the material array size to 1 and magic happens, the rock gets the right texture :-D THIS HAPPENS IN 1 FRAME all i did was change that material array in the mesh renderer component from 10 to 1 ALL THE LOGIC in my game works :-D

acriticalstrike

  • Playmaker Newbie
  • *
  • Posts: 24
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #12 on: July 18, 2014, 01:52:07 PM »
to find out MORE about the project im building visit www.quantumforgegames.com or see this video here:
I've been working on it for 6 months i know what works and what doesnt I NEED to pool as few game objects as possible to load on mobile devices more quickly !

acriticalstrike

  • Playmaker Newbie
  • *
  • Posts: 24
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #13 on: July 18, 2014, 01:57:08 PM »
what you think "works" is a matter of opinion, what i've tested proves to me that I need fewer pools, for faster loading times and therefore using a single pool that contains an "element" object that can become anything I need when it spawns is what works for me.

acriticalstrike

  • Playmaker Newbie
  • *
  • Posts: 24
Re: CAN I SET THE NUMBER OF MATERIALS ON A GAME OBJECT? (mesh renderer)
« Reply #14 on: July 18, 2014, 03:56:35 PM »
If you wonder why i need one UNIVERSAL ELEMENT that is pooled instead of pooling a rock, tree, building, bush, grass and the other 150 objects in a scene the answer is simple, using a single pool of 100 elements that can morph into anything they need to be during runtime loads much more quickly than if I had a separate prefab for each object type.

If I have 20 different objects then i would need to make 20 different pools instead of simply making one pool of objects that when spawned simply change a few parameters so that they reflect the object they should represent. It saves memory, I dont need a seperate pool for prefabs that are used rarely, just a single pool of objects that have the ability to morph into whatever they need to be. Have I done this YES this issue I share with you is just a minor glitch in a much deeper system I have taken months to develop for my game.