Created
September 7, 2022 21:12
-
-
Save ThomasGorisse/728f27a26fabaf0fe78e7c4f84cfcba0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package io.github.sceneview.mesh | |
| import com.google.android.filament.Engine | |
| import io.github.sceneview.math.Direction | |
| import io.github.sceneview.math.Position | |
| import io.github.sceneview.math.Size | |
| /** | |
| * Creates a [Geometry] in the shape of a cube with the give specifications. | |
| * | |
| * @param size the size of the constructed cube | |
| * @param center the center of the constructed cube | |
| */ | |
| class Cube(engine: Engine, size: Size, center: Position) : Geometry( | |
| engine = engine, | |
| vertices = mutableListOf<Vertex>().apply { | |
| val extents = size * 0.5f | |
| val p0 = center + Size(-extents.x, -extents.y, extents.z) | |
| val p1 = center + Size(extents.x, -extents.y, extents.z) | |
| val p2 = center + Size(extents.x, -extents.y, -extents.z) | |
| val p3 = center + Size(-extents.x, -extents.y, -extents.z) | |
| val p4 = center + Size(-extents.x, extents.y, extents.z) | |
| val p5 = center + Size(extents.x, extents.y, extents.z) | |
| val p6 = center + Size(extents.x, extents.y, -extents.z) | |
| val p7 = center + Size(-extents.x, extents.y, -extents.z) | |
| val up = Direction(y = 1.0f) | |
| val down = Direction(y = -1.0f) | |
| val front = Direction(z = -1.0f) | |
| val back = Direction(z = 1.0f) | |
| val left = Direction(x = -1.0f) | |
| val right = Direction(x = 1.0f) | |
| val uv00 = UvCoordinate(x = 0.0f, y = 0.0f) | |
| val uv10 = UvCoordinate(x = 1.0f, y = 0.0f) | |
| val uv01 = UvCoordinate(x = 0.0f, y = 1.0f) | |
| val uv11 = UvCoordinate(x = 1.0f, y = 1.0f) | |
| // Bottom | |
| addAll( | |
| listOf( | |
| Vertex(position = p0, normal = down, uvCoordinate = uv01), | |
| Vertex(position = p1, normal = down, uvCoordinate = uv11), | |
| Vertex(position = p2, normal = down, uvCoordinate = uv10), | |
| Vertex(position = p3, normal = down, uvCoordinate = uv00), | |
| // Left | |
| Vertex(position = p7, normal = left, uvCoordinate = uv01), | |
| Vertex(position = p4, normal = left, uvCoordinate = uv11), | |
| Vertex(position = p0, normal = left, uvCoordinate = uv10), | |
| Vertex(position = p3, normal = left, uvCoordinate = uv00), | |
| // Back | |
| Vertex(position = p4, normal = back, uvCoordinate = uv01), | |
| Vertex(position = p5, normal = back, uvCoordinate = uv11), | |
| Vertex(position = p1, normal = back, uvCoordinate = uv10), | |
| Vertex(position = p0, normal = back, uvCoordinate = uv00), | |
| // Front | |
| Vertex(position = p6, normal = front, uvCoordinate = uv01), | |
| Vertex(position = p7, normal = front, uvCoordinate = uv11), | |
| Vertex(position = p3, normal = front, uvCoordinate = uv10), | |
| Vertex(position = p2, normal = front, uvCoordinate = uv00), | |
| // Right | |
| Vertex(position = p5, normal = right, uvCoordinate = uv01), | |
| Vertex(position = p6, normal = right, uvCoordinate = uv11), | |
| Vertex(position = p2, normal = right, uvCoordinate = uv10), | |
| Vertex(position = p1, normal = right, uvCoordinate = uv00), | |
| // Top | |
| Vertex(position = p7, normal = up, uvCoordinate = uv01), | |
| Vertex(position = p6, normal = up, uvCoordinate = uv11), | |
| Vertex(position = p5, normal = up, uvCoordinate = uv10), | |
| Vertex(position = p4, normal = up, uvCoordinate = uv00) | |
| ) | |
| ) | |
| }, | |
| submeshes = mutableListOf<Submesh>().apply { | |
| val sideCount = 6 | |
| val verticesPerSide = 4 | |
| for (i in 0 until sideCount) { | |
| add( | |
| Submesh( | |
| // First triangle for this side. | |
| 3 + verticesPerSide * i, | |
| 1 + verticesPerSide * i, | |
| 0 + verticesPerSide * i, | |
| // Second triangle for this side. | |
| 3 + verticesPerSide * i, | |
| 2 + verticesPerSide * i, | |
| 1 + verticesPerSide * i | |
| ) | |
| ) | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment