public Grid() { BoxCollider[] boxColliders = GameObject.FindObjectsOfType <BoxCollider>(); aabb = AABB.Default; AABB[] aabbs = new AABB[boxColliders.Length]; for (int i = 0; i < boxColliders.Length; i++) { aabbs[i] = boxColliders[i].box.OuterAABB(); aabb.Union(aabbs[i]); } //计算单位距离的体素数量 Vector3 delta = aabb.transformMax - aabb.transformMin; int dim = aabb.MaximumExtent(); float dimVoxel = 3 * Mathf.Pow((float)aabbs.Length, 1.0f / 3); float unitVoxel = dimVoxel / delta[dim]; int[] nVoxel = new int[3]; for (int i = 0; i < 3; i++) { nVoxel[i] = (int)(delta[i] * unitVoxel); nVoxel[i] = Mathf.Clamp(nVoxel[i], 1, 64); } voxels = new Voxel[nVoxel[0], nVoxel[1], nVoxel[2]]; for (int i = 0; i < 3; i++) { unitWidth[i] = delta[i] / voxels.GetLength(i); invUnitWidth[i] = unitWidth[i] <= float.Epsilon ? 0 : 1 / unitWidth[i]; } int[] voxelMin = new int[3]; int[] voxelMax = new int[3]; for (int i = 0; i < aabbs.Length; i++) { for (int j = 0; j < 3; j++) { voxelMin[j] = Point2Voxel(aabbs[i].transformMin, j); voxelMax[j] = Point2Voxel(aabbs[i].transformMax, j); } for (int x = voxelMin[0]; x <= voxelMax[0]; x++) { for (int y = voxelMin[1]; y <= voxelMax[1]; y++) { for (int z = voxelMin[2]; z <= voxelMax[2]; z++) { Voxel voxel = GetVoxel(x, y, z); voxel.Add(aabbs[i]); } } } } }
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(); }