Last active
January 20, 2019 12:46
-
-
Save unitycoder/963e7210d120c9435f1b022aa7386292 to your computer and use it in GitHub Desktop.
Accord.NET Framework with Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using UnityEngine; | |
using Accord.Imaging.Filters; | |
// More info: http://unitycoder.com/blog/2016/11/04/using-accord-net-with-unity/ | |
public class AccordFrameworkTest : MonoBehaviour | |
{ | |
public Renderer sourceRenderer; // our source texture is assigned on this gameobject | |
public Renderer targetRenderer; | |
void Start() | |
{ | |
// get maintexture from source renderer | |
var sourceTexture = (Texture2D)sourceRenderer.material.mainTexture; | |
// create system bitmap | |
System.Drawing.Bitmap accordImage = new System.Drawing.Bitmap(sourceTexture.width, sourceTexture.height); | |
// copy our texture pixels to that system bitmap | |
for (int x = 0; x < accordImage.Width; x++) | |
{ | |
for (int y = 0; y < accordImage.Height; y++) | |
{ | |
var c = (Color32)sourceTexture.GetPixel(x, y); | |
var nc = System.Drawing.Color.FromArgb(c.r, c.g, c.b, c.a); | |
accordImage.SetPixel(x, y, nc); | |
} | |
} | |
// These 2 lines are the only Accord.NET code used here, create filter and then apply it to bitmap | |
IFilter gaussianFilter = new GaussianBlur(2.0, 20); | |
var resultsAccordImage = gaussianFilter.Apply(accordImage); | |
// copy result pixels into our color32 array from system.drawing.bitmap | |
var colors = new Color32[resultsAccordImage.Width * resultsAccordImage.Height]; | |
for (int x = 0; x < resultsAccordImage.Width; x++) | |
{ | |
for (int y = 0; y < resultsAccordImage.Height; y++) | |
{ | |
var c = resultsAccordImage.GetPixel(x, y); | |
colors[y * resultsAccordImage.Width + x] = new Color32(c.A,c.R, c.G, c.B); // colors are flipped in System.Drawing.Color | |
} | |
} | |
// create new results texture from that color32 array | |
var targetTexture = new Texture2D(sourceTexture.width, sourceTexture.height, sourceTexture.format, false); | |
targetTexture.SetPixels32(colors); | |
targetTexture.Apply(false); | |
// assign it to another object | |
targetRenderer.material.mainTexture = targetTexture; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment