wyozi-gmod / imgui

Immediate mode 3D2D UI for Garry's Mod
MIT License
79 stars 21 forks source link

Insert image in 3D2D #27

Closed CodingLars closed 3 years ago

CodingLars commented 3 years ago

Is it possible to load and insert images with imgui? Or is there any other way for me to display an image in 3D2D?

wyozi commented 3 years ago

Yes. It supports all the normal surface methods so surface.SetMaterial + surface.DrawTexturedRect should be sufficient

CodingLars commented 3 years ago

Thanks for the quick response! I was looking on the internet for drawing images, didn't know it was materials/textures I was looking for. 😄 I know this might not be directly related with imgui, but why is it that the texturedrect is flickering all over the place? Viewed from one angle: image Viewed from another angle: image Vired from a third angle: image This is my code:

AddCSLuaFile("imgui.lua")
local imgui = include("imgui.lua")
local mat = Material("phoenix_storms/stripes")
function ENT:Draw()
  self:DrawModel()
  if imgui.Entity3D2D(self, Vector(-47, -71, 1.6), Angle(0, 90, 0), 0.1) then
    if (self.inRange) then -- self.inRange is true when the distance from the player to the 3D2D UI entity is less than 105 units
      local cardPosX = posX + 150 -- posX and posY is frome some other code that draws a box (i removed it here for simplicity)
      local cardPosY = posY - 200

      surface.SetDrawColor(255, 255, 255, 255)
      surface.SetMaterial(mat)
      surface.DrawTexturedRect(cardPosX, cardPosY - 200, 50, 50)
    end
  end
end

I am not sure what I am doing wrong :/ EDIT: This is not all of the code, I can send the whole ENT:Draw() hook I made, but it is kinda long.

wyozi commented 3 years ago

Generally if a texture flickers its due to the wrong shader. The shader for stripes (as you can tell from its .vmt file) is VertexLitGeneric, meaning it's usually used to texture 3d meshes in the world with proper normals and position in the world. 3d2d on the other hand is just 2d stuff projected to the world without any proper normals, hence rendering a flickering texture.

You can use CreateMaterial to create a new texture using UnlitGeneric shader with the stripes texture as the $basetexture and it shouldn't flicker anymore

CodingLars commented 3 years ago

Thank you very much for your help! I couldn't quite get the phoenix_storms/stripes material to work, no matter what I tried it was always either all white or invisible. (Probably because it only works on models) But, that doesn't matter. I just used that material as a test to see how to use surface.SetMaterial(). It works fine with any other texture I've tried. I am quite new to making addons (and LUA in general), but you helped me a lot. Thanks! 👍