Ejemplo n.º 1
0
    /*
     * PaintToolのVoxelまたはグループVoxelが選択されていて、マウスクリックされたときに呼ばれる。
     * ペイントさせ方は、ぶつかったオブジェクト(Plane)よりも手前のオブジェクトを
     * 「後ろ側から手前に」辿っていき、Voxelを挿入する。
     */
    private void paintVoxels()
    {
        string paintToolName = this.model.getCurrentPaintTool();

        List <Operation> ops = new List <Operation>();
        Ray        ray       = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit       = new RaycastHit();

        if (Physics.Raycast(ray, out hit))
        {
            if (paintToolName.IndexOf(ChainXModel.PAINT_TOOL_VOXEL_ID) > -1)
            {
                //
                //単位Voxelをペイントする
                //
                float   distance      = hit.distance - 0.5f; //ヒットしたRayより少し手前のPointをたどる
                Vector3 hitPointShort = ChainXModel.GetRoundIntPoint(ray.GetPoint(distance));
                int     textureType   = int.Parse(this.paintTool.GetComponent <Text>().text);
                ops.Add(new Operation(this.socket.getID(), Operation.INSERT,
                                      "{\"posID\": \"" + ChainXModel.CreatePosID(hitPointShort) +
                                      "\", \"textureType\":\"" + textureType + "\"}"
                                      ));
            }
            else if (paintToolName.IndexOf(ChainXModel.PAINT_TOOL_GROUP_ID) > -1)
            {
                //
                //グループVoxelsをペイントする
                //
                float   cursor_d      = hit.distance - 0.5f;
                Vector3 hitPointShort = ChainXModel.GetRoundIntPoint(ray.GetPoint(cursor_d));

                //もっともヒットポイントに近いオブジェクトをグループVoxelsの中から見つける
                GameObject closeObjToHitPoint = null;
                float      minDistance        = float.MaxValue;
                GameObject groupObj           = GameObject.Find(paintToolName);
                foreach (Transform aPart in groupObj.transform)
                {
                    float d = Vector3.Distance(aPart.gameObject.transform.position, hitPointShort);
                    if (d < minDistance)
                    {
                        minDistance        = d;
                        closeObjToHitPoint = aPart.gameObject;
                    }
                }

                bool    put_enable;
                Vector3 diffV        = Vector3.zero;
                string  posIDs       = "";
                string  textureTypes = "";
                while (cursor_d > 0)
                {
                    posIDs        = "";
                    textureTypes  = "";
                    hitPointShort = ChainXModel.GetRoundIntPoint(ray.GetPoint(cursor_d));
                    diffV         = hitPointShort - closeObjToHitPoint.transform.position;
                    put_enable    = true;
                    foreach (Transform aPart in groupObj.transform)
                    {
                        Vector3 movingV = aPart.gameObject.transform.position + diffV;
                        if (GameObject.Find(Util.CreatePosID(movingV)) != null)
                        {
                            put_enable = false;
                            break;
                        }
                        posIDs       += Util.CreatePosID(movingV) + Const.SPLIT_CHAR;
                        textureTypes += aPart.gameObject.GetComponent <Text>().text + Const.SPLIT_CHAR;
                    }
                    if (put_enable)
                    {
                        break;
                    }
                    cursor_d--;
                }
                posIDs       = posIDs.TrimEnd(Const.SPLIT_CHAR);
                textureTypes = textureTypes.TrimEnd(Const.SPLIT_CHAR);
                this.selectedObjects.Remove(groupObj);

                string gid = ChainXModel.CreateGID();
                ops.Add(new Operation(this.socket.getID(), Operation.INSERT_ALL,
                                      "{\"posIDs\": \"" + posIDs +
                                      "\", \"gid\": \"" + gid +
                                      "\", \"textureTypes\":\"" + textureTypes + "\"}")
                        );
                ops.Add(new Operation(this.socket.getID(), Operation.JOIN_ALL,
                                      "{\"posIDs\": \"" + posIDs +
                                      "\", \"gid\": \"" + gid + "\"}")
                        );
            }

            long ts = ops[0].getTimestamp();
            for (int i = 0; i < ops.Count; i++)
            {
                ops[i].setTimestamp(ts + i);                 //できる限り1に近づけないと間に入り込まれる
                this.ApplyChainVoxel(ops[i]);
            }
            //Debug.DrawLine(ray.origin, hitPointShort, Color.red, 60.0f, true); //レーザービーム
        }
    }
