|
| 1 | +using UnityEngine; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Unity.Mathematics; |
| 4 | + |
| 5 | +namespace CesiumForUnity |
| 6 | +{ |
| 7 | +public static class MatrixUtils |
| 8 | +{ |
| 9 | + public static Matrix4x4 Double4x4ToMatrix4x4(double4x4 m) |
| 10 | + { |
| 11 | + return new Matrix4x4( |
| 12 | + new Vector4((float)m.c0.x, (float)m.c0.y, (float)m.c0.z, (float)m.c0.w), |
| 13 | + new Vector4((float)m.c1.x, (float)m.c1.y, (float)m.c1.z, (float)m.c1.w), |
| 14 | + new Vector4((float)m.c2.x, (float)m.c2.y, (float)m.c2.z, (float)m.c2.w), |
| 15 | + new Vector4((float)m.c3.x, (float)m.c3.y, (float)m.c3.z, (float)m.c3.w) |
| 16 | + ); |
| 17 | + } |
| 18 | + |
| 19 | + public static Matrix4x4[] Double4x4ArrayToMatrix4x4Array(double4x4[] arr) |
| 20 | + { |
| 21 | + if (arr == null) return null; |
| 22 | + Matrix4x4[] result = new Matrix4x4[arr.Length]; |
| 23 | + for (int i = 0; i < arr.Length; i++) |
| 24 | + { |
| 25 | + result[i] = Double4x4ToMatrix4x4(arr[i]); |
| 26 | + } |
| 27 | + return result; |
| 28 | + } |
| 29 | +} |
| 30 | +public class I3dmInstanceRenderer : MonoBehaviour |
| 31 | +{ |
| 32 | + // Instance groups prepared in the C++ cesium-unity dll |
| 33 | + private Dictionary<string, InstanceGroupData> instanceGroups = new Dictionary<string, InstanceGroupData>(); |
| 34 | + |
| 35 | + //private MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock(); |
| 36 | + |
| 37 | + [System.Serializable] |
| 38 | + public class InstanceGroupData |
| 39 | + { |
| 40 | + public Mesh mesh; |
| 41 | + public Material material; |
| 42 | + public Matrix4x4[] matrices; |
| 43 | + public int maxInstancesPerBatch = 1023; |
| 44 | + } |
| 45 | + |
| 46 | + void Update() |
| 47 | + { |
| 48 | + // Perform rendering for all instance groups |
| 49 | + foreach (var kvp in instanceGroups) |
| 50 | + { |
| 51 | + var groupData = kvp.Value; |
| 52 | + RenderInstanceGroup(groupData); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + void RenderInstanceGroup(InstanceGroupData groupData) |
| 57 | + { |
| 58 | + if (groupData.mesh == null || groupData.material == null || groupData.matrices == null) |
| 59 | + return; |
| 60 | + |
| 61 | + int totalInstances = groupData.matrices.Length; |
| 62 | + int maxBatchSize = groupData.maxInstancesPerBatch; |
| 63 | + |
| 64 | + // Rendering divided by batch |
| 65 | + for (int batchStart = 0; batchStart < totalInstances; batchStart += maxBatchSize) |
| 66 | + { |
| 67 | + int batchSize = Mathf.Min(maxBatchSize, totalInstances - batchStart); |
| 68 | + |
| 69 | + // Extract the matrices of the current batch |
| 70 | + Matrix4x4[] batchMatrices = new Matrix4x4[batchSize]; |
| 71 | + |
| 72 | + for(int i = 0; i < batchSize; i++) |
| 73 | + { |
| 74 | + batchMatrices[i] = transform.GetChild(i).localToWorldMatrix; |
| 75 | + } |
| 76 | + //System.Array.Copy(groupData.matrices, batchStart, batchMatrices, 0, batchSize); |
| 77 | + |
| 78 | + // Rendering with GPU Instancing |
| 79 | + Graphics.DrawMeshInstanced( |
| 80 | + groupData.mesh, |
| 81 | + 0, |
| 82 | + groupData.material, |
| 83 | + batchMatrices, |
| 84 | + batchSize |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // called from C++ cesium-unity dll |
| 90 | + public void AddInstanceGroup(string groupId, Mesh mesh, Material material, List<double4x4> matrices) |
| 91 | + { |
| 92 | + // MaterialPropertyBlock setting (for each instance) |
| 93 | + MaterialPropertyBlock propertyBlock = new MaterialPropertyBlock(); |
| 94 | + |
| 95 | + // TODO: color setting and shader property setting |
| 96 | + /* |
| 97 | + std::vector<UnityEngine::Vector4> colorVector; |
| 98 | + colorVector.reserve(batchSize); |
| 99 | + |
| 100 | + for (size_t i = 0; i < batchSize; ++i) { |
| 101 | + size_t sourceIndex = batchStart + i; |
| 102 | + const glm::dvec4& glmColor = instanceGroup->colors[sourceIndex]; |
| 103 | + |
| 104 | + std::vector<double> colorValues = { |
| 105 | + glmColor.x, glmColor.y, glmColor.z, glmColor.w |
| 106 | + }; |
| 107 | + |
| 108 | + colorVector.push_back(gltfVectorToUnityVector(colorValues, 1.0f)); |
| 109 | + } |
| 110 | + |
| 111 | + // Set the color array as a shader property |
| 112 | + propertyBlock.SetVectorArray( |
| 113 | + UnityEngine::Shader::PropertyToID(System::String("_InstanceColors")), |
| 114 | + colorArray); |
| 115 | + */ |
| 116 | + |
| 117 | + instanceGroups[groupId] = new InstanceGroupData |
| 118 | + { |
| 119 | + mesh = mesh, |
| 120 | + material = material, |
| 121 | + matrices = MatrixUtils.Double4x4ArrayToMatrix4x4Array(matrices.ToArray()) |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + public void RemoveInstanceGroup(string groupId) |
| 126 | + { |
| 127 | + instanceGroups.Remove(groupId); |
| 128 | + } |
| 129 | +} |
| 130 | +} |
0 commit comments