fehaar / FFWD

This is the FFWD framework for XNA, that allows you to port Unity3D games to XNA for use on WP7 or XBox360 (XBLIG)
Microsoft Public License
133 stars 36 forks source link

Texture implementations of GetPixel, GetPixels, SetPixel and SetPixels, plus helpers. #35

Open Elideb opened 12 years ago

Elideb commented 12 years ago

This fixes issue #27.

Testing code for different cases:

using UnityEngine;
using System.Collections;

public class PixelTests : MonoBehaviour
{
    private int frame = 0;
    private Texture2D texture;
    private Color[] pixels;

    void Start()
    {
        texture = (Texture2D)Instantiate(this.renderer.material.mainTexture);
        this.renderer.material.mainTexture = this.texture;

        pixels = texture.GetPixels(0, 0, texture.width, texture.height, 0);

        // Test SetPixel
        ////for (int i = 0; i < texture.width / 2; i++)
        ////{
        ////    for (int j = 0; j < texture.height / 2; j++)
        ////    {
        ////        texture.SetPixel(i + texture.width / 2, j + texture.height / 2, pixels[i + texture.width * j]);
        ////    }
        ////}

        ////texture.Apply(false, false);
    }

    // Update is called once per frame
    void Update ()
    {
        // GetPixel / SetPixel test
        // Shift the lower half of the cube once per frame
        ////for (int i = 0; i < texture.width; i++)
        ////{
        ////    for (int j = 0; j < texture.height / 2; j++)
        ////    {
        ////        texture.SetPixel(i + frame, j, pixels[i + texture.width * j]);
        ////    }
        ////}

        // Test SetPixels
        ////texture.SetPixels(0, texture.height / 2, texture.width, texture.height / 2, pixels);

        // Test SetPixel and GetPixel at runtime
        for (int i = texture.width - 1; i >= 0; i--)
        {
            for (int j = texture.height - 1; j >= 0; j--)
            {
                texture.SetPixel(i + 1, j + 1, texture.GetPixel(i, j));
            }
        }

        frame++;

        texture.Apply(false);
    }
}
Elideb commented 12 years ago

I have synced my fork to the latest version of FFWD and fixed the conflicts in Texture. Moved all GetPixel/SetPixel operations to Texture2D and switched to the byte[] approach.

One more test to verify things work. To fully check it, set "Generate Mipmaps" to True in the XNA texture's properties.

public void Start()
{
    texture = (Texture2D)Instantiate(this.renderer.material.mainTexture);
    this.renderer.material.mainTexture = this.texture;

    // duplicate the original texture and assign to the material
    // colors used to tint the first 3 mip levels
    var colors = new Color[3];
    colors[0] = Color.red;
    colors[1] = Color.green;
    colors[2] = Color.blue;
    var mipCount = Mathf.Min(3, texture.mipmapCount);

    // tint each mip level
    for (var mip = 0; mip < mipCount; ++mip)
    {
        var cols = texture.GetPixels(mip);
        for (var i = 0; i < cols.Length; ++i)
        {
            cols[i] = Color.Lerp(cols[i], colors[mip], 0.33f);
        }
        texture.SetPixels(cols, mip);
    }

    texture.Apply(false, false);
}
maconbot commented 12 years ago

I am getting a "the 'optional parameter' cannot be used it is not part of the 3.0 c# language specification" error. On Texture2D.cs

Total of 7 places...lines 67, 82, 127, 147, 166, 177 and 188.

Anytime something like: public void SetPixels(Color[] colors, int miplevel = 0) ...occurs

Apparently xbox and windows phone only use .NET 3.0, I am trying the suggestion linked below...just deleting the "= 0" http://forums.create.msdn.com/forums/p/89928/556025.aspx

This occurs when debugging the FFWD.Unity.Tests XNA project.

maconbot commented 12 years ago

i'll add this note to Elideb's fork...not sure if I was testing what you guys wanted tested...however I did notice the "optional parameter" still in the Texture2D.cs so it is worth noting. Just going through and deleting the "=0" made the error go away, not sure if it still functions as desired however. Materials and textures still seem to make it onto their meshes :-)

Elideb commented 12 years ago

I've learnt the lesson and created WP7 and X360 versions of my test project. I've tested the last commit and it works on WP7.

However, calling GetPixel and SetPixel each frame for each pixel in a 64x64 texture takes 6 seconds per frame in the WP7 emulator. Only calling SetPixels each frame does much better (16 ticks), which is to be expected.

maconbot commented 12 years ago

Awesome, I will test soon...hopefully whilst working on integrating Terrain, any thoughts here: https://github.com/maconbot/FFWD/issues/1

maconbot commented 12 years ago

After clearing out the UnityTests project and known errors...I compiled your XNA, the errors that were there are gone, now there is a bunch of warnings 14 or so.

And 3 errors: Error 1 Error loading pipeline assembly "C:\Users\Chad\Desktop\Elideb-FFWD-703b77d\Elideb-FFWD-703b77d\Tests\FFWD.Unity.Tests\FFWD.Unity.Tests.Scripts\bin\x86\Debug\FluentAssertions.dll". FFWD.Unity.Tests.Win Error 9 Error loading pipeline assembly "C:\Users\Chad\Desktop\Elideb-FFWD-703b77d\Elideb-FFWD-703b77d\Tests\FFWD.Unity.Tests\FFWD.Unity.Tests.Scripts\bin\x86\Debug\FluentAssertions.dll". FFWD.Unity.Tests.WP7 Error 17 Error loading pipeline assembly "C:\Users\Chad\Desktop\Elideb-FFWD-703b77d\Elideb-FFWD-703b77d\Tests\FFWD.Unity.Tests\FFWD.Unity.Tests.Scripts\bin\x86\Debug\FluentAssertions.dll". FFWD.Unity.Tests.X360

Warnings: Are just related to my machine so ignoring those.

Elideb commented 12 years ago

@maconbot I've integrated Fehaar's latest changes, which fix the compilation and dependencies of the tests. I've tested them on a fresh copy of my repo and everything seems to be fine. Please, can you check things are OK in your machine too?