private void ChangeObjectProperty(string textureName, float scale)
        {
            Debug.Log("scale : " + scale);

            if (File.Exists(textureName))
            {
                StartCoroutine(MaxstARUtil.LoadImageFromFileWithSizeAndTexture(Application.streamingAssetsPath + "/../../" + textureName, (width, height, texture) =>
                {
                    Texture2D localTexture = texture;
                    if (texture)
                    {
                        MeshFilter imagePlaneMeshFilter = gameObject.GetComponent <MeshFilter>();
                        if (imagePlaneMeshFilter.sharedMesh == null)
                        {
                            imagePlaneMeshFilter.sharedMesh      = new Mesh();
                            imagePlaneMeshFilter.sharedMesh.name = "ImagePlane";
                        }

                        float imageW = 1.0f;
                        float imageH = (float)height / (float)width;

                        float vertexWidth  = imageW * 0.5f * scale;
                        float vertexHeight = imageH * 0.5f * scale;
                        imagePlaneMeshFilter.sharedMesh.vertices = new Vector3[]
                        {
                            new Vector3(-vertexWidth, 0.0f, -vertexHeight),
                            new Vector3(-vertexWidth, 0.0f, vertexHeight),
                            new Vector3(vertexWidth, 0.0f, -vertexHeight),
                            new Vector3(vertexWidth, 0.0f, vertexHeight)
                        };

                        targetWidth  = imageW * scale;
                        targetHeight = imageH * scale;

                        imagePlaneMeshFilter.sharedMesh.triangles = new int[] { 0, 1, 2, 2, 1, 3 };
                        imagePlaneMeshFilter.sharedMesh.uv        = new Vector2[]
                        {
                            new Vector2(0, 0),
                            new Vector2(0, 1),
                            new Vector2(1, 0),
                            new Vector2(1, 1),
                        };

                        if (gameObject.GetComponent <MeshRenderer>().sharedMaterial == null)
                        {
                            gameObject.GetComponent <MeshRenderer>().sharedMaterial = new Material(Shader.Find("Unlit/Texture"));
                        }

                        gameObject.GetComponent <MeshRenderer>().sharedMaterial.SetTexture("_MainTex", texture);
                    }
                }));
            }
        }
Esempio n. 2
0
        private void TransformBackgroundPlane(Camera camera, Transform planeTransform)
        {
            float widthRatio     = (float)Screen.width / cameraWidth;
            float heightRatio    = (float)Screen.height / cameraHeight;
            float farClipPlane   = camera.farClipPlane * 0.90f;
            float tanFovWidth    = (1.0f / (float)Screen.width) * (float)Screen.height;
            float frustumWidth   = tanFovWidth * farClipPlane * camera.aspect;
            float viewWidth      = (float)frustumWidth / Screen.width;
            float viewHeight     = viewWidth * (widthRatio / heightRatio);
            float flipHorizontal = 1.0f;
            float flipVertical   = 1.0f;

            if (CameraDevice.GetInstance().IsFlipHorizontal())
            {
                flipHorizontal = -1.0f;
            }

            if (CameraDevice.GetInstance().IsFlipVertical())
            {
                flipVertical = -1.0f;
            }

            if (MaxstARUtil.IsDirectXAPI())
            {
                flipHorizontal = -flipHorizontal;
            }

            if (widthRatio > heightRatio)
            {
                planeTransform.localScale = new Vector3(widthRatio * cameraWidth * viewWidth * flipVertical,
                                                        widthRatio * cameraHeight * viewWidth * flipHorizontal, 0.1f);
                planeTransform.localPosition = new Vector3(0.0f, 0.0f, farClipPlane);
            }
            else
            {
                planeTransform.localScale = new Vector3(heightRatio * cameraWidth * viewHeight * flipVertical,
                                                        heightRatio * cameraHeight * viewHeight * flipHorizontal, 0.1f);
                planeTransform.localPosition = new Vector3(0.0f, 0.0f, farClipPlane);
            }

            if (SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D9 ||
                SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D11 ||
                SystemInfo.graphicsDeviceType == GraphicsDeviceType.Direct3D12)
            {
                planeTransform.localScale = new Vector3(planeTransform.localScale.x, -planeTransform.localScale.y, planeTransform.localScale.z);
            }
        }
        private void SetTargetTexture(string textureName)
        {
            if (File.Exists(textureName))
            {
                StartCoroutine(MaxstARUtil.LoadImageFromFileWithSizeAndTexture(Application.streamingAssetsPath + "/../../" + textureName, (width, height, texture) => {
                    Texture2D localTexture = texture;
                    if (localTexture)
                    {
                        MeshFilter imagePlaneMeshFilter = gameObject.GetComponent <MeshFilter>();
                        if (imagePlaneMeshFilter.sharedMesh == null)
                        {
                            imagePlaneMeshFilter.sharedMesh      = new Mesh();
                            imagePlaneMeshFilter.sharedMesh.name = "ImagePlane";
                        }

                        float localWidth  = (float)width * 0.5f;
                        float localHeight = (float)height * 0.5f;
                        imagePlaneMeshFilter.sharedMesh.vertices = new Vector3[]
                        {
                            new Vector3(-localWidth, -localHeight, 0.0f),
                            new Vector3(-localWidth, localHeight, 0.0f),
                            new Vector3(localWidth, -localHeight, 0.0f),
                            new Vector3(localWidth, localHeight, 0.0f)
                        };

                        imagePlaneMeshFilter.sharedMesh.triangles = new int[] { 0, 1, 2, 2, 1, 3 };
                        imagePlaneMeshFilter.sharedMesh.uv        = new Vector2[]
                        {
                            new Vector2(0, 0),
                            new Vector2(0, 1),
                            new Vector2(1, 0),
                            new Vector2(1, 1),
                        };

                        if (gameObject.GetComponent <MeshRenderer>().sharedMaterial == null)
                        {
                            gameObject.GetComponent <MeshRenderer>().sharedMaterial = new Material(Shader.Find("Unlit/Texture"));
                        }

                        gameObject.GetComponent <MeshRenderer>().sharedMaterial.SetTexture("_MainTex", texture);
                    }
                }));
            }
        }