Skip to content

Commit

Permalink
Merge pull request #4 from maxxfrazer/flip-uvs
Browse files Browse the repository at this point in the history
Flipped UVs the right way around
  • Loading branch information
maxxfrazer authored Feb 20, 2022
2 parents e3b7990 + 2c0c4be commit 1469a67
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 14 deletions.
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import PackageDescription

let package = Package(
name: "RealityGeometries",
platforms: [.iOS(.v15), .macOS(.v12)],
platforms: [.iOS(.v15), .macOS(.v12), .macCatalyst(.v15)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "RealityGeometries",
targets: ["RealityGeometries"])
Expand Down
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
# RealityGeometries

> Add more geometries to your RealityKit projects
<p align="center">
<img src="https://img.shields.io/badge/platform-iOS%20%7C%20macOS%20%7C%20macCatalyst-lightgrey"/>
<img src="https://img.shields.io/github/v/release/maxxfrazer/RealityGeometries?color=orange&label=SwiftPM&logo=swift"/>
<img src="https://img.shields.io/badge/Swift-5.5-orange?logo=swift"/>
<img src="https://github.com/maxxfrazer/RealityGeometries/workflows/swiftlint/badge.svg"/>
<img src="https://github.com/maxxfrazer/RealityGeometries/workflows/build/badge.svg"/>
<img src="https://img.shields.io/github/license/maxxfrazer/RealityGeometries"/>
</p>

By default, the only shapes available in RealityKit are a Sphere, Cuboid and Plane (with 4 vertices). Until iOS 15 the only clean way to include more geometries in your project is to load them from a USDZ file, which could unnecessarily increase the size of your app, especially when the shape you want to add is a basic one such as a cylinder or cone.

![Cone-And-Cylinder-NormalMap](https://github.com/maxxfrazer/RealityGeometries/blob/main/media/Cylinder_Cone_Normals.gif?raw=true)
<p align="center">
<img src="media/Cylinder_Cone_Normals.gif"/>
</p>

RealityGeometries is a solution to increase the basic mesh offering from RealityKit.

Expand All @@ -13,4 +26,11 @@ RealityGeometries is a solution to increase the basic mesh offering from Reality
- Path
- Torus

This repository is open to pull requests as well as feature requests.
This repository is open to pull requests as well as feature requests.

## More Images

<p align="center">
<img src="media/Torus_Cone_Cylinder_above.png"/>
<img src="media/Torus_Cone_Cylinder_below.png"/>
</p>
8 changes: 4 additions & 4 deletions Sources/RealityGeometries/MeshResource+Cone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ extension MeshResource {
vertices.append(CompleteVertex(
position: lowerPosition,
normal: [coneNormY * cos(theta - thetaInc / 2), coneNormX, coneNormY * sin(theta - thetaInc / 2)],
uv: [uStep * Float(side), 0]
uv: [1 - uStep * Float(side), 0]
))
}

Expand All @@ -103,7 +103,7 @@ extension MeshResource {
normal: smoothNormals ? coneBottomNormal : [
coneNormY * cos(theta + thetaInc / 2), coneNormX, coneNormY * sin(theta + thetaInc / 2)
],
uv: [uStep * Float(side), 0]
uv: [1 - uStep * Float(side), 0]
)

// add vertex for bottom side of cone
Expand All @@ -123,7 +123,7 @@ extension MeshResource {
// add vertex for top of the cone
let topVertex = CompleteVertex(
position: [0, height / 2, 0],
normal: coneTopNormal, uv: [0.5, 1]
normal: coneTopNormal, uv: [1 - uStep * (Float(side) + 0.5), 1]
)
upperEdgeVertices.append(topVertex)

Expand All @@ -145,7 +145,7 @@ extension MeshResource {
/// - Returns: A cone mesh
public static func generateCone(
radius: Float, height: Float, sides: Int = 24, splitFaces: Bool = false,
smoothNormals: Bool
smoothNormals: Bool = false
) throws -> MeshResource {
assert(sides > 2, "Sides must be an integer above 2")
// first vertices added to vertices will be bottom edges
Expand Down
10 changes: 5 additions & 5 deletions Sources/RealityGeometries/MeshResource+Cylinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ extension MeshResource {
vertices.append(CompleteVertex(
position: lowerPosition,
normal: [cos(theta - thetaInc / 2), 0, sin(theta - thetaInc / 2)],
uv: [uStep * Float(side), 0]
uv: [1 - uStep * Float(side), 0]
))
}

Expand All @@ -109,7 +109,7 @@ extension MeshResource {
normal: [
cos(theta + (smoothNormals ? 0 : thetaInc / 2)), 0,
sin(theta + (smoothNormals ? 0 : thetaInc / 2))
], uv: [uStep * Float(side), 0]
], uv: [1 - uStep * Float(side), 0]
)

// add vertex for bottom side of cylinder, facing out
Expand All @@ -125,19 +125,19 @@ extension MeshResource {
upperEdgeVertices.append(CompleteVertex(
position: lowerPosition + [0, height, 0],
normal: [cos(theta - thetaInc / 2), 0, sin(theta - thetaInc / 2)],
uv: [uStep * Float(side), 1]
uv: [1 - uStep * Float(side), 1]
))
}

// add vertex for top side facing out
let topVertex = CompleteVertex(
position: lowerPosition + [0, height, 0],
normal: bottomVertex.normal, uv: [uStep * Float(side), 1]
normal: bottomVertex.normal, uv: [1 - uStep * Float(side), 1]
)
upperEdgeVertices.append(topVertex)

upperCapVertices.append(CompleteVertex(
position: topVertex.position, normal: [0, 1, 0], uv: [cosTheta + 1, sinTheta + 1] / 2)
position: topVertex.position, normal: [0, 1, 0], uv: [1 - cosTheta, sinTheta + 1] / 2)
)

theta += thetaInc
Expand Down
2 changes: 1 addition & 1 deletion Sources/RealityGeometries/MeshResource+Torus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension MeshResource {
currentradius * sinf(iAngle * dToR),
zval
]
var uv: SIMD2<Float> = [iAngle / 360, 2 * jAngle / 360 - 1]
var uv: SIMD2<Float> = [1 - iAngle / 360, 2 * jAngle / 360 - 1]
if uv.y < 0 { uv.y *= -1 }
allVertices.append(CompleteVertex(position: vertexPos, normal: normVal, uv: uv))
iAngle += angleIncs
Expand Down
Binary file added media/Torus_Cone_Cylinder_above.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/Torus_Cone_Cylinder_below.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1469a67

Please sign in to comment.