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(); }