Skip to content

Instantly share code, notes, and snippets.

@neuman
Created July 18, 2017 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neuman/00b694f5185ea2d470f11d8facb9dfe8 to your computer and use it in GitHub Desktop.
Save neuman/00b694f5185ea2d470f11d8facb9dfe8 to your computer and use it in GitHub Desktop.
3d library for unity
using System;
using UnityEngine;
using System.Collections.Generic;
public static class DDD
{
/*
MILKCRATE.ddd.latLongToVector3 = function(lat, lng, radius, h)
{
// convert latitude / longitude to angles between 0 and 2*phi
var phi = (90 - lat) * Math.PI / 180;
var theta = (180 - lng) * Math.PI / 180;
// Calculate the xyz coordinate from the angles
var x = radius * Math.sin(phi) * Math.cos(theta);
var y = radius * Math.cos(phi);
var z = radius * Math.sin(phi) * Math.sin(theta);
return new THREE.Vector3(x, y, z);
}*/
public static Vector3 latLongToVector3(float lat, float lng, float radius, float h)
{
// convert latitude / longitude to angles between 0 and 2*phi
float phi = (float)((90 - lat) * Math.PI / 180);
float theta = (float)((180 - lng) * Math.PI / 180);
// Calculate the xyz coordinate from the angles
float x = (float)(radius * Math.Sin(phi) * Math.Cos(theta));
float y = (float)(radius * Math.Cos(phi));
float z = (float)(radius * Math.Sin(phi) * Math.Sin(theta));
return new Vector3(x, y, z);
}
public static Vector2 normalize_canvas_style_point(Vector2 point, float x, float y, float width, float height, float map_width, float map_height)
{
//find top corner
float corner_x = x * map_width / 100;
float corner_y = y * map_height / 100;
//normalize size
float real_width = width * map_width / 100;
float real_height = height * map_height / 100;
//normalize
float real_x = corner_x + (point.x * real_width / 100);
float real_Y = corner_y + (point.y * real_height / 100);
return new Vector2(real_x, real_Y);
}
public static Dictionary<string, float> convert_x_y_to_lat_lon(Vector2 coords, float width, float height)
{
// We just linearly convert the image coordinated to the angular latitude and longitude coordinates.
// Then we center them so the (0,0) coordinate is at the center of the image
float lat = 90 - 180 * (coords.y / height); // equirectangular projection
float lon = 360 * (coords.x / width) - 180;
Dictionary<string, float> dictionary = new Dictionary<string, float>();
dictionary.Add("lat", lat);
dictionary.Add("lon", lon);
return dictionary;
}
public static Vector3 get_average_point(List<Vector3> points)
{
float x = 0;
float y = 0;
float z = 0;
for (var i = 0; i < points.Count; i++)
{
x += points[i].x;
y += points[i].y;
z += points[i].z;
}
return new Vector3(x / points.Count, y / points.Count, z / points.Count);
}
public static Mesh create_hotspot_mesh(List<Vector3> points)
{
//create a new mesh
Mesh mesh = new Mesh();
List<Vector3> vertices = new List<Vector3>();
List<int> faces = new List<int>();
List<Vector3> normals = new List<Vector3>();
//find the average of all points and push to verticies as 0
Vector3 average_point = get_average_point(points);
vertices.Add(average_point);
//push verticies
vertices.AddRange(points);
//start at 1 to skip the average point
for (int i = 0; i <= points.Count; i++)
{
//set this point
int this_index = i;
int next_index;
//if it's the last point set next point to first point
if (i == points.Count)
{
next_index = 1;
}
else
{
next_index = i + 1;
}
//add poly from this point to next point to average
faces.Add(this_index);
faces.Add(next_index);
faces.Add(0);
normals.Add(new Vector3(-1,-1,-1));
}
mesh.vertices = vertices.ToArray();
mesh.triangles = faces.ToArray();
//mesh.normals = normals.ToArray();
return mesh;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment