Last active
January 3, 2016 01:09
-
-
Save GRGSIBERIA/8387213 to your computer and use it in GitHub Desktop.
対象の頂点INDEXが記録されたCSVを読み込んで,ノブを掴んで動かすと,対象の頂点が移動したり回転したりするスクリプト.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class KnobScript : MonoBehaviour | |
{ | |
public enum ControllType | |
{ | |
TranslateY, | |
TranslateX, | |
TranslateZ, | |
RotateX, | |
RotateY, | |
RotateZ, | |
} | |
public ControllType controllType; | |
public GameObject targetObject; | |
public TextAsset indexCsv; | |
public bool inverse; | |
Mesh mesh; | |
Vector3[] vertices; | |
List<int> targetIndices = new List<int>(); | |
Vector3 prevMousePos; | |
bool clickFlag; | |
// Use this for initialization | |
void Start() | |
{ | |
var lines = indexCsv.text.Split('\n'); | |
foreach (var line in lines) | |
{ | |
if (line != "") // 空文字列の排除 | |
targetIndices.Add(int.Parse(line)); | |
} | |
mesh = targetObject.GetComponent<SkinnedMeshRenderer>().sharedMesh; | |
prevMousePos = Input.mousePosition; | |
} | |
Vector3 CalculateCenter() | |
{ | |
// 対象頂点の中心位置を求める | |
Vector3 total = Vector3.zero; | |
foreach (var ind in targetIndices) | |
{ | |
total += vertices[ind]; | |
} | |
float div = 1f / (float)targetIndices.Count; | |
return total * div; | |
} | |
// 移動 | |
void Translate(Vector3 value) | |
{ | |
foreach (var index in targetIndices) | |
{ | |
vertices[index] = vertices[index] + value; | |
} | |
} | |
// 回転 | |
void Rotate(Vector3 value) | |
{ | |
var center = CalculateCenter(); | |
foreach (var index in targetIndices) | |
{ | |
var vector = vertices[index] - center; // 回転させるために一度ベクトルを作っておく | |
var rotated = Quaternion.Euler(value) * vector; // ベクトルの回転 | |
vertices[index] = rotated + center; // 位置の調整 | |
Debug.Log(vertices[index]); | |
} | |
} | |
void PinchKnob() | |
{ | |
// マウスの差分を取って回転させる | |
var difference = Input.mousePosition - prevMousePos; | |
gameObject.transform.Rotate(new Vector3(0, -difference.x, 0)); | |
float i = inverse ? 1f : -1f; | |
float t = i * difference.x * 0.001f; // 移動の差分値 | |
float r = i * difference.x; | |
switch (controllType) | |
{ | |
case ControllType.TranslateY: | |
Translate(new Vector3(0, t, 0)); | |
break; | |
case ControllType.TranslateX: | |
Translate(new Vector3(t, 0, 0)); | |
break; | |
case ControllType.TranslateZ: | |
Translate(new Vector3(0, 0, t)); | |
break; | |
case ControllType.RotateX: | |
Rotate(new Vector3(r, 0, 0)); | |
break; | |
case ControllType.RotateY: | |
Rotate(new Vector3(0, r, 0)); | |
break; | |
case ControllType.RotateZ: | |
Rotate(new Vector3(0, 0, r)); | |
break; | |
} | |
mesh.vertices = vertices; | |
} | |
// オブジェクトを掴んだかどうか判定する | |
bool IsPinchObject() | |
{ | |
var ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
var hit = new RaycastHit(); | |
var hit_flag = Physics.Raycast(ray, out hit, 10); | |
if (hit_flag && Input.GetMouseButton(0)) | |
{ | |
if (hit.transform == transform) | |
return true; | |
} | |
return false; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
vertices = mesh.vertices; | |
if (IsPinchObject()) | |
PinchKnob(); | |
prevMousePos = Input.mousePosition; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment