public void DrawXNAModel(Model model, IARCamera camera, Matrix world, float alpha) { Matrix[] transforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(transforms); //game.GraphicsDevice.RenderState.AlphaBlendEnable = true; //game.GraphicsDevice.RenderState.AlphaTestEnable = true; Game.GraphicsDevice.BlendState = BlendState.AlphaBlend; Game.GraphicsDevice.DepthStencilState = DepthStencilState.Default; foreach (ModelMesh mesh in model.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = transforms[mesh.ParentBone.Index] * world; effect.View = camera.View; //effect.FogEnabled = true; //effect.FogEnd = 12000; //effect.FogStart = 2000; effect.Alpha = alpha; effect.Projection = camera.Projection; } mesh.Draw(); } }
/// <summary> /// 获得鼠标对应投影在屏幕上的射线 /// </summary> /// <param name="camera"></param> /// <param name="viewport"></param> /// <returns></returns> public static Ray GetMouseRay(IARCamera camera, Viewport viewport) { Vector3 near = new Vector3(currentMouseState.X, currentMouseState.Y, 0.0f); Vector3 far = new Vector3(currentMouseState.X, currentMouseState.Y, 1.0f); Vector3 near_transformed = viewport.Unproject(near, camera.Projection, camera.View, Matrix.Identity); Vector3 far_transformed = viewport.Unproject(far, camera.Projection, camera.View, Matrix.Identity); Vector3 direction = Vector3.Normalize(far_transformed - near_transformed); return(new Ray(near_transformed, direction)); }
public ARGraphDealer(ARXNAGame game) : base(game) { painter = new ARPainter(game); modelDrawer = new ARModelDrawer(game); projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60.0f), //待编辑 1.6f, 1.0f, 5000.0f); camera = new ARBaseCamera(new Vector3(0, 45, 35), Vector3.Zero, projection); }
private void SetupBackgroundBlit() { IARCamera arCamera = GetComponent <ARCamera>().Camera; Debug.Assert(arCamera != null); Debug.Assert(arCamera.Camera != null); int renderTextureWidth = targetRenderTexture.width; int renderTextureHeight = targetRenderTexture.height; // Clean up any previous command buffer and events hooks if (m_blitCommandBuffer != null) { ARResources.DeregisterChangeCallback(SetupBackgroundBlit); arCamera.Camera.RemoveCommandBuffer(CameraEvent.BeforeForwardOpaque, m_blitCommandBuffer); arCamera.Camera.RemoveCommandBuffer(CameraEvent.AfterSkybox, m_releaseCommandBuffer); } // Create the blit command buffer m_blitCommandBuffer = new CommandBuffer(); m_blitCommandBuffer.GetTemporaryRT(workingRenderTextureID, renderTextureWidth, renderTextureHeight, 0, FilterMode.Bilinear); m_blitCommandBuffer.name = "Get ARBackground"; arCamera.BlitCameraTexture(m_blitCommandBuffer, workingRenderTextureID); if (shouldBlurRenderTexture) { // The optional blur step fastBlur = GetComponent <FastBlur>(); Debug.Assert(fastBlur); fastBlur.CreateBlurCommandBuffer(m_blitCommandBuffer, workingRenderTextureID, renderTextureWidth, renderTextureHeight); } // Copy over to the target texture. Use a blend texture to stop light flickering. m_blitCommandBuffer.Blit(workingRenderTextureID, targetRenderTexture, blendMaterial); // Run the command buffer just before opaque rendering arCamera.Camera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, m_blitCommandBuffer); // Cleanup the temp render textures m_releaseCommandBuffer = new CommandBuffer(); m_releaseCommandBuffer.name = "Release ARBackground"; m_releaseCommandBuffer.ReleaseTemporaryRT(workingRenderTextureID); arCamera.Camera.AddCommandBuffer(CameraEvent.AfterSkybox, m_releaseCommandBuffer); isCapturing = true; // Rebuild the command buffer if anything changes ARResources.RegisterChangeCallback(SetupBackgroundBlit); }
private void SetupBackgroundBlit() { Debug.Assert(Camera.main != null, "You must have a Camera tagged as MainCamera in your scene."); Camera mainCamera = Camera.main; Debug.Log("Main Camera is on GameObject - " + mainCamera.gameObject.name); IARCamera arCamera = GetComponent <ARCamera>().Camera; Debug.Assert(arCamera != null); int renderTextureWidth = targetRenderTexture.width; int renderTextureHeight = targetRenderTexture.height; // Clean up any previous command buffer and events hooks if (m_blitCommandBuffer != null) { mainCamera.RemoveCommandBuffer(CameraEvent.BeforeForwardOpaque, m_blitCommandBuffer); mainCamera.RemoveCommandBuffer(CameraEvent.AfterSkybox, m_releaseCommandBuffer); } // Create the blit command buffer m_blitCommandBuffer = new CommandBuffer(); m_blitCommandBuffer.GetTemporaryRT(workingRenderTextureID, renderTextureWidth, renderTextureHeight, 0, FilterMode.Bilinear); m_blitCommandBuffer.name = "Get ARBackground"; arCamera.BlitCameraTexture(m_blitCommandBuffer, workingRenderTextureID); if (shouldBlurRenderTexture) { // The optional blur step fastBlur = GetComponent <FastBlur>(); Debug.Assert(fastBlur); fastBlur.CreateBlurCommandBuffer(m_blitCommandBuffer, workingRenderTextureID, renderTextureWidth, renderTextureHeight); } // Copy over to the target texture. Use a blend texture to stop light flickering. m_blitCommandBuffer.Blit(workingRenderTextureID, targetRenderTexture); // Run the command buffer just before opaque rendering mainCamera.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, m_blitCommandBuffer); // Cleanup the temp render textures m_releaseCommandBuffer = new CommandBuffer(); m_releaseCommandBuffer.name = "Release ARBackground"; m_releaseCommandBuffer.ReleaseTemporaryRT(workingRenderTextureID); mainCamera.AddCommandBuffer(CameraEvent.AfterSkybox, m_releaseCommandBuffer); isCapturing = true; }
/// <summary> /// 获得鼠标在XZ平面上的投影点 /// </summary> /// <param name="camera"></param> /// <param name="viewport"></param> /// <returns></returns> public static Vector3 GetMousePointXZ(IARCamera camera, Viewport viewport) { Ray r1 = GetMouseRay(camera, viewport); if (r1.Direction.Y != 0) { Vector3 t = r1.Position + r1.Position.Y / (-Vector3.Dot(r1.Direction, Vector3.Up)) * r1.Direction; return(new Vector3(t.X, 0, t.Z)); } else { return(new Vector3(r1.Position.X, 0, r1.Position.Z)); } }
void Awake() { #if UNITY_ANDROID && !UNITY_EDITOR Camera = gameObject.AddComponent <ARCoreCamera>(); #elif UNITY_IOS && !UNITY_EDITOR ARKitCamera arkitCamera = gameObject.AddComponent <ARKitCamera>(); var arVideo = UnityEngine.Camera.main.GetComponent <UnityEngine.XR.iOS.UnityARVideo>(); Debug.Assert(arVideo); arkitCamera.clearMaterial = arVideo.m_ClearMaterial; Camera = arkitCamera; #else AREditorCamera editorCamera = gameObject.AddComponent <AREditorCamera> (); editorCamera.defaultTexture = defaultTexture; Camera = editorCamera; #endif Debug.Assert(Camera != null); }
public static void Play3DSound(SoundEffectInstance e, Vector3 position, IARCamera camera) { AudioListener al = new AudioListener(); al.Forward = Vector3.Normalize(camera.LookAt - camera.Position); al.Up = Vector3.Normalize(camera.Up); al.Position = camera.Position; AudioEmitter ae = new AudioEmitter(); ae.Position = (position - camera.Position) + camera.Position; //ae.Position = position; e.Apply3D(al, ae); //if (e.State == SoundState.Stopped) //{ e.Play(); //} }
public static void Play3DSound(SoundEffect e, Vector3 position, IARCamera camera) { SoundEffectInstance i = e.CreateInstance(); Play3DSound(i, position, camera); }
public void SetCamera(IARCamera Camera) { this.camera = Camera; }
/// <summary> /// 画一个标准模型 /// </summary> /// <param name="model">XNA模型</param> /// <param name="camera">摄像机</param> /// <param name="world">世界矩阵</param> public void DrawXNAModel(Model model, IARCamera camera, Matrix world) { DrawXNAModel(model, camera, world, 1); }
/// <summary> /// 将三维的点投影在二维 /// </summary> /// <param name="camera">相机</param> /// <param name="viewport">视点</param> /// <param name="v">三维点</param> /// <returns></returns> public static Vector2 Project(this Vector3 v, IARCamera camera, Viewport viewport) { Vector3 t = viewport.Project(v, camera.Projection, camera.View, Matrix.Identity); return(new Vector2(t.X, t.Y)); }