Ejemplo n.º 2
0
    void Update()
    {
        //Debug.Log(DateTime.UtcNow.Ticks);

        lock (ChainXController.thisLock) {
            this.UpdateVoxels();
            //this.SetUpGUICompornets ();

            //スクリーンサイズが変更された時、実行される
            if (this.screenSize.width != Screen.width ||
                this.screenSize.height != Screen.height)
            {
                this.SetPositionOfPaintTool();
                this.screenSize.width  = Screen.width;
                this.screenSize.height = Screen.height;
            }

            //マウスクリックの際の処理
            if (Input.GetMouseButtonDown(Const.MOUSE_LEFT_CLICK))
            {
                if (Input.GetKey(KeyCode.LeftAlt))
                {
                    //Do nothing for Orbit, Zoom, etc..
                }
                else if (this.clickUI())
                {
                    this.cleanSelectedObjects();                     //Voxelの選択解除
                }
                else if (this.paintTool.name == Const.UI_SELECTING_POINTER_NAME)
                {
                    this.clickVoxel();                     //マウスクリックしてオブジェクトを選択する
                }
                else
                {
                    //VoxelまたはVoxelsが選択中のとき
                    this.paintVoxels();                     //VoxelまたはVoxelsをペイントする
                }
            }

            //キーボード操作(移動、グループ作成、解除、削除)
            Operation o = null;
            if (this.selectedObjects.Count > 0)
            {
                //3. MOVEの移動方向を追加
                Vector3 arrowV = Vector3.zero;
                if (Input.GetKeyUp(KeyCode.UpArrow))
                {
                    arrowV = new Vector3(1, 0, 0);
                }
                else if (Input.GetKeyUp(KeyCode.DownArrow))
                {
                    arrowV = new Vector3(-1, 0, 0);
                }
                else if (Input.GetKeyUp(KeyCode.RightArrow))
                {
                    arrowV = new Vector3(0, 0, -1);
                }
                else if (Input.GetKeyUp(KeyCode.LeftArrow))
                {
                    arrowV = new Vector3(0, 0, 1);
                }
                else if (Input.GetKeyUp(KeyCode.K))
                {
                    arrowV = new Vector3(0, 1, 0);
                }
                else if (Input.GetKeyUp(KeyCode.J))
                {
                    arrowV = new Vector3(0, -1, 0);
                }

                if (arrowV == Vector3.zero)
                {
                    if (Input.GetKeyUp(KeyCode.D))
                    {
                        //
                        // VoxelまたはGroupVoxelを削除する
                        //
                        if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand) ||
                            Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.RightCommand))
                        {
                            List <Operation> deleteOps = this.model.CreateDeleteOperation(this.selectedObjects);
                            foreach (Operation op in deleteOps)
                            {
                                this.ApplyChainVoxel(op);
                            }
                        }
                    }
                    else if (Input.GetKeyDown(KeyCode.G))
                    {
                        //
                        // グループの参加、離脱
                        // グループの中にグループは、今は処理しない!
                        //
                        string gid;
                        if (this.selectedObjects.Count == 1)
                        {
                            if (this.selectedObjects [0].transform.childCount > 0)
                            {
                                //選択したオブジェクトが一つで、それがグループである場合、グループを解除
                                gid = this.selectedObjects [0].transform.root.name;
                                o   = new Operation(this.socket.getID(), Operation.LEAVE_ALL, "{\"posIDs\": \"" +
                                                    this.model.getPosIDsFromObj(this.selectedObjects[0]) + "\", \"gid\": \"" + gid +
                                                    "\"}");
                                this.ApplyChainVoxel(o);
                            }
                            else
                            {
                            }                                      //何もしない
                        }
                        else
                        {
                            gid = ChainXModel.CreateGID();
                            o   = new Operation(this.socket.getID(), Operation.JOIN_ALL, "{\"posIDs\": \"" +
                                                this.model.getPosIDsFromObjects(this.selectedObjects) + "\", \"gid\": \"" + gid +
                                                "\"}");
                            this.ApplyChainVoxel(o);
                        }
                    }
                }

                else
                {
                    /*
                     * foreach (GameObject anObj in this.selectedObjects) {
                     *      Debug.Log (anObj.name);
                     * }
                     */
                    //Debug.Log("MOVE");
                    this.selectedObjects = Util.ArrangeGameObjects(this.selectedObjects, arrowV);

                    List <Operation> moveOps = this.model.CreateMoveOperations(this.selectedObjects, arrowV);
                    foreach (Operation moveOp in moveOps)
                    {
                        this.ApplyChainVoxel(moveOp);
                    }
                }
            }
            if (o != null)
            {
                //this.cv.show();
                //this.cv.stt.show();
            }
        }
    }
