public ProceduralTexture(int w, int h, string exp)
            : base(w, h)
        {
            this.expression = exp;

            if (texFunc == null)
            {
                var evaluator = new GenericExpressionEvaluator<RgbSpectrum>((i) =>
                {
                    if (i is float)
                    {
                        return new RgbSpectrum((float)i);
                    }
                    if (i is double)
                    {
                        return new RgbSpectrum((float)((double)i));
                    }
                    return (RgbSpectrum)i;
                },
                    RgbSpectrum.FromStringData);
                texFunc = evaluator.Compile(expression);
            }
        }
Exemple #2
0
        private static void TestExpressions()
        {
            /* Wood
             double	s = pow ( SineWave ( RingSpacing*sqrt ( x*x+y*y ) + TurbScale * Turbulence ( p, 3 ) ), Squeeze );
         	 t.Color = ( 1 - s ) * LightWood + s * DarkWood;
            string wood = "unit-$pow<$saw<3*sqrt<x*x+y*y*>+1.8*$t<c,3>>,5>";
             */
            string wood = "(unit-($saw<3*$sqrt<x*x+y*y*>>+$t<c,3>))*lw + ( ($saw<3*$sqrt<x*x+y*y*>>+$t<c,3>)*dw)";
            string expression = wood;
            //@"unit*$sqrt<x*x+y*y>";
            float x = 0.5f;
            float y = 0.7f;
            var p = new Vector(x, y, 2 - x - y);
            var p0 = new Vector(x, y, 0);
            var u = new RgbSpectrum(1f);

            var rnd = new FastRandom();
            var c = new RgbSpectrum(NoiseProvider.Instance.Noise3D(p));
            var evaluator = new GenericExpressionEvaluator<RgbSpectrum>((i) =>
                {
                    if (i is float)
                    {
                        return new RgbSpectrum((float)i);
                    }
                    if (i is double)
                    {
                        return new RgbSpectrum((float)((double)i));
                    }
                    if (i is int)
                    {
                        return new RgbSpectrum((int)i);
                    }
                    if (i is Int64)
                    {
                        return new RgbSpectrum((int)i);
                    }
                    return (RgbSpectrum)i;
                }, RgbSpectrum.FromStringData);

            var func = evaluator.Compile(expression);
            var f = rnd.NextDouble();
            Console.WriteLine("Script version: {0}", func(new
                {
                    e = f,
                    c,
                    u,
                    unit = RgbSpectrum.UnitSpectrum(),
                    lw = new RgbSpectrum(0.5f, 0.2f, 0.3f),
                    dw = new RgbSpectrum(0.3f, 0.1f, 0.1f),
                    x,
                    y,
                    arr = new[] { 0.5, 0.6, 0.6 },
                    n = c * 0.5f,
                }));

            Console.WriteLine("C# version : {0}", c * Math.Sqrt(x * x + y * y));

            return;
        }