Neodroid  0.2.0
Machine Learning Environment Prototyping Tool
ChangeMaterialOnRenderByTag.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
4 using UnityEngine;
5 
6 namespace droid.Runtime.Utilities.GameObjects.NeodroidCamera.Segmentation.Obsolete {
10  [ExecuteInEditMode]
14  Renderer[] _all_renders;
15 
18  MaterialPropertyBlock _block;
19 
22  [SerializeField]
24 
27  LinkedList<Color>[] _original_colors;
28 
31  public bool _Replace_Untagged_Color = true;
32 
33  [SerializeField] ScriptableObjects.Segmentation _segmentation = null;
34 
37  Dictionary<string, Color> _tag_colors_dict = new Dictionary<string, Color>();
38 
41  public Color _Untagged_Color = Color.black;
42 
45  public ColorByCategory[] ColorsByCategory { get { return this._colors_by_category; } }
46 
49  public override Dictionary<String, Color> ColorsDict { get { return this._tag_colors_dict; } }
50 
53  void Awake() {
54  this._block = new MaterialPropertyBlock();
55  this._tag_colors_dict.Clear();
56  var colors_by_tag = this._colors_by_category;
57  if (colors_by_tag != null && colors_by_tag.Length > 0) {
58  foreach (var tag_color in this._colors_by_category) {
59  if (!this._tag_colors_dict.ContainsKey(tag_color._Category_Name)) {
60  this._tag_colors_dict.Add(tag_color._Category_Name, tag_color._Color);
61  }
62  }
63  }
64 
65  if (this._segmentation) {
66  var segmentation_color_by_tags = this._segmentation._color_by_categories;
67  if (segmentation_color_by_tags != null) {
68  foreach (var tag_color in segmentation_color_by_tags) {
69  if (!this._tag_colors_dict.ContainsKey(tag_color._Category_Name)) {
70  this._tag_colors_dict.Add(tag_color._Category_Name, tag_color._Color);
71  }
72  }
73  }
74  }
75 
76  this.Setup();
77  }
78 
81  void Update() {
82  this.Setup(); // Renderers maybe be disable and enabled, that is why every update we find all renderers again
83  }
84 
87  void Setup() {
88  this.CheckBlock();
89 
90  this._all_renders = FindObjectsOfType<Renderer>();
91  }
92 
95  protected override void Change() {
96  this._original_colors = new LinkedList<Color>[this._all_renders.Length];
97  for (var i = 0; i < this._original_colors.Length; i++) {
98  this._original_colors[i] = new LinkedList<Color>();
99  }
100 
101  this.CheckBlock();
102 
103  for (var i = 0; i < this._all_renders.Length; i++) {
104  var c_renderer = this._all_renders[i];
105  if (c_renderer) {
106  if (this._tag_colors_dict != null && this._tag_colors_dict.ContainsKey(this._all_renders[i].tag)) {
107  foreach (var mat in this._all_renders[i].sharedMaterials) {
108  if (mat != null && mat.HasProperty(this._Default_Color_Tag)) {
109  this._original_colors[i].AddFirst(mat.color);
110  }
111 
112  this._block.SetColor(this._Segmentation_Color_Tag,
113  this._tag_colors_dict[this._all_renders[i].tag]);
114 
115  this._block.SetColor(this._Outline_Color_Tag, this._Outline_Color);
116  this._block.SetFloat(this._Outline_Width_Factor_Tag, this._Outline_Width_Factor);
117  this._all_renders[i].SetPropertyBlock(this._block);
118  }
119  } else if (this._Replace_Untagged_Color) {
120  foreach (var mat in this._all_renders[i].sharedMaterials) {
121  if (mat != null && mat.HasProperty(this._Default_Color_Tag)) {
122  this._original_colors[i].AddFirst(mat.color);
123  }
124 
125  this._block.SetColor(this._Segmentation_Color_Tag, this._Untagged_Color);
126 
127  this._block.SetColor(this._Outline_Color_Tag, this._Outline_Color);
128  this._block.SetFloat(this._Outline_Width_Factor_Tag, this._Outline_Width_Factor);
129  this._all_renders[i].SetPropertyBlock(this._block);
130  }
131  }
132  }
133  }
134  }
135 
136  void CheckBlock() {
137  if (this._block == null) {
138  this._block = new MaterialPropertyBlock();
139  }
140  }
141 
144  protected override void Restore() {
145  this.CheckBlock();
146 
147  for (var i = 0; i < this._all_renders.Length; i++) {
148  if (this._all_renders[i]) {
149  foreach (var mat in this._all_renders[i].sharedMaterials) {
150  if (mat != null && this._original_colors != null && i < this._original_colors.Length) {
151  var c = this._original_colors[i];
152  var last = c?.Last;
153  if (last != null) {
154  var last_val = last.Value;
155  this._block.SetColor(this._Default_Color_Tag, last_val);
156  this._original_colors[i].RemoveLast();
157  this._all_renders[i].SetPropertyBlock(this._block);
158  }
159  }
160  }
161  }
162  }
163  }
164  }
165 }