0
\$\begingroup\$

I'm having a problem with GUI.DrawTexture() and I'm looking an alternative function to draw 2D Texture Images. The problem I have is that my textures draw above all other objects and when my camera moves, the images move with the camera, keeping their relative position inline with the cameras position.

using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using UnityEngine;

public class AnimatedGifDrawer : MonoBehaviour
{
public string loadingGifPath;
public float speed = 1;
public Vector2 drawPosition;

List<Texture2D> gifFrames = new List<Texture2D>();
void Awake()
{
    var gifImage = Image.FromFile(loadingGifPath);
    var dimension = new FrameDimension(gifImage.FrameDimensionsList[0]);
    int frameCount = gifImage.GetFrameCount(dimension);
    for (int i = 0; i < frameCount; i++)
    {
        gifImage.SelectActiveFrame(dimension, i);
        var frame = new Bitmap(gifImage.Width, gifImage.Height);
        System.Drawing.Graphics.FromImage(frame).DrawImage(gifImage, Point.Empty);
        var frameTexture = new Texture2D(frame.Width, frame.Height);
        for (int x = 0; x < frame.Width; x++)
            for (int y = 0; y < frame.Height; y++)
        {
            System.Drawing.Color sourceColor = frame.GetPixel(x, y);
            frameTexture.SetPixel(frame.Width - 1 - x, y, new Color32(sourceColor.R, sourceColor.G, sourceColor.B, sourceColor.A)); // for some reason, x is flipped
        }
        frameTexture.Apply();
        //frameTexture.LoadImage.
        gifFrames.Add(frameTexture);
    }
}

void OnGUI()
{
    GUI.DrawTexture(new Rect(drawPosition.x, drawPosition.y, gifFrames[0].width, -gifFrames[0].height), gifFrames[(int)(Time.frameCount * speed) % gifFrames.Count]);
}

}

The code above is designed to draw animated GIFs using System.Drawing.dll in Unity

\$\endgroup\$
1
\$\begingroup\$

You need to use camera faced billboards in order to solve this issue. Which means that you need :

  • To use a 3D quad to display those textures (which you construct by code)
  • To give this 3D quad a 3D position, and a 3D size
  • To orient this 3D quad towards camera at each frame, either by code or in its vertex shader

Example to construct a billboard here And to orient toward camera here (may be a bit outdated) General doc on 3D billboards here

\$\endgroup\$
  • \$\begingroup\$ I don't want the Images to stay inline with the camera at all, I wish them to stay in their current positions in world space even when the camera moves \$\endgroup\$ – Brendy Donnelly Aug 17 '15 at 12:04
  • \$\begingroup\$ When I say "orient this 3D quad towards camera" I am only speaking about orientation, not position, so the quads will stay in their current position in world space. However you need to rotate the quad in order to "face camera" otherwise you will sometimes see the quad "by the side", imagine what is happening for example if the camera would be rotating around such billboard ... \$\endgroup\$ – VB_overflow Aug 17 '15 at 12:19
  • \$\begingroup\$ these links and this billboard solution, does not tell me how to assign a texture, Ive spend the last hour trying to assign the texture to a mesh by creating the mesh and the vertices and the meshfilter() and meshrenderer() but its problematic, its only rendering one triangle of the two and it distorting the image, as well as adding a black backround to the gif images which contain a transparent background.. \$\endgroup\$ – Brendy Donnelly Aug 17 '15 at 14:28
  • \$\begingroup\$ id expect a function like DrawTexture() to exist for 2d graphics \$\endgroup\$ – Brendy Donnelly Aug 17 '15 at 14:30
  • 1
    \$\begingroup\$ You can use the OpenGL methods to draw directly to the screen, but having implemented such code, it is far, far easier to just create a new quad, make it face the camera, and apply a texture to it. \$\endgroup\$ – Draco18s Jan 14 '16 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.