Search Unity

MaterialPropertyBlock not batching correctly in 2017

Discussion in 'General Graphics' started by Tactics_IT, Dec 4, 2019.

  1. Tactics_IT

    Tactics_IT

    Joined:
    Jul 29, 2010
    Posts:
    28
    Ive been using this script in a project from 5.6 for some time now.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WoodOffset : MonoBehaviour {
    6.  
    7.     public Material targetMaterial;
    8.     private Renderer _renderer;
    9.     public Renderer[] lodRenderers;
    10.     private MaterialPropertyBlock _propBlock;
    11.     public int materialID = 1;
    12.     public int dividedByCount = 8;
    13.     public float horizontalRes = 1;
    14.     public float offsetDiff;
    15.     public Color[] colorTints;
    16.     public float[] materialOffsets;
    17.     public bool allowRandomOffset = true;
    18.  
    19.  
    20.     private void OnEnable()
    21.     {
    22.         offsetDiff = horizontalRes / dividedByCount;
    23.         _propBlock = new MaterialPropertyBlock();
    24.         int randomColorChoice = Random.Range(0, colorTints.Length);
    25.  
    26.  
    27.         if (lodRenderers.Length != 0)
    28.         {
    29.             foreach(Renderer rend in lodRenderers)
    30.             {
    31.                 rend.GetPropertyBlock(_propBlock);
    32.                 targetMaterial = rend.sharedMaterials[materialID];
    33.                 if (allowRandomOffset)
    34.                 {
    35.                     Vector2 getOffset = targetMaterial.GetTextureOffset("_MainTex");
    36.                     Vector2 getTile = targetMaterial.GetTextureScale("_MainTex");
    37.                     //Vector2 getTileing = targetMaterial.("_MainTex");
    38.                     _propBlock.SetVector("_MainTex_ST", new Vector4(getTile.x, getTile.y, offsetDiff * randomColorChoice, materialOffsets[randomColorChoice]));
    39.                     //targetMaterial.SetTextureOffset("_MainTex", new Vector2(offsetDiff * Random.Range(0, dividedByCount + 1), Random.Range(0.000f, 6.00f)));
    40.                 }
    41.                 _propBlock.SetColor("_Color", colorTints[randomColorChoice]);
    42.                 // Apply the edited values to the renderer.
    43.                 rend.SetPropertyBlock(_propBlock);
    44.             }
    45.         } else
    46.         {
    47.             _renderer = GetComponent<Renderer>();
    48.             _renderer.GetPropertyBlock(_propBlock);
    49.             targetMaterial = _renderer.sharedMaterials[materialID];
    50.             if (allowRandomOffset)
    51.             {
    52.                 Vector2 getOffset = targetMaterial.GetTextureOffset("_MainTex");
    53.                 Vector2 getTile = targetMaterial.GetTextureScale("_MainTex");
    54.                 //Vector2 getTileing = targetMaterial.("_MainTex");
    55.                 _propBlock.SetVector("_MainTex_ST", new Vector4(getTile.x, getTile.y, offsetDiff * randomColorChoice, materialOffsets[randomColorChoice]));
    56.                 //targetMaterial.SetTextureOffset("_MainTex", new Vector2(offsetDiff * Random.Range(0, dividedByCount + 1), Random.Range(0.000f, 6.00f)));
    57.             }
    58.             _propBlock.SetColor("_Color", colorTints[randomColorChoice]);
    59.             // Apply the edited values to the renderer.
    60.             _renderer.SetPropertyBlock(_propBlock);
    61.         }
    62.      
    63.      
    64.      
    65.     }
    66. }
    67.  
    in 5.6 this allows all wood blocks that get assigned the same values to be batched together.

    5.6.png

    I have recently moved the project over to 2017.4.27f1 and noticed that the batching that used to work no longer is working.

    2017.png

    Does anyone have an idea as to why this is no longer working? Do i have to do something different in 2017?

    Thanks for the help in advance.

    Matt
     
    Last edited: Dec 4, 2019