Unity HDRP Product Visualizer 1.0.0
Interactive 3D product showcase built with Unity 6 and HDRP
Loading...
Searching...
No Matches
BackgroundController.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using UnityEngine;
3
8
15[System.Serializable]
16public class BackgroundPreset
17{
19 public string presetName = "Dark Studio";
20
22 public Color topColor = new(0.06f, 0.06f, 0.18f, 1f);
23
25 public Color bottomColor = new(0.01f, 0.01f, 0.04f, 1f);
26
28 public float vignette = 1.2f;
29
31 public Color groundTint = new(0.10f, 0.10f, 0.12f, 1f);
32}
33
52public class BackgroundController : MonoBehaviour
53{
54 // ------------------------------------------------------------------
55 // Inspector fields
56 // ------------------------------------------------------------------
57
60
62 [Header("References")]
63 public Renderer backdropRenderer;
64
66 public Renderer groundRenderer;
67
69
71 [Header("Presets")]
72 public List<BackgroundPreset> presets = new();
73
74 // ------------------------------------------------------------------
75 // Events
76 // ------------------------------------------------------------------
77
79 public System.Action<int, BackgroundPreset> OnPresetChanged;
80
81 // ------------------------------------------------------------------
82 // Private state
83 // ------------------------------------------------------------------
84
85 private int _currentIndex;
86 private MaterialPropertyBlock _mpb;
87
88 // ------------------------------------------------------------------
89 // Unity lifecycle
90 // ------------------------------------------------------------------
91
92 void Awake() => _mpb = new MaterialPropertyBlock();
93
94 void Start()
95 {
96 if (presets.Count > 0) Apply(0);
97 }
98
99 // ------------------------------------------------------------------
100 // Public API
101 // ------------------------------------------------------------------
102
104 public void NextPreset()
105 {
106 _currentIndex = (_currentIndex + 1) % presets.Count;
107 Apply(_currentIndex);
108 OnPresetChanged?.Invoke(_currentIndex, presets[_currentIndex]);
109 }
110
113 public void SetPreset(int index)
114 {
115 if (index < 0 || index >= presets.Count) return;
116 _currentIndex = index;
117 Apply(index);
118 OnPresetChanged?.Invoke(_currentIndex, presets[_currentIndex]);
119 }
120
122 public BackgroundPreset CurrentPreset => presets.Count > 0 ? presets[_currentIndex] : null;
123
125 public int CurrentIndex => _currentIndex;
126
129
130 // ------------------------------------------------------------------
131 // Private helpers
132 // ------------------------------------------------------------------
133
136 void Apply(int index)
137 {
138 BackgroundPreset p = presets[index];
139
140 if (backdropRenderer != null)
141 {
142 backdropRenderer.GetPropertyBlock(_mpb);
143 _mpb.SetColor("_TopColor", p.topColor);
144 _mpb.SetColor("_BottomColor", p.bottomColor);
145 _mpb.SetFloat("_VignetteAmount", p.vignette);
146 backdropRenderer.SetPropertyBlock(_mpb);
147 }
148
149 if (groundRenderer != null)
150 {
151 groundRenderer.GetPropertyBlock(_mpb);
152 _mpb.SetColor("_BaseColor", p.groundTint);
153 groundRenderer.SetPropertyBlock(_mpb);
154 }
155 }
156}
Cycles through BackgroundPreset entries by writing to MaterialPropertyBlocks.
string CurrentPresetName
Display name of the active preset (empty string if none).
BackgroundPreset CurrentPreset
The currently active BackgroundPreset, or null if the list is empty.
void SetPreset(int index)
Jumps directly to a preset by index.
System.Action< int, BackgroundPreset > OnPresetChanged
Fired after a preset change with (newIndex, newPreset).
Renderer backdropRenderer
Renderer of the large backdrop quad (uses GradientBackground shader).
List< BackgroundPreset > presets
Ordered list of available studio presets.
Renderer groundRenderer
Renderer of the ground plane quad (uses HDRP/Lit).
int CurrentIndex
Zero-based index of the active preset.
void NextPreset()
Advances to the next preset, wrapping around the list.
Serialisable data record that defines one studio backdrop look.
Color bottomColor
Bottom gradient colour fed into _BottomColor.
float vignette
Radial vignette intensity fed into _VignetteAmount (0 = off, 4 = heavy).
string presetName
Human-readable name shown in the on-screen UI (e.g. "Dark Studio").
Color groundTint
Tint applied to the ground plane's _BaseColor.
Color topColor
Top gradient colour fed into the GradientBackground shader's _TopColor.