2018年6月7日木曜日

Postprocess only assets which you selects in Unity

We can select the "ToGray" menu item, then the "Do" method is called.
The OnPostprocessTexture method will do postprocessing to only assets which were preprocessed by "Do".
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class ToGrayTexture : AssetPostprocessor
{
    private static List<string> guids_ = new List<string>(8);

    [MenuItem("Assets/Texture/ToGray")]
    private static void Do()
    {
        guids_.Clear();
        Object[] objects = Selection.objects;
        if(objects.Length <= 0) {
            return;
        }
        foreach(Object obj in objects) {
            string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(obj));
            guids_.Add(guid);
        }
        EditorApplication.ExecuteMenuItem("Assets/Reimport");
    }


    void OnPostprocessTexture(Texture2D texture)
    {
        string guid = AssetDatabase.AssetPathToGUID(assetPath);
        if(!guids_.Exists(s => guid == s)) {
            return;
        }
        for(int i=0; i<texture.mipmapCount; ++i) {
            Color32[] colors = texture.GetPixels32(i);
            for(int j=0; j<colors.Length; ++j) {
                int c = (int)(0.299f * colors[j].r + 0.587f * colors[j].g + 0.114f * colors[j].b);
                colors[j].r = colors[j].g = colors[j].b = (byte)Mathf.Clamp(c, 0, 255);
            }
            texture.SetPixels32(colors, i);
        }
        texture.Apply();
        guids_.Remove(guid);
    }
}

2018年4月22日日曜日

Morton Code and Reverse Morton Code

なんとなく、MortonCodeの逆変換が欲しくなりました.
10bit対応しました. 3次元で10bitずつ各次元に割り当てる場合に使います.
typedef int s32;

s32 separateBy2(s32 x)
{
    x &= 0x09249249U;
    x = (x | (x << 8) | (x<<16)) & 0x0300F00FU;
    x = (x | (x << 4) | (x<< 8)) & 0x030C30C3U;
    x = (x | (x << 2) | (x<< 4)) & 0x09249249U;
    return x;
}

s32 combineBy2(s32 x)
{
    x = (x | (x>>2) | (x>> 4)) & 0x030C30C3U;
    x = (x | (x>>4) | (x>> 8)) & 0x0300F00FU;
    x = (x | (x>>8) | (x>>16)) & 0x3FFU;
    return x;
}

s32 mortonCode3(s32 x, s32 y, s32 z)
{
    return separateBy2(x) | (separateBy2(y) << 1) | (separateBy2(z) << 2);
}

void rmortonCode3(s32& x, s32& y, s32& z, s32 w)
{
    x = combineBy2(w);
    y = combineBy2(w>>1);
    z = combineBy2(w>>2);
}

2018年2月19日月曜日

VinoでVNC

XubuntuでVNCサーバを設定するノート

VNCサーバ

vinoをインストールする.
#vino-preferences
で設定起動.
以下のチェックを入れる.
[Sharing]
Allow other users to view your desktop
 Allow other users to control your desktop

以下のチェックを外す.
[Security]
You must confirm each access to this machine

暗号化を使わない場合,
#gsettings set org.gnome.Vino require-encryption false
とする.
サーバーの場所は以下,
#/usr/lib/vino/vino-server
スタートアップに設定しておくと楽.
VNCクライアントからは, IPアドレス:ディスプレイ番号で接続する. ディスプレイ番号が0なら省略可能.

Wake On LAN

まず, BIOS設定からそれっぽいものを有効にする.
ethtoolをインストールして, ツールにインターフェイス名を指定して情報を表示する.
以下のように, ステータスがdになっていれば無効になっている.
#apt install ethtool
#ethtool enp1s0
    ----
    Wake-on: d
    ----
以下のように有効化, 必要なら適当な箇所で有効になるように設定する.
ステータスがgになっていれば有効.
#ethtool -s enp1s0 wol g
    ----
    Wake-on: g
    ----

.Netから叩き起こす
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace WakeOnLAN
{
    class Program
    {
        static void Main(string[] args)
        {
            if(args.Length<2) {
                System.Console.WriteLine("Usage: <global ip address> <mac address>");
                return;
            }
            try {
                IPAddress globalAddress = null;
                if(!IPAddress.TryParse(args[0], out globalAddress)) {
                    return;
                }
                PhysicalAddress physicalAddress = PhysicalAddress.Parse(args[1]);
                using(MemoryStream memoryStream = new MemoryStream())
                using(BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
                using(UdpClient client = new UdpClient()) {
                    for(int i = 0; i<6; ++i) {
                        binaryWriter.Write((byte)0xFF);
                    }
                    byte[] bytes = physicalAddress.GetAddressBytes();
                    for(int i = 0; i<16; ++i) {
                        binaryWriter.Write(bytes);
                    }

                    binaryWriter.Flush();
                    byte[] sendData = memoryStream.ToArray();
                    client.EnableBroadcast = true;
                    client.Send(sendData, sendData.Length, new IPEndPoint(globalAddress, 0));
                    client.Close();
                }

            } catch {
                return;
            }
        }
    }
}

2018年2月7日水曜日

Morton Codeの計算とLBVH

Morton Code

概要

Z階数曲線(モートン符号)は, Z字型に空間を符号化します.
下図のように左上を原点とした場合にZ字型に空間を1次元に符号化します.

2018年1月20日土曜日

Simple zlib Decompression

概要

zlibフォーマットを自力で伸張するライブラリszlib(Github)を作成しました.
できるプログラマはこのようなものを簡単に作ると思いますが, 私は大変苦労したため,
ノートを残しておこうと思います.

2017年11月13日月曜日

Learning of Vulkan Part 01

はじめに

Vulkanを段階的に, 私のために学習していきます.
私のための学習ですので, プログラミング, CGの基礎, 概要は省略します.
画面のクリアまで行いました.

2017年11月4日土曜日

XCBでウィンドウ表示まで

XCBは, Xlibを置き換えるために開発されている, X Serverとお話するためのCライブラリです.
私の目的はVulkanのためなのですが, ノートを書いておきます.