bool MakeTexture() { Tex r8 = new Tex(TexType.ImageNomips, TexFormat.R8); Tex r16 = new Tex(TexType.ImageNomips, TexFormat.R16); Tex r32 = new Tex(TexType.ImageNomips, TexFormat.R32); if (r8 == null || r16 == null || r32 == null) { return(false); } int s = 8; byte [] r8Data = new byte [s * s]; ushort[] r16Data = new ushort[s * s]; float [] r32Data = new float [s * s]; for (int i = 0; i < s * s; i++) { float y = (float)(i / s) / s; r8Data [i] = (byte)(y * byte.MaxValue); r16Data[i] = (ushort)(y * ushort.MaxValue); r32Data[i] = y; } r8.SetColors(s, s, r8Data); r16.SetColors(s, s, r16Data); r32.SetColors(s, s, r32Data); r8.AddressMode = TexAddress.Clamp; r16.AddressMode = TexAddress.Clamp; r32.AddressMode = TexAddress.Clamp; singleChannelTex[MatParamName.DiffuseTex] = r8; return(true); }
/////////////////////////////////////////// static async Task RequestHeight(BoundingBox regionBounds) { var request = new ElevationRequest() { Bounds = regionBounds, Row = 32, Col = 32, BingMapsKey = _ApiKey }; Response response = await request.Execute(); if (response.StatusCode != 200) { Log.Warn("Bing Maps API error:\n" + string.Join('\n', response.ErrorDetails)); return; } ElevationData data = response.ResourceSets[0].Resources[0] as ElevationData; Color[] heights = new Color[32 * 32]; for (int y = 0; y < 32; y++) { for (int x = 0; x < 32; x++) { // Mount everest is 8,848m tall heights[x + (31 - y) * 32] = Color.White * (data.Elevations[x + y * 32] / 9000.0f); } } mapHeight = new Tex(TexType.ImageNomips, TexFormat.Rgba128); mapHeight.SetColors(32, 32, heights); mapHeight.AddressMode = TexAddress.Clamp; Geo.BoundsToWorld(regionBounds, regionBounds, out mapHeightSize, out mapHeightCenter); terrain.SetHeightData(mapHeight, mapHeightSize * worldScale, mapHeightCenter * worldScale); }
/////////////////////////////////////////// public static async Task RequestHeight(string apiKey, BoundingBox regionBounds, Action <Tex, Vec3, Vec2> OnReceivedHeight) { // Here's an elevation request! This doesn't provide an image, rather, // it gives us a grid of height values. It's limited to a maximum of // 1024 values per request, so we're only asking for a grid of 32x32 // elevations. // However! This request does work exactly within the bounds provided, // so we're getting what we expect from the results of this request. // Details about the request can be found here: // https://github.com/microsoft/BingMapsRESTToolkit/blob/master/Docs/API%20Reference.md#ElevationRequest ElevationRequest request = new ElevationRequest() { Bounds = regionBounds, Row = 32, Col = 32, BingMapsKey = apiKey }; Response response = await ServiceManager.GetResponseAsync(request); // StatusCode is a web response status code, where 200-299 means // success. Details here: // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status if (response.StatusCode < 200 || response.StatusCode >= 300) { Log.Warn("Bing Maps API error:\n" + string.Join('\n', response.ErrorDetails)); return; } // Convert the elevation data we've received into a grayscale heightmap texture! ElevationData data = response.ResourceSets[0].Resources[0] as ElevationData; Color[] heights = new Color[32 * 32]; for (int y = 0; y < 32; y++) { for (int x = 0; x < 32; x++) { float height = data.Elevations[x + y * 32] / Geo.EarthTallest; // Height data is provided upside-down, so we're flipping it with // this index on the Y axis. heights[x + (31 - y) * 32] = Color.White * height; } } // Create a texture from the elevation data! We're storing it as // Rgba128 to preserve floating point precision in the height values. Tex texture = new Tex(TexType.ImageNomips, TexFormat.Rgba128); texture.SetColors(32, 32, heights); texture.AddressMode = TexAddress.Clamp; // Our bounds should be correct, but we still need it in StereoKit // units, so convert! Geo.BoundsToWorld(regionBounds, regionBounds, out Vec3 size, out Vec2 center); // Done! Pass the results back. OnReceivedHeight(texture, size, center); }
private void ColorizeFingers(int size, Gradient horizontal, Gradient vertical) { Tex tex = new Tex(); tex.AddressMode = TexAddress.Clamp; Color32[] pixels = new Color32[size * size]; for (int y = 0; y < size; y++) { Color v = vertical.Get(y / (size - 1.0f)); for (int x = 0; x < size; x++) { Color h = horizontal.Get(x / (size - 1.0f)); pixels[x + y * size] = v * h; } } tex.SetColors(size, size, pixels); Default.MaterialHand[MatParamName.DiffuseTex] = tex; }
public void Initialize() { /// :CodeSample: Tex.SetColors /// ## Creating a texture procedurally /// It's pretty easy to create an array of colors, and /// just pass that into an empty texture! Here, we're /// building a simple grid texture, like so: /// /// ![Procedural Texture]({{site.url}}/img/screenshots/ProceduralTexture.jpg) /// /// You can call SetTexture as many times as you like! If /// you're calling it frequently, you may want to keep /// the width and height consistent to prevent from creating /// new texture objects. Use TexType.ImageNomips to prevent /// StereoKit from calculating mip-maps, which can be costly, /// especially when done frequently. // Create an empty texture! This is TextType.Image, and // an RGBA 32 bit color format. Tex gridTex = new Tex(); // Use point sampling to ensure that the grid lines are // crisp and sharp, not blended with the pixels around it. gridTex.SampleMode = TexSample.Point; // Allocate memory for the pixels we'll fill in, powers // of two are always best for textures, since this makes // things like generating mip-maps easier. int width = 128; int height = 128; Color32[] colors = new Color32[width * height]; // Create a color for the base of the grid, and the // lines of the grid Color32 baseColor = Color.HSV(0.6f, 0.1f, 0.25f); Color32 lineColor = Color.HSV(0.6f, 0.05f, 1); Color32 subLineColor = Color.HSV(0.6f, 0.05f, .6f); // Loop through each pixel for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // If the pixel's x or y value is a multiple of 64, or // if it's adjacent to a multiple of 128, then we // choose the line color! Otherwise, we use the base. if (x % 128 == 0 || (x + 1) % 128 == 0 || (x - 1) % 128 == 0 || y % 128 == 0 || (y + 1) % 128 == 0 || (y - 1) % 128 == 0) { colors[x + y * width] = lineColor; } else if (x % 64 == 0 || y % 64 == 0) { colors[x + y * width] = subLineColor; } else { colors[x + y * width] = baseColor; } } } // Put the pixel information into the texture gridTex.SetColors(width, height, colors); /// :End: /// :CodeSample: Material MatParamName /// ## Material parameter access /// Material does have an array operator overload for setting /// shader parameters really quickly! You can do this with strings /// representing shader parameter names, or use the MatParamName /// enum for compile safety. exampleMaterial[MatParamName.DiffuseTex] = gridTex; exampleMaterial[MatParamName.TexScale] = 2; /// :End: }
public void Initialize() { Color32[] colors = new Color32[8 * 8]; Tex tex1 = new Tex(TexType.Image, TexFormat.Rgba32); tex1.SetColors(8, 8, colors); Tex tex2 = new Tex(TexType.Image, TexFormat.Rgba32Linear); tex2.SetColors(6, 8, colors); float[] colors2 = new float[8 * 8]; Tex tex5 = new Tex(TexType.Image, TexFormat.R32); tex5.SetColors(8, 8, colors2); Tex tex6 = new Tex(TexType.Image, TexFormat.R32); tex6.SetColors(6, 8, colors2); Color[] colors3 = new Color[8 * 8]; Tex tex7 = new Tex(TexType.Image, TexFormat.Rgba128); tex7.SetColors(8, 8, colors3); Tex tex8 = new Tex(TexType.Image, TexFormat.Rgba128); tex8.SetColors(6, 8, colors3); byte[] colors4 = new byte[8 * 8]; Tex tex9 = new Tex(TexType.Image, TexFormat.R8); tex9.SetColors(8, 8, colors4); Tex tex10 = new Tex(TexType.Image, TexFormat.R8); tex10.SetColors(6, 8, colors4); ushort[] colors5 = new ushort[8 * 8]; Tex tex11 = new Tex(TexType.Image, TexFormat.R16); tex11.SetColors(8, 8, colors5); Tex tex12 = new Tex(TexType.Image, TexFormat.R16); tex12.SetColors(6, 8, colors5); ushort[] colors6 = new ushort[8 * 8 * 4]; Tex tex13 = new Tex(TexType.Image, TexFormat.Rgba64); tex13.SetColors(8, 8, colors6); Tex tex14 = new Tex(TexType.Image, TexFormat.Rgba64); tex14.SetColors(6, 8, colors6); byte[] colors7 = new byte[8 * 8 * 4]; Tex tex15 = new Tex(TexType.Image, TexFormat.Bgra32Linear); tex15.SetColors(8, 8, colors7); Tex tex16 = new Tex(TexType.Image, TexFormat.Bgra32); tex16.SetColors(6, 8, colors7); }