kemarofangs
Topic Author
Posts: 32
Joined: 13 Jun 2014, 21:30

Re: Particle Effects And Bitmap Pixel Manipulation

02 Mar 2017, 07:51

It's late but looking at the unity side involving texture2d I've come up with some code to test for tomorrow.
private UnityEngine.Texture2D m_texture2d = null;
        private Noesis.ImageBrush m_imageBrush = null;

        /// <summary>
        /// Important!
        /// Texture has to be set as read only in order to manipulate pixels.
        /// 
        /// ---------------
        /// Unity Inspector
        /// ---------------
        /// Texture Type: Advanced
        /// Read / Write Enabled: Yes
        /// 
        /// </summary>
        public void LoadGraphic()
        {
            m_texture2d =
                (UnityEngine.Texture2D)UnityEngine.Resources.Load("Fire.png");
            //ReadImageFromFile(@"D:\Work\Source Code\Unity\Marauders\Assets\Darren\Graphics\Raster\Texture\Fire.png");

            m_imageBrush = new Noesis.ImageBrush();
            m_imageBrush.ImageSource = (new Noesis.TextureSource(m_texture2d));
        }

        public void AlterPixels()
        {
            // TODO: Loop and change many pixels according to algorithm...
            UnityEngine.Color color = new UnityEngine.Color(1.0f, 0.5f, 0.0f, 1.0f);
            m_texture2d.SetPixel(0, 0, color);

            // After looping, then apply changes.
            m_texture2d.Apply();
        }
 
User avatar
jsantos
Site Admin
Posts: 4123
Joined: 20 Jan 2012, 17:18
Contact:

Re: Particle Effects And Bitmap Pixel Manipulation

03 Mar 2017, 06:12

This is going to work but if the texture is big and there are many pixel manipulations the performance is going to suffer because AlterPixels is uploading from CPU to GPU per frame.

The alternative would be doing that with a shader in Unity. But as said, is the texture is small this approach is probably going to be fine.
 
kemarofangs
Topic Author
Posts: 32
Joined: 13 Jun 2014, 21:30

Re: Particle Effects And Bitmap Pixel Manipulation

05 Mar 2017, 20:28

Thanks for the help. I have plenty of ideas and methods now at least.

The color mask solution works but uses around 15% cpu at the moment for a realistic looking fire effect. Once I integrate the other pieces of my project I'll check the performance again and move to a pixel shader approach if I see it is truly awful.

BTW, my current textures are 64px by 64px in a 256px texture atlas.
 
User avatar
jsantos
Site Admin
Posts: 4123
Joined: 20 Jan 2012, 17:18
Contact:

Re: Particle Effects And Bitmap Pixel Manipulation

07 Mar 2017, 07:01

Yes, that 15% if probably the CPU copying pixels and stalling the GPU. It is not optimal, but if it works for you...
 
kemarofangs
Topic Author
Posts: 32
Joined: 13 Jun 2014, 21:30

Re: Particle Effects And Bitmap Pixel Manipulation

07 Mar 2017, 19:13

Yeah that 15% is abysmal for only one small effect. I can't use the color mask approach.

So is this what I need to do...
1) Learn to make a pixel shader (HLSL files)
2) Assign said pixel shader file to a texture2d material.
3) Pass the texture2d to noesis via a RenderTexture.
 
User avatar
jsantos
Site Admin
Posts: 4123
Joined: 20 Jan 2012, 17:18
Contact:

Re: Particle Effects And Bitmap Pixel Manipulation

07 Mar 2017, 22:44

3) Pass the texture2d to noesis via a RenderTexture.
You can construct a Noesis.TextureSource using UnityEngine.Texture. Once you have it, you can set it to the source property of Noesis.Image. That way you can display the texture inside NoesisGUI.
 
kemarofangs
Topic Author
Posts: 32
Joined: 13 Jun 2014, 21:30

Re: Particle Effects And Bitmap Pixel Manipulation

23 Mar 2017, 20:55

Hey,

I've almost got a test case working but need some more help. I can assign a blank rendertexture to the material shader but do not know how to assign "frameworkElement" as the unprocessed starting image. Do I need to populate the renderTexture with the starting image (frameworkElement > {CODE MAGIC} > renderTexture now contains the pixel data)? I'd like to basically apply an effect (fragment shader) to an existing framework element. The shader code i'm currently testing with is a simple color inversion shader.

