public static float ComputeVolume(UnityEngine.Collider coll)
 {
     if (coll != null)
     {
         UnityEngine.CapsuleCollider collider = coll as UnityEngine.CapsuleCollider;
         if (collider != null)
         {
             UnityEngine.Vector3 lossyScale = collider.transform.lossyScale;
             float num  = collider.height * lossyScale.y;
             float num2 = collider.radius * UnityEngine.Mathf.Max(lossyScale.x, lossyScale.z);
             float num3 = (3.141593f * num2) * num2;
             float num4 = num3 * num;
             float num5 = (num3 * num2) * 1.333333f;
             return(num4 + num5);
         }
         UnityEngine.SphereCollider collider2 = coll as UnityEngine.SphereCollider;
         if (collider2 != null)
         {
             UnityEngine.Vector3 vector2 = collider2.transform.lossyScale;
             float[]             values  = new float[] { vector2.x, vector2.y, vector2.z };
             float num6 = collider2.radius * UnityEngine.Mathf.Max(values);
             return(4.18879f * ((num6 * num6) * num6));
         }
         UnityEngine.BoxCollider collider3 = coll as UnityEngine.BoxCollider;
         if (collider3 != null)
         {
             UnityEngine.Vector3 vector3 = collider3.transform.lossyScale;
             UnityEngine.Vector3 size    = collider3.size;
             return(((size.x * vector3.x) * (size.y * vector3.y)) * (size.z * vector3.z));
         }
         UnityEngine.Debug.LogWarning("Not implemented for " + coll.GetType() + " type!");
     }
     return(0f);
 }
        public static void DrawWireCollider(Collider c)
        {
            if (c == null) throw new System.ArgumentNullException("c");

            if (c is BoxCollider)
            {
                var box = c as BoxCollider;
                DrawWireRectoid(box.transform.TransformPoint(box.center), box.transform.rotation, Vector3.Scale(box.size, box.transform.lossyScale));
            }
            else if (c is SphereCollider)
            {
                var sphereGeom = com.spacepuppy.Geom.Sphere.FromCollider(c as SphereCollider);
                DrawWireSphere(sphereGeom.Center, c.transform.rotation, sphereGeom.Radius);
            }
            else if (c is CapsuleCollider)
            {
                var capGeom = com.spacepuppy.Geom.Capsule.FromCollider(c as CapsuleCollider);
                DrawWireCapsule(capGeom.Start, capGeom.End, capGeom.Radius);
            }
            else if (c is CharacterController)
            {
                var capGeom = com.spacepuppy.Geom.Capsule.FromCollider(c as CharacterController);
                DrawWireCapsule(capGeom.Start, capGeom.End, capGeom.Radius);
            }
            else if (c is MeshCollider)
            {
                //not supported
                throw new System.ArgumentException("Unsupported collider type '" + c.GetType().Name + "'.", "c");
            }
            else
            {
                //not supported
                throw new System.ArgumentException("Unsupported collider type '" + c.GetType().Name + "'.", "c");
            }

        }
示例#3
0
        private Vector3 getUnityColliderWorldCenter(
            UnityEngine.Collider unityCollider)
        {
            SphereCollider sphere = unityCollider as SphereCollider;

            if (sphere != null)
            {
                return(unityCollider.transform.localToWorldMatrix.MultiplyPoint3x4(
                           sphere.center
                           ));
            }

            CapsuleCollider capsule = unityCollider as CapsuleCollider;

            if (capsule != null)
            {
                Vector3 a, b;
                capsule.GetCapsulePoints(out a, out b);
                return(unityCollider.transform.localToWorldMatrix.MultiplyPoint3x4(
                           (a + b) / 2f
                           ));
            }

            BoxCollider box = unityCollider as BoxCollider;

            if (box != null)
            {
                return(unityCollider.transform.localToWorldMatrix.MultiplyPoint3x4(
                           box.center
                           ));
            }

            MeshCollider mesh = unityCollider as MeshCollider;

            if (mesh != null)
            {
                // Warning: Only valid for convex meshes.
                return(unityCollider.transform.localToWorldMatrix.MultiplyPoint3x4(
                           mesh.bounds.center
                           ));
            }

            throw new System.Exception("(Plane Collision) Collider type not supported: " +
                                       unityCollider.GetType().ToString());
        }
示例#4
0
		int GetSurfaceIndex(Collider col, Vector3 worldPos) {
			string textureName = "";

			// Case when the ground is a terrain.
			if(col.GetType() == typeof(TerrainCollider)) {
				Terrain terrain = col.GetComponent<Terrain>();
				TerrainData terrainData = terrain.terrainData;
				float[] textureMix = GetTerrainTextureMix(worldPos, terrainData, terrain.GetPosition());
				int textureIndex = GetTextureIndex(worldPos, textureMix);
				textureName = terrainData.splatPrototypes[textureIndex].texture.name;
			}
			// Case when the ground is a normal mesh.
			else {
				textureName = GetMeshMaterialAtPoint(worldPos);
			}
			// Searching for the found texture / material name in registered materials.
			foreach(var material in registeredTextures) {
				if(material.texture.name == textureName) {
					return material.surfaceIndex;
				}
			}

			return -1;
		}
示例#5
0
        internal ColliderPatch GenerateInitialPatch()
        {
            ColliderGeometry colliderGeo = null;

            if (_collider is SphereCollider sphereCollider)
            {
                colliderGeo = new SphereColliderGeometry()
                {
                    Radius = sphereCollider.radius,
                    Center = sphereCollider.center.ToMWVector3()
                };
            }
            else if (_collider is BoxCollider boxCollider)
            {
                colliderGeo = new BoxColliderGeometry()
                {
                    Size   = boxCollider.size.ToMWVector3(),
                    Center = boxCollider.center.ToMWVector3()
                };
            }
            else if (_collider is MeshCollider meshCollider)
            {
                colliderGeo = new MeshColliderGeometry();
            }
            else if (_collider is CapsuleCollider capsuleCollider)
            {
                colliderGeo = new CapsuleColliderGeometry();
            }
            else
            {
                _ownerActor.App.Logger.LogWarning($"MRE SDK does not support the following Unity collider and will not " +
                                                  $"be available in the MRE app.  Collider Type: {_collider.GetType()}");
            }

            return(colliderGeo == null ? null : new ColliderPatch()
            {
                IsEnabled = _collider.enabled,
                IsTrigger = _collider.isTrigger,
                ColliderGeometry = colliderGeo
            });
        }
        internal ColliderPatch GenerateInitialPatch()
        {
            ColliderGeometry colliderGeo = null;

            // Note: SDK has no "mesh" collider type
            if (Shape == ColliderType.Auto || Shape == ColliderType.Mesh)
            {
                colliderGeo = new AutoColliderGeometry();
            }
            else if (_collider is SphereCollider sphereCollider)
            {
                colliderGeo = new SphereColliderGeometry()
                {
                    Radius = sphereCollider.radius,
                    Center = sphereCollider.center.CreateMWVector3()
                };
            }
            else if (_collider is BoxCollider boxCollider)
            {
                colliderGeo = new BoxColliderGeometry()
                {
                    Size   = boxCollider.size.CreateMWVector3(),
                    Center = boxCollider.center.CreateMWVector3()
                };
            }
            else if (_collider is CapsuleCollider capsuleCollider)
            {
                // The size vector describes the dimensions of the bounding box containing the collider
                MWVector3 size;
                if (capsuleCollider.direction == 0)
                {
                    size = new MWVector3(capsuleCollider.height, 2 * capsuleCollider.radius, 2 * capsuleCollider.radius);
                }
                else if (capsuleCollider.direction == 1)
                {
                    size = new MWVector3(2 * capsuleCollider.radius, capsuleCollider.height, 2 * capsuleCollider.radius);
                }
                else
                {
                    size = new MWVector3(2 * capsuleCollider.radius, 2 * capsuleCollider.radius, capsuleCollider.height);
                }

                colliderGeo = new CapsuleColliderGeometry()
                {
                    Center = capsuleCollider.center.CreateMWVector3(),
                    Size   = size
                };
            }
            else
            {
                _ownerActor.App.Logger.LogWarning($"MRE SDK does not support the following Unity collider and will not " +
                                                  $"be available in the MRE app.  Collider Type: {_collider.GetType()}");
            }

            return(colliderGeo == null ? null : new ColliderPatch()
            {
                IsEnabled = _collider.enabled,
                IsTrigger = _collider.isTrigger,
                Geometry = colliderGeo
            });
        }
 /// <summary>
 /// Determines whether [is mesh collider] [the specified collider].
 /// </summary>
 /// <param name="collider">The collider.</param>
 /// <returns>
 ///   <c>true</c> if [is mesh collider] [the specified collider]; otherwise, <c>false</c>.
 /// </returns>
 public static bool IsMeshCollider(Collider collider)
 {
     return collider.GetType() == typeof(MeshCollider);
 }
示例#8
0
 private void OnTriggerEnter(Collider other)
 {
     Console.WriteLine(other.GetType());
     DoActivateTrigger();
 }
		/// <summary>
		/// Overload to skip the need for an Area to exist so this can be used during setup.
		/// </summary>
		protected void UpdateAreaRange(Collider col, Component col2D)  // Component is for 4.0 compatability
		{
			Vector3 normRange = this.GetNormalizedRange();

			if (col is SphereCollider)
            {
                var collider = (SphereCollider)col;
                collider.radius = normRange.x;
            }
            else if (col is BoxCollider)
            {
                var collider = (BoxCollider)col;
                collider.size = normRange;
            }
            else if (col is CapsuleCollider)
            {
                var collider = (CapsuleCollider)col;
                collider.radius = normRange.x;
                collider.height = normRange.y;
            }
            else if (col2D is CircleCollider2D)
            {
                var collider = (CircleCollider2D)col2D;
                collider.radius = normRange.x;
            }
            else if (col2D is BoxCollider2D)
            {
                var collider = (BoxCollider2D)col2D;
                collider.size = new Vector2(normRange.x, normRange.y);
            }
			else
            {
				string colType;
				string colName;
				if (col != null)
				{
					colType = col.GetType().Name;
					colName = col.name;
				}
				else if (col2D != null)
				{
					colType = col2D.GetType().Name;
					colName = col2D.name;
				}
				else
				{
					throw new System.NullReferenceException("No Collider Found!");
				}

				Debug.LogWarning
				(
					string.Format
					(
						"Unsupported collider type '{0}' for collider '{1}'.",
						colType,
						colName
					)
				);
            }
        }