コード例 #1
0
        public void InternalBind(Entity entity, Main main, bool creating = false, Transform transform = null, bool dataOnly = false)
        {
            if (transform == null)
            {
                transform = entity.GetOrCreate <Transform>("Transform");
            }

            entity.CannotSuspend = false;

            Voxel map = entity.Get <Voxel>();

            // Apply the position and orientation components to the map
            if (main.EditorEnabled || map.Scale.Value != 1.0f)
            {
                map.Add(new TwoWayBinding <Matrix, Matrix>
                        (
                            transform.Matrix,
                            x => x * Matrix.CreateScale(1.0f / map.Scale),
                            new IProperty[] { map.Scale },
                            map.Transform,
                            x => Matrix.CreateScale(map.Scale) * x,
                            new IProperty[] { map.Scale }
                        ));
            }
            else
            {
                map.Add(new TwoWayBinding <Matrix>(transform.Matrix, map.Transform));
            }

            map.Add(new CommandBinding(map.CompletelyEmptied, delegate()
            {
                if (!main.EditorEnabled)
                {
                    entity.Delete.Execute();
                }
            }));

            Entity world = main.Get("World").FirstOrDefault();

            if (dataOnly && !main.EditorEnabled)
            {
                map.EnablePhysics.Value = false;
            }
            else
            {
                map.CreateModel = delegate(Vector3 min, Vector3 max, Voxel.State state)
                {
                    if (state.Invisible && !main.EditorEnabled)
                    {
                        return(null);
                    }

                    DynamicModel <Voxel.Vertex> model = new DynamicModel <Voxel.Vertex>(Voxel.Vertex.VertexDeclaration);
                    model.EffectFile.Value = "Effects\\Environment";
                    model.Lock             = new object();
                    state.ApplyTo(model);

                    if (state.Invisible)
                    {
                        model.Add(new Binding <bool>(model.Enabled, Editor.EditorModelsVisible));
                    }

                    /*
                     * ModelAlpha debug = new ModelAlpha { Serialize = false };
                     * debug.Alpha.Value = 0.01f;
                     * debug.DrawOrder.Value = 11; // In front of water
                     * debug.Color.Value = new Vector3(1.0f, 0.8f, 0.6f);
                     * debug.Filename.Value = "AlphaModels\\box";
                     * debug.CullBoundingBox.Value = false;
                     * debug.DisableCulling.Value = true;
                     * debug.Add(new Binding<Matrix>(debug.Transform, delegate()
                     * {
                     *      BoundingBox box = model.BoundingBox;
                     *      return Matrix.CreateScale(box.Max - box.Min) * Matrix.CreateTranslation((box.Max + box.Min) * 0.5f) * transform.Matrix;
                     * }, transform.Matrix, model.BoundingBox));
                     * result.Add(debug);
                     */

                    if (main.EditorEnabled || map.Scale.Value != 1.0f)
                    {
                        model.Add(new Binding <Matrix>(model.Transform, () => Matrix.CreateScale(map.Scale) * transform.Matrix, transform.Matrix, map.Scale));
                    }
                    else
                    {
                        model.Add(new Binding <Matrix>(model.Transform, transform.Matrix));
                    }

                    model.Add(new Binding <Vector3>(model.GetVector3Parameter("Offset"), map.Offset));

                    Voxel.State s = state;

                    model.Add(new Binding <Matrix>(model.GetMatrixParameter("UVScaleRotation"), delegate()
                    {
                        Matrix m    = Matrix.CreateRotationZ(map.UVRotation * (float)Math.PI / 180.0f);
                        float scale = 0.075f * s.Tiling;
                        m.M11      *= scale;
                        m.M12      *= scale;
                        m.M21      *= scale;
                        m.M22      *= scale;
                        return(m);
                    }, map.UVRotation));

                    model.Add(new Binding <Vector2>(model.GetVector2Parameter("UVOffset"), x => x * 0.075f * s.Tiling, map.UVOffset));

                    if (!s.ShadowCast)
                    {
                        model.UnsupportedTechniques.Add(Technique.Shadow);
                    }

                    entity.Add(model);

                    // We have to create this binding after adding the model to the entity
                    // Because when the model loads, it automatically calculates a bounding box for it.
                    model.Add(new Binding <BoundingBox, Vector3>(model.BoundingBox, x => new BoundingBox(min - x, max - x), map.Offset));
                    model.CullBoundingBox.Value = true;

                    return(model);
                };
            }

            this.SetMain(entity, main);
            map.Offset.Changed();
        }
