public static Material FromImage(string image) { var cache = Application.Current.ResourceCache; var material = new Material(); material.SetTexture(TextureUnit.Diffuse, cache.GetTexture2D(image)); material.SetTechnique(0, CoreAssets.Techniques.Diff, 0, 0); return material; }
public static Material FromColor(Color color) { var material = new Material(); var cache = Application.Current.ResourceCache; material.SetTechnique(0, color.A == 1 ? CoreAssets.Techniques.NoTexture : CoreAssets.Techniques.NoTextureAlpha, 1, 1); material.SetShaderParameter("MatDiffColor", color); return material; }
protected override async void Start() { base.Start(); environmentNode = Scene.CreateChild(); // Allow tap gesture EnableGestureTapped = true; // Create a bucket bucketNode = Scene.CreateChild(); bucketNode.SetScale(0.1f); // Create instructions textNode = bucketNode.CreateChild(); var text3D = textNode.CreateComponent<Text3D>(); text3D.HorizontalAlignment = HorizontalAlignment.Center; text3D.VerticalAlignment = VerticalAlignment.Top; text3D.ViewMask = 0x80000000; //hide from raycasts text3D.Text = "Place on a horizontal\n surface and click"; text3D.SetFont(CoreAssets.Fonts.AnonymousPro, 26); text3D.SetColor(Color.White); textNode.Translate(new Vector3(0, 3f, -0.5f)); // Model and Physics for the bucket var bucketModel = bucketNode.CreateComponent<StaticModel>(); bucketMaterial = Material.FromColor(validPositionColor); bucketModel.Model = ResourceCache.GetModel("Models/bucket.mdl"); bucketModel.SetMaterial(bucketMaterial); bucketModel.ViewMask = 0x80000000; //hide from raycasts bucketNode.CreateComponent<RigidBody>(); var shape = bucketNode.CreateComponent<CollisionShape>(); shape.SetTriangleMesh(bucketModel.Model, 0, Vector3.One, Vector3.Zero, Quaternion.Identity); // Material for spatial surfaces spatialMaterial = new Material(); spatialMaterial.SetTechnique(0, CoreAssets.Techniques.NoTextureUnlitVCol, 1, 1); // make sure 'spatialMapping' capabilaty is enabled in the app manifest. var spatialMappingAllowed = await StartSpatialMapping(new Vector3(50, 50, 10), 1200); }
public static Material SkyboxFromImages( string imagePositiveX, string imageNegativeX, string imagePositiveY, string imageNegativeY, string imagePositiveZ, string imageNegativeZ) { var cache = Application.Current.ResourceCache; var material = new Material(); TextureCube cube = new TextureCube(); cube.SetData(CubeMapFace.PositiveX, cache.GetFile(imagePositiveX, false)); cube.SetData(CubeMapFace.NegativeX, cache.GetFile(imageNegativeX, false)); cube.SetData(CubeMapFace.PositiveY, cache.GetFile(imagePositiveY, false)); cube.SetData(CubeMapFace.NegativeY, cache.GetFile(imageNegativeY, false)); cube.SetData(CubeMapFace.PositiveZ, cache.GetFile(imagePositiveZ, false)); cube.SetData(CubeMapFace.NegativeZ, cache.GetFile(imageNegativeZ, false)); material.SetTexture(TextureUnit.Diffuse, cube); material.SetTechnique(0, CoreAssets.Techniques.DiffSkybox, 0, 0); material.CullMode = CullMode.None; return material; }
void CreateVideoTexturePlaceholder(int width, int height) { cameraTexture = new Texture2D(this.Context); cameraTexture.SetNumLevels(1); cameraTexture.SetSize(width, height, Urho.Graphics.RGBFormat, TextureUsage.Dynamic); var material = new Material(); material.SetTexture(TextureUnit.Diffuse, cameraTexture); material.SetTechnique(0, CoreAssets.Techniques.Diff, 0, 0); planeNode = scene.CreateChild(); planeNode.Position = new Vector3(0, 0, 7); const float xScale = 5; planeNode.Scale = new Vector3(xScale, xScale * height / width, xScale); var planeModel = planeNode.CreateComponent<StaticModel>(); planeModel.Model = CoreAssets.Models.Box; planeModel.SetMaterial(material); }
async Task<string> CaptureAndAnalyze(bool readText = false) { var imgFormat = ImageEncodingProperties.CreateJpeg(); //NOTE: this is how you can save a frame to the CameraRoll folder: //var file = await KnownFolders.CameraRoll.CreateFileAsync($"MCS_Photo{DateTime.Now:HH-mm-ss}.jpg", CreationCollisionOption.GenerateUniqueName); //await mediaCapture.CapturePhotoToStorageFileAsync(imgFormat, file); //var stream = await file.OpenStreamForReadAsync(); // Capture a frame and put it to MemoryStream var memoryStream = new MemoryStream(); using (var ras = new InMemoryRandomAccessStream()) { await mediaCapture.CapturePhotoToStreamAsync(imgFormat, ras); ras.Seek(0); using (var stream = ras.AsStreamForRead()) stream.CopyTo(memoryStream); } var imageBytes = memoryStream.ToArray(); memoryStream.Position = 0; if (withPreview) { InvokeOnMain(() => { var image = new Image(); image.Load(new Urho.MemoryBuffer(imageBytes)); Node child = Scene.CreateChild(); child.Position = LeftCamera.Node.WorldPosition + LeftCamera.Node.WorldDirection * 2f; child.LookAt(LeftCamera.Node.WorldPosition, Vector3.Up, TransformSpace.World); child.Scale = new Vector3(1f, image.Height / (float)image.Width, 0.1f) / 10; var texture = new Texture2D(); texture.SetData(image, true); var material = new Material(); material.SetTechnique(0, CoreAssets.Techniques.Diff, 0, 0); material.SetTexture(TextureUnit.Diffuse, texture); var box = child.CreateComponent<Box>(); box.SetMaterial(material); child.RunActions(new EaseBounceOut(new ScaleBy(1f, 5))); }); } try { var client = new VisionServiceClient(VisionApiKey); if (readText) { var ocrResult = await client.RecognizeTextAsync(memoryStream, detectOrientation: false); var words = ocrResult.Regions.SelectMany(region => region.Lines).SelectMany(line => line.Words).Select(word => word.Text); return "it says: " + string.Join(" ", words); } else { // just describe the picture, you can also use cleint.AnalyzeImageAsync method to get more info var result = await client.DescribeAsync(memoryStream); return result?.Description?.Captions?.FirstOrDefault()?.Text; } } catch (ClientException exc) { return exc?.Error?.Message ?? "Failed"; } catch (Exception exc) { return "Failed"; } }