コード例 #1
0
    public bool StartHandTracking()
    {
        if (this.Initialized && !handTrackingEnabled)
        {
            ulong feature = GetSupportedFeatures();
            PrintInfoLog("StartHandTracking() supported feature: " + feature);
            if ((feature & (ulong)WVR_SupportedFeature.WVR_SupportedFeature_HandTracking) == 0)
            {
                return(false);
            }

            WVR_Result result = Interop.WVR_StartHandTracking();
            handTrackingEnabled = result == WVR_Result.WVR_Success ? true : false;
            PrintInfoLog("StartHandTracking() " + result);
        }

        return(handTrackingEnabled);
    }
コード例 #2
0
        private void LoadModel(string renderModelName)
        {
            Debug.Log(transform.parent.parent.name + " Try LoadModel " + renderModelName);
#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                Debug.LogWarning("LoadModel failed! This function only works in playing mode");
                return;
            }
#endif
            if (string.IsNullOrEmpty(loadedModelName) && string.IsNullOrEmpty(renderModelName))
            {
                return;
            }

            if (loadedModelName == renderModelName)
            {
                return;
            }

            if (m_loadingRenderModels.Contains(renderModelName))
            {
                return;
            }

            ClearModel();

            if (!m_isAppQuit && !string.IsNullOrEmpty(renderModelName))
            {
                Debug.Log(transform.parent.parent.name + " LoadModel " + renderModelName);
                m_loadingRenderModels.Add(renderModelName);

#if VIU_WAVEXR_ESSENCE_CONTROLLER_MODEL
                IntPtr ctrlModel = IntPtr.Zero;
                int    IntBits   = IntPtr.Size;

                WVR_Result r = Interop.WVR_GetCurrentControllerModel(preferedModelName.Contains("Left") ? WVR_DeviceType.WVR_DeviceType_Controller_Left : WVR_DeviceType.WVR_DeviceType_Controller_Right, ref ctrlModel, true);

                if (r == WVR_Result.WVR_Success)
                {
                    if (ctrlModel != IntPtr.Zero)
                    {
                        WVR_CtrlerModel ctrl = (WVR_CtrlerModel)Marshal.PtrToStructure(ctrlModel, typeof(WVR_CtrlerModel));

                        //Debug.Log("render model name = " + ctrl.name + " , load from asset = " + ctrl.loadFromAsset);

                        WVR_CtrlerCompInfoTable cit = ctrl.compInfos;

                        int szStruct = Marshal.SizeOf(typeof(WVR_CtrlerCompInfo));

                        //Debug.Log("Controller component size = " + cit.size);

                        modelResource.FBXInfo       = new FBXInfo_t[cit.size];
                        modelResource.sectionCount  = cit.size;
                        modelResource.SectionInfo   = new MeshInfo_t[cit.size];
                        modelResource.loadFromAsset = ctrl.loadFromAsset;

                        for (int i = 0; i < cit.size; i++)
                        {
                            WVR_CtrlerCompInfo wcci;

                            if (IntBits == 4)
                            {
                                wcci = (WVR_CtrlerCompInfo)Marshal.PtrToStructure(new IntPtr(cit.table.ToInt32() + (szStruct * i)), typeof(WVR_CtrlerCompInfo));
                            }
                            else
                            {
                                wcci = (WVR_CtrlerCompInfo)Marshal.PtrToStructure(new IntPtr(cit.table.ToInt64() + (szStruct * i)), typeof(WVR_CtrlerCompInfo));
                            }

                            modelResource.FBXInfo[i]     = new FBXInfo_t();
                            modelResource.SectionInfo[i] = new MeshInfo_t();

                            modelResource.FBXInfo[i].meshName    = Marshal.StringToHGlobalAnsi(wcci.name);
                            modelResource.SectionInfo[i]._active = wcci.defaultDraw;

                            //Debug.Log("Controller component name = " + wcci.name + ", tex index = " + wcci.texIndex + ", active= " + modelResource.SectionInfo[i]._active);

                            // local matrix
                            //Matrix4x4 lt = RigidTransform.toMatrix44(wcci.localMat, false);
                            //Matrix4x4 t = RigidTransform.RowColumnInverse(lt);
                            //Debug.Log(" matrix = (" + t.m00 + ", " + t.m01 + ", " + t.m02 + ", " + t.m03 + ")");
                            //Debug.Log(" matrix = (" + t.m10 + ", " + t.m11 + ", " + t.m12 + ", " + t.m13 + ")");
                            //Debug.Log(" matrix = (" + t.m20 + ", " + t.m21 + ", " + t.m22 + ", " + t.m23 + ")");
                            //Debug.Log(" matrix = (" + t.m30 + ", " + t.m31 + ", " + t.m32 + ", " + t.m33 + ")");

                            //curr.FBXInfo[i].matrix = RigidTransform.ToWVRMatrix(t, false);

                            WVR_VertexBuffer vertices = wcci.vertices;

                            if (vertices.dimension == 3)
                            {
                                uint verticesCount = (vertices.size / vertices.dimension);

                                //Debug.Log(" vertices size = " + vertices.size + ", dimension = " + vertices.dimension + ", count = " + verticesCount);

                                modelResource.SectionInfo[i]._vectice = new Vector3[verticesCount];
                                float[] verticeArray = new float[vertices.size];

                                Marshal.Copy(vertices.buffer, verticeArray, 0, verticeArray.Length);

                                int verticeIndex = 0;
                                int floatIndex   = 0;

                                while (verticeIndex < verticesCount)
                                {
                                    modelResource.SectionInfo[i]._vectice[verticeIndex]   = new Vector3();
                                    modelResource.SectionInfo[i]._vectice[verticeIndex].x = verticeArray[floatIndex++];
                                    modelResource.SectionInfo[i]._vectice[verticeIndex].y = verticeArray[floatIndex++];
                                    modelResource.SectionInfo[i]._vectice[verticeIndex].z = verticeArray[floatIndex++] * -1f;

                                    verticeIndex++;
                                }
                            }
                            else
                            {
                                Debug.Log("[WARNING] vertices buffer's dimension incorrect!");
                            }

                            // normals
                            WVR_VertexBuffer normals = wcci.normals;

                            if (normals.dimension == 3)
                            {
                                uint normalsCount = (normals.size / normals.dimension);
                                //Debug.Log(" normals size = " + normals.size + ", dimension = " + normals.dimension + ", count = " + normalsCount);
                                modelResource.SectionInfo[i]._normal = new Vector3[normalsCount];
                                float[] normalArray = new float[normals.size];

                                Marshal.Copy(normals.buffer, normalArray, 0, normalArray.Length);

                                int normalsIndex = 0;
                                int floatIndex   = 0;

                                while (normalsIndex < normalsCount)
                                {
                                    modelResource.SectionInfo[i]._normal[normalsIndex]   = new Vector3();
                                    modelResource.SectionInfo[i]._normal[normalsIndex].x = normalArray[floatIndex++];
                                    modelResource.SectionInfo[i]._normal[normalsIndex].y = normalArray[floatIndex++];
                                    modelResource.SectionInfo[i]._normal[normalsIndex].z = normalArray[floatIndex++];

                                    normalsIndex++;
                                }

                                //Debug.Log(" normals size = " + normals.size + ", dimension = " + normals.dimension + ", count = " + normalsCount);
                            }
                            else
                            {
                                Debug.Log("[WARNING] normals buffer's dimension incorrect!");
                            }

                            // texCoord
                            WVR_VertexBuffer texCoord = wcci.texCoords;

                            if (texCoord.dimension == 2)
                            {
                                uint uvCount = (texCoord.size / texCoord.dimension);
                                //Debug.Log(" texCoord size = " + texCoord.size + ", dimension = " + texCoord.dimension + ", count = " + uvCount);
                                modelResource.SectionInfo[i]._uv = new Vector2[uvCount];
                                float[] texCoordArray = new float[texCoord.size];

                                Marshal.Copy(texCoord.buffer, texCoordArray, 0, texCoordArray.Length);

                                int uvIndex    = 0;
                                int floatIndex = 0;

                                while (uvIndex < uvCount)
                                {
                                    modelResource.SectionInfo[i]._uv[uvIndex]   = new Vector2();
                                    modelResource.SectionInfo[i]._uv[uvIndex].x = texCoordArray[floatIndex++];
                                    modelResource.SectionInfo[i]._uv[uvIndex].y = texCoordArray[floatIndex++];

                                    uvIndex++;
                                }
                            }
                            else
                            {
                                Debug.Log("[WARNING] normals buffer's dimension incorrect!");
                            }

                            // indices
                            WVR_IndexBuffer indices = wcci.indices;
                            //Debug.Log(" indices size = " + indices.size);

                            modelResource.SectionInfo[i]._indice = new int[indices.size];
                            Marshal.Copy(indices.buffer, modelResource.SectionInfo[i]._indice, 0, modelResource.SectionInfo[i]._indice.Length);

                            uint indiceIndex = 0;

                            while (indiceIndex < indices.size)
                            {
                                int tmp = modelResource.SectionInfo[i]._indice[indiceIndex];
                                modelResource.SectionInfo[i]._indice[indiceIndex]     = modelResource.SectionInfo[i]._indice[indiceIndex + 2];
                                modelResource.SectionInfo[i]._indice[indiceIndex + 2] = tmp;
                                indiceIndex += 3;
                            }
                        }

                        // Controller texture section
                        WVR_CtrlerTexBitmapTable wctbt = ctrl.bitmapInfos;
                        //Debug.Log("Controller textures = " + wctbt.size);
                        int bmStruct = Marshal.SizeOf(typeof(WVR_CtrlerTexBitmap));
                        modelResource.modelTextureCount = (int)wctbt.size;
                        modelResource.modelTextureInfo  = new TextureInfo[wctbt.size];
                        modelResource.modelTexture      = new Texture2D[wctbt.size];

                        for (int mt = 0; mt < wctbt.size; mt++)
                        {
                            TextureInfo ct = new TextureInfo();

                            WVR_CtrlerTexBitmap wctb;

                            if (IntBits == 4)
                            {
                                wctb = (WVR_CtrlerTexBitmap)Marshal.PtrToStructure(new IntPtr(wctbt.table.ToInt32() + (bmStruct * mt)), typeof(WVR_CtrlerTexBitmap));
                            }
                            else
                            {
                                wctb = (WVR_CtrlerTexBitmap)Marshal.PtrToStructure(new IntPtr(wctbt.table.ToInt64() + (bmStruct * mt)), typeof(WVR_CtrlerTexBitmap));
                            }

                            //Debug.Log(" [" + mt + "] bitmap width = " + wctb.width);
                            //Debug.Log(" [" + mt + "] bitmap height = " + wctb.height);
                            //Debug.Log(" [" + mt + "] bitmap stride = " + wctb.stride);
                            //Debug.Log(" [" + mt + "] bitmap format = " + wctb.format);
                            // bitmap size
                            var rawImageSize = wctb.height * wctb.stride;

                            ct.modelTextureData = new byte[rawImageSize];
                            Marshal.Copy(wctb.bitmap, ct.modelTextureData, 0, ct.modelTextureData.Length);
                            ct.width  = (int)wctb.width;
                            ct.height = (int)wctb.height;
                            ct.stride = (int)wctb.stride;
                            ct.format = (int)wctb.format;
                            ct.size   = (int)rawImageSize;

                            modelResource.modelTextureInfo[mt] = ct;
                        }

                        // Touchpad section
                        //Debug.Log("[DEBUG] ---  Get touch info from runtime  ---");
                        WVR_TouchPadPlane wtpp = ctrl.touchpadPlane;

                        modelResource.TouchSetting = new TouchSetting();
                        modelResource.TouchSetting.touchCenter.x = wtpp.center.v0 * 100f;
                        modelResource.TouchSetting.touchCenter.y = wtpp.center.v1 * 100f;
                        modelResource.TouchSetting.touchCenter.z = (-1.0f * wtpp.center.v2) * 100f;
                        //Debug.Log(" touchCenter! x: " + modelResource.TouchSetting.touchCenter.x + " ,y: " + modelResource.TouchSetting.touchCenter.y + " ,z: " + modelResource.TouchSetting.touchCenter.z);

                        modelResource.TouchSetting.raidus = wtpp.radius * 100;

                        modelResource.TouchSetting.touchptHeight = wtpp.floatingDistance * 100;

                        modelResource.isTouchSetting = wtpp.valid;

                        modelResource.TouchSetting.touchPtU.x = wtpp.u.v0;
                        modelResource.TouchSetting.touchPtU.y = wtpp.u.v1;
                        modelResource.TouchSetting.touchPtU.z = wtpp.u.v2;

                        modelResource.TouchSetting.touchPtV.x = wtpp.v.v0;
                        modelResource.TouchSetting.touchPtV.y = -1.0f * wtpp.v.v1;
                        modelResource.TouchSetting.touchPtV.z = wtpp.v.v2;

                        modelResource.TouchSetting.touchPtW.x = wtpp.w.v0;
                        modelResource.TouchSetting.touchPtW.y = wtpp.w.v1;
                        modelResource.TouchSetting.touchPtW.z = -1.0f * wtpp.w.v2;
                        //Debug.Log(" Floating distance : " + modelResource.TouchSetting.touchptHeight);

                        //Debug.Log(" touchPtW! x: " + modelResource.TouchSetting.touchPtW.x + " ,y: " + modelResource.TouchSetting.touchPtW.y + " ,z: " + modelResource.TouchSetting.touchPtW.z);
                        //Debug.Log(" touchPtU! x: " + modelResource.TouchSetting.touchPtU.x + " ,y: " + modelResource.TouchSetting.touchPtU.y + " ,z: " + modelResource.TouchSetting.touchPtU.z);
                        //Debug.Log(" touchPtV! x: " + modelResource.TouchSetting.touchPtV.x + " ,y: " + modelResource.TouchSetting.touchPtV.y + " ,z: " + modelResource.TouchSetting.touchPtV.z);
                        //Debug.Log(" raidus: " + modelResource.TouchSetting.raidus);
                        //Debug.Log(" isTouchSetting: " + modelResource.isTouchSetting);

                        // Battery section

                        //Debug.Log("[DEBUG] ---  Get battery info from runtime  ---");
                        WVR_BatteryLevelTable wblt = ctrl.batteryLevels;

                        List <BatteryIndicator> batteryTextureList = new List <BatteryIndicator>();
                        modelResource.batteryTextureList = batteryTextureList;

                        //Debug.Log("Battery levels = " + wblt.size);

                        int btStruct = Marshal.SizeOf(typeof(WVR_CtrlerTexBitmap));
                        int sizeInt  = Marshal.SizeOf(typeof(int));

                        for (int b = 0; b < wblt.size; b++)
                        {
                            WVR_CtrlerTexBitmap batteryImage;
                            int batteryMin = 0;
                            int batteryMax = 0;

                            if (IntBits == 4)
                            {
                                batteryImage = (WVR_CtrlerTexBitmap)Marshal.PtrToStructure(new IntPtr(wblt.texTable.ToInt32() + (btStruct * b)), typeof(WVR_CtrlerTexBitmap));
                                batteryMin   = (int)Marshal.PtrToStructure(new IntPtr(wblt.minTable.ToInt32() + (sizeInt * b)), typeof(int));
                                batteryMax   = (int)Marshal.PtrToStructure(new IntPtr(wblt.maxTable.ToInt32() + (sizeInt * b)), typeof(int));
                            }
                            else
                            {
                                batteryImage = (WVR_CtrlerTexBitmap)Marshal.PtrToStructure(new IntPtr(wblt.texTable.ToInt64() + (btStruct * b)), typeof(WVR_CtrlerTexBitmap));
                                batteryMin   = (int)Marshal.PtrToStructure(new IntPtr(wblt.minTable.ToInt64() + (sizeInt * b)), typeof(int));
                                batteryMax   = (int)Marshal.PtrToStructure(new IntPtr(wblt.maxTable.ToInt64() + (sizeInt * b)), typeof(int));
                            }

                            BatteryIndicator tmpBI = new BatteryIndicator();
                            tmpBI.level = b;
                            tmpBI.min   = (float)batteryMin;
                            tmpBI.max   = (float)batteryMax;

                            var batteryImageSize = batteryImage.height * batteryImage.stride;

                            tmpBI.batteryTextureInfo = new TextureInfo();
                            tmpBI.batteryTextureInfo.modelTextureData = new byte[batteryImageSize];
                            Marshal.Copy(batteryImage.bitmap, tmpBI.batteryTextureInfo.modelTextureData, 0, tmpBI.batteryTextureInfo.modelTextureData.Length);
                            tmpBI.batteryTextureInfo.width  = (int)batteryImage.width;
                            tmpBI.batteryTextureInfo.height = (int)batteryImage.height;
                            tmpBI.batteryTextureInfo.stride = (int)batteryImage.stride;
                            tmpBI.batteryTextureInfo.format = (int)batteryImage.format;
                            tmpBI.batteryTextureInfo.size   = (int)batteryImageSize;
                            tmpBI.textureLoaded             = true;
                            //Debug.Log(" Battery Level[" + tmpBI.level + "] min: " + tmpBI.min + " max: " + tmpBI.max + " loaded: " + tmpBI.textureLoaded + " w: " + batteryImage.width + " h: " + batteryImage.height + " size: " + batteryImageSize);

                            batteryTextureList.Add(tmpBI);
                        }
                        modelResource.isBatterySetting = true;

                        //Debug.Log("WVR_ReleaseControllerModel, ctrlModel IntPtr = " + ctrlModel.ToInt32());

                        //Debug.Log("Call WVR_ReleaseControllerModel");
                        Interop.WVR_ReleaseControllerModel(ref ctrlModel);
                    }
                    else
                    {
                        Debug.Log("[WARNING] WVR_GetCurrentControllerModel return model is null");
                    }

                    wfef = new WaitForEndOfFrame();

                    ImgMaterial = new Material(Shader.Find("Unlit/Texture"));
                    StartCoroutine(SpawnRenderModel());
                }
#endif
            }
        }