Hi Everyone.
Glad to all are safe.
I have some help with reading and loading 3D from JSON. I am a little bit low in c# I need your help guys I will share what code I used and model cs also pls help anyone with this issue: (Mesh. UV is out of bounds. The supplied array needs to be the same size as the Mesh. vertices array) I don’t know in my code side issue or JSON side plz help me, guys.
Code I will share plz help guys…
I will share the code below.
using Assets.Base.Common;
using Assets.Base.Models;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
public class MainScreenManager : MonoBehaviour
{
private Color color;
public Texture Texture;
public GameObject MainObject;
public MainScreenManager()
{
}
void Start()
{
var url = Path.Combine(Application.streamingAssetsPath, "test.json");
GetData(url);
}
public void GetData(string url)
{
var myJsonResponse = RoomUtility.proxyAPI.GetRequest(url);
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
Debug.Log(myDeserializedClass);
float width = myDeserializedClass.data.width;
float height = myDeserializedClass.data.height;
ColorUtility.TryParseHtmlString(myDeserializedClass.data.ground.color.ToString(), out color);
GameObject plane = new GameObject("Plane");
plane.transform.localScale = new Vector3(-height, width, 0.001f);
plane.transform.position = new Vector3(0.0f, 1f, 1614.00f);
Quaternion target = Quaternion.Euler(-90f, 0f, 0.0f);
plane.transform.rotation = target;
MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
meshFilter.mesh = CreateMesh(width, height);
MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
//renderer.material.shader = Shader.Find("Particles/Additive");
renderer.material.shader = Shader.Find("Standard");
Texture2D tex = new Texture2D(1, 1);
tex.SetPixel(0, 0, color);
tex.Apply();
renderer.material.mainTexture = tex;
renderer.material.color = color;
foreach (var item in myDeserializedClass.data.items)
{
Debug.Log(item.className);
for (int i = 0; i < item.items.Count; i++)
{
Debug.Log(item.items*.getValueCountNV.roomsize);*
if (item.items*.className == “Room”)*
{
int width1 = Convert.ToInt16(item.items*.x / Math.PI);*
int height1 = Convert.ToInt16(item.items*.y / Math.PI);*
int sx = item.items*.sX;*
int sy = item.items*.sY;*
// CreatePlane(width1, height1, sx, sy);
GameObject planew = new GameObject(“Plane”);
MeshFilter meshFilters = (MeshFilter)planew.AddComponent(typeof(MeshFilter));
var meshFiltere = planew.GetComponent();
CreatePlane(width1, height1, sx, sy);
// meshFiltere.sharedMesh = mesh;
//MeshRenderer renderer1 = planew.GetComponent() as MeshRenderer;
// renderer1.material.color = Color.blue;
//float width1 = item.items_.x * 0.0002645833f;
//float height1 = item.items.y * 0.0002645833f;
//float height2 = item.items.z * 0.0002645833f;
//float sx = item.items.sX * 0.0002645833f;
//float sy = item.items.sY * 0.0002645833f;
// //ColorUtility.TryParseHtmlString(item.items.materials*.color, out color);
// GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);*_
///// cube.transform.parent = MainObject.transform;
// cube.transform.localScale = new Vector3(sx, sy, 0.01f);
// cube.transform.position = new Vector3(0, 5f, 0);
// Quaternion target1 = Quaternion.Euler(-90f, 0f, 0.0f);
// cube.transform.rotation = target1;
// //MeshFilter meshFilter1 = (MeshFilter)cube.GetComponent();
// //meshFilter1.mesh = CreateMesh(width, height);
// MeshRenderer renderer1 = cube.GetComponent() as MeshRenderer;
// renderer1.material.color = Color.black;
// renderer1.material.mainTexture = Texture;
for (int j = 0; j < item.items*.items.Count; j++)*
{
if (item.items*.items[j].className == “Wall”)*
{
for (int f = 0; f < item.items*.items[j].items.Count; f++)*
{
int height11 = Convert.ToInt16(item.items*.items[j].items[f].x / Math.PI); ;*
int width11 = Convert.ToInt16(item.items*.items[j].items[f].y / Math.PI); ;*
int sx1 = item.items*.sX;*
int sy1 = item.items*.sY;*
GameObject cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
var meshFiltere2 = (MeshFilter)cube2.GetComponent();
CreatePlane(width11, height11, sx, sy);
cube2.transform.localPosition= new Vector3((float)height11, (float)width11,0f);
cube2.transform.localScale = new Vector3((float)sx1, (float)sy1, 0f);
//meshFiltere2.sharedMesh = mesh2;
//MeshRenderer renderer12 = planew.GetComponent() as MeshRenderer;
//renderer12.material.color = Color.blue;
//ColorUtility.TryParseHtmlString(item.items_.materials*.color, out color);
//GameObject cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
//// cube2.transform.parent = MainObject.transform;
//cube2.transform.localScale = new Vector3(sx, sy, 0.1f);
//cube2.transform.position = new Vector3(-item.items.items[j].items[f].x * 0.0002645833f, item.items.items[j].items[f].y * 0.0002645833f, 0);
//Quaternion target12 = Quaternion.Euler(0f, 0f, 0.0f);
//cube2.transform.rotation = target12;
////MeshFilter meshFilter1 = (MeshFilter)cube.GetComponent();
////meshFilter1.mesh = CreateMesh(width, height);
//MeshRenderer renderer12 = cube.GetComponent() as MeshRenderer;
//renderer12.material.color = Color.black;
//renderer12.material.mainTexture = Texture;
}
}
}
}
}
}
}*_
public Mesh CreatePlane(int width, int height, int segmentsWidth, int segmentsHeight)
{
int ix, iy,
width_half = Convert.ToInt16(width / Math.PI),
height_half = Convert.ToInt16(height / Math.PI),
gridX = segmentsWidth,
gridY = segmentsHeight,
gridX1 = gridX + 1,
gridY1 = gridY + 1,
segment_width = width / gridX,
segment_height = height / gridY;
#pragma warning disable IDE0059 // Unnecessary assignment of a value
Vector3 normal = new Vector3(0, 0, 1);
#pragma warning restore IDE0059 // Unnecessary assignment of a value
List vertices = new List();
List vertexNormals = new List();
List faces = new List();
List faceVertexUvs = new List();
for (iy = 0; iy < gridY1; iy++)
{
for (ix = 0; ix < gridX1; ix++)
{
float x = ix * segment_width - width_half;
float y = iy * segment_height - height_half;
vertices.Add(new Vector3(x, -y, 0));
}
}
for (iy = 0; iy < gridY; iy++)
{
for (ix = 0; ix < gridX; ix++)
{
int a = ix + gridX1 * iy;
int b = ix + gridX1 * (iy + 1);
int c = (ix + 1) + gridX1 * (iy + 1);
int d = (ix + 1) + gridX1 * iy;
faces.Add(a);
faces.Add(b);
faces.Add(c);
faces.Add(d);
faceVertexUvs.Add(new Vector2(ix / gridX, iy / gridY));
faceVertexUvs.Add(new Vector2(ix / gridX, (iy + 1) / gridY));
faceVertexUvs.Add(new Vector2((ix + 1) / gridX, (iy + 1) / gridY));
faceVertexUvs.Add(new Vector2((ix + 1) / gridX, iy / gridY));
}
}
// create mesh
Mesh m = new Mesh();
m.name = “Cube”;
m.vertices = vertices.ToArray();
m.uv = faceVertexUvs.ToArray();
m.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
m.RecalculateNormals();
return m;
//GameObject plane = new GameObject(“Plane”);
//MeshFilter meshFilter = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
//Mesh m = plane.GetComponent().mesh;
//m.vertices = vertices.ToArray();
//m.SetIndices(faces.ToArray(), MeshTopology.Quads, 0);
//m.uv = faceVertexUvs.ToArray();
//MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
}
public void get(string s, int x)
{
if (s != null && x >= 0 && x < s.Length)
{
get(s, x);
}
}
Mesh CreateMesh(float width, float height)
{
Mesh m = new Mesh();
m.name = “ScriptedMesh”;
m.vertices = new Vector3[] {
new Vector3(-width, -height, 0.01f),
new Vector3(width, -height, 0.01f),
new Vector3(width, height, 0.01f),
new Vector3(-width, height, 0.01f)
};
m.uv = new Vector2[] {
new Vector2 (0, 0),
new Vector2 (0, 1),
new Vector2(1, 1),
new Vector2 (1, 0)
};
m.triangles = new int[] { 0, 1, 2, 0, 2, 3 };
m.RecalculateNormals();
return m;
}
}
Model Script
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assets.Base.Models
{
// Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse);
public class Ground
{
public string texture { get; set; }
public string color { get; set; }
}
public class Floor
{
public string texture { get; set; }
public string color { get; set; }
public int scale { get; set; }
public int rotate { get; set; }
}
public class Ceil
{
public string texture { get; set; }
public string color { get; set; }
public int scale { get; set; }
public int rotate { get; set; }
}
public class Indoor
{
public string texture { get; set; }
public string color { get; set; }
public int scale { get; set; }
public int rotate { get; set; }
}
public class Outdoor
{
public string texture { get; set; }
public string color { get; set; }
public int scale { get; set; }
public int rotate { get; set; }
}
public class Materials
{
public Floor floor { get; set; }
public Ceil ceil { get; set; }
public Indoor indoor { get; set; }
public Outdoor outdoor { get; set; }
}
public class GetValueCountNV
{
public string SmallCornnerBricks { get; set; }
public string bigCornnerBricks { get; set; }
public string concreteblockshalf { get; set; }
public string concreteblocksstandard { get; set; }
public string roomsize { get; set; }
public string indoor { get; set; }
public string outdoor { get; set; }
}
public class GetValueCountNV2
{
public string indoor { get; set; }
public string outdoor { get; set; }
public string roomsize { get; set; }
}
public class Indoor2
{
public string texture { get; set; }
public string color { get; set; }
public int scale { get; set; }
public int rotate { get; set; }
}
public class Outdoor2
{
public string texture { get; set; }
public string color { get; set; }
public int scale { get; set; }
public int rotate { get; set; }
}
public class Materials2
{
public Indoor2 indoor { get; set; }
public Outdoor2 outdoor { get; set; }
}
public class Item4
{
public string className { get; set; }
public int x { get; set; }
public int y { get; set; }
public string puid { get; set; }
public bool hidden { get; set; }
public object getValueCountNV { get; set; }
public int w { get; set; }
public object materials { get; set; }
public int z { get; set; }
public int sX { get; set; }
public int sY { get; set; }
public object rtype { get; set; }
public object h { get; set; }
public bool rhidden { get; set; }
public bool fhidden { get; set; }
public object id { get; set; }
public object otf { get; set; }
public object sZ { get; set; }
public object fX { get; set; }
public object fY { get; set; }
public object a { get; set; }
public object aframe { get; set; }
public object getheight { get; set; }
public object getwindowdoor { get; set; }
public object wall { get; set; }
public object lightObjectId { get; set; }
public object name { get; set; }
public List items { get; set; }
}
public class Item3
{
public string className { get; set; }
public bool hidden { get; set; }
public GetValueCountNV2 getValueCountNV { get; set; }
public int w { get; set; }
public Materials2 materials { get; set; }
public string puid { get; set; }
public List items { get; set; }
}
public class Item2
{
public string className { get; set; }
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
public int sX { get; set; }
public int sY { get; set; }
public int rtype { get; set; }
public int h { get; set; }
public Materials materials { get; set; }
public bool rhidden { get; set; }
public bool fhidden { get; set; }
public GetValueCountNV getValueCountNV { get; set; }
public object GetValueCountNV { get; internal set; }
public string puid { get; set; }
public List items { get; set; }
}
public class Item
{
public string className { get; set; }
public string name { get; set; }
public int h { get; set; }
public string puid { get; set; }
public List items { get; set; }
}
public class Data
{
public string className { get; set; }
public int version { get; set; }
public int width { get; set; }
public int height { get; set; }
public List ss { get; set; }
public int sscounter { get; set; }
public int s { get; set; }
public int currentFloor { get; set; }
public int autoinc { get; set; }
public Ground ground { get; set; }
public object backgroundImageSize { get; set; }
public object backgroundImagePosition { get; set; }
public int v { get; set; }
public List items { get; set; }
}
public class Root
{
public string hash { get; set; }
public int readOnly { get; set; }
public string name { get; set; }
public int s { get; set; }
public Data data { get; set; }
}
}
Json file
{
“hash”: “noAuthProject”,
“readOnly”: 0,
“name”: “Newproject”,
“s”: 0,
“data”: {
“className”: “Project”,
“version”: 2,
“width”: 8000,
“height”: 8000,
“ss”: [],
“sscounter”: 0,
“s”: 0,
“currentFloor”: 0,
“autoinc”: 27,
“ground”: {
“texture”: “bg_1_fill.png”,
“color”: “#fff”
},
“v”: 1,
“items”: [
{
“className”: “Floor”,
“name”: “First floor”,
“h”: 275,
“puid”: “p1”,
“items”: [
{
“className”: “Room”,
“x”: 3774,
“y”: 3829,
“z”: 0,
“sX”: 204,
“sY”: 204,
“rtype”: 0,
“h”: 270,
“materials”: {
“floor”: {
“texture”: “laminate_0_1_1”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
},
“ceil”: {
“texture”: “linen_1_4”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
},
“indoor”: {
“texture”: “wallp_0”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
},
“outdoor”: {
“texture”: “wallp_0”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
}
},
“rhidden”: false,
“fhidden”: false,
“getValueCountNV”: {
“SmallCornnerBricks”: “xx”,
“bigCornnerBricks”: “xx”,
“concreteblockshalf”: “xx”,
“concreteblocksstandard”: “xx”,
“roomsize”: “L:500xB:500xH:270”
},
“puid”: “p15”,
“items”: [
{
“className”: “Wall”,
“hidden”: false,
“getValueCountNV”: {
“indoor”: “”,
“outdoor”: “”,
“roomsize”: “L:500xB:500xH:270”
},
“w”: 10,
“materials”: {
“indoor”: {
“texture”: “wallp_0”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
},
“outdoor”: {
“texture”: “wallp_0”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
}
},
“puid”: “p16”,
“items”: [
{
“className”: “Point”,
“x”: 0,
“y”: 0,
“puid”: “p17”
},
{
“className”: “Point”,
“x”: 510,
“y”: 0,
“puid”: “p18”
}
]
},
{
“className”: “Wall”,
“hidden”: false,
“getValueCountNV”: {
“indoor”: “”,
“outdoor”: “”,
“roomsize”: “L:500xB:500xH:270”
},
“w”: 10,
“materials”: {
“indoor”: {
“texture”: “wallp_0”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
},
“outdoor”: {
“texture”: “wallp_0”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
}
},
“puid”: “p19”,
“items”: [
{
“className”: “Point”,
“x”: 510,
“y”: 0,
“puid”: “p20”
},
{
“className”: “Point”,
“x”: 510,
“y”: 510,
“puid”: “p21”
}
]
},
{
“className”: “Wall”,
“hidden”: false,
“getValueCountNV”: {
“indoor”: “”,
“outdoor”: “”,
“roomsize”: “L:500xB:500xH:270”
},
“w”: 10,
“materials”: {
“indoor”: {
“texture”: “wallp_0”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
},
“outdoor”: {
“texture”: “wallp_0”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
}
},
“puid”: “p22”,
“items”: [
{
“className”: “Point”,
“x”: 510,
“y”: 510,
“puid”: “p23”
},
{
“className”: “Point”,
“x”: 0,
“y”: 510,
“puid”: “p24”
}
]
},
{
“className”: “Wall”,
“hidden”: false,
“getValueCountNV”: {
“indoor”: “”,
“outdoor”: “”,
“roomsize”: “L:500xB:500xH:270”
},
“w”: 10,
“materials”: {
“indoor”: {
“texture”: “wallp_0”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
},
“outdoor”: {
“texture”: “wallp_0”,
“color”: “#fff”,
“scale”: 1,
“rotate”: 0
}
},
“puid”: “p25”,
“items”: [
{
“className”: “Point”,
“x”: 0,
“y”: 510,
“puid”: “p26”
},
{
“className”: “Point”,
“x”: 0,
“y”: 0,
“puid”: “p27”
}
]
}
]
}
]
}
]
}
}