| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL.Skybox |
| | 6 | | { |
| | 7 | | public class ReflectionProbeRuntime : MonoBehaviour |
| | 8 | | { |
| | 9 | | public ReflectionProbe realtimeProbe; |
| 0 | 10 | | public float updateAfter = 60; |
| | 11 | | float timer; |
| | 12 | | public Transform followTransform; |
| 0 | 13 | | public int resolution = 256; |
| | 14 | | [SerializeField] bool fixedSkybox = false; |
| | 15 | | [SerializeField] bool fixedSkyboxBaked = false; |
| 0 | 16 | | [SerializeField] float fixedSkyboxUpdateTime = 3; |
| | 17 | | [SerializeField] float fixedSkyboxTimer = 0; |
| | 18 | |
|
| | 19 | | private void Start() |
| | 20 | | { |
| 0 | 21 | | timer = 0; |
| 0 | 22 | | realtimeProbe.resolution = resolution; |
| 0 | 23 | | BakeNewReflection(); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | private void Update() |
| | 27 | | { |
| 0 | 28 | | if (fixedSkybox) |
| | 29 | | { |
| 0 | 30 | | UpdateFixedSkybox(); |
| 0 | 31 | | } |
| | 32 | | else |
| | 33 | | { |
| 0 | 34 | | UpdateDynamicSkybox(); |
| | 35 | | } |
| | 36 | |
|
| | 37 | |
|
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | void UpdateFixedSkybox() |
| | 41 | | { |
| 0 | 42 | | if (fixedSkyboxBaked) |
| | 43 | | { |
| 0 | 44 | | return; |
| | 45 | | } |
| | 46 | |
|
| 0 | 47 | | fixedSkyboxTimer += Time.deltaTime; |
| | 48 | |
|
| 0 | 49 | | if (fixedSkyboxTimer >= fixedSkyboxUpdateTime) |
| | 50 | | { |
| 0 | 51 | | BakeNewReflection(); |
| 0 | 52 | | fixedSkyboxBaked = true; |
| | 53 | | } |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | void UpdateDynamicSkybox() |
| | 57 | | { |
| 0 | 58 | | timer += Time.deltaTime / updateAfter; |
| | 59 | |
|
| 0 | 60 | | if (timer >= 1) |
| | 61 | | { |
| 0 | 62 | | timer = 0; |
| 0 | 63 | | BakeNewReflection(); |
| | 64 | | } |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | private void LateUpdate() |
| | 68 | | { |
| 0 | 69 | | if (followTransform != null) |
| | 70 | | { |
| 0 | 71 | | transform.position = followTransform.position; |
| | 72 | | } |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | void BakeNewReflection() |
| | 76 | | { |
| 0 | 77 | | realtimeProbe.resolution = resolution; |
| 0 | 78 | | realtimeProbe.RenderProbe(); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public void UpdateResolution(int resolution) |
| | 82 | | { |
| 0 | 83 | | this.resolution = resolution; |
| 0 | 84 | | realtimeProbe.resolution = resolution; |
| | 85 | |
|
| 0 | 86 | | if (fixedSkybox) |
| | 87 | | { |
| 0 | 88 | | FixedSkyboxTimeChanged(); |
| 0 | 89 | | } |
| | 90 | | else |
| | 91 | | { |
| 0 | 92 | | timer = 0; |
| 0 | 93 | | BakeNewReflection(); |
| | 94 | | } |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void SkyboxModeChanged(bool dynamicSkybox) |
| | 98 | | { |
| 0 | 99 | | fixedSkybox = !dynamicSkybox; |
| | 100 | |
|
| 0 | 101 | | if (fixedSkybox) |
| | 102 | | { |
| 0 | 103 | | fixedSkyboxTimer = 0; |
| 0 | 104 | | fixedSkyboxBaked = false; |
| 0 | 105 | | } |
| | 106 | | else |
| | 107 | | { |
| 0 | 108 | | timer = 0; |
| 0 | 109 | | BakeNewReflection(); |
| | 110 | | } |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | public void FixedSkyboxTimeChanged() |
| | 114 | | { |
| 0 | 115 | | fixedSkyboxTimer = 0; |
| 0 | 116 | | fixedSkyboxBaked = false; |
| 0 | 117 | | } |
| | 118 | | } |
| | 119 | | } |