Why the polygon does not appear - xamarin

I have a custom map and a map renderer. Inside the renderer, I have this method in Android folder:
public void AddMapPolygon(double[][] polygon, MapResource mapResource)
{
PolygonOptions options = new PolygonOptions();
var points = new LatLng[polygon.Length];
int index = 0;
foreach (double[] loc in polygon)
{
points[index] = new LatLng(loc[0], loc[1]);
index++;
}
options.Add(points);
options.InvokeFillColor(Color.Argb(128, 255, 0, 0));
options.InvokeStrokeColor(Color.Argb(200, 0, 0, 0));
options.InvokeStrokeWidth(4f);
NativeMap.AddPolygon(options);
}
The method is called, but the polygon is not visible on the map.
What I did wrong?

The reason that the polygon didn't appear was that I didn't enclose it in Device.BeginInvokeOnMainThread(). I believe UI related changes should be invoked in Device.BeginInvokeOnMainThread().

Related

How do I delete or move 3D prefab clones of the same name and tag on collision?

What I am trying to achieve is when my prefabs instantiate in my spawn area when they collide then one either moves from another or is destroyed and another spawn replaces it. I tried to use tags to get the other prefab touching. I tried getting the names' prefabs. I don't know what I am doing wrong. I did just drag my game object into the prefabs folder to get prefabs. Heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnArea : MonoBehaviour
{
int counter = 0;
public int max;
//public GameObject objects;
public GameObject[] objects;
public GameObject[] objectsPrefabs;
public Vector3 size;
public Vector3 center;
int xRotation = 0;
// Start is called before the first frame update
void Start()
{
Spawn();
}
// Update is called once per frame
void Update()
{
if (counter < max)
{
Spawn();
}
}
//void Spawn()
//{
// Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), Random.Range(-size.y / 2, size.y / 2), Random.Range(-size.z / 2, size.z / 2));
// Instantiate(objects, pos, Quaternion.Euler(0, xRotation, 0));
// xRotation += 45;
// counter++;
//}
void Spawn()
{
objects = new GameObject[objectsPrefabs.Length];
Vector3 pos = center + new Vector3(Random.Range(-size.x / 2, size.x / 2), Random.Range(-size.y / 2, size.y / 2), Random.Range(-size.z / 2, size.z / 2));
for (int i = 0; i < objectsPrefabs.Length; i++)
{
objects[i] = Instantiate(objectsPrefabs[i], pos, Quaternion.Euler(0, xRotation, 0)) as GameObject;
}
xRotation += 45;
counter++;
}
void OnCollisionEnter(Collision collision)
{
for (int i = 0; i < objectsPrefabs.Length; i++)
{
if (collision.transform.CompareTag("spawnwall"))
{
//if (objects[i].gameObject)
//{
Destroy(objectsPrefabs[i]);
counter--;
Spawn();
//}
}
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1, 0, 0, 0.5f);
Gizmos.DrawCube(center, size);
}
}
According to the docs on collision, you must refer to collision.gameObject in order to refer to the other object the collision is referring to. Right now you are comparing the spawnwall tag against the tag of the game object this component belongs to.
When posing the questions, it would also be nice to know the following:
1. The desired output
2. The current symptoms
I also have the following concerns:
1. Are you sure your class members are initialized with the correct data? It seems to me that they are all either starting with C# default values.
2. Do you want the objects inside the area to do what you described when they (a) touch each other or (b) when they touch the area boundaries.
The way your current code is written makes me thinks that you have a bunch of objects inside a rectangular area. Then, when the objects collide with the boundaries of the rectangular area, they should be destroyed.

How to dynamically change texture of PIXI.Sprite when PIXI.Sprite reaches certain position - Pixi.js?

I have a class which extends PIXI.Sprite. Here i create the sprite initially. The texture i use is a spritesheet and i create sprites from random sections of this spritesheet.png by creating random frames for the texture. There I add 10000 sprites and move them in random directions. Then I add the PIXI.Sprite class in another class which extends PIXI.ParticleContainer 10,000 times.
createTexture() {
this.textureWidth = 2048;
this.rectX = () => {
let number;
while (number % 32 !== 0) number = Math.floor(Math.random() * this.textureWidth) + 0;
return number;
}
this.rectY = () => {
let number;
while (number % 32 !== 0) number = Math.floor(Math.random() * 128) + 0;
return number;
}
this.initialTexture = PIXI.Texture.from(this.resources[assets.images[0].src].name);
this.rectangle = new PIXI.Rectangle(this.rectX(), this.rectY(), 32, 32);
this.initialTexture.frame = this.rectangle;
this.texture = new PIXI.Texture(this.initialTexture.baseTexture, this.initialTexture.frame);
this.texture.requiresUpdate = true;
this.texture.updateUvs();
this.timesChangedVy = 0;
}
When a Sprite hits window borders, i call the method change texture in the class of PIXI.Sprite:
changeTexture() {
let newTexture = PIXI.Texture.from(this.resources[assets.images[0].src].name);
let rectangle = new PIXI.Rectangle(this.rectX(), this.rectY(), 32, 32);
newTexture.frame = rectangle;
// this.texture.frame = rectangle
this.texture = newTexture;
// this.texture = new PIXI.Texture.from(this.resources[assets.images[0].src].name)
// this.texture._frame = rectangle
// this.texture.orig = rectangle
// this._texture = newTexture
// this.texture = new PIXI.Texture(newTexture.baseTexture, rectangle)
this.texture.update()
this.texture.requiresUpdate = true;
this.texture.updateUvs();
}
I tried different approaches. When i console.log the texture after changing it , i see that the frame and origins have been changed, but the new texture is not being rendered.
Does someone know where the problem lies and how i can fix it?
Finally, I found the reason for my sprites not updating on texture change.
It is because I add them as children of Pixi.ParticleContainer, which has less functionality than Pixi.Container and does not update Uvs of children by default.
THE SOLUTION IS TO SET uvs to true when creating PIXI.ParticleContainer.
It looks like this: new PIXI.ParticleContainer(10000, { uvs: true }).
This will solve the problem of changing textures not being updated and uvs will be uploaded and applied.
https://pixijs.download/dev/docs/PIXI.ParticleContainer.html

How to draw circles or Line in UrhoSharp

am using urhosharp game engine from xamarim to develop a cross-platform game .... it was really good choice and compatible with WPF but I did not know how to draw shapes , I went through the guide and documentation and did not find a way maybe to draw circles,Rounded Shapes,annulus or any geometric type anyone knows or could help me . I would be very grateful .
Thanks in advance
I tried this piece of code but no circle appeared in the screen:
DebugRenderer debug = new DebugRenderer();
debug.AddCircle(new Vector3(50,50,0),new Vector3(100,25,0),50,Color.Magenta,2 ,true );
for (int i = 0; i < 512; ++i)
{
var start = new Vector3(i, 0, 0);
var end = new Vector3(i, 100, 0);
debug.AddLine(start, end, Color.White, false);
debug.AddCircle(start, end, 50f, Color.Red, i, false);
}
scene.AddComponent(debug);
Just want to note that DebugRenderRender is intended for debugging purposes. If you want to make DebugRenderer work you will need to subscribe to PostRenderUpdate like this:
Engine.SubscribeToPostRenderUpdate(args => YourMethodHandler);
Usually you will do that in your setup. And then YourMethodHandler is like:
protected void YourMethodHandler()
{
// this requires that you have already added a DebugRenderer
// component in your scene object
var debugRenderer = scene.GetComponent<DebugRenderer>();
if (debugRenderer != null)
{
// do your drawing code here
// to draw a rectangle for example:
var upperBound = new Vector3(-4.0f, 2.0f, 0.0f);
var lowerBound = new Vector3(4.0f, -2.0f, 0.0f);
debugRenderer.AddBoundingBox(
new BoundingBox(upperBound, lowerBound),
Color.White,
false);
}
}
To be more precise, it works only when implemented like this:
var dr = app.Scene.GetOrCreateComponent<DebugRenderer>();
app.Engine.PostRenderUpdate += (arg4) => {
app.Renderer.DrawDebugGeometry(false);
};
app.Renderer.BeginViewRender += (arg5) => {
dr.AddBoundingBox(new BoundingBox(-1000, 1000), Color.Blue);
dr.LineAntiAlias = true;
dr.AddTriangle(new Vector3(2,0,0), new Vector3(2,1,0), new Vector3(2,0,1), Color.Magenta, false);
dr.AddCircle(new Vector3(2,0,0), new Vector3(1,1,1), 5.0f, Color.Red);
};

SlimDX Handling Window Resizing

