2017-07-05 12 views
5

में एक विमान पर एक बनावट दोहराएं मेरे पास 32x32 .png छवि है जिसे मैं एक एससीएनप्लेन पर दोहराना चाहता हूं। मुझे जो कोड मिला है (नीचे देखें) परिणामस्वरूप दोहराए जाने के बजाय विमान के आकार में फिट होने के लिए छवि को बढ़ाया जा रहा है।SceneKit

कोड:

let planeGeo = SCNPlane(width: 15, height: 15) 

let imageMaterial = SCNMaterial() 
imageMaterial.diffuse.contents = UIImage(named: "art.scnassets/grid.png") 

planeGeo.firstMaterial = imageMaterial 

let plane = SCNNode(geometry: planeGeo) 

plane.geometry?.firstMaterial?.diffuse.wrapS = SCNWrapMode.repeat 
plane.geometry?.firstMaterial?.diffuse.wrapT = SCNWrapMode.repeat 

उत्तर

7

मैं इसे तय की। ऐसा लगता है कि छवि ज़ूम इन थी। अगर मैं imageMaterial.diffuse.contentsTransform = SCNMatrix4MakeScale(32, 32, 0) करता हूं, तो छवि दोहराती है।

+0

जीयूआई में ऐसा करने का कोई तरीका पता है? दुर्भाग्यवश – jfisk

+0

@jfisk नहीं। मेरा सबसे अच्छा अनुमान किसी भी तरह से sprites का उपयोग करना होगा, लेकिन मैंने इसे एक अच्छा जवाब देने के लिए पर्याप्त नहीं देखा है। – krntz

2

एआरकेट में विमान दृश्यता को लागू करते समय मुझे एक समान समस्या का सामना करना पड़ा। मैं पता लगाया विमान को चेकरबोर्ड पैटर्न के रूप में देखना चाहता था। मैंने कस्टम SCNNode को "प्लेन नोड" नामक एक सही कॉन्फ़िगर किया गया है जिसे सही ढंग से कॉन्फ़िगर किया गया SCNMaterial है। सामग्री wraps, wrapT = .pepeat का उपयोग करती है और विमान के आकार के आधार पर सही ढंग से स्केल की गणना करती है।

इस तरह लग रहा है:

enter image description here

नीचे कोड पर एक नज़र डालें, इनलाइन टिप्पणी स्पष्टीकरण होते हैं।

class PlaneNode : SCNNode { 

    init(planeAnchor: ARPlaneAnchor) { 
     super.init() 
     // Create the 3D plane geometry with the dimensions reported 
     // by ARKit in the ARPlaneAnchor instance 
     let planeGeometry = SCNPlane(width:CGFloat(planeAnchor.extent.x), height:CGFloat(planeAnchor.extent.z)) 
     // Instead of just visualizing the grid as a gray plane, we will render 
     // it in some Tron style colours. 
     let material = SCNMaterial() 
     material.diffuse.contents = PaintCode.imageOfViewARPlane 
     //the scale gives the number of times the image is repeated 
     //ARKit givest the width and height in meters, in this case we want to repeat 
     //the pattern each 2cm = 0.02m so we divide the width/height to find the number of patterns 
     //we then round this so that we always have a clean repeat and not a truncated one 
     let scaleX = (Float(planeGeometry.width)/0.02).rounded() 
     let scaleY = (Float(planeGeometry.height)/0.02).rounded() 
     //we then apply the scaling 
     material.diffuse.contentsTransform = SCNMatrix4MakeScale(scaleX, scaleY, 0) 
     //set repeat mode in both direction otherwise the patern is stretched! 
     material.diffuse.wrapS = .repeat 
     material.diffuse.wrapT = .repeat 
     //apply material 
     planeGeometry.materials = [material]; 
     //make a node for it 
     self.geometry = planeGeometry 
     // Move the plane to the position reported by ARKit 
     position.x = planeAnchor.center.x 
     position.y = 0 
     position.z = planeAnchor.center.z 
     // Planes in SceneKit are vertical by default so we need to rotate 
     // 90 degrees to match planes in ARKit 
     transform = SCNMatrix4MakeRotation(-Float.pi/2.0, 1.0, 0.0, 0.0); 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    func update(planeAnchor: ARPlaneAnchor) { 
     guard let planeGeometry = geometry as? SCNPlane else { 
      fatalError("update(planeAnchor: ARPlaneAnchor) called on node that has no SCNPlane geometry") 
     } 
     //update the size 
     planeGeometry.width = CGFloat(planeAnchor.extent.x) 
     planeGeometry.height = CGFloat(planeAnchor.extent.z) 
     //and material properties 
     let scaleX = (Float(planeGeometry.width)/0.02).rounded() 
     let scaleY = (Float(planeGeometry.height)/0.02).rounded() 
     planeGeometry.firstMaterial?.diffuse.contentsTransform = SCNMatrix4MakeScale(scaleX, scaleY, 0) 
     // Move the plane to the position reported by ARKit 
     position.x = planeAnchor.center.x 
     position.y = 0 
     position.z = planeAnchor.center.z 

    } 
} 
+0

अच्छी तरह से सचित्र – MoVod

संबंधित मुद्दे