public void Load(byte[] data)
        {
            // Create the reader:
            TextureReader reader = new TextureReader(data);

            // Magic number (ASCII 'LIM'):
            if (reader.ReadByte() != 'L' || reader.ReadByte() != 'I' || reader.ReadByte() != 'M')
            {
                throw new Exception("That's not a native Loonim 'lim' file.");
            }

            // Version and any flags (should just be 0):
            ulong vflags = reader.ReadCompressed();

            if (vflags != 0)
            {
                throw new Exception("Tried to load a newer Loonim file than this loader can handle.");
            }

            // Load properties:
            LoadProperties(reader);

            // Load the root:
            Root_ = reader.ReadTextureNode();
        }
 public Edges(TextureNode src, TextureNode radius, TextureNode str, TextureNode method) : base(4)
 {
     SourceModule = src;
     RadiusX      = radius;
     Strength     = str;
     Method       = method;
 }
Beispiel #3
0
 public Percentile(TextureNode src, TextureNode rx, TextureNode ry, TextureNode prc) : base(4)
 {
     SourceModule = src;
     RadiusX      = rx;
     RadiusY      = ry;
     Percent      = prc;
 }
 public ScaleInput(TextureNode _0, TextureNode _1) : base(4)
 {
     Sources[0] = _0;
     Sources[1] = _1;
     Sources[2] = new Property(1f);
     Sources[3] = new Property(1f);
 }
Beispiel #5
0
        /// <summary>Adds the given loader to the global set.</summary>
        private static void Add(Type type)
        {
            if (All == null)
            {
                All = new Dictionary <int, TextureNodeMeta>();
            }

            // Create an instance to pull TypeID and any other settings from:
            TextureNode node = Activator.CreateInstance(type) as TextureNode;

            int id = node.TypeID;

            if (id == -1)
            {
                return;
            }

            // Create the meta:
            TextureNodeMeta meta = new TextureNodeMeta();

            meta.Type = type;
            meta.ID   = id;
            meta.Name = type.Name;

            // Push to set:
            All[id] = meta;
        }
 public HueSatLum(TextureNode src, TextureNode hueChange, TextureNode satChange, TextureNode lumChange) : base(4)
 {
     Sources[0] = src;
     HueModule  = hueChange;
     SatModule  = satChange;
     LumModule  = lumChange;
 }
 public Refraction(TextureNode src, TextureNode height, TextureNode ratio, TextureNode normals) : base(4)
 {
     SourceModule = src;
     Height       = height;
     Ratio        = ratio;
     Normals      = normals;
 }
        public override double GetWrapped(double x, double y, int wrap)
        {
            // Get control (0-1):
            double control = ControlModule.GetWrapped(x, y, wrap);

            int count = Sources.Length - 1;

            if (count == 0)
            {
                return(control);
            }
            else if (count == 1)
            {
                return(Sources[1].GetWrapped(x, y, wrap));
            }

            // Reduce count (such that it ranges from 0->count-1):
            count--;

            // Map to being relative to the points:
            control *= (double)count;

            int index = (int)control;

            if (index < 0)
            {
                // Sample it:
                return(Sources[1].GetWrapped(x, y, wrap));
            }
            else if (index >= count)
            {
                // Sample there:
                return(Sources[count + 1].GetWrapped(x, y, wrap));
            }
            else
            {
                // Make relative:
                control -= (double)index;
            }

            // Control is now 0-1 relative to Sources[index+1].

            // Get current:
            TextureNode current = Sources[index + 1];

            if (control == 0)
            {
                // Just read it:
                return(current.GetWrapped(x, y, wrap));
            }

            // Note that index is never equal to (actual count-1), therefore next is just:
            TextureNode next = Sources[index + 2];

            // Blend:
            double a = current.GetWrapped(x, y, wrap);
            double b = next.GetWrapped(x, y, wrap);

            return(a + ((b - a) * control));
        }
 public ScaleInput(TextureNode _0, TextureNode _1, TextureNode _2, TextureNode _3) : base(4)
 {
     Sources[0] = _0;
     Sources[1] = _1;
     Sources[2] = _2;
     Sources[3] = _3;
 }
