Ejemplo n.º 1
0
        /// <summary>
        /// This Init function also doubles as an update function
        /// in case the position or size of the billboard changes.
        /// </summary>
        public void InitDeviceResources(GraphicsDevice device)
        {
            // Init the vertex buffer.
            if (vbuf == null)
            {
                vbuf = new VertexBuffer(device, typeof(Vertex), 4, BufferUsage.WriteOnly);

                // Create local vertices.
                Vertex[] localVerts = new Vertex[4];
                {
                    localVerts[0] = new Vertex(new Vector3(-size.X / 2.0f, size.Y / 2.0f, 0.0f), new Vector2(0, 0));
                    localVerts[1] = new Vertex(new Vector3(size.X / 2.0f, size.Y / 2.0f, 0.0f), new Vector2(1, 0));
                    localVerts[2] = new Vertex(new Vector3(size.X / 2.0f, -size.Y / 2.0f, 0.0f), new Vector2(1, 1));
                    localVerts[3] = new Vertex(new Vector3(-size.X / 2.0f, -size.Y / 2.0f, 0.0f), new Vector2(0, 1));
                }

                // Copy to vertex buffer.
                vbuf.SetData <Vertex>(localVerts);
            }

            // Init the effect.
            if (effect == null)
            {
                effect = BokuGame.Load <Effect>(BokuGame.Settings.MediaPath + @"Shaders\Billboard");
                ShaderGlobals.RegisterEffect("Billboard", effect);
                effectCache.Load(effect);
                technique = premultipliedAlpha
                    ? effect.Techniques["PremultipliedAlphaColorPass"]
                    : effect.Techniques["NormalAlphaColorPass"];
            }

            // Load the texture.
            if (textureFilename != null)
            {
                // First try and load it as a programming tile.
                texture = CardSpace.Cards.CardFaceTexture(textureFilename);

                // If that didn't work, treat it as an asset name.
                if (texture == null)
                {
                    texture = BokuGame.Load <Texture2D>(BokuGame.Settings.MediaPath + textureFilename);
                }
            }
            if (texture != null && size == Vector2.Zero)
            {
                UiCamera c = new UiCamera();
                size.X = texture.Width / c.Dpi;  // 96 DPI
                size.Y = texture.Height / c.Dpi;
            }
        }   // end of InitDeviceResources()
Ejemplo n.º 2
0
        }   // end of GetHitUV

        /// <summary>
        /// Returns it in world coords of ray through mouse pixel.
        /// </summary>
        /// <param name="camera">UiCamera (orthographic)</param>
        /// <param name="invWorldMatrix">Inverse of object's world matrix.</param>
        /// <param name="useRtCoords">Adjust mouse hit for rendering to a render target?</param>
        /// <returns></returns>
        public static Vector2 GetHitOrtho(UiCamera camera, ref Matrix invWorldMatrix, bool useRtCoords)
        {
            Vector2 mousePosition;

            if (useRtCoords)
            {
                mousePosition = MouseInput.GetMouseInRtCoords();
            }
            else
            {
                mousePosition = new Vector2(MouseInput.Position.X, MouseInput.Position.Y);
            }

            Vector2 hit = Vector2.Zero;

            // Convert from pixels to world units.
            hit.X = mousePosition.X * camera.Width / camera.Resolution.X;
            hit.Y = mousePosition.Y * camera.Height / camera.Resolution.Y;

            // Put origin at center.
            hit.X -= camera.Width / 2.0f;
            hit.Y -= camera.Height / 2.0f;

            // Flip vertical axis.
            hit.Y = -hit.Y;

            Vector3 actualFrom = camera.ActualFrom;
            Vector3 actualAt   = camera.ActualAt;

            // Apply object and camera translation
            hit.X += invWorldMatrix.Translation.X + actualAt.X + camera.Offset.X;
            hit.Y += invWorldMatrix.Translation.Y + actualAt.Y + camera.Offset.Y;

            // Adjust if camera not going along Z axis.
            hit.X *= (float)Math.Cos(Math.Atan(camera.ViewDir.X / camera.ViewDir.Z));
            hit.Y *= (float)Math.Cos(Math.Atan(camera.ViewDir.Y / camera.ViewDir.Z));

            // Need to adjust if object is not at z==0.
            if (invWorldMatrix.Translation.Z != 0)
            {
                float fraction = invWorldMatrix.Translation.Z / (actualFrom.Z - actualAt.Z);
                hit.X -= (actualFrom.X - actualAt.X) * fraction;
                hit.Y -= (actualFrom.Y - actualAt.Y) * fraction;
            }

            return(hit);
        }   // end of GetHitOrtho()