private void OnEnable() { // Create backbuffer if (output == null) { output = RenderTarget.New(); } output.Init(PixelFormat.R8G8B8A8_UNorm, (int)resolution.X, (int)resolution.Y); // Create rendering task if (task == null) { task = RenderTask.Create <SceneRenderTask>(); } task.Order = -100; task.Camera = Cam; task.Output = output; task.Enabled = false; // Use dynamic material instance if (Material && material == null) { material = Material.CreateVirtualInstance(); } setMaterial = true; }
/// <inheritdoc /> public override void OnInit() { // Create cache folder if (!Directory.Exists(_cacheFolder)) { Directory.CreateDirectory(_cacheFolder); } // Find atlases in a Editor cache directory var files = Directory.GetFiles(_cacheFolder, "cache_*.flax", SearchOption.TopDirectoryOnly); int atlases = 0; for (int i = 0; i < files.Length; i++) { // Load asset var asset = FlaxEngine.Content.LoadAsync(files[i]); if (asset == null) { continue; } // Validate type if (asset is PreviewsCache atlas) { // Cache atlas atlases++; _cache.Add(atlas); } else { // Skip asset Editor.LogWarning(string.Format("Asset \'{0}\' is inside Editor\'s private directory for Assets Thumbnails Cache. Please move it.", asset.Path)); } } Editor.Log(string.Format("Previews cache count: {0} (capacity for {1} icons)", atlases, atlases * PreviewsCache.AssetIconsPerAtlas)); // Prepare at least one atlas if (_cache.Count == 0) { GetValidAtlas(); } // Create render task but disabled for now _output = RenderTarget.New(); _output.Init(PreviewsCache.AssetIconsAtlasFormat, PreviewsCache.AssetIconSize, PreviewsCache.AssetIconSize); _task = RenderTask.Create <CustomRenderTask>(); _task.Order = 50; // Render this task later _task.Enabled = false; _task.OnRender += OnRender; }
/// <summary> /// Initializes a new instance of the <see cref="AssetPreview"/> class. /// </summary> /// <param name="useWidgets">if set to <c>true</c> use widgets.</param> public AssetPreview(bool useWidgets) : base(RenderTask.Create <SceneRenderTask>(), new ArcBallCamera(Vector3.Zero, 50), useWidgets) { DockStyle = DockStyle.Fill; Task.Flags = ViewFlags.DefaulAssetPreview; Task.AllowGlobalCustomPostFx = false; ((ArcBallCamera)ViewportCamera).SetView(new Quaternion(0.424461186f, -0.0940724313f, 0.0443938486f, 0.899451137f)); // Setup preview scene PreviewLight = DirectionalLight.New(); PreviewLight.Brightness = 6.0f; PreviewLight.ShadowsMode = ShadowsCastingMode.None; PreviewLight.Orientation = Quaternion.Euler(new Vector3(52.1477f, -109.109f, -111.739f)); // EnvProbe = EnvironmentProbe.New(); EnvProbe.AutoUpdate = false; EnvProbe.CustomProbe = FlaxEngine.Content.LoadAsyncInternal <CubeTexture>(EditorAssets.DefaultSkyCubeTexture); // Sky = Sky.New(); Sky.SunLight = PreviewLight; Sky.SunPower = 8.0f; // SkyLight = SkyLight.New(); SkyLight.Mode = SkyLight.Modes.CustomTexture; SkyLight.Brightness = 2.0f; SkyLight.CustomTexture = EnvProbe.CustomProbe; // PostFxVolume = PostFxVolume.New(); PostFxVolume.IsBounded = false; PostFxVolume.Settings.Eye_MinLuminance = 0.1f; // Link actors for rendering Task.ActorsSource = ActorsSources.CustomActors; Task.CustomActors.Add(PreviewLight); Task.CustomActors.Add(EnvProbe); Task.CustomActors.Add(Sky); Task.CustomActors.Add(SkyLight); Task.CustomActors.Add(PostFxVolume); }
private void OnEnable() { // Create backbuffer if (_output == null) { _output = RenderTarget.New(); } _output.Init(PixelFormat.R8G8B8A8_UNorm, (int)_resolution.X, (int)_resolution.Y); // Create rendering task if (_task == null) { _task = RenderTask.Create <SceneRenderTask>(); } _task.Order = -100; _task.Camera = Cam; _task.Output = _output; _task.Enabled = false; if (Material && _material == null) { // Use dynamic material instance if (Material.WaitForLoaded()) { throw new Exception("Failed to load material."); } _material = Material.CreateVirtualInstance(); // Set render task output to draw on model _material.GetParam("Image").Value = _output; // Bind material to parent model if (Actor is StaticModel staticModel && staticModel.Model) { staticModel.Model.WaitForLoaded(); staticModel.Entries[0].Material = _material; } } _task.Enabled = true; }
/// <summary> /// Initializes a new instance of the <see cref="MainEditorGizmoViewport"/> class. /// </summary> /// <param name="editor">Editor instance.</param> public MainEditorGizmoViewport(Editor editor) : base(RenderTask.Create <SceneRenderTask>(), editor.Undo) { _editor = editor; // Prepare rendering task Task.ActorsSource = ActorsSources.ScenesAndCustomActors; Task.Flags = ViewFlags.DefaultEditor; Task.Begin += RenderTaskOnBegin; Task.Draw += RenderTaskOnDraw; Task.End += RenderTaskOnEnd; // Create post effects SelectionOutline = FlaxEngine.Object.New <SelectionOutline>(); SelectionOutline.SelectionGetter = () => _editor.SceneEditing.Selection; Task.CustomPostFx.Add(SelectionOutline); EditorPrimitives = FlaxEngine.Object.New <EditorPrimitives>(); EditorPrimitives.DrawDebugDraw = true; EditorPrimitives.Viewport = this; Task.CustomPostFx.Add(EditorPrimitives); // Add transformation gizmo TransformGizmo = new TransformGizmo(this); TransformGizmo.OnApplyTransformation += ApplyTransform; TransformGizmo.ModeChanged += OnGizmoModeChanged; TransformGizmo.Duplicate += Editor.Instance.SceneEditing.Duplicate; Gizmos.Active = TransformGizmo; // Add grid Grid = new GridGizmo(this); Grid.EnabledChanged += gizmo => _showGridButton.Icon = gizmo.Enabled ? Style.Current.CheckBoxTick : Sprite.Invalid; editor.SceneEditing.SelectionChanged += OnSelectionChanged; // Transform space widget var transformSpaceWidget = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperRight); var transformSpaceToggle = new ViewportWidgetButton(string.Empty, editor.Icons.World16, null, true) { Checked = TransformGizmo.ActiveTransformSpace == TransformGizmo.TransformSpace.World, TooltipText = "Gizmo transform space (world or local)", Parent = transformSpaceWidget }; transformSpaceToggle.OnToggle += OnTransformSpaceToggle; transformSpaceWidget.Parent = this; // Scale snapping widget var scaleSnappingWidget = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperRight); var enableScaleSnapping = new ViewportWidgetButton(string.Empty, editor.Icons.ScaleStep16, null, true) { Checked = TransformGizmo.ScaleSnapEnabled, TooltipText = "Enable scale snapping", Parent = scaleSnappingWidget }; enableScaleSnapping.OnToggle += OnScaleSnappingToggle; var scaleSnappingCM = new ContextMenu(); _scaleSnapping = new ViewportWidgetButton(TransformGizmo.ScaleSnapValue.ToString(), Sprite.Invalid, scaleSnappingCM); _scaleSnapping.TooltipText = "Scale snapping values"; for (int i = 0; i < EditorViewportScaleSnapValues.Length; i++) { var v = EditorViewportScaleSnapValues[i]; var button = scaleSnappingCM.AddButton(v.ToString()); button.Tag = v; } scaleSnappingCM.ButtonClicked += OnWidgetScaleSnapClick; scaleSnappingCM.VisibleChanged += OnWidgetScaleSnapShowHide; _scaleSnapping.Parent = scaleSnappingWidget; scaleSnappingWidget.Parent = this; // Rotation snapping widget var rotateSnappingWidget = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperRight); var enableRotateSnapping = new ViewportWidgetButton(string.Empty, editor.Icons.RotateStep16, null, true) { Checked = TransformGizmo.RotationSnapEnabled, TooltipText = "Enable rotation snapping", Parent = rotateSnappingWidget }; enableRotateSnapping.OnToggle += OnRotateSnappingToggle; var rotateSnappingCM = new ContextMenu(); _rotateSnapping = new ViewportWidgetButton(TransformGizmo.RotationSnapValue.ToString(), Sprite.Invalid, rotateSnappingCM); _rotateSnapping.TooltipText = "Rotation snapping values"; for (int i = 0; i < EditorViewportRotateSnapValues.Length; i++) { var v = EditorViewportRotateSnapValues[i]; var button = rotateSnappingCM.AddButton(v.ToString()); button.Tag = v; } rotateSnappingCM.ButtonClicked += OnWidgetRotateSnapClick; rotateSnappingCM.VisibleChanged += OnWidgetRotateSnapShowHide; _rotateSnapping.Parent = rotateSnappingWidget; rotateSnappingWidget.Parent = this; // Translation snapping widget var translateSnappingWidget = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperRight); var enableTranslateSnapping = new ViewportWidgetButton(string.Empty, editor.Icons.Grid16, null, true) { Checked = TransformGizmo.TranslationSnapEnable, TooltipText = "Enable position snapping", Parent = translateSnappingWidget }; enableTranslateSnapping.OnToggle += OnTranslateSnappingToggle; var translateSnappingCM = new ContextMenu(); _translateSnappng = new ViewportWidgetButton(TransformGizmo.TranslationSnapValue.ToString(), Sprite.Invalid, translateSnappingCM); _translateSnappng.TooltipText = "Position snapping values"; for (int i = 0; i < EditorViewportTranslateSnapValues.Length; i++) { var v = EditorViewportTranslateSnapValues[i]; var button = translateSnappingCM.AddButton(v.ToString()); button.Tag = v; } translateSnappingCM.ButtonClicked += OnWidgetTranslateSnapClick; translateSnappingCM.VisibleChanged += OnWidgetTranslateSnapShowHide; _translateSnappng.Parent = translateSnappingWidget; translateSnappingWidget.Parent = this; // Gizmo mode widget var gizmoMode = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperRight); _gizmoModeTranslate = new ViewportWidgetButton(string.Empty, editor.Icons.Translate16, null, true) { Tag = TransformGizmo.Mode.Translate, TooltipText = "Translate gizmo mode", Checked = true, Parent = gizmoMode }; _gizmoModeTranslate.OnToggle += OnGizmoModeToggle; _gizmoModeRotate = new ViewportWidgetButton(string.Empty, editor.Icons.Rotate16, null, true) { Tag = TransformGizmo.Mode.Rotate, TooltipText = "Rotate gizmo mode", Parent = gizmoMode }; _gizmoModeRotate.OnToggle += OnGizmoModeToggle; _gizmoModeScale = new ViewportWidgetButton(string.Empty, editor.Icons.Scale16, null, true) { Tag = TransformGizmo.Mode.Scale, TooltipText = "Scale gizmo mode", Parent = gizmoMode }; _gizmoModeScale.OnToggle += OnGizmoModeToggle; gizmoMode.Parent = this; // Show grid widget _showGridButton = ViewWidgetButtonMenu.AddButton("Show grid", () => Grid.Enabled = !Grid.Enabled); _showGridButton.Icon = Style.Current.CheckBoxTick; _showGridButton.IndexInParent = 1; // Create camera widget ViewWidgetButtonMenu.AddSeparator(); ViewWidgetButtonMenu.AddButton("Create camera here", CreateCameraAtView); DragHandlers.Add(_dragActorType); DragHandlers.Add(_dragAssets); }
/// <summary> /// Initializes the rendering stuff. /// </summary> private void InitRendering() { Debug.Log("[VR] rendering init begin"); // Get HMD display size uint w = 0; uint h = 0; Hmd.GetRecommendedRenderTargetSize(ref w, ref h); int width = (int)w; int height = (int)h; // Create RT for each eye //TODO: Combine two RTs to one with [w*2, h] leftEyeRenderTarget = RenderTarget.New(); leftEyeRenderTarget.Init(PixelFormat.R8G8B8A8_UNorm, width, height); rightEyeRenderTarget = RenderTarget.New(); rightEyeRenderTarget.Init(PixelFormat.R8G8B8A8_UNorm, width, height); // Create texture structs for OpenVR leftEyeTexture = new Texture_t { handle = leftEyeRenderTarget.NativePtr, eColorSpace = EColorSpace.Auto, eType = ETextureType.DirectX }; rightEyeTexture = new Texture_t { handle = rightEyeRenderTarget.NativePtr, eColorSpace = EColorSpace.Auto, eType = ETextureType.DirectX }; // Calculate bounds and FOV // bounds and FOV calculation could be replaced with uv [0,0] - [1,1] and custom projection matrix from eye.Projection float l_left = 0.0f, l_right = 0.0f, l_top = 0.0f, l_bottom = 0.0f; Hmd.GetProjectionRaw(EVREye.Eye_Left, ref l_left, ref l_right, ref l_top, ref l_bottom); float r_left = 0.0f, r_right = 0.0f, r_top = 0.0f, r_bottom = 0.0f; Hmd.GetProjectionRaw(EVREye.Eye_Right, ref r_left, ref r_right, ref r_top, ref r_bottom); var tanHalfFov = new Vector2( Mathf.Max(-l_left, l_right, -r_left, r_right), Mathf.Max(-l_top, l_bottom, -r_top, r_bottom)); leftEyeTextureBounds = new VRTextureBounds_t { uMin = 0.5f + 0.5f * l_left / tanHalfFov.X, uMax = 0.5f + 0.5f * l_right / tanHalfFov.X, vMin = 0.5f - 0.5f * l_bottom / tanHalfFov.Y, vMax = 0.5f - 0.5f * l_top / tanHalfFov.Y }; rightEyeTextureBounds = new VRTextureBounds_t { uMin = 0.5f + 0.5f * r_left / tanHalfFov.X, uMax = 0.5f + 0.5f * r_right / tanHalfFov.X, vMin = 0.5f - 0.5f * r_bottom / tanHalfFov.Y, vMax = 0.5f - 0.5f * r_top / tanHalfFov.Y }; FieldOfView = 2.0f * Mathf.Atan(tanHalfFov.Y) * Mathf.Rad2Deg; /////// // Create Eye textures EyeTextures = new VREyeTexture[2]; EyeTextures[0] = new VREyeTexture { Viewport = new Viewport(0, 0, width, height), RenderTarget = leftEyeRenderTarget }; EyeTextures[1] = new VREyeTexture { Viewport = new Viewport(0, 0, width, height), RenderTarget = rightEyeRenderTarget }; // HMDMirrorRenderTarget = leftEyeRenderTarget; // Create render tasks leftEyeRenderTask = RenderTask.Create <SceneRenderTask>(); // Camera leftEyeRenderTask.Output = leftEyeRenderTarget; leftEyeRenderTask.End += (task, ctx) => { Submit(EVREye.Eye_Left, ref leftEyeTexture, ref leftEyeTextureBounds); }; rightEyeRenderTask = RenderTask.Create <SceneRenderTask>(); // Camera rightEyeRenderTask.Output = rightEyeRenderTarget; rightEyeRenderTask.End += (task, ctx) => { Submit(EVREye.Eye_Right, ref rightEyeTexture, ref rightEyeTextureBounds); }; // Create eyes EyesProperties = new VREye[3]; for (int i = 0; i < EyesProperties.Length; i++) { VREye eye = new VREye(); if (i < EyeTextures.Length) { eye.Texture = EyeTextures[i]; } EyesProperties[i] = eye; } Debug.Log("[VR] rendering init end"); IsConnected = true; }