コード例 #2
0
ファイル: MapFactory.cs プロジェクト: kernelbitch/Lemma
        public void InternalBind(Entity result, Main main, bool creating = false, Transform transform = null)
        {
            if (transform == null)
                transform = result.GetOrCreate<Transform>("Transform");

            result.CannotSuspend = false;

            Map map = result.Get<Map>();

            // Apply the position and orientation components to the map
            map.Add(new TwoWayBinding<Matrix>(transform.Matrix, map.Transform));

            map.Add(new CommandBinding(map.CompletelyEmptied, delegate()
            {
                if (!main.EditorEnabled)
                    result.Delete.Execute();
            }));

            Entity world = main.Get("World").FirstOrDefault();

            map.Chunks.ItemAdded += delegate(int index, Map.Chunk chunk)
            {
                Dictionary<int, bool> models = new Dictionary<int, bool>();

                Action<Map.CellState> createModel = delegate(Map.CellState state)
                {
                    if (state.ID == 0)
                        return; // 0 = empty

                    DynamicModel<Map.MapVertex> model = new DynamicModel<Map.MapVertex>(Map.MapVertex.VertexDeclaration);
                    model.EffectFile.Value = "Effects\\Environment";
                    model.Lock = map.Lock;
                    state.ApplyTo(model);

                    /*
                    ModelAlpha debug = new ModelAlpha { Serialize = false };
                    debug.Alpha.Value = 0.01f;
                    debug.DrawOrder.Value = 11; // In front of water
                    debug.Color.Value = new Vector3(1.0f, 0.8f, 0.6f);
                    debug.Filename.Value = "Models\\alpha-box";
                    debug.CullBoundingBox.Value = false;
                    debug.DisableCulling.Value = true;
                    debug.Add(new Binding<Matrix>(debug.Transform, delegate()
                    {
                        BoundingBox box = model.BoundingBox;
                        return Matrix.CreateScale(box.Max - box.Min) * Matrix.CreateTranslation((box.Max + box.Min) * 0.5f) * transform.Matrix;
                    }, transform.Matrix, model.BoundingBox));
                    result.Add(debug);
                    */

                    model.Add(new Binding<Matrix>(model.Transform, transform.Matrix));

                    Vector3 min = new Vector3(chunk.X, chunk.Y, chunk.Z);
                    Vector3 max = min + new Vector3(map.ChunkSize);

                    model.Add(new Binding<Vector3>(model.GetVector3Parameter("Offset"), map.Offset));

                    Map.CellState s = state;

                    if (!s.ShadowCast)
                        model.UnsupportedTechniques.Add(new[] { Technique.Shadow, Technique.PointLightShadow });

                    model.Add(new ListBinding<Map.MapVertex, Map.Box>
                    (
                        model.Vertices,
                        chunk.Boxes,
                        delegate(Map.Box box)
                        {
                            Map.MapVertex[] vertices = new Map.MapVertex[box.Surfaces.Where(x => x.HasArea).Count() * 4];
                            int i = 0;
                            foreach (Map.Surface surface in box.Surfaces)
                            {
                                if (surface.HasArea)
                                {
                                    Array.Copy(surface.Vertices, 0, vertices, i, 4);
                                    i += 4;
                                }
                            }
                            return vertices;
                        },
                        x => x.Type == s
                    ));

                    result.Add(model);

                    // We have to create this binding after adding the model to the entity
                    // Because when the model loads, it automatically calculates a bounding box for it.
                    model.Add(new Binding<BoundingBox, Vector3>(model.BoundingBox, x => new BoundingBox(min - x, max - x), map.Offset));

                    models[state.ID] = true;
                };

                chunk.Boxes.ItemAdded += delegate(int i, Map.Box box)
                {
                    if ((!box.Type.Invisible || main.EditorEnabled) && !models.ContainsKey(box.Type.ID))
                        createModel(box.Type);
                };

                chunk.Boxes.ItemChanged += delegate(int i, Map.Box oldBox, Map.Box newBox)
                {
                    if ((!newBox.Type.Invisible || main.EditorEnabled) && !models.ContainsKey(newBox.Type.ID))
                        createModel(newBox.Type);
                };
            };

            this.SetMain(result, main);
            map.Offset.Changed();
        }