2016年6月11日 星期六

Unreal: Level Blueprint > Array

since: 2016/06/11
update: 2016/06/11
reference:
1. Unreal Engine 4 Documentation | Unreal Engine


A. Create Array

    1. 新增變數: MyInventory

    2. 在 Details 裡, 將變數種類改成: String , 並設為 array 型別

    3. 增加預設值:

-----------------------------------------------------------------------------------------------

B. 按下 "I" 鍵, 執行 ForEachLoop 功能:

-----------------------------------------------------------------------------------------------

C. 取得(Get) MyInventory 的 array 值, 輸入給  ForEachLoop 的 Array

-----------------------------------------------------------------------------------------------

D. 由  ForEachLoop 取得 Array 元素, 分別印出字串後, 停留 10 秒才消失.

    結果:  進入遊戲, 按下 "I" 鍵: Array 元素會從下往上疊上來

-----------------------------------------------------------------------------------------------

E. 新增要附加給 Array 的變數:

    1. 新增一個變數: NewItem, 設為 String 型態, 並給予預設值: potion


-----------------------------------------------------------------------------------------------

F. 附加元素給 MyInventory Array:
   1. 新增 AND Node
   2. 取得(get) NewItem 的值給 ADD node
   3. 將 ADD node 的值附加給  MyInventory Array
   4. AND node 執行結束後, 呼叫 Print String 函式, 顯示: "A potion added" 10 秒


-----------------------------------------------------------------------------------------------

   G. 按下 "P" 鍵, 執行 ADD 功能:

    結果:  進入遊戲, 先按下 "P" 鍵: 顯示: "A potion added"
               , 接著再按 "I" 鍵: 列出所有元素.(多了 potion)

-----------------------------------------------------------------------------------------------

   H. Array 相關函式


2016年6月10日 星期五

Unreal: Level Blueprint > Variables

since: 2016/06/10
update: 2016/06/10

reference:
1. Unreal Engine 4 Documentation | Unreal Engine


A. Get Variables

     當進入此關卡, 開始計時 3 秒, 時間一到就離開遊戲.
---------------------------------------------------------------------

     新增 Float 變數: DelayDuration
     設定 Default Value: 3.0

-----------------------------------------------------------------------------------------------

B. Set Variables

     當進入此關卡, 開始計時 5 秒, 時間一到就離開遊戲.
---------------------------------------------------------------------

     新增 Float 變數: DelayDuration
     設定 Default Value: 3.0 (值會被之後的設定取代)
     進入關卡, 設定 DelayDuration  變數值: 5.0

Unreal: Level Blueprint > Quit Game

since: 2016/06/10
update: 2016/06/10
reference
:
1. Unreal Engine 4 Documentation | Unreal Engine


A. 說明:

     當進入此關卡, 開始計時 3 秒, 時間一到就離開遊戲.
   
     ---------------------------------------------------------------------

     打開 Level Blueprint:


     Blueprint Type:
     Level Blueprint

     Event:
     Event BeginPlay


     Function:
     Delay

     Quit Game


2016年6月8日 星期三

ColorPicker For Swift

since: 2016/06/08
update: 2016/06/08
reference:
1. johankasperi/SwiftHSVColorPicker
2. PrashantMangukiya/SwiftColorPicker
3. How to get the RGB Code (INT) from an UIColor in Swift - Stack Overflow


A. HSV Wheel Color Picker:
    1. 從
johankasperi/SwiftHSVColorPicker
        > 下載檔案: SwiftHSVColorPicker-master.zip
並解壓縮
        > 打開 SwiftHSVColorPickerExample.xcodeproj

    2. 修改: ViewController.swift 如下:
//
//  ViewController.swift
//  SwiftHSVColorPicker
//
//  Created by johankasperi on 2015-08-20.
//

//@add ####################
//
// Hue Saturation Value; HSV
//
// 係用一個倒六稜錐體定義顏色空間的模型。
// 其中,色調用H表示,以0到360度角表示各種顏色,如0度為紅色,120度為綠色。
// S為0與1間取值,反映顏色飽和或濃淡程度。
// V表示明度或亮度,在0與1間取值,0為最暗,1為最亮。


import Foundation
import UIKit
//@add ####################
import AudioToolbox

