質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

673閲覧

Unity GLで描いた線に当たり判定を付けることはできますか?

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2018/11/30 14:45

編集2018/12/04 09:33

先日フィードバック提示機能について質問した者です.
こちらの機能について,下記のようなことができないかと考えて質問しています.
前提として,下記サイトを参考に,赤色の線と緑色の〇を人物に重ねてリアルタイムに描画しています.(下記コード参照)
https://docs.unity3d.com/ja/current/ScriptReference/GL.html

(画像)左にお手本,右に学習者

実現したいこと:当たり判定のついた緑色の〇に変更することはできるでしょうか.
具体的には,透明なオブジェクトをWebカメラを映し出している領域(画像右側)に置いて,画像の緑色の〇に当たり判定を付け,緑色の〇がオブジェクトの範囲(当たり判定を付けた範囲)に入っていなかったらフィードバック提示して,オブジェクトの範囲に入っていたら,フィードバック提示しないというのはできるでしょうか.
出来るのであれば,ご教授お願い致します.
Unityもプログラミングも初心者なので,詳しく教えていただけると助かります.

緑の〇,赤の線を描画しているスクリプト

C#

1using System; 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class GLRenderer : MonoBehaviour 7{ 8 static Material lineMaterial; 9 PoseNet posenet = new PoseNet(); 10 11 void Start() 12 { 13 14 } 15 16 static void CreateLineMaterial() 17 { 18 if (!lineMaterial) 19 { 20 Shader shader = Shader.Find("Hidden/Internal-Colored"); 21 lineMaterial = new Material(shader); 22 lineMaterial.hideFlags = HideFlags.HideAndDontSave; 23 lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); 24 lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); 25 lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off); 26 lineMaterial.SetInt("_ZWrite", 0); 27 } 28 } 29 //ボーン情報を赤線で表示 30 public void DrawResults(PoseNet.Pose[] poses) 31 { 32 CreateLineMaterial(); 33 lineMaterial.SetPass(0); 34 GL.PushMatrix(); 35 GL.MultMatrix(transform.localToWorldMatrix); 36 GL.Begin(GL.QUADS); 37 GL.Color(Color.red); 38 float minPoseConfidence = 0.5f; 39 40 foreach (var pose in poses) 41 { 42 //DrawResults(poses); 43 if (pose.score >= minPoseConfidence) 44 { 45 //DrawKeypoint(pose.keypoints, 46 // minPoseConfidence, 0.02f); 47 DrawSkeleton(pose.keypoints, 48 minPoseConfidence, 0.02f); 49 } 50 } 51 52 GL.End(); 53 GL.PopMatrix(); 54 55 foreach (var pose in poses) 56 { 57 //DrawResults(poses); 58 if (pose.score >= minPoseConfidence) 59 { 60 DrawKeypoint(pose.keypoints, 61 minPoseConfidence, 0.02f); 62 } 63 } 64 } 65 66 //各関節部分を緑色の○で表示 67 public void DrawKeypoint(PoseNet.Keypoint[] keypoints, float minConfidence, float scale) 68 { 69 float radius = 0.08f; 70 71 foreach (var keypoint in keypoints) 72 { 73 74 if (keypoint.score < minConfidence) { continue; } 75 76 GL.PushMatrix(); 77 lineMaterial.SetPass(0); 78 GL.MultMatrix(transform.localToWorldMatrix); 79 GL.Begin(GL.LINES); 80 GL.Color(Color.green); 81 82 //Debug.Log("keypoint.score = " + keypoint.score); 83 Debug.Log("Names = " + keypoint.part);//各部位の名称 84 Debug.Log("keypoint.position.x = " + keypoint.position.x);//各部位のx座標 85 Debug.Log("keypoint.position.y = " + keypoint.position.y);//各部位のy座標 86 87 for (float theta = 0.0f; theta < (2 * Mathf.PI); theta += 0.01f) 88 { 89 GL.Vertex3( 90 Mathf.Cos(theta) * radius + keypoint.position.x * scale, 91 Mathf.Sin(theta) * radius + keypoint.position.y * scale, 0f); 92 } 93 GL.End(); 94 GL.PopMatrix(); 95 } 96 } 97 98 public void DrawSkeleton(PoseNet.Keypoint[] keypoints, float minConfidence, float scale) 99 { 100 var adjacentKeyPoints = posenet.GetAdjacentKeyPoints( 101 keypoints, minConfidence); 102 103 foreach (var keypoint in adjacentKeyPoints) 104 { 105 DrawLine2D(new Vector2(keypoint.Item1.position.x * scale, keypoint.Item1.position.y * scale), 106 new Vector2(keypoint.Item2.position.x * scale, keypoint.Item2.position.y * scale), 0.03f); 107 108 } 109 } 110 111 void DrawLine2D(Vector3 v0, Vector3 v1, float lineWidth) 112 { 113 Vector3 n = ((new Vector3(v1.y, v0.x, 0.0f)) - (new Vector3(v0.y, v1.x, 0.0f))).normalized * lineWidth; 114 GL.Vertex3(v0.x - n.x, v0.y - n.y, 0.0f); 115 GL.Vertex3(v0.x + n.x, v0.y + n.y, 0.0f); 116 GL.Vertex3(v1.x + n.x, v1.y + n.y, 0.0f); 117 GL.Vertex3(v1.x - n.x, v1.y - n.y, 0.0f); 118 119 } 120} 121

当たり判定を付けて,フィードバックを提示するためのスクリプト

C#

1using UnityEngine; 2using System.Collections; 3 4public class FeedBackGL : MonoBehaviour 5{ 6 public Canvas FBCanvas = null; 7 public Canvas DialogCanvas = null; 8 public Canvas DialogCanvas2 = null; 9 10 public bool is_pressing = false; 11 Collider m_ObjectCollider; 12 13 /*public GameObject fb_0; 14 public GameObject fb_1; 15 public GameObject fb_2; 16 public GameObject fb_3; 17 public GameObject fb_4; 18 public GameObject fb_5; 19 public GameObject fb_6; 20 public GameObject fb_7; 21 public GameObject fb_8; 22 */ 23 //GLRenderer gl = new GLRenderer(); 24 25 // Use this for initialization 26 void Start() 27 { 28 29 // FBを表示するときまで、 FBCanvas を無効にしておく。 30 if (FBCanvas != null) 31 { 32 FBCanvas.enabled = false; 33 } 34 // ダイアログを表示するときまで、 DialogCanvas を無効にしておく。 35 if (DialogCanvas != null) 36 { 37 DialogCanvas.enabled = false; 38 } 39 // ダイアログを表示するときまで、 DialogCanvas2 を無効にしておく。 40 if (DialogCanvas2 != null) 41 { 42 DialogCanvas2.enabled = false; 43 } 44 m_ObjectCollider = GetComponent<Collider>(); 45 m_ObjectCollider.isTrigger = true; 46 } 47 /* 48 public void OnClick() 49 { 50 Debug.Log("Clicked"); 51 FBCanvas.enabled = true; 52 Global.ObjectActiveChanger("FBCanvas", "Head1", true); 53 } 54 */ 55 void OnTiriggerEnter(Collider other) 56 { 57 if( other.gameObject.tag == "Head"){ 58 is_pressing = true; 59 } 60 61 if (is_pressing) 62 { 63 Global.ObjectActiveChanger("FBCanvas", "Head2", true); 64 } 65 } 66 void Update() 67 { 68 69 } 70}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

赤い線の配列を用意してKinematicのRigidBody付きのCube又はCapsuleを動的に生成したり削除してみてはどうでしょうか
CubeのlocalScaleのyを線の長さ、rotationはQuarternion.LookAt()で求められると思います

投稿2018/12/18 09:50

bochan2

総合スコア2050

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問