Beispiel #10
0
        /// <summary>Gets the node with the given ID.</summary>
        public TextureNode GetLayer(int id, int count)
        {
            if (id == count)
            {
                return(this);
            }

            if (Sources == null)
            {
                return(null);
            }

            for (int i = 0; i < Sources.Length; i++)
            {
                count++;
                TextureNode result = Sources[i].GetLayer(id, count);

                if (result != null)
                {
                    return(result);
                }
            }

            return(null);
        }
Beispiel #11
0
 public Blur(TextureNode src, TextureNode rx, TextureNode ry, TextureNode method) : base(4)
 {
     SourceModule = src;
     RadiusX      = rx;
     RadiusY      = ry;
     Method       = method;
 }
Beispiel #12
0
 public Blend(TextureNode src1, TextureNode src2, TextureNode weight, TextureNode mode) : base(4)
 {
     SourceModule1 = src1;
     SourceModule2 = src2;
     WeightModule  = weight;
     ModeModule    = mode;
 }
Beispiel #13
0
        /// <summary>Allocates GPU drawing meta now.</summary>
        public virtual void Draw(DrawInfo info)
        {
            if (Sources != null)
            {
                for (int i = 0; i < Sources.Length; i++)
                {
                    // Let the sources know we're the active target:
                    info.CurrentParent = this;
                    info.CurrentIndex  = i;

                    // Draw:
                    TextureNode src = Sources[i];

                    if (src != null)
                    {
                        src.Draw(info);
                    }
                }
            }

            if (DrawStore != null)
            {
                DrawStore.Draw(info);
            }
        }
 public AbsThreshold(TextureNode src1, TextureNode src2, TextureNode threshold, TextureNode gain) : base(4)
 {
     SourceModule1 = src1;
     SourceModule2 = src2;
     Threshold     = threshold;
     Gain          = gain;
 }
Beispiel #15
0
        public LiveStackNode(TextureNode node, Texture img)
        {
            Node = node;

            // Raw image (no buffer though):
            Image          = img;
            Image.wrapMode = TextureWrapMode.Repeat;
        }
Beispiel #16
0
        /// <summary>Adds a new source to this node.</summary>
        public void AddSource(TextureNode source)
        {
            if (Sources == null)
            {
                Sources = new TextureNode[] { source };
                return;
            }

            TextureNode[] newSet = new TextureNode[Sources.Length + 1];

            System.Array.Copy(Sources, 0, newSet, 0, Sources.Length);

            newSet[Sources.Length] = source;

            Sources = newSet;
        }
Beispiel #17
0
        public override DrawStackNode Allocate(DrawInfo info, SurfaceTexture tex, ref int stackID)
        {
            // Stack required.

            // Allocate a target stack now:
            int       targetStack = stackID;
            DrawStack stack       = tex.GetStack(targetStack, info);

            stackID++;

            int subID = SubMaterialID;

            // Create the material:
            Material material = GetMaterial(TypeID, subID);

            // _Data (Seed, Frequency, Amplitude, Jitter):
            material.SetVector(
                "_Data",
                DataVector
                );

            if (subID == 3)
            {
                // Minkowski number required.

                // Get the input node:
                TextureNode input = MinkowskiNumber;

                // Allocate it now (must not allocate targetStack in the direct kids):
                int           inputStacks = (targetStack == 0)?1:0;
                DrawStackNode drawNode    = input.Allocate(info, tex, ref inputStacks);

                // Apply it to our material:
                material.SetTexture("_Src0", drawNode.Texture);
            }

            // Create our node:
            MaterialStackNode matNode = new MaterialStackNode();

            DrawStore        = matNode;
            matNode.Mesh     = info.Mesh;
            matNode.Material = material;
            matNode.Stack    = stack;

            return(matNode);
        }