//@add ####################
extension UIColor {
    func rgb() -> (red:Int, green:Int, blue:Int, alpha:Int)? {
        var fRed : CGFloat = 0
        var fGreen : CGFloat = 0
        var fBlue : CGFloat = 0
        var fAlpha: CGFloat = 0
        if self.getRed(&fRed, green: &fGreen, blue: &fBlue, alpha: &fAlpha) {
            let iRed = Int(fRed * 255.0)
            let iGreen = Int(fGreen * 255.0)
            let iBlue = Int(fBlue * 255.0)
            let iAlpha = Int(fAlpha * 255.0)
           
            return (red:iRed, green:iGreen, blue:iBlue, alpha:iAlpha)
        } else {
            // Could not extract RGBA components:
            return nil
        }
    }
}


//@add ####################
extension Int
{
    func toString() -> String
    {
        let myString = String(self)
        return myString
    }
}


//@add ####################
extension CGFloat
{
    func toString() -> String
    {
        let myString = String(self)
        return myString
    }
}


class ViewController: UIViewController {
   
    // Init ColorPicker with yellow
    var selectedColor: UIColor = UIColor.whiteColor()
   
    // IBOutlet for the ColorPicker
    @IBOutlet var colorPicker: SwiftHSVColorPicker!
   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // Setup Color Picker
        colorPicker.setViewColor(selectedColor)
    }
   
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   
//    @IBAction func getSelectedColor(sender: UIButton) {
//        // Get the selected color from the Color Picker.
//        let selectedColor = colorPicker.color
//       
//        print(selectedColor)
//    }
   
    //@update  ####################
    @IBAction func getSelectedColor(sender: UIButton) {
        // Get the selected color from the Color Picker.
        // colorPicker.hue
        // colorPicker.saturation
        // colorPicker.brightness

        let selectedColor = colorPicker.color
        //print(selectedColor)
       
        AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
       
        if let rgb = selectedColor.rgb() {
            print()
            print(selectedColor)
            print("red = " + (rgb.red).toString())
            print("green = " + (rgb.green).toString())
            print("blue = " + (rgb.blue).toString())
            print("alpha = " + (rgb.alpha).toString())
            print("brightness = " + (colorPicker.brightness).toString())
        } else {
            print("conversion failed")
        }
    }

}

    3. 執行結果:



-----------------------------------------------------------------------------------------------

B. HSV Palette Color Picker:
    1. 從 PrashantMangukiya/SwiftColorPicker
        > 下載檔案: SwiftColorPicker-master.zip 並解壓縮
        > 打開 SwiftColorPicker \ SwiftColorPicker.xcodeproj

    2. 修改: ViewController.swift 如下:
