Last active
October 18, 2023 00:15
-
-
Save norman784/61025558fff27eb76162b7e4d1719a97 to your computer and use it in GitHub Desktop.
Custom geometry with UV texture mapping
This file contains 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
//************** | |
// It works now, but theres some things that I don't get it yet | |
// like the texture faces in the UV mapping, theres no logic order (at least that I can understand) | |
//************** | |
// Texture image | |
// https://imgur.com/jwKWRBJ | |
let texture = #imageLiteral(resourceName: "texture") | |
// Half geometry size | |
let halfSide: Float = 0.5 | |
// Geometry vertices | |
let vertices: [SCNVector3] = [ | |
SCNVector3(-halfSide, -halfSide, halfSide), | |
SCNVector3( halfSide, -halfSide, halfSide), | |
SCNVector3(-halfSide, -halfSide, -halfSide), | |
SCNVector3( halfSide, -halfSide, -halfSide), | |
SCNVector3(-halfSide, halfSide, halfSide), | |
SCNVector3( halfSide, halfSide, halfSide), | |
SCNVector3(-halfSide, halfSide, -halfSide), | |
SCNVector3( halfSide, halfSide, -halfSide), | |
// repeat exactly the same | |
SCNVector3(-halfSide, -halfSide, halfSide), | |
SCNVector3( halfSide, -halfSide, halfSide), | |
SCNVector3(-halfSide, -halfSide, -halfSide), | |
SCNVector3( halfSide, -halfSide, -halfSide), | |
SCNVector3(-halfSide, halfSide, halfSide), | |
SCNVector3( halfSide, halfSide, halfSide), | |
SCNVector3(-halfSide, halfSide, -halfSide), | |
SCNVector3( halfSide, halfSide, -halfSide), | |
// repeat exactly the same | |
SCNVector3(-halfSide, -halfSide, halfSide), | |
SCNVector3( halfSide, -halfSide, halfSide), | |
SCNVector3(-halfSide, -halfSide, -halfSide), | |
SCNVector3( halfSide, -halfSide, -halfSide), | |
SCNVector3(-halfSide, halfSide, halfSide), | |
SCNVector3( halfSide, halfSide, halfSide), | |
SCNVector3(-halfSide, halfSide, -halfSide), | |
SCNVector3( halfSide, halfSide, -halfSide), | |
] | |
// Geometry Indices | |
// Don't know if the order of the faces is important | |
let indices: [Int16] = [ | |
// Bottom | |
0, 2, 1, | |
1, 2, 3, | |
// Back | |
10, 14, 11, | |
11, 14, 15, | |
// Left | |
16, 20, 18, | |
18, 20, 22, | |
// Right | |
17, 19, 21, | |
19, 23, 21, | |
// Front | |
8, 9, 12, | |
9, 13, 12, | |
// Top | |
4, 5, 6, | |
5, 7, 6, | |
] | |
// Tile width | |
// because the width of the texture is 1 | |
// and we have 6 tiles inside. | |
// I just imagine that the texture coords will be applied | |
// in the order of the faces was added, | |
// so it starts with top, bottom, front, back, left, right. | |
// I separate them by group of 4, | |
// thats why is a new line between each group | |
let width: CGFloat = 1 / 6 | |
let coordinates: [CGPoint] = [ | |
CGPoint(x: 0, y: 0),// Bottom | |
CGPoint(x: width, y: 0),// Bottom | |
CGPoint(x: width, y: 1),// Bottom | |
CGPoint(x: 0, y: 1),// Bottom | |
CGPoint(x: width * 5, y: 0),// Top | |
CGPoint(x: 1, y: 0),// Top | |
CGPoint(x: 1, y: 1),// Top | |
CGPoint(x: width * 5, y: 1),// Top | |
CGPoint(x: width * 4, y: 0),// Front | |
CGPoint(x: width * 5, y: 0),// Front | |
CGPoint(x: width, y: 0),// Back | |
CGPoint(x: width * 2, y: 0),// Back | |
CGPoint(x: width * 5, y: 1),// Front | |
CGPoint(x: width * 4, y: 1),// Front | |
CGPoint(x: width * 2, y: 1),// Back | |
CGPoint(x: width, y: 1),// Back | |
CGPoint(x: width * 2, y: 0),// Left | |
CGPoint(x: width * 3, y: 0),// Right | |
CGPoint(x: width * 3, y: 1),// Left | |
CGPoint(x: width * 4, y: 1),// Right | |
CGPoint(x: width * 3, y: 0),// Left | |
CGPoint(x: width * 4, y: 0),// Right | |
CGPoint(x: width * 2, y: 1),// Left | |
CGPoint(x: width * 3, y: 1),// Right | |
] | |
// Create the sources | |
let sources = [ | |
SCNGeometrySource(vertices: vertices), | |
SCNGeometrySource(textureCoordinates: coordinates) | |
] | |
// Setup the indices | |
let indicesData = Data(bytes: indices, count: MemoryLayout<Int16>.size * indices.count) | |
let elements = [ | |
SCNGeometryElement(data: indicesData, primitiveType: .triangles, primitiveCount: indices.count / 3, bytesPerIndex: MemoryLayout<Int16>.size) | |
] | |
// Create the geometry | |
let geometry = SCNGeometry(sources: sources, elements: elements) | |
// Add the texture to it as the first material content | |
geometry.firstMaterial?.diffuse.contents = texture | |
// Add the geometry to the node and scene | |
let node = SCNNode(geometry: geometry) | |
scene.rootNode.addChildNode(node) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solved the UV mapping with this stackoverflow answer