/// <inheritdoc /> public SkullBlockEntity(World level, Block block, PooledTexture2D texture) : base(level, block) { ModelRenderer = new EntityModelRenderer(new SkullBlockEntityModel(), texture); Width = 8; Height = 8; }
public static PooledTexture2D BitmapToTexture2D(GraphicsDevice device, Image <Rgba32> image, out long byteSize) { var bmp = image;//.CloneAs<Rgba32>(); var pixels = bmp.GetPixelSpan(); var colorData = pixels.ToArray().Select(x => x.Rgba).ToArray(); PooledTexture2D texture = GpuResourceManager.GetTexture2D("Image converter", device, image.Width, image.Height); texture.SetData(colorData); byteSize = texture.MemoryUsage; return(texture); /*for (int x = 0; x < bmp.Width; x++) * { * for (int y = 0; y < bmp.Height; y++) * { * * } * } * using (MemoryStream ms = new MemoryStream()) * { * bmp.SaveAsPng(ms); * * ms.Position = 0; * byteSize = ms.Length; * return GpuResourceManager.GetTexture2D("Alex.Api.Utils.TextureUtils.ImageToTexture2D", device, ms); * }*/ }
public static bool TryGetSkin(Uri skinUri, GraphicsDevice graphics, out PooledTexture2D texture) { try { byte[] data; using (WebClient wc = new WebClient()) { data = wc.DownloadData(skinUri); } using (MemoryStream ms = new MemoryStream(data)) { texture = GpuResourceManager.GetTexture2D("SkinUtils", graphics, ms); } return(true); } catch (Exception ex) { Log.Warn(ex, $"Could not retrieve skin: {ex.ToString()}"); } texture = null; return(false); }
/// <inheritdoc /> public ChestBlockEntity(Block block, World level, PooledTexture2D texture) : base(level, block) { Width = 16; Height = 16; ModelRenderer = new EntityModelRenderer(new ChestEntityModel(), texture); }
internal void UpdateSkin(PooledTexture2D skinTexture) { //if (skinSlim) { var gotModel = ModelFactory.TryGetModel(GeometryName, out EntityModel m); ValidModel = gotModel; if (gotModel || ModelFactory.TryGetModel("geometry.humanoid.customSlim", out m)) { if (!gotModel) { } _model = m; ModelRenderer = new EntityModelRenderer(_model, skinTexture); UpdateModelParts(); } } /*else * { * if (ModelFactory.TryGetModel("geometry.humanoid.custom", * out EntityModel m)) * { * _model = m; * ModelRenderer = new EntityModelRenderer(_model, skinTexture); * UpdateModelParts(); * } * }*/ }
internal void UpdateSkin(PooledTexture2D skinTexture) { if (skinTexture == null) { skinTexture = TextureUtils.BitmapToTexture2D(Alex.Instance.GraphicsDevice, Alex.PlayerTexture); } //if (skinSlim) { var gotModel = ModelFactory.TryGetModel(GeometryName, out EntityModel m); ValidModel = gotModel; if (gotModel || ModelFactory.TryGetModel("geometry.humanoid.customSlim", out m)) { if (!gotModel) { } _model = m; ModelRenderer = new EntityModelRenderer(_model, skinTexture); //UpdateModelParts(); } } /*else * { * if (ModelFactory.TryGetModel("geometry.humanoid.custom", * out EntityModel m)) * { * _model = m; * ModelRenderer = new EntityModelRenderer(_model, skinTexture); * UpdateModelParts(); * } * }*/ }
private PooledTexture2D GetMipMappedTexture2D(GraphicsDevice device, Image <Rgba32> image) { //device.VertexSamplerStates. PooledTexture2D texture = GpuResourceManager.GetTexture2D(this, device, image.Width, image.Height, true, SurfaceFormat.Color); // Directory.CreateDirectory(name); // var resampler = new BicubicResampler(); // var encoder = new PngEncoder(); for (int level = 0; level < Alex.MipMapLevel; level++) { int mipWidth = (int)System.Math.Max(1, image.Width >> level); int mipHeight = (int)System.Math.Max(1, image.Height >> level); var bmp = image.CloneAs <Rgba32>(); //.CloneAs<Rgba32>(); bmp.Mutate(x => x.Resize(mipWidth, mipHeight, KnownResamplers.NearestNeighbor, true)); uint[] colorData; if (bmp.TryGetSinglePixelSpan(out var pixelSpan)) { colorData = pixelSpan.ToArray().Select(x => x.Rgba).ToArray(); } else { throw new Exception("Could not get image data!"); } //TODO: Resample per texture instead of whole texture map. texture.SetData(level, null, colorData, 0, colorData.Length); } return(texture); }
public PlayerMob(string name, World level, NetworkProvider network, PooledTexture2D skinTexture, string geometry = "geometry.humanoid.customSlim") : base(63, level, network) { //DoRotationCalculations = false; Name = name; Uuid = new UUID(Guid.NewGuid().ToByteArray()); Width = 0.6; Length = 0.6; Height = 1.80; IsSpawned = false; NameTag = name; //Skin = new Skin { Slim = false, SkinData = Encoding.Default.GetBytes(new string('Z', 8192)) }; // ItemInHand = new ItemAir(); HideNameTag = false; IsAlwaysShowName = true; ShowItemInHand = true; IsInWater = false; NoAi = true; //HealthManager.IsOnFire = false; Velocity = Vector3.Zero; PositionOffset = 1.62f; GeometryName = geometry; UpdateSkin(skinTexture); //Inventory = new Inventory(46); }
private static EntityModelRenderer TryGetRendererer(EntityData data, PooledTexture2D texture) { string lookupName = data.OriginalName; if (lookupName == "firework_rocket") { lookupName = "fireworks_rocket"; } if (_registeredRenderers.TryGetValue(lookupName, out var func)) { return(func(texture)); } else { var f = _registeredRenderers.Where(x => x.Key.Path.Length >= data.OriginalName.Length) .OrderBy(x => (x.Key.Path.Length - data.OriginalName.Length)).FirstOrDefault( x => x.Key.ToString().ToLowerInvariant().Contains(data.OriginalName.ToLowerInvariant())).Value; if (f != null) { return(f(texture)); } } return(null); }
internal void UpdateSkin(PooledTexture2D skinTexture) { if (skinTexture != null && ModelRenderer != null) { ModelRenderer.Texture = skinTexture; return; } string geometry = "geometry.humanoid.customSlim"; if (skinTexture == null) { string skinVariant = "entity/alex"; var uuid = UUID.GetBytes(); if ((uuid[3] ^ uuid[7] ^ uuid[11] ^ uuid[15]) % 2 == 0) { skinVariant = "entity/steve"; geometry = "geometry.humanoid.custom"; } if (Alex.Instance.Resources.ResourcePack.TryGetBitmap(skinVariant, out var rawTexture)) { skinTexture = TextureUtils.BitmapToTexture2D(Alex.Instance.GraphicsDevice, rawTexture); //skinBitmap = rawTexture; } else { skinTexture = TextureUtils.BitmapToTexture2D(Alex.Instance.GraphicsDevice, Alex.PlayerTexture); } } //if (skinSlim) { //var gotModel = ModelFactory.TryGetModel(GeometryName, // out EntityModel m); //ValidModel = gotModel; if (ModelFactory.TryGetModel(geometry, out var m)) { _model = m; ValidModel = true; ModelRenderer = new EntityModelRenderer(_model, skinTexture); //UpdateModelParts(); } } /*else * { * if (ModelFactory.TryGetModel("geometry.humanoid.custom", * out EntityModel m)) * { * _model = m; * ModelRenderer = new EntityModelRenderer(_model, skinTexture); * UpdateModelParts(); * } * }*/ }
internal void SetTexture(PooledTexture2D texture) { if (Effect != null) { Effect.Texture = texture; } Texture = texture; }
public static PooledTexture2D BitmapToTexture2D(GraphicsDevice device, Image <Rgba32> image, out long byteSize) { var bmp = image; //.CloneAs<Rgba32>(); uint[] colorData; if (bmp.TryGetSinglePixelSpan(out var pixelSpan)) { colorData = pixelSpan.ToArray().Select(x => x.Rgba).ToArray(); } else { throw new Exception("Could not get image data!"); } // var colorData = pixels.ToArray().Select(x => x.Rgba).ToArray(); PooledTexture2D result = null; if (Thread.CurrentThread != RenderThread) { AutoResetEvent resetEvent = new AutoResetEvent(false); QueueOnRenderThread( () => { result = GpuResourceManager.GetTexture2D("Image converter", device, image.Width, image.Height); result.SetData(colorData); resetEvent.Set(); }); resetEvent.WaitOne(); } else { result = GpuResourceManager.GetTexture2D("Image converter", device, image.Width, image.Height); result.SetData(colorData); } byteSize = result.MemoryUsage; return(result); /*for (int x = 0; x < bmp.Width; x++) * { * for (int y = 0; y < bmp.Height; y++) * { * * } * } * using (MemoryStream ms = new MemoryStream()) * { * bmp.SaveAsPng(ms); * * ms.Position = 0; * byteSize = ms.Length; * return GpuResourceManager.GetTexture2D("Alex.Api.Utils.TextureUtils.ImageToTexture2D", device, ms); * }*/ }
public static void LoadResources(GraphicsDevice graphicsDevice, McResourcePack resourcePack) { if (resourcePack.TryGetBitmap("minecraft:entity/chest/normal", out var bmp)) { ChestTexture = TextureUtils.BitmapToTexture2D(graphicsDevice, bmp); } else { Log.Warn($"Could not load chest texture."); } }
/// <inheritdoc /> public ChestBlockEntity(Block block, World level, PooledTexture2D texture) : base(level, block) { Width = 16; Height = 16; ModelRenderer = new EntityModelRenderer(new ChestModel(), texture); if (ModelRenderer.GetBone("head", out var head)) { HeadBone = head; } }
private void Cache(PooledTexture2D texture, EntityModel model, Dictionary <string, ModelBone> modelBones) { List <VertexPositionColorTexture> vertices = new List <VertexPositionColorTexture>(); //var modelTextureSize = model.Description != null ? // new Vector2(model.Description.TextureWidth, model.Description.TextureHeight) : // new Vector2(model.Texturewidth, model.Textureheight); var modelTextureSize = new Vector2(model.Description.TextureWidth, model.Description.TextureHeight); var actualTextureSize = new Vector2(texture.Width, texture.Height); if (modelTextureSize.X == 0 && modelTextureSize.Y == 0) { modelTextureSize = actualTextureSize; } var uvScale = actualTextureSize / modelTextureSize; // / actualTextureSize; foreach (var bone in model.Bones.Where(x => string.IsNullOrWhiteSpace(x.Parent))) { //if (bone.NeverRender) continue; if (modelBones.ContainsKey(bone.Name)) { continue; } var processed = ProcessBone(texture, model, bone, vertices, uvScale, actualTextureSize, modelBones); if (!modelBones.TryAdd(bone.Name, processed)) { Log.Warn($"Failed to add bone! {bone.Name}"); } } if (vertices.Count == 0) { //Log.Warn($"No vertices."); Valid = true; CanRender = false; return; } VertexBuffer = GpuResourceManager.GetBuffer(this, Alex.Instance.GraphicsDevice, VertexPositionColorTexture.VertexDeclaration, vertices.Count, BufferUsage.WriteOnly); VertexBuffer.SetData(vertices.ToArray()); Valid = true; _texture = texture; }
internal void SetTexture(PooledTexture2D texture) { if (_disposed) { return; } if (Effect != null) { Effect.Texture = texture; } Texture = texture; }
public static bool TryGetSkin(string json, GraphicsDevice graphics, out PooledTexture2D texture, out bool isSlim) { isSlim = false; try { TexturesResponse r = JsonConvert.DeserializeObject <TexturesResponse>(json); if (r != null) { string url = r.textures?.SKIN?.url; if (url != null) { byte[] data; using (WebClient wc = new WebClient()) { data = wc.DownloadData(url); } ManualResetEvent resetEvent = new ManualResetEvent(false); PooledTexture2D text = null; Alex.Instance.UIThreadQueue.Enqueue( () => { using (MemoryStream ms = new MemoryStream(data)) { text = GpuResourceManager.GetTexture2D( "SkinUtils", graphics, ms); // Texture2D.FromStream(graphics, ms); } resetEvent.Set(); }); resetEvent.WaitOne(); texture = text; isSlim = (r.textures.SKIN.metadata?.model == "slim"); return(true); } } } catch (Exception ex) { Log.Warn(ex, $"Could not retrieve skin: {ex.ToString()}"); } texture = null; return(false); }
private void BuildModel(PooledTexture2D texture, EntityModel model, Dictionary <string, ModelBone> modelBones) { List <VertexPositionColorTexture> vertices = new List <VertexPositionColorTexture>(); var actualTextureSize = new Vector2(texture.Width, texture.Height); int counter = 0; foreach (var bone in model.Bones.Where(x => string.IsNullOrWhiteSpace(x.Parent))) { //if (bone.NeverRender) continue; if (string.IsNullOrWhiteSpace(bone.Name)) { bone.Name = $"bone{counter++}"; } if (modelBones.ContainsKey(bone.Name)) { continue; } var processed = ProcessBone(model, bone, ref vertices, actualTextureSize, modelBones); if (!modelBones.TryAdd(bone.Name, processed)) { Log.Warn($"Failed to add bone! {bone.Name}"); } } if (vertices.Count == 0) { Valid = true; return; } VertexBuffer = GpuResourceManager.GetBuffer(this, Alex.Instance.GraphicsDevice, VertexPositionColorTexture.VertexDeclaration, vertices.Count, BufferUsage.WriteOnly); VertexBuffer.SetData(vertices.ToArray()); Effect = new AlphaTestEffect(Alex.Instance.GraphicsDevice); Effect.Texture = texture; Effect.VertexColorEnabled = true; Valid = true; _texture = texture; }
private static EntityModelRenderer TryGetRendererer(EntityData data, PooledTexture2D texture) { if (_registeredRenderers.TryGetValue(data.Name, out var func)) { return(func(texture)); } else { var f = _registeredRenderers.FirstOrDefault(x => x.Key.ToLowerInvariant().Contains(data.Name.ToLowerInvariant())).Value; if (f != null) { return(f(texture)); } } return(null); }
public EntityModelRenderer(EntityModel model, PooledTexture2D texture) { Model = model; // Model = model; if (texture == null) { Log.Warn($"No texture set for rendererer for {model}!"); return; } if (model != null) { var bones = new Dictionary <string, ModelBone>(); BuildModel(texture, model, bones); Bones = bones; } }
//public float Height { get; private set; } = 0f; public EntityModelRenderer(EntityModel model, PooledTexture2D texture) { // Model = model; if (texture == null) { Log.Warn($"No texture set for rendererer for {model.Name}!"); return; } if (model != null) { var bones = new Dictionary <string, ModelBone>(); Cache(texture, model, bones); Bones = bones; Valid = bones.Count > 0; } }
public static bool TryGetSkin(Uri skinUri, GraphicsDevice graphics, out PooledTexture2D texture) { if (!Program.IsRunningOnStartupThread()) { AutoResetEvent resetEvent = new AutoResetEvent(false); bool result = false; PooledTexture2D rTexture = null; Alex.Instance.UIThreadQueue.Enqueue( () => { result = TryGetSkin(skinUri, graphics, out rTexture); resetEvent.Set(); }); resetEvent.WaitOne(); texture = rTexture; return(result); } try { byte[] data; using (WebClient wc = new WebClient()) { data = wc.DownloadData(skinUri); } using (MemoryStream ms = new MemoryStream(data)) { texture = GpuResourceManager.GetTexture2D("SkinUtils", graphics, ms); } return(true); } catch (Exception ex) { Log.Warn(ex, $"Could not retrieve skin: {ex.ToString()}"); } texture = null; return(false); }
public EntityModelRenderer(MinecraftGeometry geometry, PooledTexture2D texture) { Texture = texture; var cubes = new Dictionary <string, ModelBone>(); var converted = geometry.Convert(); if (converted != null) { Cache(converted, cubes); } else { Cache(geometry, cubes); } Bones = cubes; }
public RemotePlayer(string name, World level, NetworkProvider network, PooledTexture2D skinTexture, string geometry = "geometry.humanoid.customSlim") : base(63, level, network) { _texture = skinTexture; SkinFlags = new PlayerSkinFlags() { Value = 0xff }; Name = name; Width = 0.6; //Length = 0.6; Height = 1.80; IsSpawned = false; NameTag = name; HideNameTag = false; IsAlwaysShowName = true; ShowItemInHand = true; IsInWater = false; //NoAi = true; Velocity = Vector3.Zero; PositionOffset = 1.62f; GeometryName = geometry; _skinDirty = true; //UpdateSkin(skinTexture); MovementSpeed = 0.1f; //0000000149011612f;//0000000149011612f; FlyingSpeed = 0.4f; // MovementSpeed = 0.1f; // FlyingSpeed = 0.4f; IsAffectedByGravity = false; HasPhysics = false; }
public static EntityModelRenderer GetEntityRenderer(string name, PooledTexture2D texture) { if (_registeredRenderers.TryGetValue(name, out var func)) { if (func != null) { return(func(texture)); } } else { var f = _registeredRenderers.FirstOrDefault(x => x.Key.ToString().ToLowerInvariant().Contains(name.ToLowerInvariant())).Value; if (f != null) { return(f(texture)); } } return(null); }
//public float Height { get; private set; } = 0f; public EntityModelRenderer(EntityModel model, PooledTexture2D texture) { // Model = model; Texture = texture; if (texture == null) { Log.Warn($"No texture set for rendererer for {model.Name}!"); return; } if (model != null) { var cubes = new Dictionary <string, ModelBone>(); Cache(model, cubes); Bones = cubes; Valid = true; } }
public SkinEntry(LoadedSkin skin, PooledTexture2D texture2D, Action <SkinEntry> onDoubleClick) { Skin = skin; OnDoubleClick = onDoubleClick; MinWidth = 92; MaxWidth = 92; MinHeight = 128; MaxHeight = 128; // AutoSizeMode = AutoSizeMode.GrowOnly; AddChild( new GuiTextElement() { Text = skin.Name, Margin = Thickness.Zero, Anchor = Alignment.TopCenter, Enabled = false }); Margin = new Thickness(0, 8); Anchor = Alignment.FillY; // AutoSizeMode = AutoSizeMode.GrowAndShrink; // BackgroundOverlay = new GuiTexture2D(GuiTextures.OptionsBackground); var mob = new RemotePlayer(skin.Name, null, null, texture2D); mob.ModelRenderer = new EntityModelRenderer(skin.Model, texture2D); ModelView = new GuiEntityModelView(mob) /*"geometry.humanoid.customSlim"*/ { BackgroundOverlay = new Color(Color.Black, 0.15f), Background = null, // Margin = new Thickness(15, 15, 5, 40), Width = 92, Height = 128, Anchor = Alignment.Fill, }; AddChild(ModelView); }
public static bool TryGetSkin(Uri skinUri, GraphicsDevice graphics, out PooledTexture2D texture) { try { byte[] data; using (WebClient wc = new WebClient()) { data = wc.DownloadData(skinUri); } ManualResetEvent resetEvent = new ManualResetEvent(false); PooledTexture2D text = null; Alex.Instance.UIThreadQueue.Enqueue( () => { using (MemoryStream ms = new MemoryStream(data)) { text = GpuResourceManager.GetTexture2D( "SkinUtils", graphics, ms); // Texture2D.FromStream(graphics, ms); } resetEvent.Set(); }); resetEvent.WaitOne(); texture = text; return(true); } catch (Exception ex) { Log.Warn(ex, $"Could not retrieve skin: {ex.ToString()}"); } texture = null; return(false); }
public static void LoadResources(GraphicsDevice graphicsDevice, McResourcePack resourcePack) { if (resourcePack.TryGetBitmap("minecraft:entity/chest/normal", out var bmp)) { ChestTexture = TextureUtils.BitmapToTexture2D(graphicsDevice, bmp); } else { Log.Warn($"Could not load chest texture."); } if (resourcePack.TryGetBitmap("minecraft:entity/chest/ender", out var enderBmp)) { EnderChestTexture = TextureUtils.BitmapToTexture2D(graphicsDevice, enderBmp); } else { Log.Warn($"Could not load enderchest texture"); } if (resourcePack.TryGetBitmap("minecraft:entity/steve", out var steveBmp)) { SkullTexture = TextureUtils.BitmapToTexture2D(graphicsDevice, steveBmp); } else { Log.Warn($"Could not load skull texture"); } if (resourcePack.TryGetBitmap("minecraft:entity/signs/oak", out var signBmp)) { SignTexture = TextureUtils.BitmapToTexture2D(graphicsDevice, signBmp); } else { Log.Warn($"Could not load sign texture"); } }
private void SetVertices() { var def = _entityDefinitions[_index]; EntityModelRenderer renderer = null; //if (def.Value != null) //{ // renderer = EntityFactory.GetEntityRenderer(def.Key, null); //} if (def.Value != null && def.Value.Geometry != null && def.Value.Geometry.ContainsKey("default") && def.Value.Textures != null) { EntityModel model; if (ModelFactory.TryGetModel(def.Value.Geometry["default"], out model) && model != null) { var textures = def.Value.Textures; string texture; if (!textures.TryGetValue("default", out texture)) { texture = textures.FirstOrDefault().Value; } if (Alex.Resources.BedrockResourcePack.Textures.TryGetValue(texture, out var bmp)) { PooledTexture2D t = TextureUtils.BitmapToTexture2D(Alex.GraphicsDevice, bmp.Value); renderer = new EntityModelRenderer(model, t); renderer.Scale = 1f / 16f; } } } _currentRenderer = renderer; }