gologiusの巣

プログラミングなどの技術メモです。誰かの役に立てるとうれしいです。

Unity 扇型 描画

扇形を描画します.

ちなみにUIのゲージ等に利用したい場合は,UIのImageコンポーネント

  • ImageType -> Filled
  • Fill Meshod -> Radial 360

を利用したほうが楽です.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class Sector : MonoBehaviour
{

    public float radius = 10.0f;
    public float startDegree = 10.0f;
    public float endDegree = 170.0f;
    public int triangleNum = 5;

    void Start()
    {
        MeshFilter m = this.GetComponent<MeshFilter>();
        m.mesh = createMesh();
    }

    void Update()
    {

    }

    Mesh createMesh()
    {
        Mesh mesh = new Mesh();

        //頂点座標計算
        Vector3[] vertices = new Vector3[2 + triangleNum];
        Vector2[] uv = new Vector2[2 + triangleNum];

        vertices[0] = new Vector3(0f, 0f, 0f);
        uv[0] = new Vector2(0.5f, 0.5f);

        float deltaRad = Mathf.Deg2Rad * ((endDegree - startDegree) / (float)triangleNum);
        for (int i = 1; i < 2 + triangleNum; i++)
        {
            float x = Mathf.Cos(deltaRad * (i - 1) + (Mathf.Deg2Rad * startDegree));
            float y = Mathf.Sin(deltaRad * (i - 1) + (Mathf.Deg2Rad * startDegree));
            vertices[i] = new Vector3(
                x * radius,
                y * radius,
                0.0f);

            uv[i] = new Vector2(x * 0.5f + 0.5f, y * 0.5f + 0.5f);
        }
        mesh.vertices = vertices;
        mesh.uv = uv;

        //三角形を構成する頂点のindexを,順に設定していく
        int[] triangles = new int[3 * triangleNum];
        for (int i = 0; i < triangleNum; i++)
        {
            triangles[(i * 3)] = 0;
            triangles[(i * 3) + 1] = i + 1;
            triangles[(i * 3) + 2] = i + 2;
        }
        mesh.triangles = triangles;

        return mesh;
    }
}

UVの設定までしないとテクスチャが表示されません.

使い方は適当な空のGameObjectにこのスクリプトを追加するだけです.

実行結果は以下の通り. あと,マテリアルのシェーダーをSprites/Defaultにしておかないとテクスチャが表示されなかったので注意. f:id:kamiwo_koete:20170313004403p:plain

参考

http://www.itdadao.com/articles/c15a1049295p0.html

nanmo.hateblo.jp

Unity5.5でAndroid用にビルドする

本題ではないエラーその1

ビルドする際にこんなエラーが出た

Error building Player because scripts have compile errors in the editor

エラーがあるスクリプトがあるので直せとのこと. ただ,エラー表示が出てない. とりあえず,エディタを再起動したらエラーが出力されるようになった.

僕の場合は,外部アセットで使っていないデモ用スクリプトがエラーの原因だったので,そのまま消した.

本題ではないエラーその2

「PlayerSettings->Identification->Bundle Identifer」がデフォルトになってのでオリジナルにした.

これは初心者がよくやりそう

本題

ビルドはできたが,以下のエラーで起動できない.

your hardware does not support this application 

環境はAndroid 6.0, HTC HTV31

このエラーについては以下の通り

Failure to initialize! Your hardware does not support this application, sorry! - Unity Answers

そしてこれを参考に,PlayerSettingsの以下の図の場所をforce internalに変更する f:id:kamiwo_koete:20170305220807p:plain

そうすると動いた

OpencCV 3 &quot; Ptr&lt;FeatureDetector&gt; blobDetector = new SimpleBlobDetector(params);&quot; が使えない

OpenCVで,Circle Gridの検出などで,検出器のパラメータを変えたい場合,

answers.opencv.org

なんかを参考にすると,エラーが出た.

SimpleBlobDetector::Params params;
params.maxArea = paramMaxArea; // 100 * 100
params.minArea = paramMinArea; // 10 * 10
Ptr<FeatureDetector> blobDetector = new SimpleBlobDetector(params); //ここで引数のエラーがでる

なんでかと調べてたら,OpenCV 3以降は上記の方法ではダメみたいですね. ここに解決策がありました. c++ - opencv 3, blobdetection, The function/feature is not implemented () in detectAndCompute - Stack Overflow

Ptr<cv::SimpleBlobDetector> detector = cv::SimpleBlobDetector::create(params); 

にすればコンパイル通ります.

Python+OpenCVで動画のトリミングツールを作った

FPS(240FPSとか)のトリミングツールが欲しかったが,よさげのがパッとみつからなかったので自分で作った

 

OpenCVSample/Trimming.py at master · gologius/OpenCVSample · GitHub

 

awsdキーで全部の操作ができる.

自分用につくったのでクオリティはお察し

Python cv2.connectedComponentsWithStats

返り値で迷った.というかOpenCVのリファレンスPythonに厳しくないですか?

OpenCVSample/Labeling.py at master · gologius/OpenCVSample · GitHub

# -*- coding: utf-8 -*-

import cv2
import numpy as np


img = np.zeros((500,500,3),dtype=np.uint8)
for i in xrange(1,5):
    img = cv2.circle(img, (i*80,i*80), 5, (255,255,255), -1)     
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

labelnum, labelimg, contours, GoCs = cv2.connectedComponentsWithStats(gray)

for label in xrange(1,labelnum):
    x,y = GoCs[label]
    img = cv2.circle(img, (int(x),int(y)), 1, (0,0,255), -1)    
    
    x,y,w,h,size = contours[label]
    img = cv2.rectangle(img, (x,y), (x+w,y+h), (255,255,0), 1)    
    
    
print "label num ", labelnum 
print "contours ", contours
print "Gravity of Centers ", GoCs
cv2.imshow("img", img)
cv2.waitKey(-1)
cv2.imwrite("labeling.png",img)

f:id:kamiwo_koete:20161119214434p:plain

これでラベル数,ラベル付けされた画像,各ラベルを包括する矩形たち,各ラベルの重心がとれる. contours(包括する矩形)は最後にsize(矩形の面積,ピクセル数)が入ってるので注意. またcv2.circleに突っ込む場合,座標はtupleかつintでないと死ぬ

error C1001: コンパイラで内部エラーが発生しました。 とバトルした話

普通のC++で製作していたものにGUIつけようと思って,Visual StudioC++/CLI を用いて移植をしていたときに起きたエラーです.

ググっても情報量が少ない,状況も解決策もバラバラそもそもエラー説明が役に立たないのでかなり苦労しました.

私の場合の解決策は,自作クラスAを引数にする関数があり,その関数が参照渡しにすることでした.

つまり

class A 
{
}
class B
{
  void calc(A x, A y);
}

になっていたのを

class A
{
}
class B
{
  void calc(A &x, A &y)
}

にしたら直りました.

また,私の場合はEigenを class A で使用していたため,

class A
{
  void add(Eigen::Vector3f s, Eigen::Vector3f t)
}
を
class A
{
  void add(Eigen::Vector3f &s, Eigen::Vector3f &t)
}

にする必要がありました.

メタセコイア ブーリアン 失敗する条件

メタセコイアVer4.5.4の話です.

 

失敗する条件として考えられるのは

・オブジェクトが完全に重なっている場合

完全に重なっていると気づきにくい.

 

・面の方向が統一されていない

統一してください

 

思いついたら追記します.

 

解決策

・頂点数を減らしてみる

よくわからんがこれで出来た時があった.