How can I batch draw meshes in my Unity scene? - unity3d

I am using Unity3d to show a dock scene
the containers are updated based on realtime messages. I am optimizing the draw call for this scene. I found that containers are drawing one by one with the draw mesh method. What I do in the code is load a container from prefab and set color
instance = (GameObject)Instantiate(Resources.Load("Prefabs/box1"));
Material material = instance.GetComponent<MeshRenderer>().material;
material.color = new Color(r / 255f, g / 255f, b / 255f, 1f);
then gameObjects are added to the scene one by one. Is there any way to batch the gameObjects and draw them once?
UPDATE:
I do some changes for my container prefab to enable the GPU Instancing. Yes, the draw calls are down from 6k to 2k by the dynamic batch. But It cause another problem. All of containers are same color since I use
gameObject.GetComponent<MeshRenderer>().sharedMaterial.color = ContainerColor
to set the containers color. Is there any way can solve it ?

You can also use Graphics.DrawMeshInstanced. It draws meshes with GPU instancing. But, You can only draw a maximum of 1023 instances at once. So you need to add custom batches.
https://docs.unity3d.com/2018.1/Documentation/ScriptReference/Graphics.DrawMeshInstanced.html

Depends of what you need to do with the containers, if they are static, you should look at StaticBatchingUtility.Combine (https://docs.unity3d.com/ScriptReference/StaticBatchingUtility.Combine.html)
However, if you need them to be dynamic, you should use 1 material for all your meshes, just change the color in the shader will reduce the number of setPass calls.

You used .material. That creates a new instance of the material, so don't do that.
Use .sharedMaterial. To have different colors, create one material for each color and cache that.

Related

How to feed a transform.position into shader graph?

I am trying to mask a sprite by using shader graph.
Here is a picture of what I have so far:
my problem is that when I feed the mask texture into the shader it centers itself on the main texture, as you see in this image.
See the slightly transparent checkerboard to the right side of the character? I only want this operation to affect the parts where the checkerboard intercepts with the character.
For this I would need someway to displace and scale(?) the mask texture so that it detaches from the center of the main texture.
What nodes do I have to add to accomplish this, and where to connect them? Thanks.
As I commended I do not fully understand the question, but I will give it a try!
You want to move the UV of the texture to only make it apply to part of the image, and disable wrapping!
This can be done by using UV node while using the same channel you have on the overlapping texture and then adding the offset to it.
In order to disable wrapping you need to disable it on texture itself
Change Wrap Mode to Clamp
Set offset to values below 1. Now you need to just calculate what offset value for you will be as 1 is the full width of the image.
EDIT:
Instead of manually changing the UV texture you should use Tiling and Offset node which has easy scaling by changing tiling values.
Good luck :)

Am I able to make certain parts of a texture glow with post process bloom?

So I have these mushroom models, and some of the faces are blue as opposed to purple.
I was hoping to make the top part of the mushroom glow, but not the stem of the mushroom. Currently i just use a point light in Unity but it doesn't look very good.
Any help would be awesome! Thank you
Glowing Mushrooms
U can try making the textures emissive by adding an emission map, that way they will glow when using post processing bloom.
The bloom post-processing effect relies on a material or light's intensity values. So, you'd need a separate material for the stem and another for the head. For the head of the mushroom, you then need to set the material's emission colour as well as the albedo. This will allow you to make it glow slightly and then the bloom will then spread out that glow for you.
As per Bean5's post, you can assign an emissions map if you don't want to use two separate materials. That way, if you have one mesh then only parts of your model will glow.

Is it possible to force SpriteKit to respect depth buffer in Metal with a SKRenderer?

I want to mix SpriteKit and Metal, and I found that there is a special class for that - SKRenderer. But, as I see, it can only draw whole SKScene as a single layer above all screen. So, can it use the depth buffer and zPosition property for the proper rendering? For example, if my scene contains two SKNodes, and I want to draw "Metal object" between them?
I see in a GPU debugger that every SKNode is rendered with a separate draw call (even more that one, actually), so, in theory, it's possible to use SKNode.zPosition not only for sorting inside SKScene. For example, they can just translate it into viewport's z-position as is (and, of course, keep depth test).
The reason why I think that it's possible is that in the documentation, I see this sentence:
For example, you might write the environmental effects layer of your app that does fog, clouds, and rain, with custom Metal shaders, and continue to layer content below and above that with SpriteKit.
and I just can't believe that as "continue to layer content below and above that with SpriteKit" they mean "OK, you can create two different SKScenes".

How to add a a get a simple flag with 3 stripes on a Cube in Unity

is there any way to give a simple 3D cube in Unity the look of a flag without displaying the whole flag on every side. So that as example the top stripe also fill's the whole top?
The default mesh unity provides for cubes always show the same texture an all6 sides, so they are always all the same. Whatever material/texture you apply.
The one material is used an all sides, with UV values from 0 to 1 (whole texture).
Even fiddling the UV scales on the material does not help as the 6 sides all start with the same range.
I you wish to change this, you need to edit/create you own mesh for a cube which allows different UV settings for the 6 surfaces, or multiple materials. You could make one which has a second material for one of the 6 faces and assign your flag texture there.
(Unity does not really provide mesh editing. I used blender for this.)
In Unity, you could create an empty gameobject, add 6 'quad' objects, and use different textures on these. Then this set of 7 objects behaves like a cube, but with different textures (and less performance when using lots)
Why not just use a single quad for the flag?

Duplicate a gameobject in right way Unity 5

I have a game object named Bolt with for example white texture , when I duplicate that and rename duplicate into EnemyBolt and change texture to orange the Bolt texture is changing into orange too , how can I copy or duplicate a gameObject that dosen't change its reference?
When you duplicate a GameObject, you also duplicate all the components that are attached to it. This way the new object gets its own set of component instances. This is in Hierarchy panel representing the content of the scene.
The Project panel represents the assets to be used in the different scenes. When you drag a material to a Renderer, that renderer instance points to a material that will be instantiated at runtime.
In order to optimise rendering process, if a material is shared among many object, only one material is created, this will reduce draw calls.
Now for your solution, you need to duplicate your material and assign the new one to the new object. Now, when you change it on one side, it does not affect the other since they are using different material. On the other hand, you increased the draw call.