Unity CS Code
Noesis.Border border = m_frameworkElement.FindName("m_border_playerHead") as Noesis.Border;
        Noesis.FrameworkElement frameworkElement = border.Child as Noesis.FrameworkElement;
        //Darren.HelmetSilver frameworkElement = new Darren.HelmetSilver(); // Size is 30 x 30
        RenderTexture renderTexture = new RenderTexture(
            32,//(int)frameworkElement.Width,
            32, 0);//(int)frameworkElement.Height, 0);
        renderTexture.isPowerOfTwo = true;
        renderTexture.Create();
        Material material = new Material(shader_heatHaze);
        material.SetTexture("_MainTex", renderTexture);
        //material.SetFloat("Wobble", 0.7f);

        Noesis.TextureSource textureSource = new Noesis.TextureSource(renderTexture);
        Noesis.Image image = new Noesis.Image();
        //image.Margin = new Noesis.Thickness(-50, 0, 0, 0);
        image.Source = textureSource;
        image.Width = renderTexture.width;
        image.Height = renderTexture.height;
        m_player1.Grid.Children.Add(image);
        Noesis.Canvas.SetZIndex(image, int.MaxValue);
Image Effect Shader
Shader "Hidden/HeatHaze"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader
	{
		// No culling or depth
		Cull Off ZWrite Off ZTest Always

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
				o.uv = v.uv;
				return o;
			}
			
			sampler2D _MainTex;

			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 color = tex2D(_MainTex, i.uv);
				// just invert the colors
				color = 1 - color;
				return color;
			}

			/*
			!!! IMPORTANT !!!

			Unity GUI
			-----------------
			To make Shader.Find(string name) work perform the following
			Unity Menu > Edit > Project Settings > Graphics
			Under "Always Included Shaders" increase the "Size" by one.
			Assign "Element X" where X is "Size" minus one to the shader.
			Example: "Hidden/HeatHaze"
			
			CSharp Code Behind
			-----------------
			Shader shader_heatHaze = Shader.Find("Hidden/HeatHaze");
			*/

			// Data to pass into shader
			/*uniform float1 Timer;
			uniform float1 Refraction;
			uniform float1 Wobble;*/

			ENDCG
		}
	}
}
 
User avatar
jsantos
Site Admin
Posts: 4123
Joined: 20 Jan 2012, 17:18
Contact:

Re: Particle Effects And Bitmap Pixel Manipulation

23 Mar 2017, 21:18

Cool! Keep us posted.
 
kemarofangs
Topic Author
Posts: 32
Joined: 13 Jun 2014, 21:30

Re: Particle Effects And Bitmap Pixel Manipulation

23 Mar 2017, 22:48

Is there a way to get a RenderTexture from an existing FrameworkElement?

I can call this from unity to process the shader effect.
Graphics.Blit(renderTexture_source, renderTexture_destination, material);
I'd like to reference a FrameworkElement's texture at the current frame as the source RenderTexture for the shader effect then assign the destination RenderTexture to an image. I can already assign the destination rendertexture.

I suppose I'm asking for something like...
FrameworkElement fe;
RenderTexture rt = fe.ToRenderTexture();
I'd like to process a xaml graphic during a storyboard with the pixel shader. Is a reference to the texture in the graphics pipeline at this point even possible? Do I need to wait for the future "Effects" update in order to accomplish all this?

I'm so looking forward to "effects" (Drop Shadow, Etc..) being implemented in a future version of Noesis. I've already bought and read a book by Walt Ritscher (HLSL and Pixel Shaders for XAML Developers). It gave me the basics on creating and using them in WPF. Converting HLSL to Cg is very simple for use in Unity. That author even wrote a free WPF program to help write and test the shaders Shazzam. It lets you animate the values passed into the shader and even creates c# code with dependency properties for each shader. I'd highly recommend this for any WPF people. It may even come in handy when Noesis releases their version of "effects". Will Noesis use the HLSL language for this future update?
 
User avatar
jsantos
Site Admin
Posts: 4123
Joined: 20 Jan 2012, 17:18
Contact:

Re: Particle Effects And Bitmap Pixel Manipulation

07 Apr 2017, 21:44

Is there a way to get a RenderTexture from an existing FrameworkElement?
Have a look at the file NoesisXamlEditor.cs. There you will find code for creating previews and thumbnails that it is basically what I think you need. For example, the function RenderStaticPreview renders a view into a texture. Just set the root of the view to the desired FrameworkElement,

Who is online

Users browsing this forum: Bing [Bot], Killian and 2 guests