Beispiel #18
0
        public override DrawStackNode Allocate(DrawInfo info, SurfaceTexture tex, ref int stackID)
        {
            // Stack required.

            // Allocate a target stack now:
            int       targetStack = stackID;
            DrawStack stack       = tex.GetStack(targetStack, info);

            stackID++;

            // Allocate sources all to use the exact same stack for their output:

            for (int i = 0; i < Sources.Length; i++)
            {
                // Get the input node:
                TextureNode input = Sources[i];

                // Allocate it now, always allocating the same one as our target:
                int           inputStacks = targetStack;
                DrawStackNode dsn         = input.Allocate(info, tex, ref inputStacks);

                // If it's cleared, set cleared to false for anything but i=0:
                if (i != 0)
                {
                    dsn.Clear = false;
                }
            }

            // Create our node:
            StackerStackNode matNode = DrawStore as StackerStackNode;

            if (matNode == null)
            {
                matNode   = new StackerStackNode();
                DrawStore = matNode;
            }

            matNode.Stack = stack;

            return(matNode);
        }
Beispiel #19
0
        public TextureStackNode(TextureNode node, bool hdr, int x)
        {
            Node = node;

            // 0D (x==1) values potentially hold a 4 channel colour.
            // 1D (else) values will only ever hold a 1 channel colour.
            TextureFormat format;

            if (hdr)
            {
                format = (x == 1)?TextureFormat.RGBAFloat : TextureFormat.RFloat;
            }
            else
            {
                format = TextureFormat.ARGB32;
            }

            Image          = new Texture2D(x, 1, format, false);
            Image.wrapMode = TextureWrapMode.Repeat;
            Buffer         = new Color[x];
        }
Beispiel #20
0
        public override DrawStackNode Allocate(DrawInfo info, SurfaceTexture tex, ref int stackID)
        {
            // Stack required.

            // Allocate a target stack now:
            int       targetStack = stackID;
            DrawStack stack       = tex.GetStack(targetStack, info);

            stackID++;

            int subID = SubMaterialID;

            // Create the material:
            Material material = GetMaterial(TypeID, subID);

            // Allocate Src0:
            TextureNode   input       = Sources[0];
            int           inputStacks = (targetStack == 0)?1:0;
            DrawStackNode drawNode    = input.Allocate(info, tex, ref inputStacks);

            // Apply it to our material:
            material.SetTexture("_Src0", drawNode.Texture);

            // _Data (Rx,Ry,Rx,-Ry):
            material.SetVector(
                "_Data",
                GetDataVector(info)
                );

            // Create our node:
            MaterialStackNode matNode = new MaterialStackNode();

            DrawStore        = matNode;
            matNode.Mesh     = info.Mesh;
            matNode.Material = material;
            matNode.Stack    = stack;

            return(matNode);
        }
        public TextureNode ReadTextureNode()
        {
            // Get the type:
            int type = (int)ReadCompressed();

            if (type == 0)
            {
                return(null);
            }


            // Get the node meta:
            TextureNodeMeta meta = TextureNodes.Get(type);

            // Instance it:
            TextureNode instance = meta.GetInstance();

            // Load now:
            instance.Read(this);

            return(instance);
        }
Beispiel #22
0
        protected void AllocateSources(Material material, DrawInfo info, SurfaceTexture tex, int targetStack, int count)
        {
            int inputStacks = (targetStack == 0)?1:0;

            for (int i = 0; i < count; i++)
            {
                // Get the input node:
                TextureNode input = Sources[i];

                // Allocate it now (must not allocate targetStack in the direct kids):

                DrawStackNode drawNode = input.Allocate(info, tex, ref inputStacks);

                if (inputStacks == targetStack)
                {
                    // Skip:
                    inputStacks++;
                }

                // Apply it to our material:
                material.SetTexture("_Src" + i, drawNode.Texture);
            }
        }
Beispiel #23
0
 public Lookup(TextureNode gradient, TextureNode x) : base(3)
 {
     SourceModule = gradient;
     XModule      = x;
     YModule      = new Property(0f);
 }
Beispiel #24
0
 public Lookup(TextureNode gradient, TextureNode x, TextureNode y) : base(3)
 {
     SourceModule = gradient;
     XModule      = x;
     YModule      = y;
 }
Beispiel #25
0
 public Hue(TextureNode src, TextureNode hueChange)
 {
     Sources[0] = src;
     Sources[1] = hueChange;
 }
