2014-12-16 12 views
14

Tôi mới phát triển iOS và tôi đã có bản thân mình stumped. Tôi đang cố gắng render một khối lập phương bằng SceneKit có màu sắc khác nhau cho mỗi khuôn mặt.SCNBox màu sắc hoặc kết cấu khác nhau trên mỗi khuôn mặt

Đây là những gì tôi đã có cho đến nay:

func sceneSetup() { 
    // 1 
    let scene = SCNScene() 

    // 2 
    let BoxGeometry = SCNBox(width: 0.9, height: 0.9, length: 0.9, chamferRadius: 0.0) 

    BoxGeometry.firstMaterial?.diffuse.contents = UIColor.redColor() 
    let cube = SCNNode(geometry: BoxGeometry) 
    cube.position = SCNVector3(x: 0, y: 0, z: -1) 
    scene.rootNode.addChildNode(cube) 

    // 3 
    sceneView.scene = scene 
    sceneView.autoenablesDefaultLighting = true 
    sceneView.allowsCameraControl = true 

Nhưng tôi muốn mỗi khuôn mặt để có một màu khác nhau. Làm thế nào để làm điều đó?

Trả lời

18

Hộp được tạo thành từ sáu phần tử khác nhau (một cho mỗi bên). Bạn cũng có thể nhận thấy rằng đối tượng hình học có một thuộc tính cho tài liệu đầu tiên mà còn là thuộc tính cho một loạt tài liệu.

Một đối tượng có nhiều thành phần và nhiều vật liệu sẽ chọn tăng vật liệu (và bọc) cho từng phần tử.

Ví dụ 4 yếu tố và 1 tài liệu

Element 1 2 3 4 
Material 1 1 1 1 

hoặc 4 yếu tố và 2 vật liệu

Element 1 2 3 4 
Material 1 2 1 2 // note that they are repeating 

Ví dụ 4 yếu tố và 7 nguyên liệu

Element 1 2 3 4 
Material 1 2 3 4 // (5, 6, 7) is unused 

Trong trường hợp của hộp này mea ns mà bạn có thể sử dụng một mảng sáu tài liệu để có một tài liệu độc đáo ở mỗi bên của hộp. Tôi có một ví dụ về điều này trong the sample code for one of the chapters cho cuốn sách Kit Scene của tôi (trong Objective-C):

// Each side of the box has its own color 
// -------------------------------------- 
// All have the same diffuse and ambient colors to show the 
// effect of the ambient light, even with these materials. 

SCNMaterial *greenMaterial    = [SCNMaterial material]; 
greenMaterial.diffuse.contents   = [NSColor greenColor]; 
greenMaterial.locksAmbientWithDiffuse = YES; 

SCNMaterial *redMaterial    = [SCNMaterial material]; 
redMaterial.diffuse.contents   = [NSColor redColor]; 
redMaterial.locksAmbientWithDiffuse  = YES; 

SCNMaterial *blueMaterial    = [SCNMaterial material]; 
blueMaterial.diffuse.contents   = [NSColor blueColor]; 
blueMaterial.locksAmbientWithDiffuse = YES; 

SCNMaterial *yellowMaterial    = [SCNMaterial material]; 
yellowMaterial.diffuse.contents   = [NSColor yellowColor]; 
yellowMaterial.locksAmbientWithDiffuse = YES; 

SCNMaterial *purpleMaterial    = [SCNMaterial material]; 
purpleMaterial.diffuse.contents   = [NSColor purpleColor]; 
purpleMaterial.locksAmbientWithDiffuse = YES; 

SCNMaterial *magentaMaterial   = [SCNMaterial material]; 
magentaMaterial.diffuse.contents  = [NSColor magentaColor]; 
magentaMaterial.locksAmbientWithDiffuse = YES; 


box.materials = @[greenMaterial, redMaterial, blueMaterial, 
        yellowMaterial, purpleMaterial, magentaMaterial]; 
+4

Ngon lành, thưa ông! Một điều đáng chú ý nữa: với nhiều hình học, bạn có thể cho biết có bao nhiêu tài liệu mà nó hỗ trợ bằng cách truy vấn 'geometryElementCount' của nó. Điều này không làm việc cho 'SCNBox', mặc dù ... nó sẽ tự động tái tạo các phần tử hình học của nó tùy thuộc vào số lượng tài liệu bạn gán (theo cách đó không cần sáu lệnh gọi vẽ để hiển thị nếu nó không có sáu tài liệu). – rickster

+0

cảm ơn bị bệnh cho những gì bạn đặt ra – may19c19

+0

Mặt nào (phía trước, bên phải, mặt sau, bên trái, trên cùng, dưới cùng)? –

2

Thái bạn đã giúp đỡ nhanh chóng. i sử dụng mã bạn đặt ra nhưng không thể sử dụng NSColor vì vậy tôi cố gắng UIColor nhưng tất cả tôi đã nhận được là một khối đen nên tôi đã cố gắng này và tôi đã nhận nó làm việc

let BoxGeometry = SCNBox(width: 0.95, height: 0.95, length: 0.95, chamferRadius: 0.0) 

    let greenMaterial = SCNMaterial() 
    greenMaterial.diffuse.contents = UIImage(named: "g") 
    greenMaterial.locksAmbientWithDiffuse = true; 

    let redMaterial = SCNMaterial() 
    redMaterial.diffuse.contents = UIImage(named: "r") 
    redMaterial.locksAmbientWithDiffuse = true; 


    let blueMaterial = SCNMaterial() 
    blueMaterial.diffuse.contents = UIImage(named: "b") 
    blueMaterial.locksAmbientWithDiffuse = true; 


    let yellowMaterial = SCNMaterial() 
    yellowMaterial.diffuse.contents = UIImage(named: "y") 
    yellowMaterial.locksAmbientWithDiffuse = true; 


    let purpleMaterial = SCNMaterial() 
    purpleMaterial.diffuse.contents = UIImage(named: "p") 
    purpleMaterial.locksAmbientWithDiffuse = true; 


    let WhiteMaterial = SCNMaterial() 
    WhiteMaterial.diffuse.contents = UIImage(named: "w") 
    WhiteMaterial.locksAmbientWithDiffuse = true; 


    BoxGeometry.materials = [greenMaterial, redMaterial, blueMaterial, 
    yellowMaterial, purpleMaterial, WhiteMaterial]; 

g là một jpeg của một màu xanh lá cây và do đó và nó đã hoạt động ngay bây giờ.

4

Đây là câu trả lời của Swift 4.

let colors = [UIColor.green, // front 
        UIColor.red, // right 
        UIColor.blue, // back 
        UIColor.yellow, // left 
        UIColor.purple, // top 
        UIColor.gray] // bottom 

    let sideMaterials = colors.map { color -> SCNMaterial in 
     let material = SCNMaterial() 
     material.diffuse.contents = color 
     material.locksAmbientWithDiffuse = true 
     return material 
    } 

    materials = sideMaterials 

Để thay đổi vật liệu trước chỉ cần lấy vật liệu và thay đổi nội dung của nó

let frontMaterial = materials[0] 
frontMaterial.diffuse.contents = UIColor.white 
Các vấn đề liên quan