Cesium for Unity 1.15.2
Loading...
Searching...
No Matches
CesiumPrimitiveFeatures.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using UnityEngine;
4
5namespace CesiumForUnity
6{
15 [IconAttribute("Packages/com.cesium.unity/Editor/Resources/Cesium-24x24.png")]
16 [AddComponentMenu("")]
17 public partial class CesiumPrimitiveFeatures : MonoBehaviour
18 {
23 {
24 get; internal set;
25 }
26
27 #region Helper functions
28
29 // Cache a list of indices to prevent the allocation of a new array every time
30 // GetFirstVertexFromTriangle is called.
31 private static List<int> _indices;
32
33 private static int GetFirstVertexFromTriangle(MeshFilter meshFilter, int triangleIndex)
34 {
35 if (meshFilter == null || meshFilter.mesh == null)
36 {
37 return -1;
38 }
39
40 if (CesiumPrimitiveFeatures._indices == null)
41 {
42 CesiumPrimitiveFeatures._indices = new List<int>();
43 }
44
45 meshFilter.mesh.GetTriangles(CesiumPrimitiveFeatures._indices, 0);
46 int targetVertex = triangleIndex * 3;
47 return targetVertex < CesiumPrimitiveFeatures._indices.Count ? CesiumPrimitiveFeatures._indices[targetVertex] : -1;
48 }
49 #endregion
50
51 #region Public methods
58 public static int GetFirstVertexFromHitTriangle(RaycastHit hitInfo)
59 {
60 MeshFilter meshFilter = hitInfo.transform.GetComponent<MeshFilter>();
61 return GetFirstVertexFromTriangle(meshFilter, hitInfo.triangleIndex);
62 }
63
71 {
72 return Array.FindAll(this.featureIdSets, set => (set.type == type));
73 }
74
87
88 public Int64 GetFeatureIdFromTriangle(int triangleIndex, Int64 featureIdSetIndex = 0)
89 {
90 MeshFilter meshFilter = this.gameObject.GetComponent<MeshFilter>();
91 if (meshFilter == null || featureIdSetIndex < 0 || featureIdSetIndex >= this.featureIdSets.Length)
92 {
93 return -1;
94 }
95
96 CesiumFeatureIdSet featureIdSet = this.featureIdSets[featureIdSetIndex];
97
98 return featureIdSet.GetFeatureIdForVertex(
99 GetFirstVertexFromTriangle(meshFilter, triangleIndex));
100 }
101
115 public Int64 GetFeatureIdFromRaycastHit(RaycastHit hitInfo, Int64 featureIdSetIndex = 0)
116 {
117 if (hitInfo.transform.GetComponent<CesiumPrimitiveFeatures>() != this ||
118 featureIdSetIndex < 0 || featureIdSetIndex >= this.featureIdSets.Length)
119 {
120 return -1;
121 }
122
123 CesiumFeatureIdSet featureIdSet = this.featureIdSets[featureIdSetIndex];
124
125 return featureIdSet.GetFeatureIdFromRaycastHit(hitInfo);
126 }
127
128 #endregion
129 }
130}
Represents a feature ID set from a glTF primitive.
virtual Int64 GetFeatureIdForVertex(Int64 vertexIndex)
Gets the feature ID associated with a given vertex.
virtual Int64 GetFeatureIdFromRaycastHit(RaycastHit hitInfo)
Gets the feature ID from the feature ID set using the given raycast hit.
Represents the EXT_mesh_features of a glTF primitive in a Cesium3DTileset.
CesiumFeatureIdSet[] GetFeatureIdSetsOfType(CesiumFeatureIdSetType type)
Gets all the feature ID sets of the given type.
Int64 GetFeatureIdFromTriangle(int triangleIndex, Int64 featureIdSetIndex=0)
Gets the feature ID associated with the given triangle, specified by index.
static int GetFirstVertexFromHitTriangle(RaycastHit hitInfo)
Given a successful raycast hit, finds the index of the first vertex from the hit triangle.
CesiumFeatureIdSet[] featureIdSets
The CesiumFeatureIdSets available on this primitive.
Int64 GetFeatureIdFromRaycastHit(RaycastHit hitInfo, Int64 featureIdSetIndex=0)
Gets the feature ID from the given raycast hit, assuming it has hit a glTF primitive component contai...
CesiumFeatureIdSetType
The type of a feature ID set.