public VoxelCollision(VoxelGeometry voxel, Transform transform, BoundingBox bounds) { geometry = voxel; transformation = transform; boundsWorldSpaceCollision = bounds; boundsWorldSpaceCollision.Min = bounds.Min * 1.5f; boundsWorldSpaceCollision.Max = bounds.Max * 1.5f; }
public VoxelCollision(VoxelGeometry voxel, Transform transform, BoundingBox bounds, Scene scene) { geometry = voxel; transformation = transform; this.scene = scene; boundsWorldSpaceCollision = bounds; boundsWorldSpaceCollision.Min = bounds.Min * 1.5f; boundsWorldSpaceCollision.Max = bounds.Max * 1.5f; if (geometry.CanRender) { //GenerateCollisionMesh(); } }
void InitializeVoxels() { int voxelCountX = (DensityFieldWidth - 1) / VoxelGridSize; int voxelCountY = (DensityFieldHeight - 1) / VoxelGridSize; int voxelCountZ = (DensityFieldDepth - 1) / VoxelGridSize; Voxels = new VoxelGeometry[voxelCountX * voxelCountY * voxelCountZ]; VoxelBounds = new BoundingBox[Voxels.Length]; voxelKDTree = new KDTree<VoxelElement>(VoxelCompareFunction, VoxelBoundsFunction, false, true); Vector3 ratio = Vector3.One * 2.0f * (float)VoxelGridSize / new Vector3(DensityFieldWidth-1,DensityFieldHeight-1,DensityFieldDepth-1); for (int z = 0; z < voxelCountZ; z++) { int zOff = voxelCountX * voxelCountY * z; for (int y = 0; y < voxelCountY; y++) { int yOff = voxelCountX * y; for (int x = 0; x < voxelCountX; x++) { int idx = x + yOff + zOff; /* VoxelBounds[idx] = new BoundingBox(new Vector3(x, y, z) * ratio - Vector3.One, new Vector3(x + 1, y + 1, z + 1) * ratio - Vector3.One); VoxelBounds[idx].Min = Vector3.Transform(VoxelBounds[idx].Min, Transformation.GetTransform()); VoxelBounds[idx].Max = Vector3.Transform(VoxelBounds[idx].Max, Transformation.GetTransform()); */ Voxels[idx] = new VoxelGeometry((ushort)idx); Voxels[idx].renderElement.Transform = new Matrix[1] { Transformation.GetTransform() }; Vector3 geometryRatio = 2.0f*Vector3.One / new Vector3(DensityFieldWidth-1,DensityFieldHeight-1,DensityFieldDepth-1); Voxels[idx].GenerateGeometry(ref DensityField, IsoValue, DensityFieldWidth, DensityFieldHeight, DensityFieldDepth, VoxelGridSize, VoxelGridSize, VoxelGridSize, x * VoxelGridSize, y * VoxelGridSize, z * VoxelGridSize, geometryRatio, this.Transformation.GetTransform()); VoxelBounds[idx] = MathUtils.TransformBounds(Voxels[idx].GetBounds(), Transformation.GetTransform()); if(Voxels[idx].CanRender) voxelKDTree.AddElement(new VoxelElement(Voxels[idx], VoxelBounds[idx]), false); } } } voxelKDTree.BuildTree(); RecursivelyBuildBounds(voxelKDTree.GetRoot()); terrainQuadMaterial = ResourceManager.Inst.GetMaterial("TerrainQuadMaterial"); giantQuadElement = GFXPrimitives.Decal.GetRenderElement(); Matrix quadMatrix = Matrix.CreateScale(1.5f) * Matrix.CreateRotationX(MathHelper.Pi) * this.Transformation.GetTransform(); quadMatrix.Translation = Vector3.Up * this.Transformation.GetBounds().Min.Y; giantQuadElement.Transform = new Matrix[1] { quadMatrix }; }
void GenerateCollisionMesh(VoxelGeometry geometry) { List<Vector3> vertices = new List<Vector3>(geometry.verts.Length); List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>(geometry.ib.Length / 3); Matrix transform = this.Transformation.GetTransform(); for (int i = 0; i < geometry.verts.Length; i++) vertices.Add(Vector3.Transform(geometry.verts[i].Position, transform)); for (int i = 0; i < geometry.ib.Length; i += 3) { TriangleVertexIndices tri = new TriangleVertexIndices(geometry.ib[i + 2], geometry.ib[i + 1], geometry.ib[i]); indices.Add(tri); } TriangleMesh collisionMesh = new TriangleMesh(vertices, indices); CollisionSkin collision = new CollisionSkin(null); collision.AddPrimitive(collisionMesh, (int)MaterialTable.MaterialID.NotBouncyRough); CollisionSkin collision2 = new CollisionSkin(null); collision2.AddPrimitive(new JigLibX.Geometry.Plane(Vector3.Up, Transformation.GetBounds().Min.Y-3f), (int)MaterialTable.MaterialID.NotBouncyRough); scene.GetPhysicsEngine().CollisionSystem.AddCollisionSkin(collision); //scene.GetPhysicsEngine().CollisionSystem.AddCollisionSkin(collision2); }
static int SceneCompareFunction(VoxelGeometry elementA, VoxelGeometry elementB, int axis) { BoundingBox boundsA = elementA.GetBounds();//.Transform.TransformBounds(elementA.Mesh.GetBounds()); Vector3 posA = (boundsA.Max + boundsA.Min) * 0.5f;// elementA.Transform.GetPosition(); float valueA = (axis == 0) ? posA.X : (axis == 1) ? posA.Y : posA.Z; BoundingBox boundsB = elementB.GetBounds();//.Transform.TransformBounds(elementB.Mesh.GetBounds()); Vector3 posB = (boundsB.Max + boundsB.Min) * 0.5f;//elementB.Transform.GetPosition(); float valueB = (axis == 0) ? posB.X : (axis == 1) ? posB.Y : posB.Z; if (valueA < valueB) return -1; if (valueA > valueB) return 1; return 0; }
static Vector2 SceneBoundsFunction(VoxelGeometry element, int axis) { BoundingBox bounds = element.GetBounds();//.Transform.TransformBounds(element.Mesh.GetBounds()); return (axis == 0) ? new Vector2(bounds.Min.X, bounds.Max.X) : ((axis == 1) ? new Vector2(bounds.Min.Y, bounds.Max.Y) : new Vector2(bounds.Min.Z, bounds.Max.Z)); }
public VoxelElement(VoxelGeometry geometry, BoundingBox bounds) { this.geometry = geometry; this.bounds = bounds; }
void generateGem(Vector3 position) { position.Y = position.Y + 2.0f; float radiusMax = (DensityFieldSize / 2); float radiusMin = (DensityFieldSize / 16); Vector3 cylinderCenter = Vector3.One * DensityFieldSize * 0.5f; minPos = maxPos = cylinderCenter; DensityField = new byte[DensityFieldSize * DensityFieldSize * DensityFieldSize]; for (int x = 0; x < DensityFieldSize; x++) { for (int y = 1; y < (DensityFieldSize - 1); y++) { for (int z = 0; z < DensityFieldSize; z++) { Vector3 pos = new Vector3(x, y, z); float offset = Math.Abs(pos.Y - cylinderCenter.Y); pos.Y = cylinderCenter.Y; float radius = MathHelper.Lerp(radiusMax, radiusMin, offset/cylinderCenter.Y); float density = Math.Max(1.0f - (pos - cylinderCenter).Length() / (radius), 0.0f); if (density > 0.0f) { pos = (pos / DensityFieldSize) * 2.0f - Vector3.One; minPos = Vector3.Min(pos, minPos); maxPos = Vector3.Max(pos, maxPos); } DensityField[x + (y + z * DensityFieldSize) * DensityFieldSize] = (byte)(density * 255.0f); } } } gemGeometry = new VoxelGeometry(); gemGeometry.renderElement.Transform = new Matrix[1] { Matrix.CreateScale(5.0f) * Matrix.CreateTranslation(position) }; gemGeometry.GenerateGeometry(ref DensityField, IsoValue, DensityFieldSize, DensityFieldSize, DensityFieldSize, DensityFieldSize - 1, DensityFieldSize - 1, DensityFieldSize - 1, 0, 0, 0, 2.0f / (float)(DensityFieldSize - 1)); boundingBox = new BoundingBox(); boundingBox.Max = Vector3.Transform(maxPos, gemGeometry.renderElement.Transform[0]); boundingBox.Min = Vector3.Transform(minPos, gemGeometry.renderElement.Transform[0]); collected = false; Vector3 lightPosition = position; lightPosition.X = position.X + 12.0f; emitterLight = new Light(LightType.Point, new Vector3(0.83f, 0.06f, 0.36f), lightPosition, false); emitterLight.Parameters = new Vector4(70, 45, 0, 0); }
void InitializeVoxels() { int voxelCount = (DensityFieldSize - 1) / VoxelGridSize; Voxels = new VoxelGeometry[voxelCount * voxelCount * voxelCount]; VoxelBounds = new BoundingBox[Voxels.Length]; float ratio = 2.0f * (float)VoxelGridSize / (float)(DensityFieldSize - 1); for (int z = 0; z < voxelCount; z++) { int zOff = voxelCount * voxelCount * z; for (int y = 0; y < voxelCount; y++) { int yOff = voxelCount * y; for (int x = 0; x < voxelCount; x++) { int idx = x + yOff + zOff; VoxelBounds[idx] = new BoundingBox(new Vector3(x, y, z) * ratio - Vector3.One, new Vector3(x + 1, y + 1, z + 1) * ratio - Vector3.One); VoxelBounds[idx].Min = Vector3.Transform(VoxelBounds[idx].Min, Transformation.GetTransform()); VoxelBounds[idx].Max = Vector3.Transform(VoxelBounds[idx].Max, Transformation.GetTransform()); Voxels[idx] = new VoxelGeometry(); Voxels[idx].renderElement.Transform = new Matrix[1] { Transformation.GetTransform() }; Voxels[idx].GenerateGeometry(ref DensityField, IsoValue, DensityFieldSize, DensityFieldSize, DensityFieldSize, VoxelGridSize, VoxelGridSize, VoxelGridSize, x * VoxelGridSize, y * VoxelGridSize, z * VoxelGridSize, 2.0f / (float)(DensityFieldSize - 1)); } } } }
void GenerateGeometry() { byte[] DensityField; int DensityFieldSize = 17; byte IsoValue = 127; DensityField = new byte[DensityFieldSize * DensityFieldSize * DensityFieldSize]; Vector3 center = Vector3.One * DensityFieldSize * 0.5f; Vector3 minPos = center; Vector3 maxPos = center; float radius = DensityFieldSize / 2; for (int x = 0; x < DensityFieldSize; x++) { for (int y = 1; y < (DensityFieldSize - 1); y++) { for (int z = 0; z < DensityFieldSize; z++) { Vector3 pos = new Vector3(x, y, z); float density = MathHelper.Clamp(1.0f-(pos-center).Length()/radius, 0, 1); if (density > 0.0f) { pos = (pos / DensityFieldSize) * 2.0f - Vector3.One; minPos = Vector3.Min(pos, minPos); maxPos = Vector3.Max(pos, maxPos); } DensityField[x + (y + z * DensityFieldSize) * DensityFieldSize] = (byte)(density * 255.0f); } } } Geometry = new VoxelGeometry(); Vector3 ratio = 2.0f * Vector3.One / (float)(DensityFieldSize - 1); Geometry.GenerateGeometry(ref DensityField, IsoValue, DensityFieldSize, DensityFieldSize, DensityFieldSize, DensityFieldSize - 1, DensityFieldSize - 1, DensityFieldSize - 1, 0, 0, 0, ratio, Matrix.Identity); }
void GenerateGeometry() { byte[] DensityField; int DensityFieldSize = 17; byte IsoValue = 127; float radiusMax = (DensityFieldSize / 2); float radiusMin = (DensityFieldSize / 16); Vector3 cylinderCenter = Vector3.One * DensityFieldSize * 0.5f; Vector3 minPos, maxPos; minPos = maxPos = cylinderCenter; DensityField = new byte[DensityFieldSize * DensityFieldSize * DensityFieldSize]; for (int x = 0; x < DensityFieldSize; x++) { for (int y = 1; y < (DensityFieldSize - 1); y++) { for (int z = 0; z < DensityFieldSize; z++) { Vector3 pos = new Vector3(x, y, z); float offset = Math.Abs(pos.Y - cylinderCenter.Y); pos.Y = cylinderCenter.Y; float radius = MathHelper.Lerp(radiusMax, radiusMin, offset / cylinderCenter.Y); float density = Math.Max(1.0f - (pos - cylinderCenter).Length() / (radius), 0.0f); if (density > 0.0f) { pos = (pos / DensityFieldSize) * 2.0f - Vector3.One; minPos = Vector3.Min(pos, minPos); maxPos = Vector3.Max(pos, maxPos); } DensityField[x + (y + z * DensityFieldSize) * DensityFieldSize] = (byte)(density * 255.0f); } } } Geometry = new VoxelGeometry(); Vector3 ratio = Vector3.One * 2.0f / (float)(DensityFieldSize - 1); Geometry.GenerateGeometry(ref DensityField, IsoValue, DensityFieldSize, DensityFieldSize, DensityFieldSize, DensityFieldSize - 1, DensityFieldSize - 1, DensityFieldSize - 1, 0, 0, 0, ratio, Matrix.Identity); }
void GenerateGeometry() { byte[] DensityField; int DensityFieldSize = 17; byte IsoValue = 127; DensityField = new byte[DensityFieldSize * DensityFieldSize * DensityFieldSize]; Vector3 center = Vector3.One * DensityFieldSize * 0.5f; Vector3 minPos = center; Vector3 maxPos = center; float radiusInner = DensityFieldSize / 6; float numSpikes = 5; float period = MathHelper.TwoPi * numSpikes; float minRad = DensityFieldSize / 4; float maxRad = DensityFieldSize / 3; for (int x = 0; x < DensityFieldSize; x++) { for (int y = 1; y < (DensityFieldSize - 1); y++) { for (int z = 0; z < DensityFieldSize; z++) { Vector3 pos = new Vector3(x, y, z); float innerRingTerm = (1.0f - new Vector2(pos.X-center.X, pos.Y-center.Y).Length() / radiusInner); float innerRing = (innerRingTerm > 0.0f) ? 1 : 0; float zElem = Math.Abs((z - center.Z) / center.Z); float sinTerm = (float)Math.Sin(period * zElem); float cosTerm = (float)Math.Cos(period * zElem); float radiusOuter = minRad; if (cosTerm <= 0.0f) { radiusOuter = MathHelper.Lerp(minRad, maxRad, 1.0f + cosTerm); } float outerRingTerm = (1.0f - new Vector2(pos.X - center.X, pos.Y - center.Y).Length() / radiusOuter); float density = MathHelper.Clamp(outerRingTerm-innerRing, 0, 1); if (density > 0.0f) { pos = (pos / DensityFieldSize) * 2.0f - Vector3.One; minPos = Vector3.Min(pos, minPos); maxPos = Vector3.Max(pos, maxPos); } DensityField[x + (y + z * DensityFieldSize) * DensityFieldSize] = (byte)(density * 255.0f); } } } gateGeometry = new VoxelGeometry(); gateGeometry.renderElement.Transform = new Matrix[1] { this.Transformation.GetTransform() }; gateGeometry.GenerateGeometry(ref DensityField, IsoValue, DensityFieldSize, DensityFieldSize, DensityFieldSize, DensityFieldSize - 1, DensityFieldSize - 1, DensityFieldSize - 1, 0, 0, 0, 2.0f / (float)(DensityFieldSize - 1)); bounds = new BoundingBox(); bounds.Max = Vector3.Transform(maxPos, this.Transformation.GetTransform()); bounds.Min = Vector3.Transform(minPos, this.Transformation.GetTransform()); }
void InitializeVoxels() { int voxelCountX = (DensityFieldWidth - 1) / VoxelGridSize; int voxelCountY = (DensityFieldHeight - 1) / VoxelGridSize; int voxelCountZ = (DensityFieldDepth - 1) / VoxelGridSize; Voxels = new VoxelGeometry[voxelCountX * voxelCountY * voxelCountZ]; VoxelBounds = new BoundingBox[Voxels.Length]; Vector3 ratio = Vector3.One * 2.0f * (float)VoxelGridSize / new Vector3(DensityFieldWidth-1,DensityFieldHeight-1,DensityFieldDepth-1); for (int z = 0; z < voxelCountZ; z++) { int zOff = voxelCountX * voxelCountY * z; for (int y = 0; y < voxelCountY; y++) { int yOff = voxelCountX * y; for (int x = 0; x < voxelCountX; x++) { int idx = x + yOff + zOff; VoxelBounds[idx] = new BoundingBox(new Vector3(x, y, z) * ratio - Vector3.One, new Vector3(x + 1, y + 1, z + 1) * ratio - Vector3.One); VoxelBounds[idx].Min = Vector3.Transform(VoxelBounds[idx].Min, Transformation.GetTransform()); VoxelBounds[idx].Max = Vector3.Transform(VoxelBounds[idx].Max, Transformation.GetTransform()); Voxels[idx] = new VoxelGeometry(); Voxels[idx].renderElement.Transform = new Matrix[1] { Transformation.GetTransform() }; Vector3 geometryRatio = 2.0f*Vector3.One / new Vector3(DensityFieldWidth-1,DensityFieldHeight-1,DensityFieldDepth-1); Voxels[idx].GenerateGeometry(ref DensityField, IsoValue, DensityFieldWidth, DensityFieldHeight, DensityFieldDepth, VoxelGridSize, VoxelGridSize, VoxelGridSize, x * VoxelGridSize, y * VoxelGridSize, z * VoxelGridSize, geometryRatio, this.Transformation.GetTransform()); } } } }