I bought the Bitmap2Material yesterday, which is a neat tool, but have been trying to get something to work with success yet. I created a Bitmap2Material the way I want in the Unity editor, and what I want to do is apply that specific Bitmap2Material to different textures at runtime, and then assign the resulting materials to different meshes.
For example, if I have 3 different textures that I want to apply to the 3 plane meshes, and I have one Bitmap2Material which I want applied to each texture to generate its normals, etc., how would I make that happen at runtime? Note: I don’t want to create 3 Bitmap2Material instances in the Unity editor because the Bitmap2Material settings are identical for the 3 textures.
Here is what I have tried:
Step 1 – get the main ProceduralMaterial that I want applied to all textures:
Object[] procedureMaterialObjects = Resources.LoadAll("Substances", typeof(ProceduralMaterial) );
ProceduralMaterial substance = (ProceduralMaterial) procedureMaterialObjects[0];
Step 2 – Clone a new instance of substance, and then set the textures on this new instance, call RebuildTexturesImmediately to generate the normals, etc., then apply the clone to mesh planes*:*
```
*for (int i=0; i < planes.Length; i++) {
ProceduralMaterial tempSubstance = (ProceduralMaterial) Instantiate(substance);
//Set the texture[i]:
tempSubstance.mainTexture = textures[i];
//Set the diffuse texture - THIS DOES NOT WORK when Instantiate is used:
tempSubstance.SetProceduralTexture(“Bitmap_Diffuse”, textures[i]);
tempSubstance.RebuildTexturesImmediately();
planes[i].renderer.material = tempSubstance;
}*
* <em>The above code does not work because tempSubstance.SetProceduralTexture("Bitmap_Diffuse", textures*); does nothing when a new instance is created using Instantiate. For some reason Instantiate does not appear to clone the entire object.*</em> <em>*Note that I also tried not using Instantiate, and resused the substance, but the problem is that all meshes will have the final texture applied to all of the time (i.e. the mainTexture and Bitmap_Diffuse is written over each time, so the last texture in the loop ends up showing on all meshes):*</em> _*
_
*for (int i=0; i < planes.Length; i++) {
//Set the texture[i]:
substance.mainTexture = textures[i];
substance.SetProceduralTexture(“Bitmap_Diffuse”, textures[i]);
substance.RebuildTexturesImmediately();
planes[i].renderer.material = substance;
}
_```*_
I suppose I could manually go in the Unity editor, and ctrl-D duplicate many instances of the Bitmap2Material, and at runtime assign each texture to each Bitmap2Material (i.e each texture would have to have a corresponding Bitmap2Material predefined in the Unity editor), and then apply them to the object. But this is a silly way to do this – I should be able to make clone copies of the Bitmap2Material at runtime and use them to create the normals, etc. for different textures.
Any suggestions would be appreciated! Thanks!