public void OnSceneChange() { var renderers = Object.FindObjectsOfType <Renderer>(); var mpb = new MaterialPropertyBlock(); foreach (var r in renderers) { var id = r.gameObject.GetInstanceID(); var layer = r.gameObject.layer; var tag = r.gameObject.tag; mpb.SetColor("_ObjectColor", ColorEncoding.EncodeIDAsColor(id)); mpb.SetColor("_CategoryColor", ColorEncoding.EncodeLayerAsColor(layer)); r.SetPropertyBlock(mpb); } }
private void SetSegmentationEffect() { var renderers = FindObjectsOfType <Renderer>(); var mpb = new MaterialPropertyBlock(); foreach (var r in renderers) { var id = r.gameObject.GetInstanceID(); segmentationIds.TryGetValue(r.gameObject.name, out id); var layer = r.gameObject.layer; mpb.SetColor("_ObjectColor", ColorEncoding.EncodeIDAsColor(id)); mpb.SetColor("_CategoryColor", ColorEncoding.EncodeLayerAsColor(layer)); r.SetPropertyBlock(mpb); } myCamera.renderingPath = RenderingPath.Forward; SetupCameraWithReplacementShader(0, Color.gray); }
private void Start() { myCamera = GetComponent <Camera>(); var renderers = FindObjectsOfType <Renderer>(); var mpb = new MaterialPropertyBlock(); foreach (var r in renderers) { var id = r.gameObject.GetInstanceID(); var layer = r.gameObject.layer; mpb.SetColor("_ObjectColor", ColorEncoding.EncodeIDAsColor(id)); mpb.SetColor("_CategoryColor", ColorEncoding.EncodeLayerAsColor(layer)); r.SetPropertyBlock(mpb); var objectName = r.gameObject.name; if (!segmentationIds.ContainsKey(objectName)) { segmentationIds.Add(r.gameObject.name, id); } } UpdateCameraEffect(); }
/// <summary> /// </summary> void Setup() { if (this._colors_by_category != null && this._colors_by_category.Length > 0) { foreach (var tag_color in this._colors_by_category) { if (!this.ColorsDictGameObject.ContainsKey(tag_color._Category_Name)) { this.ColorsDictGameObject.Add(tag_color._Category_Name, tag_color._Color); } } } if (this._segmentation_preset) { var segmentation_color_by_tags = this._segmentation_preset._color_by_categories; if (segmentation_color_by_tags != null) { foreach (var tag_color in segmentation_color_by_tags) { if (!this.ColorsDictGameObject.ContainsKey(tag_color._Category_Name)) { this.ColorsDictGameObject.Add(tag_color._Category_Name, tag_color._Color); } } } } this._all_renders = FindObjectsOfType <Renderer>(); if (!this._camera) { this._camera = this.GetComponent <Camera>(); } if (this.ColorsDictGameObject == null) { this.ColorsDictGameObject = new Dictionary <string, Color>(); } switch (this._segmentation_mode) { case SegmentationMode.Tag_: this._capture_passes = new[] { new SynthesisUtilities.CapturePass { _Name = "_tag_id", _ReplacementMode = SynthesisUtilities.ReplacementModes .Tag_id_, _SupportsAntialiasing = false } }; break; case SegmentationMode.Layer_: this._capture_passes = new[] { new SynthesisUtilities.CapturePass { _Name = "_layer_id", _ReplacementMode = SynthesisUtilities.ReplacementModes .Layer_id_, _SupportsAntialiasing = false } }; break; default: throw new ArgumentOutOfRangeException(); } SynthesisUtilities.SetupCapturePassesReplacementShader(this._camera, this.segmentation_shader, ref this._capture_passes); this.CheckBlock(); foreach (var a_renderer in this._all_renders) { a_renderer.GetPropertyBlock(this._block); string category_name; var category_int = 0; Color color; string shader_data_name; switch (this._segmentation_mode) { case SegmentationMode.Tag_: category_name = a_renderer.tag; shader_data_name = SynthesisUtilities._Shader_Tag_Color_Name; break; case SegmentationMode.Layer_: category_int = a_renderer.gameObject.layer; category_name = LayerMask.LayerToName(category_int); shader_data_name = SynthesisUtilities._Shader_Layer_Color_Name; break; default: throw new ArgumentOutOfRangeException(); } if (!this.ColorsDictGameObject.ContainsKey(category_name)) { if (!this._Replace_Untagged_Color) { switch (this._segmentation_mode) { case SegmentationMode.Tag_: category_int = category_name.GetHashCode(); color = ColorEncoding.EncodeTagHashCodeAsColor(category_int); //color = ColorEncoding.EncodeIdAsColor(category_int); break; case SegmentationMode.Layer_: color = ColorEncoding.EncodeLayerAsColor(category_int); break; default: //color = ColorEncoding.EncodeIdAsColor(category_int); throw new ArgumentOutOfRangeException(); } } else { color = this._Untagged_Color; } this.ColorsDictGameObject.Add(category_name, color); } else { color = this.ColorsDictGameObject[category_name]; } this._block.SetColor(shader_data_name, color); a_renderer.SetPropertyBlock(this._block); } }