Beispiel #1
0
        public void SetupBounds()
        {
            if (!needRebuildBounds)
            {
                return;
            }

            float sideX = 0.5f;
            float sideY = 0.5f;
            float sideZ = 0.5f;

            Vector3 p1 = new Vector3(-sideX, -sideY * 2, -sideZ);
            Vector3 p2 = new Vector3(-sideX, -sideY * 2, sideZ);
            Vector3 p3 = new Vector3(sideX, -sideY * 2, sideZ);
            Vector3 p4 = new Vector3(sideX, -sideY * 2, -sideZ);
            Vector3 p5 = new Vector3(-sideX, 0, -sideZ);
            Vector3 p6 = new Vector3(-sideX, 0, sideZ);
            Vector3 p7 = new Vector3(sideX, 0, sideZ);
            Vector3 p8 = new Vector3(sideX, 0, sideZ);

            p1 = CachedTransform.TransformPoint(p1);
            p2 = CachedTransform.TransformPoint(p2);
            p3 = CachedTransform.TransformPoint(p3);
            p4 = CachedTransform.TransformPoint(p4);
            p5 = CachedTransform.TransformPoint(p5);
            p6 = CachedTransform.TransformPoint(p6);
            p7 = CachedTransform.TransformPoint(p7);
            p8 = CachedTransform.TransformPoint(p8);

            float minX = Mathf.Min(p1.x, p2.x, p3.x, p4.x, p5.x, p6.x, p7.x, p8.x);
            float minY = Mathf.Min(p1.y, p2.y, p3.y, p4.y, p5.y, p6.y, p7.y, p8.y);
            float minZ = Mathf.Min(p1.z, p2.z, p3.z, p4.z, p5.z, p6.z, p7.z, p8.z);

            float maxX = Mathf.Max(p1.x, p2.x, p3.x, p4.x, p5.x, p6.x, p7.x, p8.x);
            float maxY = Mathf.Max(p1.y, p2.y, p3.y, p4.y, p5.y, p6.y, p7.y, p8.y);
            float maxZ = Mathf.Max(p1.z, p2.z, p3.z, p4.z, p5.z, p6.z, p7.z, p8.z);

            Vector3 min = new Vector3(minX, minY, minZ);
            Vector3 max = new Vector3(maxX, maxY, maxZ);

            var bounds = Bounds;

            bounds.SetMinMax(min, max);
            Bounds = bounds;

            needRebuildBounds = false;
        }
Beispiel #2
0
        /// <summary>
        /// Calculate KLAudioSource position using current KLListener
        /// </summary>
        public Vector3 GetSourcePosition()
        {
            if (ReferenceEquals(KLListener.Current, null))
            {
                return(CachedTransform.position);
            }

            switch (sourceShape)
            {
            case SourceType.Sphere:
                return(KLMath.NearPointToSphere(CachedTransform.position, sphereShapeRadius, KLListener.Current.CachedTransform.position));

            case SourceType.Line:
                return(KLMath.ProjectPointOnLineSegment(
                           CachedTransform.TransformPoint(lineShapeStart),
                           CachedTransform.TransformPoint(lineShapeEnd),
                           KLListener.Current.CachedTransform.position
                           ));

            case SourceType.Area:
                // TODO: Move this to KLMath
                Vector3 half = areaShapeSize / 2f;
                Vector3 d    = KLListener.Current.CachedTransform.position - CachedTransform.position;
                Vector3 ld   = CachedTransform.InverseTransformDirection(d);
                ld = new Vector3(
                    Mathf.Clamp(ld.x, -half.x, half.x),
                    Mathf.Clamp(ld.y, -half.y, half.y),
                    Mathf.Clamp(ld.z, -half.z, half.z)
                    );

                return(CachedTransform.position + CachedTransform.TransformDirection(ld));

            default:
                return(CachedTransform.position);
            }
        }