Beispiel #26
0
 public Sepia(TextureNode src)
 {
     SourceModule    = src;
     IntensityModule = new Property(1f);
 }
Beispiel #27
0
 public Sepia(TextureNode src, TextureNode intensity)
 {
     SourceModule    = src;
     IntensityModule = intensity;
 }
        public override UnityEngine.Color GetColour(double x, double y)
        {
            // Get control (0-1):
            double control = ControlModule.GetValue(x, y);

            int count = Sources.Length - 1;

            if (count == 0)
            {
                return(new UnityEngine.Color(0, 0, 0, 1));
            }
            else if (count == 1)
            {
                return(Sources[1].GetColour(x, y));
            }

            // Reduce count (such that it ranges from 0->count-1):
            count--;

            // Map to being relative to the points:
            control *= (double)count;

            int index = (int)control;

            if (index < 0)
            {
                // Sample it:
                return(Sources[1].GetColour(x, y));
            }
            else if (index >= count)
            {
                // Sample there:
                return(Sources[count + 1].GetColour(x, y));
            }
            else
            {
                // Make relative:
                control -= (double)index;
            }

            // Get current:
            TextureNode current = Sources[index + 1];

            if (control == 0)
            {
                // Just read it:
                return(current.GetColour(x, y));
            }

            // Control is now 0-1 relative to Sources[index+1]. Get as float:
            float blend = (float)control;

            // Note that index is never equal to (actual count-1), therefore next is just:
            TextureNode next = Sources[index + 2];

            // Blend:
            UnityEngine.Color col1 = current.GetColour(x, y);
            UnityEngine.Color col2 = next.GetColour(x, y);

            col1.r += (col1.r - col2.r) * blend;
            col1.g += (col1.g - col2.g) * blend;
            col1.b += (col1.b - col2.b) * blend;
            col1.a += (col1.a - col2.a) * blend;

            return(col1);
        }
        /// <summary>Builds a Loonim filter from the given set of one or more CSS functions.</summary>
        public static void BuildFilter(Css.Value value, RenderableData context, FilterEventDelegate fe)
        {
            if (value is Css.Functions.UrlFunction)
            {
                // - (A URL to an SVG filter) (not supported yet here)
                // - A URL to a Loonim filter (with a property called source0)

                // Load it now!
                DataPackage dp = new DataPackage(value.Text, context.Document.basepath);

                dp.onload = delegate(UIEvent e){
                    // Got the data! Load a filter now:
                    SurfaceTexture st = null;

                    // Create:
                    st = new SurfaceTexture();

                    // Load it:
                    st.Load(dp.responseBytes);

                    // Run the callback:
                    fe(context, st);
                };

                // Send:
                dp.send();
            }
            else
            {
                Loonim.TextureNode first = null;
                Loonim.TextureNode last  = null;

                // - One or more filter functions
                if (value is Css.CssFunction)
                {
                    // Just one:
                    first = last = value.ToLoonimNode(context);
                }
                else if (value is Css.ValueSet)
                {
                    // Many
                    for (int i = 0; i < value.Count; i++)
                    {
                        // Convert to a Loonim node:
                        Loonim.TextureNode next = value[i].ToLoonimNode(context);

                        if (next == null)
                        {
                            // Quit!
                            first = null;
                            last  = null;
                            break;
                        }

                        // Hook it on:
                        if (first == null)
                        {
                            first = last = next;
                        }
                        else
                        {
                            // Add it to the chain:
                            next.Sources[0] = last;
                            last            = next;
                        }
                    }
                }

                SurfaceTexture st = null;

                if (last != null)
                {
                    // Create the texture:
                    st = new SurfaceTexture();
                    st.Set("source0", (UnityEngine.Texture)null);
                    st.Root          = last;
                    first.Sources[0] = new Loonim.Property(st, "source0");
                }

                // Run the callback now!
                fe(context, st);
            }
        }
 public Multiply(TextureNode _0, TextureNode _1)
 {
     SourceModule1 = _0;
     SourceModule2 = _1;
 }