/// <summary> /// Refresh the barrel distortion mesh scene /// </summary> private void RefreshBarrelDistortionScene() { this.RefreshEyeTextures(); if (this.isBarrelDistortionEnabled && this.cachedVRMode != VRMode.AttachedMode) { if (this.barrelDistortionEntity == null) { if (this.distortionMesh == null) { var assembly = ReflectionHelper.GetTypeAssembly(typeof(CardboardVRProvider)); using (var stream = ResourceLoader.GetEmbeddedResourceStream(assembly, "WaveEngine.Cardboard.BarrelDistortionMesh.wpk")) { this.distortionMesh = this.Assets.LoadAsset <InternalStaticModel>(BarrelDistortionModel, stream); } } if (this.RenderManager.FindLayer(typeof(VRLensLayer)) == null) { this.RenderManager.RegisterLayer(new VRLensLayer(this.RenderManager)); } Dictionary <string, Material> materials = new Dictionary <string, Material>(); materials.Add("vrMeshLeft", new StandardMaterial() { LightingEnabled = false, Diffuse = this.eyeTextures[0].RenderTarget, LayerType = typeof(VRLensLayer), SamplerMode = AddressMode.LinearClamp }); materials.Add("vrMeshRight", new StandardMaterial() { LightingEnabled = false, Diffuse = this.eyeTextures[1].RenderTarget, LayerType = typeof(VRLensLayer), SamplerMode = AddressMode.LinearClamp }); MaterialsMap materialsMap = new MaterialsMap(materials); this.barrelDistortionEntity = new Entity("_VRCardboardDistortMesh"); Entity distortMeshEntity = new Entity("VRMesh") .AddComponent(new Transform3D()) .AddComponent(new Model(BarrelDistortionModel)) .AddComponent(materialsMap) .AddComponent(new ModelRenderer()); var vrCamera = new FixedCamera3D("VRDistortCamera", new Vector3(0, 1, 0), Vector3.Zero); var camera = vrCamera.Entity.FindComponent <Camera3D>(); camera.UpVector = -Vector3.UnitZ; camera.SetCustomProjection(Matrix.CreateOrthographic(1, 1, 0.1f, 100)); camera.LayerMaskDefaultValue = false; camera.LayerMask.Add(typeof(VRLensLayer), true); this.barrelDistortionEntity.AddChild(distortMeshEntity); this.barrelDistortionEntity.AddChild(vrCamera.Entity); this.EntityManager.Add(this.barrelDistortionEntity); } } else { if (this.barrelDistortionEntity != null) { this.EntityManager.Remove(this.barrelDistortionEntity); this.barrelDistortionEntity = null; var lensLayer = this.RenderManager.FindLayer(typeof(VRLensLayer)); if (lensLayer != null) { this.RenderManager.RemoveLayer(lensLayer); } } } }
/// <summary> /// Refresh the barrel distortion mesh scene /// </summary> private void RefreshBarrelDistortionScene() { this.RefreshEyeTextures(); if (this.isBarrelDistortionEnabled && this.cachedVRMode != VRMode.AttachedMode) { if (this.barrelDistortionEntity == null) { if (!this.distortionMeshGenerated) { this.CreateDistortedMeshes(); } int layerId = this.FindVRLensLayerId(); this.vrMeshLeftMaterial = new StandardMaterial() { LightingEnabled = false, LayerId = layerId }; this.vrMeshRightMaterial = new StandardMaterial() { LightingEnabled = false, LayerId = layerId }; this.RefreshBarrelDistortionMaterials(); this.barrelDistortionEntity = new Entity("_VRCardboardDistortMesh"); Entity distortMeshEntityLeft = new Entity("VRMeshLeft") .AddComponent(new Transform3D() { LocalScale = Vector3.One * 0.5f }) .AddComponent(new MaterialComponent() { Material = this.vrMeshLeftMaterial }) .AddComponent(new CustomMesh() { Mesh = this.distortionMeshLeft }) .AddComponent(new MeshRenderer()); Entity distortMeshEntityRight = new Entity("VRMeshRight") .AddComponent(new Transform3D() { LocalScale = Vector3.One * 0.5f }) .AddComponent(new MaterialComponent() { Material = this.vrMeshRightMaterial }) .AddComponent(new CustomMesh() { Mesh = this.distortionMeshRight }) .AddComponent(new MeshRenderer()); var vrCamera = new FixedCamera3D("VRDistortCamera", new Vector3(0, 1, 0), Vector3.Zero); var camera = vrCamera.Entity.FindComponent <Camera3D>(); camera.CameraOrder = float.MaxValue; camera.UpVector = -Vector3.UnitZ; camera.SetCustomProjection(Matrix.CreateOrthographic(1, 1, 0.1f, 100)); camera.LayerMaskDefaultValue = false; camera.LayerMask.Add(layerId, true); this.barrelDistortionEntity.AddChild(distortMeshEntityLeft); this.barrelDistortionEntity.AddChild(distortMeshEntityRight); this.barrelDistortionEntity.AddChild(vrCamera.Entity); this.EntityManager.Add(this.barrelDistortionEntity); } } else { if (this.barrelDistortionEntity != null) { this.EntityManager.Remove(this.barrelDistortionEntity); this.barrelDistortionEntity = null; var lensLayer = this.RenderManager.FindLayerByName(VRLensLayerName); if (lensLayer != null) { this.RenderManager.RemoveLayer(lensLayer); } } } }