User avatar
jsantos
Site Admin
Topic Author
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

RenderTexture

05 Feb 2014, 04:15

A sample showing how to integrate render to texture inside noesisGUI. The details are described at the end of the Image Tutorial.

Sample Source Code

After opening the Unity project, you would need to import NoesisGUI 1.2 (available from our forums: viewtopic.php?f=3&t=91) to make it work.



Older version compatible with NoesisGUI 1.1 can be downloaded here.
 
elusive52
Posts: 8
Joined: 05 May 2014, 19:31

Re: RenderTexture

15 Jan 2015, 19:52

Hi

is it possible to download the 'duck shoot' game scene in the demo video? with the full working GUi?

thanks.
 
kguner
Posts: 17
Joined: 07 Apr 2015, 18:38
Contact:

Re: RenderTexture

15 Jul 2015, 19:13

Hello,

I was trying to use the spaceboy example with the latest version of noesisgui and ran into compile errors.

I updated the Start() code in spaceboy.cs to the following
 
        // Find the rectangle where texture will be drawn
        var gui = GetComponent<NoesisGUIPanel>();
        var root = (Noesis.FrameworkElement)gui.GetContent();
        var rect = (Noesis.Rectangle)root.FindName("rtRect");

        // Create render texture
        UnityEngine.RenderTexture renderTexture = new RenderTexture(
            512, 512, 1, UnityEngine.RenderTextureFormat.RGB565);
        UnityEngine.RenderTexture.active = renderTexture;

        // Set render texture as camera target
        this._offscreenCamera.targetTexture = renderTexture;
        this._offscreenCamera.aspect = 1;
...
The compile time errors are gone but i get a runtime error: RenderTexture.Create failed: format unsupported.

Image
 
kguner
Posts: 17
Joined: 07 Apr 2015, 18:38
Contact:

Re: RenderTexture

16 Jul 2015, 01:52

I got this working by changing the following line:
UnityEngine.RenderTexture renderTexture = new RenderTexture(512, 512, 1, UnityEngine.RenderTextureFormat.Default);
This sample now works.

Here is my whole SpaceBoy.cs file.
using UnityEngine;
using System;

public class SpaceBoy : MonoBehaviour
{
    public UnityEngine.Camera _offscreenCamera;
    public UnityEngine.GameObject _spaceBoy;

    Noesis.Border _titleBar;
    Noesis.TranslateTransform _panelPosition;

    void Start()
    {
        // Find the rectangle where texture will be drawn
        var gui = GetComponent<NoesisGUIPanel>();
        var root = (Noesis.FrameworkElement)gui.GetContent();
        var rect = (Noesis.Rectangle)root.FindName("rtRect");

        
        // Create render texture
        UnityEngine.RenderTexture renderTexture = new RenderTexture(512, 512, 1, UnityEngine.RenderTextureFormat.Default);
        UnityEngine.RenderTexture.active = renderTexture;

        // Set render texture as camera target
        this._offscreenCamera.targetTexture = renderTexture;
        this._offscreenCamera.aspect = 1;

        // Create brush to store render texture
        var texBrush = new Noesis.ImageBrush();
        texBrush.ImageSource = (new Noesis.TextureSource(renderTexture));
        texBrush.Stretch = (Noesis.Stretch.UniformToFill);
        texBrush.Opacity = (0.9f);

        // Set the rectangle fill brush to use texture
        rect.Fill = (texBrush);

        // Title bar drag to move
        this._titleBar = (Noesis.Border)root.FindName("titleBar");
        this._titleBar.MouseLeftButtonDown += this.OnTitleBarMouseDown;
        this._titleBar.MouseLeftButtonUp += this.OnTitleBarMouseUp;
        this._titleBar.MouseMove += this.OnTitleBarMouseMove;

        var panel = (Noesis.Panel)root.FindName("panel");
        this._panelPosition = (Noesis.TranslateTransform)panel.RenderTransform;

        // Model rotation
        var rotateLeft = (Noesis.RepeatButton)root.FindName("rotateLeft");
        rotateLeft.Click += this.OnRotateLeft;

        var rotateRight = (Noesis.RepeatButton)root.FindName("rotateRight");
        rotateRight.Click += this.OnRotateRight;
    }

    Noesis.Point _lastPosition;

    void OnTitleBarMouseDown(object sender, Noesis.MouseButtonEventArgs e)
    {
        this._titleBar.CaptureMouse();
        this._lastPosition = new Noesis.Point(Input.mousePosition.x, - Input.mousePosition.y);
    }

    void OnTitleBarMouseUp(object sender, Noesis.MouseButtonEventArgs e)
    {
        this._titleBar.ReleaseMouseCapture();
    }

    void OnTitleBarMouseMove(object sender, Noesis.MouseEventArgs e)
    {
        if (this._titleBar.GetMouse().Captured == this._titleBar)
        {
            var pos = new Noesis.Point(Input.mousePosition.x, - Input.mousePosition.y);

            Noesis.Point delta = pos - this._lastPosition;
            this._lastPosition = pos;

            this._panelPosition.X = (this._panelPosition.X + delta.X);
            this._panelPosition.Y = (this._panelPosition.Y + delta.Y);
        }
    }

    void OnRotateLeft(object sender, Noesis.RoutedEventArgs e)
    {
        this._spaceBoy.transform.Rotate(Vector3.up, 2.0f);
    }

    void OnRotateRight(object sender, Noesis.RoutedEventArgs e)
    {
        this._spaceBoy.transform.Rotate(Vector3.up, -2.0f);
    }
}
 
User avatar
jsantos
Site Admin
Topic Author
Posts: 3905
Joined: 20 Jan 2012, 17:18
Contact:

Re: RenderTexture

16 Jul 2015, 18:40

I was going to inform you how to solve the RenderTarget issue but I see that you already solved it.

Thanks for the fix! We will update our sample and upload it to github.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: RenderTexture

16 Jul 2015, 22:22

Sample updated to NoesisGUI 1.2 and ready for Unity 5.1 is now available from GitHub ;)
 
kguner
Posts: 17
Joined: 07 Apr 2015, 18:38
Contact:

Re: RenderTexture

16 Jul 2015, 23:51

I looked at the github code. I see that you can pass null to MouseButtonEventArgs.GetPosition(). Useful to know, thanks!
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: RenderTexture

17 Jul 2015, 11:31

Passing null means that mouse position is relative to the root element (usually the screen).

Who is online

Users browsing this forum: No registered users and 11 guests