1#pragma warning disable 649
2using System.Collections;
3using System.Collections.Generic;
4using System.Diagnostics;
22 private const float MaxCameraDistanceRatio = 3f;
42 [SerializeField]
protected GameObject
Skybox;
47 [SerializeField]
private Renderer _skyboxRenderer;
52 [SerializeField]
private Light _light;
57 [SerializeField]
private Material _skyboxMaterialPreset;
62 [SerializeField]
private ReflectionProbe _reflectionProbe;
67 [SerializeField]
private Slider _skyboxExposureSlider;
82 private Vector2 _lightAngle =
new Vector2(0f, -45f);
92 private Material _skyboxMaterial;
97 private Texture2D _skyboxTexture;
102 private List<AnimationClip> _animations;
107 private Animation _animation;
109 private Stopwatch _stopwatch;
112 private AnimationState CurrentAnimationState
122 private bool AnimationIsPlaying => _animation !=
null && _animation.isPlaying;
127 _stopwatch =
new Stopwatch();
129 base.LoadModelFromFile();
136 var title =
"Select a skybox image";
137 var extensions =
new[]
149 if (_skyboxMaterial ==
null) _skyboxMaterial = Instantiate(_skyboxMaterialPreset);
150 _skyboxMaterial.mainTexture =
null;
151 _skyboxExposureSlider.value = 1f;
165 if (_animation ==
null)
return;
174 if (_animation ==
null)
return;
177 SampleAnimationAt(0f);
191 if (!AnimationIsPlaying)
193 var animationState = CurrentAnimationState;
194 if (animationState !=
null) SampleAnimationAt(value);
200 private void SampleAnimationAt(
float value)
204 animationClip.SampleAnimation(
RootGameObject, animationClip.length * value);
211 private void OnSkyboxStreamSelected(IList<ItemWithStream> files)
213 if (files !=
null && files.Count > 0 && files[0].HasData)
215#if (UNITY_WSA || UNITY_ANDROID) && !UNITY_EDITOR
216 Dispatcher.InvokeAsync(
new ContextualizedAction<Stream>(LoadSkybox, files[0].OpenStream()));
218 LoadSkybox(files[0].OpenStream());
223#if (UNITY_WSA || UNITY_ANDROID) && !UNITY_EDITOR
224 Dispatcher.InvokeAsync(
new ContextualizedAction(
ClearSkybox));
234 private IEnumerator DoLoadSkybox(Stream stream)
237 yield
return new WaitForEndOfFrame();
238 yield
return new WaitForEndOfFrame();
239 if (_skyboxTexture !=
null) Destroy(_skyboxTexture);
241 _skyboxTexture = HDRLoader.HDRLoader.Load(stream, out var gamma, out var exposure);
242 _skyboxMaterial.mainTexture = _skyboxTexture;
243 _skyboxExposureSlider.value = 1f;
251 private void LoadSkybox(Stream stream)
254 StartCoroutine(DoLoadSkybox(stream));
261 _skyboxMaterial.SetFloat(
"_Exposure", exposure);
262 _skyboxRenderer.material = _skyboxMaterial;
263 RenderSettings.skybox = _skyboxMaterial;
264 DynamicGI.UpdateEnvironment();
265 _reflectionProbe.RenderProbe();
278 private void Update()
287 ProcessInputInternal(Camera.main.transform);
294 private void ProcessInputInternal(Transform cameraTransform)
296 if (!EventSystem.current.IsPointerOverGameObject())
298 if (Input.GetMouseButton(0))
299 if (Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt))
301 _lightAngle.x = Mathf.Repeat(_lightAngle.x + Input.GetAxis(
"Mouse X"), 360f);
302 _lightAngle.y = Mathf.Clamp(_lightAngle.y + Input.GetAxis(
"Mouse Y"), -
MaxPitch,
MaxPitch);
309 if (Input.GetMouseButton(2))
332 private void UpdateHUD()
334 var animationState = CurrentAnimationState;
335 var time = animationState ==
null
337 :
PlaybackSlider.value * animationState.length % animationState.length;
338 var seconds = time % 60f;
339 var milliseconds = time * 100f % 100f;
341 var normalizedTime = animationState ==
null ? 0f : animationState.normalizedTime % 1f;
342 if (AnimationIsPlaying)
PlaybackSlider.value =
float.IsNaN(normalizedTime) ? 0f : normalizedTime;
343 var animationIsPlaying = AnimationIsPlaying;
344 if (_animation !=
null)
346 Play.gameObject.SetActive(!animationIsPlaying);
347 Stop.gameObject.SetActive(animationIsPlaying);
351 Play.gameObject.SetActive(
true);
352 Stop.gameObject.SetActive(
false);
360 base.OnBeginLoadModel(hasFiles);
361 if (hasFiles) _animations =
null;
369 protected override void OnLoad(AssetLoaderContext assetLoaderContext)
371 base.OnLoad(assetLoaderContext);
373 if (assetLoaderContext.RootGameObject !=
null)
376 if (assetLoaderContext.Options.AnimationType == AnimationType.Legacy)
378 _animation = assetLoaderContext.RootGameObject.GetComponent<Animation>();
379 if (_animation !=
null)
381 _animations = _animation.GetAllAnimationClips();
382 if (_animations.Count > 0)
384 for (var i = 0; i < _animations.Count; i++)
386 var animationClip = _animations[i];
430 protected override void OnError(IContextualizedError contextualizedError)
432 base.OnError(contextualizedError);
439 base.OnMaterialsLoad(assetLoaderContext);
441 Debug.Log(
"Loaded in:" + _stopwatch.Elapsed);
TriLibCore.AssetLoaderOptions AssetLoaderOptions
Represents a platform-specific file browser.
static void OpenFilePanelAsync(string title, string directory, string extension, bool multiselect, Action< IList< ItemWithStream > > cb)
Native open file dialog async
Represents a base class used in TriLib samples.
Text PlaybackTime
Animation playback time.
Selectable Play
Play button.
Vector2 CameraAngle
Current camera pitch and yaw angles.
Dropdown PlaybackAnimation
Animation selector.
void SetLoading(bool value)
Enables/disables the loading flag.
GameObject RootGameObject
Loaded game object.
Slider PlaybackSlider
Animation playback slider.
Selectable Stop
Stop button.
const float InputMultiplierRatio
Mouse input multiplier. Higher values will make the mouse movement more sensible.
void UpdateCamera()
Updates the Camera based on mouse Input.
const float MaxPitch
Maximum camera pitch and light pitch (rotation around local X-axis).
Represents a TriLib sample which allows the user to load models and HDR skyboxes from the local file-...
override void OnBeginLoadModel(bool hasFiles)
Event triggered when the user selects a file or cancels the Model selection dialog.
GameObject Skybox
Skybox game object.
const float MinCameraDistance
minimum camera distance.
override void Start()
Initializes the base-class and clears the skybox Texture.
virtual void ProcessInput()
Handles the input and moves the Camera accordingly.
override void PlaybackSliderChanged(float value)
Event triggered when the Animation slider value has been changed by the user.
void OnSkyboxExposureChanged(float exposure)
Event triggered when the skybox exposure Slider has changed.
override void StopAnimation()
Stop playing the selected animation.
float InputMultiplier
Input multiplier based on loaded model bounds.
override void OnLoad(AssetLoaderContext assetLoaderContext)
Event triggered when the Model Meshes and hierarchy are loaded.
float CameraDistance
Current camera distance.
override void PlayAnimation()
Plays the selected animation.
Vector3 CameraPivot
Current camera pivot position.
const float SkyboxScale
Skybox scale based on model bounds.
void LoadModelFromFile()
Shows the file picker for loading a model from the local file-system.
override void OnError(IContextualizedError contextualizedError)
Event is triggered when any error occurs.
virtual void ModelTransformChanged()
Changes the camera placement when the Model has changed.
void ClearSkybox()
Removes the skybox texture.
void LoadSkyboxFromFile()
Shows the file picker for loading a skybox from the local file-system.
const float CameraDistanceRatio
Camera distance ratio based on model bounds.
override void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
Event is triggered when the Model (including Textures and Materials) has been fully loaded.
override void PlaybackAnimationChanged(int index)
Switches to the animation selected on the Dropdown.
Represents a file picker extension filter.