Skip to content

Commit

Permalink
feat: continuing to build custom meshes, added BlenderSpace to transl…
Browse files Browse the repository at this point in the history
…ate to UnityWorld
  • Loading branch information
dbirman committed Mar 1, 2024
1 parent 071aa5c commit 92defe2
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 17 deletions.
6 changes: 4 additions & 2 deletions API/oursin/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ class CustomMesh:
def __init__(self, vertices, triangles, normals = None):
"""Create a Custom 3D object based on a set of vertices and triangle faces
Unity can automatically calculate the normals to create a convex object, or you can pass them yourself.
Parameters
----------
vertices : list of vector3
Vertex coordinates
Vertex coordinates, the x/y/z directions will correspond to AP/DV/ML if your object was exported from Blender
triangles : list of vector3
Triangle vertices
Triangle vertex indexes
normals : list of vector3, optional
Normal directions, by default None
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ Material:
- _WorkflowMode: 1
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _BaseColor: {r: 0.22744447, g: 0.8584906, b: 0.14983091, a: 1}
- _Color: {r: 0.22744447, g: 0.8584906, b: 0.14983091, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.2, g: 0.2, b: 0.2, a: 1}
m_BuildTextureStacks: []
Expand Down
8 changes: 8 additions & 0 deletions UnityClient/Packages/vbl.urchin/Scripts/Coordinates.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using UnityEngine;
using BrainAtlas.CoordinateSystems;

public class BlenderSpace : CoordinateSpace
{
public override string Name => throw new System.NotImplementedException();

public override Vector3 Dimensions => throw new System.NotImplementedException();

public override Vector3 Space2World(Vector3 coordSpace, bool useReference = true)
{
return Space2World(coordSpace);
}

public override Vector3 Space2World_Vector(Vector3 vecSpace)
{
return new Vector3(vecSpace.y, vecSpace.z, vecSpace.x);
}

public override Vector3 World2Space(Vector3 coordWorld, bool useReference = true)
{
return World2Space_Vector(coordWorld);
}

public override Vector3 World2Space_Vector(Vector3 vecWorld)
{
return new Vector3(vecWorld.z, vecWorld.x, vecWorld.y);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "urchin.spaces",
"rootNamespace": "",
"references": [
"vbl.brainatlas.coordsystems",
"vbl.brainatlas.runtime"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public struct CustomMeshData
{
public string ID;
public Vector3[] vertices;
public Vector3Int[] triangles;
public int[] triangles;
public Vector3[] normals;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BrainAtlas;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Entities.UniversalDelegates;
using UnityEngine;
using Urchin.API;
Expand All @@ -14,13 +15,16 @@ public class CustomMeshManager : MonoBehaviour
#endregion

#region Private
Dictionary<string, GameObject> _customMeshGOs;
private Dictionary<string, GameObject> _customMeshGOs;
private BlenderSpace _blenderSpace;
#endregion

private void Start()
{
_customMeshGOs = new();

_blenderSpace = new();

Client_SocketIO.CustomMeshCreate += Create;
Client_SocketIO.CustomMeshDestroy += Destroy;
Client_SocketIO.CustomMeshPosition += SetPosition;
Expand All @@ -34,22 +38,18 @@ public void Create(CustomMeshData data)
go.transform.SetParent(_customMeshParentT);

Mesh mesh = new Mesh();
mesh.vertices = data.vertices;

int[] triangles = new int[data.triangles.Length * 3];
for (int i = 0; i < data.triangles.Length; i++)
{
triangles[i + 0] = data.triangles[i].x;
triangles[i + 1] = data.triangles[i].y;
triangles[i + 2] = data.triangles[i].z;
}
mesh.triangles = triangles;
// the vertices are assumed to have been passed in ap/ml/dv directions
mesh.vertices = data.vertices.Select(x => _blenderSpace.Space2World_Vector(x)).ToArray();

mesh.triangles = data.triangles;

if (data.normals != null)
mesh.normals = data.normals;

go.AddComponent<MeshFilter>().mesh = mesh;
go.AddComponent<MeshRenderer>().material = MaterialManager.MeshMaterials["default"];
go.AddComponent<MeshRenderer>().material = MaterialManager.MeshMaterials["opaque-lit"];
go.GetComponent<MeshRenderer>().material.color = Color.gray;

_customMeshGOs.Add(data.ID, go);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:5dedf88781c06a443aeac3e770ba73f9",
"GUID:ef948df2bf4bfe448ad38f3511278da4",
"GUID:2a17cf7e212916c498a6b6adfced9bcf"
"GUID:2a17cf7e212916c498a6b6adfced9bcf",
"GUID:8afc55bf9836c9443bf227ce72d6a976"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private void Awake()
{
if (Instance != null)
throw new Exception("Only a single instance of MaterialManager should exist in the scene.");
Instance = this;

if (_meshMaterials.Count != _materialNames.Count)
Debug.LogWarning("Mesh material and name count should be equal");
Expand Down

0 comments on commit 92defe2

Please sign in to comment.