ARKit — 2. 텍스트 나타내기

naljin
4 min readOct 23, 2018

--

본글은 ARKit 동영상 강의를 기반으로 작성한 글입니다. 출처는 아래에서 확인할 수 있습니다.

오역이나 잘못된 정보에 대한 수정은 언제든지 환영합니다 :)

ARKit 을 이용해 텍스트 만들기

  1. 우선 이전의 예제와 같이 아래의 세단계를 수행
  • import SceneKit, ARKit
  • ARSCNViewDelegate 상속
  • ARSCNView 를 상속받는 sceneView 추가

2. SCNScene 객체 생성

let scene = SCNScene()

3. SCNText 객체 생성

let textGeometry : SCNGeometry = SCNText(string: "hello world", extrusionDepth: 1.0)

이전에 직육면체는 SCNGeometry를 상속받은 SCNBox가 필요했다면, text를 만들기 위해서는 SCNGeometry 를 상속 받은 SCNText 객체가 필요

여기서 extrusionDepth 는 text의 depth 를 말하는 것으로, 1.0으로 설정시에는 z 축으로-0.5 부터0.5 깊이를 가짐. 따라서 0 이면 평평한 모양이 됨

class SCNText : SCNGeometry - A geometry based on a string of text, optionally extruded to create a three-dimensional object.

cf) SCNBox : SCNGeometry - A six-sided polyhedron geometry whose faces are all rectangles, optionally with rounded edges and corners.

4. SCNText 객체에 material 속성 추가

textGeometry.firstMaterial?.diffuse.contents = UIColor.black

바꾸지 않으면 default 색상은 white.

아래 코드와 같은 역할을 함

let material = SCNMaterial()
material.diffuse.contents = UIColor.black
textNode.geometry?.materials = [material]

5. SCNNode 객체 생성 후 속성 추가

let textNode = SCNNode(geometry: textGeometry)textNode.position = SCNVector3(x: 0, y: 0.1, z: -0.5)

SCNNode의 geometry 를 생성자를 통해 한번에 설정해주고 position 을 잡아줌.

6. scene의 rootNode 에 생성한 node 를 childNode로서 추가

scene.rootNode.addChildNode(node)

7. .scale 속성 통해서 크기 조절(optional)

textNode.scale = SCNVector3(0.02, 0.02, 0.02)

6번까지 코드 작성후 실행시키면 너무 크게 나타남. 따라서 .scale 속성을 통해서 노드의 크기를 조정해줌

전체코드

실제 구동 화면

scale 조정 전. 너무 큼
scale 조정 후
extrusionDepth = 0 / 평평함

이전 글 👈🏻

다음 글 👉🏻

출처

--

--

No responses yet