//
//  ViewController.swift
//  SwiftColorPicker
//
//  Created by Prashant on 02/09/15.
//  Copyright (c) 2015 PrashantKumar Mangukiya. All rights reserved.
//
import UIKit

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate, ColorPickerDelegate {
   
    // outlet - selected color preview
    @IBOutlet var colorPreview: UIView!
   
    // outlet - change color button
    @IBOutlet var changeColorButton: UIButton!
   
    // action - called when change color button clicked
    @IBAction func changeColorButtonClicked(sender: UIButton) {
        self.showColorPicker()
    }
   
    // outlet - change color button placed at navigation bar (right side)
    @IBOutlet var changeColorButtonNav: UIBarButtonItem!
   
    // action - change color button placed at navigation bar (right side)
    @IBAction func changeColorButtonNavClicked(sender: UIBarButtonItem) {
        self.showColorPickerFromNavBar()
    }
   
    // class varible maintain selected color value
    var selectedColor: UIColor = UIColor.blueColor()
    var selectedColorHex: String = "0000FF"
   
    // MARK: - View functions
   
    // called after view loaded
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        // set corner radious
        self.colorPreview.layer.cornerRadius = self.colorPreview.layer.frame.width/6
       
        // set default background color
        self.colorPreview.backgroundColor = self.selectedColor
    }

    // called if received memory warning
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   
    // MARK: Popover delegate functions

    // Override iPhone behavior that presents a popover as fullscreen.
    // i.e. now it shows same popover box within on iPhone & iPad
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
       
        // show popover box for iPhone and iPad both
        return UIModalPresentationStyle.None
    }
   
    // MARK: Color picker delegate functions
    // called by color picker after color selected.
    func colorPickerDidColorSelected(selectedUIColor selectedUIColor: UIColor, selectedHexColor: String) {
      
        // update color value within class variable
        self.selectedColor = selectedUIColor
        self.selectedColorHex = selectedHexColor
       
        // set preview background to selected color
        self.colorPreview.backgroundColor = selectedUIColor
       
        //@add ############################################
        print("selectedColorHex = "+selectedColorHex)
    }
  
    // MARK: - Utility functions
    // show color picker from UIButton
    private func showColorPicker(){
       
        // initialise color picker view controller
        let colorPickerVc = storyboard?.instantiateViewControllerWithIdentifier("sbColorPicker") as! ColorPickerViewController
       
        // set modal presentation style
        colorPickerVc.modalPresentationStyle = .Popover
       
        // set max. size
        colorPickerVc.preferredContentSize = CGSizeMake(265, 400)
       
        // set color picker deleagate to current view controller
        // must write delegate method to handle selected color
        colorPickerVc.colorPickerDelegate = self
       
        // show popover
        if let popoverController = colorPickerVc.popoverPresentationController {
           
            // set source view
            popoverController.sourceView = self.view
           
            // show popover form button
            popoverController.sourceRect = self.changeColorButton.frame
           
            // show popover arrow at feasible direction
            popoverController.permittedArrowDirections = UIPopoverArrowDirection.Any
           
            // set popover delegate self
            popoverController.delegate = self
        }
       
        //show color popover
        presentViewController(colorPickerVc, animated: true, completion: nil)
    }
   
   
    // show color picker from Navigation Bar Button
    private func showColorPickerFromNavBar(){
       
        // initialise color picker view controller
        let colorPickerVc = storyboard?.instantiateViewControllerWithIdentifier("sbColorPicker") as! ColorPickerViewController
       
        // set modal presentation style
        colorPickerVc.modalPresentationStyle = .Popover
       
        // set max. size
        colorPickerVc.preferredContentSize = CGSizeMake(265, 400)
       
        // set color picker deleagate to current view controller
        // must write delegate method to handle selected color
        colorPickerVc.colorPickerDelegate = self
       
        // show popover
        if let popoverController = colorPickerVc.popoverPresentationController {
           
            // show popover from navigation bar button
            popoverController.barButtonItem = self.changeColorButtonNav
           
            // show popover arrow at feasible direction
            popoverController.permittedArrowDirections = UIPopoverArrowDirection.Any
           
            // set popover delegate self
            popoverController.delegate = self
        }
       
        // show color popover
        presentViewController(colorPickerVc, animated: true, completion: nil)
    }
}

    3. 執行結果:


2016年6月7日 星期二

Unreal: Begin A Virtual Reality Project

since: 2016/06/07
update: 2016/11/05
reference:
1. Setting up UE4 to work with SteamVR | Unreal Engine
2. Unreal Engine 4.12 Released!


A. SteamVR is Ready

     > 確認 SteamVR 開啟後, 是處在就緒的狀態.

-----------------------------------------------------------------------------------------------

B. 建立新專案:
   
> Launch Unreal Engine 4.12.0


     1. New Project > Blueprint > Blank

     2. Mobile / Tablet > Scalable 3D or 2D > No Starter Content >
        Folder: E:\RD\projects\Unreal
        Name: HelloVR

     3.檢查是否啟用支援該 VR 設備
        a. Edit > Plugins

        b. Virtual Reality >  SteamVR 支援, 勾選 Enabled

     4. VR Preview
        a.  按下 Play 鍵旁的小三角形 > VR Preview

        b. 結果: (可戴上 VR 頭盔觀看)

-----------------------------------------------------------------------------------------------

C. 修正 VR 的地平線水平高度
     1. 新增 Map 與 Blueprint 資料夾:

        > Content Brower > Add New > New Folder

        結果: 在 Content Brower 的 Content 底下, 可以看到 Map 與 Blueprint 資料夾

     2. 進入 Blueprint 資料夾, 新增二個 Blueprint 類別:
         > Content Brower > Add New > Blueprint Class

     > Blueprint Node: Game Mode              Name: VR_GameMode
     > Blueprint Node: Pawn                         Name: VR_Pawn

     結果:

     3. 開啟 VR_GameMode Blueprint:
         > Details > classes:
         > Default Pawn Class: VR_Pawn

     4. 關閉 VR_GameMode Blueprint 後, 回到關卡:
         Window
> World Settings

     > World Settings > GameMode:
        GameMode Override: VR_GameMode
       
