示例#1
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            GeometryParameters param = new GeometryParameters(
                new Point3D(-50, -50, -200), new Point3D(20, 20, 1), TestVisibility, TestMaterial);

            geom = new MathGeometry(this, param);

            spriteFont = Content.Load <SpriteFont>("MathFont");
        }
        public MathGeometry(Game game, GeometryParameters parameters)
        {
            graphics        = game.GraphicsDevice;
            this.game       = game;
            this.parameters = parameters;
            effect          = game.Content.Load <Effect>("VoxelEffect");
            materials       = game.Content.Load <Texture2D>("materials");
            if (!init)
            {
                Init(game.GraphicsDevice);
            }

            List <VoxelVertex> voxels = new List <VoxelVertex>();

            for (int x = 0; x < parameters.Size.X; x++)
            {
                for (int y = 0; y < parameters.Size.Y; y++)
                {
                    for (int z = 0; z < parameters.Size.Y; z++)
                    {
                        Point3D p = new Point3D(x, y, z);
                        if (!parameters.Visible(p))
                        {
                            continue;
                        }

                        int material = parameters.MaterialID(p);

                        VoxelVertex v = new VoxelVertex()
                        {
                            Position   = p.ToVector3(),
                            MaterialID = material
                        };

                        voxels.Add(v);
                    }
                }
            }

            instanceBuffer = new VertexBuffer(graphics, typeof(VoxelVertex), voxels.Count, BufferUsage.WriteOnly);
            instanceBuffer.SetData(voxels.ToArray());
            numInstances = voxels.Count;
        }
 public void AddGeometry(GeometryParameters parameters)
 {
     geometries.Add(new MathGeometry(Game, parameters));
 }
        public EvalResult Evaluate(String input)
        {
            String prelude =
                "import clr\n" +
                "clr.AddReference('Mathcraft')\n" +
                "from Mathcraft import *\n" +
                "clr.AddReference('System.Core')\n" +
                "from System import Func\n";

            input = prelude + input;

            String suffix = "\n" +
                            "wrapper.visible_func = Func[Point3D,bool](visible)\n" +
                            "wrapper.material_func = Func[Point3D,int](material)\n";

            if (input.Contains("def visible") && input.Contains("def material"))
            {
                input += suffix;
            }

            EvalResult result = new EvalResult();

            Point3D size = new Point3D(20, 20, 20);
            Point3D pos  = new Point3D(-50, -50, -200);
            Func <Point3D, bool> visFunc = null;
            Func <Point3D, int>  matFunc = null;

            var scope = engine.CreateScope();

            scope.SetVariable("wrapper", wrapper);
            scope.SetVariable("pos", pos);
            scope.SetVariable("size", size);
            scope.SetVariable("visibility", visFunc);
            scope.SetVariable("material", matFunc);

            ScriptSource source   = engine.CreateScriptSourceFromString(input, SourceCodeKind.Statements);
            CompiledCode compiled = null;

            wrapper.material_func = null;
            wrapper.visible_func  = null;

            try
            {
                compiled = source.Compile();
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
                return(result);
            }

            // Executes in the scope of Python
            compiled.Execute(scope);

            bool gotPos  = scope.TryGetVariable <Point3D>("pos", out pos);
            bool gotSize = scope.TryGetVariable <Point3D>("size", out size);

            if (wrapper.visible_func != null && wrapper.material_func != null)
            {
                var param = new GeometryParameters(pos, size,
                                                   wrapper.visible_func, wrapper.material_func);

                result.GeometryParameters = param;

                var gm = game.Services.GetService(typeof(GeometryManager)) as GeometryManager;
                gm.Clear();

                try
                {
                    gm.AddGeometry(param);
                }
                catch (Exception ex)
                {
                    result.Message = ex.Message;
                    result.Success = false;
                }
            }

            result.Message = "OK";
            result.Success = true;

            return(result);
        }
        public MathGeometry(Game game, GeometryParameters parameters)
        {
            graphics = game.GraphicsDevice;
            this.game = game;
            this.parameters = parameters;
            effect = game.Content.Load<Effect>("VoxelEffect");
            materials = game.Content.Load<Texture2D>("materials");
            if (!init) Init(game.GraphicsDevice);

            List<VoxelVertex> voxels = new List<VoxelVertex>();

            for (int x = 0; x < parameters.Size.X; x++)
            {
                for (int y = 0; y < parameters.Size.Y; y++)
                {
                    for (int z = 0; z < parameters.Size.Y; z++)
                    {
                        Point3D p = new Point3D(x, y, z);
                        if (!parameters.Visible(p)) continue;

                        int material = parameters.MaterialID(p);

                        VoxelVertex v = new VoxelVertex()
                        {
                            Position = p.ToVector3(),
                            MaterialID = material
                        };

                        voxels.Add(v);
                    }
                }
            }

            instanceBuffer = new VertexBuffer(graphics, typeof(VoxelVertex), voxels.Count, BufferUsage.WriteOnly);
            instanceBuffer.SetData(voxels.ToArray());
            numInstances = voxels.Count;
        }