Ejemplo n.º 3
0
    public IEnumerator Listen()
    {
        //this.SendBinary(-1, Const.START_BINARY_HEADER);

        byte[]   msgBinary = null;
        string[] filepaths = { "", "" };

        while (true)
        {
            msgBinary = this.ws.Recv();
            if (msgBinary != null)
            {
                //Debug.Log (Encoding.ASCII.GetString(msgBinary));
                //byte[] idBinary = this.getIdFromEndUntilAt(ref receivedBinary);
                //int destID = System.BitConverter.ToInt32(idBinary, 0); //送り先!!
                //byte[] msgBinary = this.getOperationFromEndUntilAt(ref receivedBinary);

                if (this.partEqual(ref msgBinary, ref Const.OPERATION_BINARY_HEADER))
                {
                    string line = Encoding.UTF8.GetString(msgBinary);
                    line = line.Remove(0, Const.OPERATION_HEADER.Length).Trim();
                    Operation op = Operation.FromJson(line);
                    this.controller.cv.apply(op, ChainVoxel.REMOTE_OPERATION);
                }
                else if (this.partEqual(ref msgBinary, ref Const.JOIN_BINARY_HEADER))
                {
                    string msg = Encoding.UTF8.GetString(msgBinary);
                    msg = msg.Replace(Const.JOIN_HEADER, "");
                    int intID = int.Parse(msg);
                    if (!this.siteIDs.Contains(intID))
                    {
                        this.siteIDs.Add(intID);
                    }
                    //foreach (int sid in this.siteIDs) { Debug.Log("sid: " + sid); }
                    //Debug.Log("this.id:" + this.id);
                    //Debug.Log("joined id:" + intID);
                }
                else if (this.partEqual(ref msgBinary, ref Const.ID_LIST_BINARY_HEADER))
                {
                    string   msg     = Encoding.UTF8.GetString(msgBinary);
                    int      atIndex = msg.IndexOf(Const.MSG_SPLIT_CHAR);
                    string   idsLine = msg.Substring(atIndex + 1);                   //atIndex+1から最後まで
                    String[] strIDs  = idsLine.Split(Const.SPLIT_CHAR);
                    foreach (string strID in strIDs)
                    {
                        int intID = int.Parse(strID);
                        if (!this.siteIDs.Contains(intID))
                        {
                            this.siteIDs.Add(intID);
                        }
                    }
                    this.leaderID = this.siteIDs[0];
                    this.id       = this.siteIDs[this.siteIDs.Count - 1];
                }
                else if (this.partEqual(ref msgBinary, ref Const.SOME_FILE_BINARY_HEADER))
                {
                    string     filepath = this.getPathUntilAt(ref msgBinary);
                    int        start_i  = Const.SOME_FILE_HEADER.Length + filepath.Length + 1;             //1='@'
                    string     path     = Application.persistentDataPath + "/" + filepath;
                    FileStream fs       = new FileStream(path, FileMode.Create, FileAccess.Write);
                    fs.Write(msgBinary, start_i, msgBinary.Length - start_i);
                    fs.Close();

                    if (Path.GetExtension(path) == ".txt")
                    {
                        //Debug.Log (path);
                        this.controller.cv.LoadSavedData(path);
                    }
                    else if (Path.GetExtension(path) == ".obj")
                    {
                        filepaths[0] = path;
                    }
                    else if (Path.GetExtension(path) == ".jpg")
                    {
                        filepaths[1] = path;
                    }

                    if (filepaths[0] != "" && filepaths[1] != "")
                    {
                        //
                        // When all dependent files of a 3d obj have been collected,
                        // Build the 3d obj.
                        //
                        string[] posIDs = ObjLoadHelper.LoadObj(filepaths, new Vector3(0, 5, 0));
                        //string[] posIDs = ObjLoadHelper.LoadOnlyObj(filepaths[0], new Vector3 (0,5,0));
                        Operation op = new Operation(0, Operation.INSERT_POLYGON,
                                                     "{\"posIDs\": \"" + Util.GetCommaLineFrom(posIDs) +
                                                     "\", \"gid\": \"" + ChainXModel.CreateGID() +
                                                     "\", \"objPath\":\"" + filepaths[0] + "\"}");
                        //Debug.Log(Operation.ToJson(op));
                        this.controller.cv.apply(op, ChainVoxel.LOCAL_OPERATION);

                        filepaths = new string[] { "", "" };                      //Clear for the next 3d objs
                    }
                }
            }

            if (this.ws.error != null)
            {
                Debug.LogError("Error: " + this.ws.error);
                break;
            }
            yield return(0);
        }
        this.ws.Close();
    }