I'm trying to handle the program window being resized, and the (I think inefficient) code I've flung together below seems to do the trick.
Is there a better way to do this, preferably one that does not create a stutter when resizing the window and which does not constantly use 12-17% of a CPU? I also suspect MessagePump.Run may somehow run before form.Resize finishes setting up the device again, and throw an error.
Thanks!
using System;
using System.Drawing;
using System.Windows.Forms;
using SlimDX;
using SlimDX.Direct3D9;
using SlimDX.Windows;
namespace SlimDX_1
{
struct Vertex
{
public Vector4 Position;
public int Color;
}
static class Program
{
private static VertexBuffer vertices;
private static Device device;
private static RenderForm form;
private static PresentParameters present;
private static VertexDeclaration vertexDecl;
private static VertexElement[] vertexElems;
private static bool wasMinimized = false;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
form = new RenderForm("Tutorial 1: Basic Window");
init();
form.Resize += (o, e) =>
{
if (form.WindowState == FormWindowState.Minimized)
{
foreach (var item in ObjectTable.Objects)
{
item.Dispose();
}
wasMinimized = true;
}
else
{
foreach (var item in ObjectTable.Objects)
{
item.Dispose();
}
init();
device.SetRenderState(RenderState.FillMode, FillMode.Wireframe);
device.SetRenderState(RenderState.CullMode, Cull.None);
present.BackBufferHeight = form.ClientSize.Height;
present.BackBufferWidth = form.ClientSize.Width;
device.Reset(present);
}
};
MessagePump.Run(form, () =>
{
if (form.WindowState == FormWindowState.Minimized)
{
return;
}
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
device.BeginScene();
device.SetStreamSource(0, vertices, 0, 20); // 20 is the size of each vertex
device.VertexDeclaration = vertexDecl;
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
device.EndScene();
device.Present();
});
foreach (var item in ObjectTable.Objects)
{
item.Dispose();
}
}
private static void init()
{
present = new PresentParameters();
//present.EnableAutoDepthStencil = false;
//present.BackBufferCount = 1;
//present.SwapEffect = SwapEffect.Discard;
present.Windowed = true;
present.BackBufferHeight = form.ClientSize.Height;
present.BackBufferWidth = form.ClientSize.Width;
//present.BackBufferFormat = Format.Unknown;
device = new Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, present);
vertices = new VertexBuffer(device, 3 * 20, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
vertices.Lock(0, 0, LockFlags.None).WriteRange(new Vertex[]
{
new Vertex() { Color = Color.Red.ToArgb(), Position = new Vector4(400.0f, 100.0f, 0.5f, 1.0f) },
new Vertex() { Color = Color.Blue.ToArgb(), Position = new Vector4(650.0f, 500.0f, 0.5f, 1.0f) },
new Vertex() { Color = Color.Green.ToArgb(), Position = new Vector4(150.0f, 500.0f, 0.5f, 1.0f) }
});
vertices.Unlock();
// specifies the layout of the vertexes
vertexElems = new VertexElement[]
{
new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.PositionTransformed, 0),
new VertexElement(0, 16, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
VertexElement.VertexDeclarationEnd
};
vertexDecl = new VertexDeclaration(device, vertexElems);
}
}
}
You're going way above and beyond what you need to do when the window is resized. You're releasing every single DirectX object you've created, including the graphics device, and then recreating everything. This is going to take a comparatively long time, which is why you're seeing performance issues.
In fact, none of your objects need to be released. Simply call the Reset() function on the device to recreate the backbuffer to match the new window size. Check out some of the native Direct3D9 tutorials on window resizing to see how in general how the process works.

How to shape pickable verices?

I am using JUNG to make a network diagram. I want to shape the vertices depending upon its type. The vertices are pickable and colored. The code for vertices so far is as under:
class VertexColors extends PickableVertexPaintTransformer<Number> {
VertexColors(PickedInfo<Number> pi) {
super(pi, Color.blue, Color.yellow);
}
public Paint transform(Number v) {
if (pi.isPicked(v.intValue())) return picked_paint;
return v.intValue()%2==1 ? Color.blue : Color.green;
}
}
I am using the following statement for each vertex:
vv.getRenderContext().setVertexFillPaintTransformer(new VertexColors(vv.getPickedVertexState()));
Now, I cannot find a way to shape the vertices while keeping them pickable and to wrap the vertices around their labels.
All you need is to add another Transformer that provides the vertex shape when it is selected. The Transformer should choose the shape based on the whether the vertex is "picked" or not. To get the picked state, you need to obtain a PickedState object from the visualization. When the selection is changed, the transformer will be asked for the shape and the vertices will be updated with the returned shape. Here is an example of how to do this:
final VisualizationViewer<Integer, String> vv = new
VisualizationViewer<Integer, String>(layout);
// Transformer for cycling the vertices between three unique shapes.
Transformer<Integer, Shape> vertexShape = new
Transformer<Integer, Shape>() {
private final Shape[] styles = {
new Ellipse2D.Double(-25, -10, 50, 20),
new Arc2D.Double(-15, -15, 30, 30, 30, 150, Arc2D.PIE) };
#Override
public Shape transform(Integer i) {
// Choose a shape according to the "picked" state.
PickedState<Integer> pickedState = vv.getPickedVertexState();
int shapeIndex = 0;
if (pickedState.isPicked(i)) {
shapeIndex = 1;
}
return styles[shapeIndex];
}
};
vv.getRenderContext().setVertexShapeTransformer(vertexShape);

Resources