Lời Nói Đầu - Trang Chủ Nohu17
Một trải nghiệm cá cược tuyệt vời trên Trang Chủ Nohu17. Đăng ký ngay để tham gia
Tạo Hình Cầu Từ Khối Lập Phương Trong SceneKit
Hình cầu từ khối lập phương (Cube Sphere) là một loại hình cầu đặc biệt, khác với hình cầu địa lý (Geodesic Sphere) và hình cầu thông thường. Nó giống như một khối lập phương đã trải qua một số biến đổi để trở thành hình cầu. Ưu điểm của Cube Sphere là rất phù hợp để tạo hành tinh theo quy trình sinh ra, vì mỗi mặt có thể được chia nhỏ vô hạn bằng QuadTree.
Quy Trình Thực Hiện
- Tạo mới SCNBox kèo cá cược bóng đá
- Chỉnh sửa đỉnh (vertices)
- Tính toán lại Normal
- Cập nhật lại Geometry cho SCNode
1. Tạo Mới SCNBox
SCNBox *SCNBoxToSphereMapping = [SCNBox boxWithWidth:60.0f height:60.0f length:60.0f chamferRadius:0.0f];
SCNBoxToSphereMapping.widthSegmentCount = 16;
SCNBoxToSphereMapping.heightSegmentCount = 16;
SCNBoxToSphereMapping.lengthSegmentCount = 16;
SCNNode *PlanetNode = [SCNNode nodeWithGeometry:SCNBoxToSphereMapping];
[PlanetSceneKitView.scene.rootNode addChildNode:PlanetNode];
[SCNTransaction flush];
- SegmentCount là lũy thừa của 2 vì sau này sẽ sử dụng QuadTree.
- [SCNTransaction flush]; là bước quan trọng, nếu không flush, dữ liệu geometry nhận được sẽ chỉ là dữ liệu mặc định của SCNBox với SegmentCount = 1, mà SCNBox mặc định chỉ có 8 đỉnh, không đủ để biến đổi thành hình cầu.
2. Chỉnh Sửa Đỉnh (Vertices)
// Lấy nguồn đỉnh
NSArray *vertexSources = [PlanetNode.geometry geometrySourcesForSemantic:SCNGeometrySourceSemanticVertex];
// Lấy nguồn đầu tiên
SCNGeometrySource *vertexSource = vertexSources[0];
long stride = vertexSource.dataStride; // tính bằng byte
long offset = vertexSource.dataOffset; // tính bằng byte
long componentsPerVector = vertexSource.componentsPerVector;
long bytesPerVector = componentsPerVector * vertexSource.bytesPerComponent;
long vectorCount = (long)vertexSource.vectorCount;
SCNVector3 vertices[vectorCount]; // Mảng mới cho các đỉnh
// Đọc từng vectơ
for (long i=0; i<vectorCount; i++) {
float [ca cuoc bong da bang the cao](/posts/2025-3-15/) vectorData[componentsPerVector];
NSRange byteRange = NSMakeRange(i*stride + offset, bytesPerVector);
[vertexSource.data getBytes:&vectorData range:byteRange];
float x = vectorData[0] / SCNBoxToSphereMapping.width * 2.0f;
float y = vectorData[1] / SCNBoxToSphereMapping.width * 2.0f;
float z = vectorData[2] / SCNBoxToSphereMapping.width * 2.0f;
float SphereX = x*sqrt(1-pow(y,2)/2.0f-pow(z,2)/2.0f + pow(y*z,2)/3.0f) * SCNBoxToSphereMapping.width / 2.0f;
float SphereY = y*sqrt(1-pow(z,2)/2.0f-pow(x,2)/2.0f + pow(x*z,2)/3.0f) * SCNBoxToSphereMapping.width / 2.0f;
float SphereZ = z*sqrt(1-pow(x,2)/2.0f-pow(y,2)/2.0f + pow(y*x,2)/3.0f) * SCNBoxToSphereMapping.width / 2.0f;
vertices[i] = SCNVector3Make(SphereX, SphereY, SphereZ);
}
- Vertices được lấy từ SCNGeometrySource.
- Cần ánh xạ các vertices trên khối lập phương lên vị trí trên hình cầu bằng cách thay đổi giá trị xyz.
3. Tính Toán Lại Normal
SCNGeometrySource *DeformedGeometrySource = [SCNGeometrySource geometrySourceWithVertices:vertices count:vectorCount];
NSArray *SCNGeometrySourceArray = [NSArray arrayWithObject:DeformedGeometrySource];
NSArray *DeformGeometryElement = [PlanetNode.geometry geometryElements];
SCNGeometry *DeformedGeometry = [SCNGeometry geometryWithSources:SCNGeometrySourceArray elements:DeformGeometryElement];
MDLMesh *DeformedGeometryUsingMDL = [MDLMesh meshWithSCNGeometry:DeformedGeometry];
[DeformedGeometryUsingMDL addNormalsWithAttributeNamed:MDLVertexAttributeNormal creaseThreshold:1.0f];
- Một SCNGeometry tùy chỉnh cần hai phần tử: SCNGeometrySource và SCNGeometryElements.
- MDLMesh từ Model I/O có thể tính toán Normal và Tangent.
4. Cập Nhật Lại Geometry
DeformedGeometry = [SCNGeometry geometryWithMDLMesh:DeformedGeometryUsingMDL];
PlanetNode.geometry = DeformedGeometry;
PlanetSceneKitView.debugOptions = SCNDebugOptionShowWireframe;
Kết quả cuối cùng sẽ là một hình cầu được tạo từ khối lập phương ban đầu.
Để kiểm tra kết quả, bạn có thể bật 78win+đăng+nhập chế độ xem khung dây bằng cách thiết lập debugOptions trong SceneKit.
Sửa đổi lần cuối vào 2025-05-09