(只對目前的關卡改寫預設值)

     5. 開啟 VR_Pawn Blueprint:
         > Details > Camera:
            Base Eye Height: 0.0

   /* 以下為 4.12 版本 ################################################
     6. 在 Components 頁籤, 新增: SteamVRChaperone 與 Camera component


    > 並確定: camera 的 Transform Location 設為 0,0,0.

     7. 關閉 VR_Pawn Blueprint 後, 回到關卡:
         > World Outliner > Floor:
         > Transform > Location: X: 0.0 Y: 0.0 Z: 0.0

         > World Outliner > Player Start:
         > Transform > Location: X: 0.0 Y: 0.0 Z: 1.0

     8. 再執行一次: VR Preview

   以上為 4.12 版本 ################################################ */


   /* 更新: 4.13 版本 */
     6. 在 Components 頁籤, 新增: Capsule, Scene, Camera  與 SteamVRChaperone component

     7. 點選 Capsule Components , 在右邊 Details 面板的 Shape 部分, 更改:
         Capsule Half Height: 96
         Capsule Radius: 16

     8. 點選 Camera Components , 將它拖拉到 Scene Components 下

     9. 點選 Scene Components , 在右邊 Details 面板的 Shape 部分, 更改:
         Transform > Location: 將 Z 值設為: -110
         , 然後編譯並儲存.

     10. 回到關卡, 點選 floor (Static Mesh), 將其 X, Y, Z 位置皆設為: 0,0,0

     11. 備註: 在 VR Preview 時, 如果覺得太接近或太遠離地板, 開啟 VR_Pawn Blueprint,
           點選 Scene Component, 調整 Z 的高度值.
-----------------------------------------------------------------------------------------------

D. VR Editor: (4.12)
    1. Edit > Editor Preferences...

    2. General > Experimental > VR:
        勾選: Enable VR Editing

    3. 結果: 多出一個啟用 VR Editor 的功能

-----------------------------------------------------------------------------------------------

E. 備註

     1. 將 GameMode 套用到整個專案裡
         > Level Editor > Editor > Project Settings...
         > Maps & Modes
         > Default Modes
         > Default GameMode: VR_GameMode

AudioKit for Swift

since: 2016/06/07
update: 2016/06/07
reference:
1. AudioKit - Powerful audio synthesis, processing, and analysis

A. 於專案中, 加入 AudioKit.framework 

    1. 接續上個專案: OSC for Swift
        到 AudioKit 下載 AudioKit-iOS-3.1.zip, 解壓縮後,  將 AudioKit.framework 拷貝到
        專案目錄裡(可以先建立 Frameworks 資料夾) 

    2. 在專案下新增 Frameworks 群組, 按下 Embedded Binaries 下的 "+"

    3. 瀏覽  AudioKit.framework 的檔案位置 > Open

    4. 點選 Create folder references > Finish

    5. 結果如下:

-----------------------------------------------------------------------------------------------

B. 專案加入 AudioKit 功能
    1. 修改 ViewController.swift 如下:
//
//  ViewController.swift
//  Hello_OSC
//
//  Created by LanliChen on 2016/6/7.
//  Copyright © 2016年 Lanli. All rights reserved.
//

import UIKit

//@add for AudioKit ############

import AudioKit

//class ViewController: UIViewController {
//@update for F53OSC ############
class ViewController: UIViewController, F53OSCPacketDestination {

    //@add for F53OSC ############
    var oscServer: F53OSCServer!
    var myAddressPattern = ""
    var myArguments = [AnyObject]()
   
    //@add for AudioKit ############

    var oscillator = AKOscillator()
   
    // @add for AudioKit ############
    func toggleSound() {
        if oscillator.isPlaying {
            oscillator.stop()
        } else {
            oscillator.amplitude = random(0.5, 1)
            oscillator.frequency = random(220, 880)
            oscillator.start()
        }
    }
   
    //@add for F53OSC ############
    func takeMessage(message: F53OSCMessage!) {
       
        print("Received '\(message)'")

       
        //@add for AudioKit ############
        toggleSound()
       
        //        let addressPattern = message.addressPattern
        //        let arguments = message.arguments
        myAddressPattern = message.addressPattern
        myArguments = message.arguments
        print("myAddressPattern = " + myAddressPattern)
        print(myArguments) // print(myArguments[0])
    }

   
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
       
        //@add for F53OSC ############
        print("Starting OSC Server")
        oscServer = F53OSCServer.init()
        oscServer.port = 3030
        oscServer.delegate = self
        if oscServer.startListening() {
            print("Listening for messages on port \(oscServer.port)")
        } else {
            print("Error: Server was unable to start listening on port \(oscServer.port)")
        }

       
        //@add for AudioKit ############
        AudioKit.output = oscillator
        AudioKit.start()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}