protected override bool OnFileLoaded(XMLAsset <AnimatedSprite> file) { ResetEditors(); _selectedAnimation = file.Content.Animations.First().Key; var assetFileLoaded = Engine.AssetLoader.Get <TextureAsset>(file.Content.AssetFile, false); _currentAssetTexture = assetFileLoaded?.Texture; _controller = new SpriteAnimationController(file.Content); _controller.SetAnimation(_selectedAnimation); GLThread.ExecuteGLThreadAsync(() => { if (_textureFb == null) { _textureFb = new FrameBuffer(_currentAssetTexture.Size).WithColor(); } else { _textureFb.Resize(_currentAssetTexture.Size); } }); // Patch legacy files foreach (KeyValuePair <string, SpriteAnimationData> anim in file.Content.Animations) { // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (anim.Value.FrameIndices == null) { anim.Value.FrameIndices = Array.Empty <int>(); } } return(true); }
public override void DetachedFromController(UIController controller) { if (_textureFb != null) { GLThread.ExecuteGLThreadAsync(_textureFb.Dispose); } base.DetachedFromController(controller); }
public SpriteArrayFrameSource(Texture t) { GLThread.ExecuteGLThreadAsync(() => { Frames = AutoDetectFrames(t); FrameOffsets = new Vector2[Frames.Length]; FrameOrigins = new OriginPosition[Frames.Length]; }); }
public override void Dispose() { if (RenderBufferPtr != 0) { GLThread.ExecuteGLThreadAsync(() => Gl.DeleteRenderbuffers(RenderBufferPtr)); } base.Dispose(); }
protected override void DisposeInternal() { base.DisposeInternal(); GLThread.ExecuteGLThreadAsync(() => { BaseTexture.Dispose(); foreach (KeyValuePair <Palette, Texture> p in PaletteSwaps) { p.Value.Dispose(); } }); }
/// <inheritdoc /> public override void Dispose() { // Clear CPU memory. Marshal.FreeHGlobal(_batchedVertices); // If owning graphics memory, clear that too. if (_createGl) { GLThread.ExecuteGLThreadAsync(() => { _vbo?.Dispose(); _vao?.Dispose(); }); } }
protected virtual void UploadTexture(Vector2 size, byte[] pixels, bool flipped, PixelFormat pixelFormat) { Texture = Texture.NonGLThreadInitialize(size); Texture.FlipY = flipped; GLThread.ExecuteGLThreadAsync(() => { PerfProfiler.ProfilerEventStart($"Uploading Image {Name}", "Loading"); Texture.NonGLThreadInitializedCreatePointer(Texture); Texture.Upload(size, pixels, pixelFormat, pixelFormat == PixelFormat.Red ? InternalFormat.Red : null); PerfProfiler.ProfilerEventEnd($"Uploading Image {Name}", "Loading"); #if DEBUG Texture.CreationStack = new string(' ', 3) + Name + new string(' ', 100) + "\n" + Texture.CreationStack; #endif }); }
private void CreateNewFromTexture(bool gridMode, AnimatedSprite data) { var explorer = new FileExplorer <TextureAsset>(f => { if (f.Texture == null) { return; } SpriteAnimationFrameSource source; if (gridMode) { source = new SpriteGridFrameSource(f.Texture.Size); } else { source = new SpriteArrayFrameSource(f.Texture); } data.AssetFile = f.Name; data.FrameSource = source; data.Animations = new Dictionary <string, SpriteAnimationData> { { "Default", new SpriteAnimationData("Default", 0) } }; _currentAssetTexture = f.Texture; _controller = new SpriteAnimationController(data); ResetEditors(); _selectedAnimation = "Default"; GLThread.ExecuteGLThreadAsync(() => { if (_textureFb == null) { _textureFb = new FrameBuffer(_currentAssetTexture.Size).WithColor(); } else { _textureFb.Resize(_currentAssetTexture.Size); } }); }); _toolsRoot.AddLegacyWindow(explorer); }
public void Dispose() { uint ptr = Pointer; Pointer = 0; if (Engine.Host == null) { return; } if (ptr == Bound) { Bound = 0; } GLThread.ExecuteGLThreadAsync(() => { Gl.DeleteVertexArrays(ptr); }); }
public void Dispose() { uint ptr = Pointer; Pointer = 0; if (Engine.Host == null) { return; } if (Bound[Type] == ptr) { Bound[Type] = 0; } GLThread.ExecuteGLThreadAsync(() => { Gl.DeleteBuffers(ptr); }); }
public Viewer3D() : base("3D Viewer") { DisplayObject = new Object3D(); _oldCamera = Engine.Renderer.Camera; Engine.Renderer.Camera = new Camera3D(new Vector3(20, 50, 200)); Engine.Renderer.Camera.LookAt = Vector3.Normalize(Vector3.Zero - Engine.Renderer.Camera.Position); _skeletalShader ??= Engine.AssetLoader.Get <ShaderAsset>("Shaders/SkeletalAnim.xml"); if (_boneVerticesStream == null) { GLThread.ExecuteGLThreadAsync(() => { _boneVerticesStream = new RenderStreamBatch <VertexDataWithBones>(0, 1, false); }); } _terrain = new Terrain3D(32, 32, 16); _windowFlags |= ImGuiWindowFlags.MenuBar; }
/// <summary> /// Destroy the shader, cleaning up used resources. /// </summary> public void Dispose() { uint ptr = Pointer; Pointer = 0; Valid = false; if (Engine.Host == null) { return; } if (Bound == ptr) { Bound = 0; } GLThread.ExecuteGLThreadAsync(() => { Gl.DeleteProgram(ptr); }); }
private void RenderAnimationPreview(RenderComposer c) { AnimatedSprite currentFileContext = _currentAsset !.Content !; SpriteAnimationFrameSource frameSource = currentFileContext.FrameSource; if (_animatedPreviewInvalidated) { var size = new Vector2(); for (var i = 0; i < frameSource.GetFrameCount(); i++) { Rectangle frameUV = frameSource.GetFrameUV(i); size.X = MathF.Max(size.X, frameUV.Width); size.Y = MathF.Max(size.Y, frameUV.Height); } if (size.X > size.Y) { size.Y = size.X; } else if (size.Y > size.X) { size.X = size.Y; } size *= 2; GLThread.ExecuteGLThreadAsync(() => { if (_animatedPreviewFb == null) { _animatedPreviewFb = new FrameBuffer(size).WithColor(); } else { _animatedPreviewFb.Resize(size, true); } }); _animatedPreviewInvalidated = false; } if (_animatedPreviewFb != null) { c.RenderToAndClear(_animatedPreviewFb); Vector2 size = _animatedPreviewFb.Size; c.RenderSprite(Vector3.Zero, size, new Color(32, 32, 32)); c.RenderLine(new Vector2(0, size.Y / 2), new Vector2(size.X, size.Y / 2), Color.White * 0.2f); c.RenderLine(new Vector2(size.X / 2, 0), new Vector2(size.X / 2, size.Y), Color.White * 0.2f); if (_animatedPreviewAnchorMode) { // Draw a shadow of the previous frame. if (_frameAnchor != 0) { _controller.GetRenderDataForFrame(_frameAnchor - 1, out Vector3 renderPosSh, out Texture textureSh, out Rectangle uvSh); renderPosSh = renderPosSh.RoundClosest(); c.RenderSprite((size / 2f).RoundClosest().ToVec3() + renderPosSh, uvSh.Size, Color.White * 0.3f, textureSh, uvSh); } _controller.GetRenderDataForFrame(_frameAnchor, out Vector3 renderPos, out Texture texture, out Rectangle uv); renderPos = renderPos.RoundClosest(); c.RenderSprite((size / 2f).RoundClosest().ToVec3() + renderPos, uv.Size, Color.White, texture, uv); } else { _controller.GetRenderData(out Vector3 renderPos, out Texture texture, out Rectangle uv); renderPos = renderPos.RoundClosest(); c.RenderSprite((size / 2f).RoundClosest().ToVec3() + renderPos, uv.Size, Color.White, texture, uv); } c.RenderTo(null); } }
public static MeshEntity EntityFromByteArray(ReadOnlyMemory <byte> byteArray) { var entity = new MeshEntity(); var memoryStream = new ReadOnlyMemoryStream(byteArray); var reader = new BinaryReader(memoryStream); char check = reader.ReadChar(); char check2 = reader.ReadChar(); char check3 = reader.ReadChar(); Debug.Assert(check == 'E' && check2 == 'M' && check3 == '3'); byte version = reader.ReadByte(); if (version != 1) { return(null); } entity.Name = reader.ReadString(); entity.Scale = reader.ReadSingle(); var materialMap = new Dictionary <string, MeshMaterial>(); var textureMap = new Dictionary <string, Texture>(); int meshesCount = reader.ReadInt32(); var meshArray = new Mesh[meshesCount]; entity.Meshes = meshArray; for (var i = 0; i < meshesCount; i++) { var newMesh = new Mesh { Name = reader.ReadString() }; string materialName = reader.ReadString(); if (materialMap.TryGetValue(materialName, out MeshMaterial mat)) { newMesh.Material = mat; } else { mat = new MeshMaterial { Name = materialName, DiffuseColor = new Color(reader.ReadUInt32()) }; bool hasDiffuseTexture = reader.ReadBoolean(); if (hasDiffuseTexture) { string textureName = reader.ReadString(); mat.DiffuseTextureName = textureName; if (textureMap.TryGetValue(textureName, out Texture diffuseTexture)) { mat.DiffuseTexture = diffuseTexture; } else { int textureByteLength = reader.ReadInt32(); float width = reader.ReadSingle(); float height = reader.ReadSingle(); int textureFormat = reader.ReadInt32(); Texture t = Texture.NonGLThreadInitialize(new Vector2(width, height)); mat.DiffuseTexture = t; byte[] data = reader.ReadBytes(textureByteLength); GLThread.ExecuteGLThreadAsync(() => { Texture.NonGLThreadInitializedCreatePointer(t); t.Upload(t.Size, data, (PixelFormat)textureFormat); }); textureMap.Add(textureName, t); } } newMesh.Material = mat; materialMap.Add(materialName, mat); } int vertexFormat = reader.ReadByte(); int length = reader.ReadInt32(); if (vertexFormat == 0) // Normal vertices { var vertices = new VertexData[length]; for (var j = 0; j < length; j++) { ref VertexData vert = ref vertices[j]; vert.Vertex = ReadVector3(reader); vert.UV = ReadVector2(reader); vert.Color = reader.ReadUInt32(); } newMesh.Vertices = vertices; }