Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Runtime/ConfigureReinterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public void ExposeToCPP()

Vector4 v = new Vector4(1.0f, 0.0f, 1.0f, 0.0f);

MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();
propertyBlock.SetVectorArray(0, new Vector4[0]);
propertyBlock.SetVectorArray(0, new List<Vector4>());
propertyBlock.SetVectorArray("name", new Vector4[0]);
propertyBlock.SetVectorArray("name", new List<Vector4>());

t.position = new Vector3();
Vector3 p = t.position;
float x = p.x;
Expand Down Expand Up @@ -131,19 +137,26 @@ public void ExposeToCPP()

Mesh mesh = new Mesh();
Mesh[] meshes = new[] { mesh };

Matrix4x4[] mats = new[] { m2 };

mesh = meshes[0];
int meshesLength = meshes.Length;
mesh.SetVertices(new NativeArray<Vector3>());
mesh.SetNormals(new NativeArray<Vector3>());
mesh.SetUVs(0, new NativeArray<Vector2>());
mesh.SetIndices(new NativeArray<int>(), MeshTopology.Triangles, 0, true, 0);
mesh.RecalculateBounds();
mesh.indexFormat = IndexFormat.UInt32;
int vertexCount = mesh.vertexCount;
int instanceID = mesh.GetInstanceID();

Vector3[] vertices = mesh.vertices;
Vector3 vertex = vertices[0];

int[] triangles = mesh.triangles;
int triangle = triangles[0];

Bounds bounds = new Bounds(new Vector3(0, 0, 0), new Vector3(1, 2, 1));

MeshCollider meshCollider = go.AddComponent<MeshCollider>();
Expand Down Expand Up @@ -177,6 +190,7 @@ public void ExposeToCPP()
meshRenderer.material.shaderKeywords = meshRenderer.material.shaderKeywords;
meshRenderer.sharedMaterial = meshRenderer.sharedMaterial;
meshRenderer.material.shader = meshRenderer.material.shader;
meshRenderer.material.enableInstancing = true;
UnityEngine.Object.Destroy(meshGameObject);
UnityEngine.Object.DestroyImmediate(meshGameObject, true);
UnityEngine.Object.DestroyImmediate(meshGameObject);
Expand Down Expand Up @@ -508,6 +522,16 @@ public void ExposeToCPP()
stringList.Clear();
count = stringList.Count;

List<Matrix4x4> matList = new List<Matrix4x4>();
matList.Add(m2);
matList.Clear();
count = matList.Count;

List<double4x4> double4x4List = new List<double4x4>();
double4x4List.Add(ecefToLocal);
double4x4List.Clear();
count = double4x4List.Count;

string test = string.Concat("string", "string2");
string[] stringArray = stringList.ToArray();
test = stringArray[0];
Expand Down Expand Up @@ -540,6 +564,14 @@ Cesium3DTilesetLoadFailureDetails tilesetDetails
double3 cv4 = new double3(1.0, 2.0, 3.0);
double3x3 matrix3x3 = double3x3.identity;

go.GetComponent<I3dmInstanceRenderer>();
I3dmInstanceRenderer[] i3dmRenderers = go.GetComponentsInChildren<I3dmInstanceRenderer>();
i3dmRenderers = go.GetComponentsInChildren<I3dmInstanceRenderer>(true);
I3dmInstanceRenderer i3dmRenderer = i3dmRenderers[i3dmRenderers.Length - 1];
i3dmRenderer = go.AddComponent<I3dmInstanceRenderer>();

i3dmRenderer.AddInstanceGroup("groupId",mesh,meshRenderer.material,double4x4List);

go.GetComponent<CesiumGlobeAnchor>();
CesiumGlobeAnchor[] globeAnchors = go.GetComponentsInChildren<CesiumGlobeAnchor>();
globeAnchors = go.GetComponentsInChildren<CesiumGlobeAnchor>(true);
Expand Down
131 changes: 131 additions & 0 deletions Runtime/I3dmInstanceRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using UnityEngine;
using System.Collections.Generic;
using Unity.Mathematics;

namespace CesiumForUnity
{
public static class MatrixUtils
{
public static Matrix4x4 Double4x4ToMatrix4x4(double4x4 m)
{
return new Matrix4x4(
new Vector4((float)m.c0.x, (float)m.c0.y, (float)m.c0.z, (float)m.c0.w),
new Vector4((float)m.c1.x, (float)m.c1.y, (float)m.c1.z, (float)m.c1.w),
new Vector4((float)m.c2.x, (float)m.c2.y, (float)m.c2.z, (float)m.c2.w),
new Vector4((float)m.c3.x, (float)m.c3.y, (float)m.c3.z, (float)m.c3.w)
);
}

public static Matrix4x4[] Double4x4ArrayToMatrix4x4Array(double4x4[] arr)
{
if (arr == null) return null;
Matrix4x4[] result = new Matrix4x4[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
result[i] = Double4x4ToMatrix4x4(arr[i]);
}
return result;
}
}
[ExecuteInEditMode]
public class I3dmInstanceRenderer : MonoBehaviour
{
// Instance groups prepared in the C++ cesium-unity dll
private Dictionary<string, InstanceGroupData> instanceGroups = new Dictionary<string, InstanceGroupData>();

//private MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();

[System.Serializable]
public class InstanceGroupData
{
public Mesh mesh;
public Material material;
public Matrix4x4[] matrices;
public int maxInstancesPerBatch = 1023;
}

void Update()
{
// Perform rendering for all instance groups
foreach (var kvp in instanceGroups)
{
var groupData = kvp.Value;
RenderInstanceGroup(groupData);
}
}

void RenderInstanceGroup(InstanceGroupData groupData)
{
if (groupData.mesh == null || groupData.material == null || groupData.matrices == null)
return;

int totalInstances = groupData.matrices.Length;
int maxBatchSize = groupData.maxInstancesPerBatch;

// Rendering divided by batch
for (int batchStart = 0; batchStart < totalInstances; batchStart += maxBatchSize)
{
int batchSize = Mathf.Min(maxBatchSize, totalInstances - batchStart);

// Extract the matrices of the current batch
Matrix4x4[] batchMatrices = new Matrix4x4[batchSize];

for(int i = 0; i < batchSize; i++)
{
batchMatrices[i] = transform.GetChild(i).localToWorldMatrix;
}
//System.Array.Copy(groupData.matrices, batchStart, batchMatrices, 0, batchSize);

// Rendering with GPU Instancing
Graphics.DrawMeshInstanced(
groupData.mesh,
0,
groupData.material,
batchMatrices,
batchSize
);
}
}

// called from C++ cesium-unity dll
public void AddInstanceGroup(string groupId, Mesh mesh, Material material, List<double4x4> matrices)
{
// MaterialPropertyBlock setting (for each instance)
MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock();

// TODO: color setting and shader property setting
/*
std::vector<UnityEngine::Vector4> colorVector;
colorVector.reserve(batchSize);

for (size_t i = 0; i < batchSize; ++i) {
size_t sourceIndex = batchStart + i;
const glm::dvec4& glmColor = instanceGroup->colors[sourceIndex];

std::vector<double> colorValues = {
glmColor.x, glmColor.y, glmColor.z, glmColor.w
};

colorVector.push_back(gltfVectorToUnityVector(colorValues, 1.0f));
}

// Set the color array as a shader property
propertyBlock.SetVectorArray(
UnityEngine::Shader::PropertyToID(System::String("_InstanceColors")),
colorArray);
*/

instanceGroups[groupId] = new InstanceGroupData
{
mesh = mesh,
material = material,
matrices = MatrixUtils.Double4x4ArrayToMatrix4x4Array(matrices.ToArray())
};
}

public void RemoveInstanceGroup(string groupId)
{
instanceGroups.Remove(groupId);
}
}
}
11 changes: 11 additions & 0 deletions Runtime/I3dmInstanceRenderer.cs.meta

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

Loading