HLSL Swizzle - C# 语言

发布于 2024-08-06 12:48:46 字数 414 浏览 8 评论 0原文

我正在寻找一种在 C# 中实现 HLSL 中的 swizzle 功能的方法,对于那些不熟悉这是什么的人来说 - 它用于轻松访问向量元素。

Vector4  v1, v2;
// (t,x,y,z) or (alpha,r,g,b)
v1 = new Vector4 (1,2,0,0);
v2 = new Vector4 (0,0,3,4);

// v1 (Green, Z) = v2 (Y, Blue)
v1.gz = v2.yb;
// Result : v1 = (1,2,3,4)

可以创建很多属性(每种可能的组合一个)。我感觉可以通过 Linq 来完成,但我对此并没有太多经验。

我不知道 XNA 是否有类似类型的东西,但我不想走这条路,因为这就是我使用它的全部目的,也就是说,如果它有的话。

谢谢。

I am looking for a way to implement the swizzle functionality found in HLSL in C#, for those of you unfamiliar with what this is - it is used for easy vector element access.

Vector4  v1, v2;
// (t,x,y,z) or (alpha,r,g,b)
v1 = new Vector4 (1,2,0,0);
v2 = new Vector4 (0,0,3,4);

// v1 (Green, Z) = v2 (Y, Blue)
v1.gz = v2.yb;
// Result : v1 = (1,2,3,4)

It would be possible to create a LOT of properties (one for each possible combination). I have a feeling it might be possible to do via Linq but I don't really have much experience with it.

I don't know if XNA has anything like the type, but I don't want to go down that path as this is all I would be using it for, that is, if it has it.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2024-08-13 12:48:46

在 C# 3.5 及更早版本中,最好的选择是简单地使用一大堆属性。

然而,在 C# 4.0 中,您可以使用动态类型和子类DynamicObject来获取您正在寻找的功能。这可能是也可能不是更好的选择,我对此功能了解不多。

编辑:

我对这个问题非常感兴趣,因此我去实现了一个 C# 4.0 解决方案。代码如下,但您也可以下载完整的解决方案(如果您更喜欢的话) [github]。与往常一样,您可以随心所欲地使用/破坏/制作此代码,如果它擦除了您的硬盘,请不要责怪我。

编辑3:
这是我在这里进行的最后一次编辑,但我可能会更多地使用它,并且我会为以后来到这里的任何人保持链接版本为最新。

编辑 2:

在让您了解代码之前,我们先提供一些示例,说明代码本身的 Program.Main() 中哪些内容可以使用,哪些内容无法使用。

现在,我们来看看代码。

namespace Swizzle
{
    /// <summary>
    /// This implements the Vector4 class as described in the question, based on our totally generic
    /// Vector class.
    /// </summary>
    class Vector4 : Vector<int>
    {
        public Vector4(int val0, int val1, int val2, int val3)
            : base(new Dictionary<char, int> { {'t', 0}, {'x', 1}, {'y', 2}, {'z', 3},
                                               {'a', 0}, {'r', 1}, {'g', 2}, {'b', 3}},
                   new int[] { val0, val1, val2, val3 })
        { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            dynamic v1, v2, v3;

            v1 = new Vector4(1, 2, 3, 4);

            v2 = v1.rgb;
            // Prints: v2.r: 2
            Console.WriteLine("v2.r: {0}", v2.r);
            // Prints: red: 2
            int red = v2.r;
            Console.WriteLine("red: {0}", red);
            // Prints: v2 has 3 elements.
            Console.WriteLine("v2 has {0} elements.", v2.Length);

            v3 = new Vector4(5, 6, 7, 8);
            v3.ar = v2.gb; // yes, the names are preserved! v3 = (3, 4, 7, 8)

            v2.r = 5;
            //v2.a = 5; // fails: v2 has no 'a' element, only 'r', 'g', and 'b'

            // Something fun that will also work
            Console.WriteLine("v3.gr: {0}", v3.gr);
            v3.rg = v3.gr; // switch green and red
            Console.WriteLine("v3.gr: {0}", v3.gr);

            Console.WriteLine("\r\nPress any key to continue.");
            Console.ReadKey(true);
        }
    }

    class Vector<T> : DynamicObject
    {
        private T[] m_values;
        private Dictionary<char, int> m_positions;

        public Vector(Dictionary<char, int> positions, params T[] values)
        {
            this.m_positions = positions;
            this.m_values = values;
        }

        public T this[int index] {
            get { return this.m_values[index]; }
        }

        public int Length
        {
            get { return this.m_values.Length; }
        }

        public override string ToString()
        {
            List<string> elements = new List<string>(this.Length);

            for (int i = 0; i < this.Length; i++)
            {
                elements.Add(m_values[i].ToString());
            }

            return string.Join(", ", elements.ToArray());
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (binder.Name == "Length") {
                result = this.Length;
                return true;
            }

            if (binder.Name.Length == 1 && this.m_positions.ContainsKey(binder.Name[0]))
            {
                result = m_values[this.m_positions[binder.Name[0]]];
                return true;
            }

            Dictionary<char, int> positions = new Dictionary<char, int>(binder.Name.Length);
            List<T> values = new List<T>(binder.Name.Length);
            int i = 0;
            foreach (char c in binder.Name)
            {
                if (!this.m_positions.ContainsKey(c))
                    return base.TryGetMember(binder, out result);

                values.Add(m_values[m_positions[c]]);
                positions.Add(c, i);

                i++;
            }

            result = new Vector<T>(positions, values.ToArray());
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            // sanity checking.
            foreach (char c in binder.Name)
            {
                if (!this.m_positions.ContainsKey(c))
                    return base.TrySetMember(binder, value);
            }

            Vector<T> vectorValue = value as Vector<T>;

            if (vectorValue == null && binder.Name.Length == 1 && value is T)
            {
                m_values[m_positions[binder.Name[0]]] = (T)value;
                return true;
            }
            else if (vectorValue == null)
                throw new ArgumentException("You may only set properties of a Vector to another Vector of the same type.");
            if (vectorValue.Length != binder.Name.Length)
                throw new ArgumentOutOfRangeException("The length of the Vector given does not match the length of the Vector to assign it to.");

            int i = 0;
            foreach (char c in binder.Name)
            {
                m_values[m_positions[c]] = vectorValue[i];
                i++;
            }

            return true;
        }
    }
}

In C# 3.5 and earlier, your best bet is to simply use a whole bunch of properties.

In C# 4.0 however, you can use dynamic typing and subclass DynamicObject to get the functionality you're looking for. This may or may not be a better option, I don't know alot about this functionality.

EDIT:

I was so intrigued by the question, that I went and implemented a C# 4.0 solution to this. The code follows, but you can also download a full solution if that's more your thing [github]. As always, you're allowed to use/break/make this code however you want, just don't blame me if it erases your harddrive.

EDIT 3:
This is the last edit I'll make here, but I'll probably play with this more, and I'll keep the linked version up to date for anyone who comes by here later.

EDIT 2:

Before I let you get to the code, there are examples of what will and won't work in Program.Main() in the code itself.

And now, on to the code.

namespace Swizzle
{
    /// <summary>
    /// This implements the Vector4 class as described in the question, based on our totally generic
    /// Vector class.
    /// </summary>
    class Vector4 : Vector<int>
    {
        public Vector4(int val0, int val1, int val2, int val3)
            : base(new Dictionary<char, int> { {'t', 0}, {'x', 1}, {'y', 2}, {'z', 3},
                                               {'a', 0}, {'r', 1}, {'g', 2}, {'b', 3}},
                   new int[] { val0, val1, val2, val3 })
        { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            dynamic v1, v2, v3;

            v1 = new Vector4(1, 2, 3, 4);

            v2 = v1.rgb;
            // Prints: v2.r: 2
            Console.WriteLine("v2.r: {0}", v2.r);
            // Prints: red: 2
            int red = v2.r;
            Console.WriteLine("red: {0}", red);
            // Prints: v2 has 3 elements.
            Console.WriteLine("v2 has {0} elements.", v2.Length);

            v3 = new Vector4(5, 6, 7, 8);
            v3.ar = v2.gb; // yes, the names are preserved! v3 = (3, 4, 7, 8)

            v2.r = 5;
            //v2.a = 5; // fails: v2 has no 'a' element, only 'r', 'g', and 'b'

            // Something fun that will also work
            Console.WriteLine("v3.gr: {0}", v3.gr);
            v3.rg = v3.gr; // switch green and red
            Console.WriteLine("v3.gr: {0}", v3.gr);

            Console.WriteLine("\r\nPress any key to continue.");
            Console.ReadKey(true);
        }
    }

    class Vector<T> : DynamicObject
    {
        private T[] m_values;
        private Dictionary<char, int> m_positions;

        public Vector(Dictionary<char, int> positions, params T[] values)
        {
            this.m_positions = positions;
            this.m_values = values;
        }

        public T this[int index] {
            get { return this.m_values[index]; }
        }

        public int Length
        {
            get { return this.m_values.Length; }
        }

        public override string ToString()
        {
            List<string> elements = new List<string>(this.Length);

            for (int i = 0; i < this.Length; i++)
            {
                elements.Add(m_values[i].ToString());
            }

            return string.Join(", ", elements.ToArray());
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (binder.Name == "Length") {
                result = this.Length;
                return true;
            }

            if (binder.Name.Length == 1 && this.m_positions.ContainsKey(binder.Name[0]))
            {
                result = m_values[this.m_positions[binder.Name[0]]];
                return true;
            }

            Dictionary<char, int> positions = new Dictionary<char, int>(binder.Name.Length);
            List<T> values = new List<T>(binder.Name.Length);
            int i = 0;
            foreach (char c in binder.Name)
            {
                if (!this.m_positions.ContainsKey(c))
                    return base.TryGetMember(binder, out result);

                values.Add(m_values[m_positions[c]]);
                positions.Add(c, i);

                i++;
            }

            result = new Vector<T>(positions, values.ToArray());
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            // sanity checking.
            foreach (char c in binder.Name)
            {
                if (!this.m_positions.ContainsKey(c))
                    return base.TrySetMember(binder, value);
            }

            Vector<T> vectorValue = value as Vector<T>;

            if (vectorValue == null && binder.Name.Length == 1 && value is T)
            {
                m_values[m_positions[binder.Name[0]]] = (T)value;
                return true;
            }
            else if (vectorValue == null)
                throw new ArgumentException("You may only set properties of a Vector to another Vector of the same type.");
            if (vectorValue.Length != binder.Name.Length)
                throw new ArgumentOutOfRangeException("The length of the Vector given does not match the length of the Vector to assign it to.");

            int i = 0;
            foreach (char c in binder.Name)
            {
                m_values[m_positions[c]] = vectorValue[i];
                i++;
            }

            return true;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文