示例#1
0
        public void RaycastNow()
        {
            if (pointA != null && pointB != null)
            {
                var vector      = pointB.position - pointA.position;
                var maxDistance = vector.magnitude;
                var ray         = new Ray(pointA.position, vector);
                var hit         = default(RaycastHit);

                if (Physics.Raycast(ray, out hit, maxDistance, layers) == true)
                {
                    var finalUp       = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up;
                    var finalPosition = hit.point + hit.normal * offset;
                    var finalNormal   = normal == NormalType.HitNormal ? hit.normal : -ray.direction;
                    var finalRotation = Quaternion.LookRotation(finalNormal, finalUp);

                    hitCache.InvokeRaycast(gameObject, null, null, preview, hit, pressure);

                    DispatchHits(preview, hit.collider, finalPosition, finalRotation, pressure, this);

                    fraction = (hit.distance + offset) / maxDistance;

                    UpdatePointAndLine();
                }
                else
                {
                    BreakHits(this);

                    fraction = 1.0f;

                    UpdatePointAndLine();
                }
            }
        }
示例#2
0
        private IEnumerator DelayedOnHit(Vector3 pointA, Vector3 pointB)
        {
            var vector      = pointB - pointA;
            var maxDistance = vector.magnitude;
            var ray         = new Ray(pointA, vector);
            var hit         = default(RaycastHit);

            if (Physics.Raycast(ray, out hit, maxDistance, layers) == true)
            {
                var distance01 = Mathf.InverseLerp(0.0f, radius, hit.distance);

                // Wait based on hit distance
                yield return(new WaitForSeconds(distance01 * delayMax));

                var finalUp       = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up;
                var finalPosition = hit.point + hit.normal * offset;
                var finalNormal   = normal == NormalType.HitNormal ? hit.normal : -ray.direction;
                var finalRotation = Quaternion.LookRotation(-finalNormal, finalUp);
                var finalPressure = 1.0f - distance01;

                hitCache.InvokePoint(gameObject, preview, priority, finalPressure, finalPosition, finalRotation);

                hitCache.InvokeRaycast(gameObject, preview, priority, finalPressure, hit, finalRotation);
            }
        }
        protected virtual void OnParticleCollision(GameObject hitGameObject)
        {
            // Get the collision events array
            var count = cachedParticleSystem.GetSafeCollisionEventSize();

            // Expand collisionEvents list to fit all particles
            for (var i = particleCollisionEvents.Count; i < count; i++)
            {
                particleCollisionEvents.Add(new ParticleCollisionEvent());
            }

            count = cachedParticleSystem.GetCollisionEvents(hitGameObject, particleCollisionEvents);

            // Calculate up vector ahead of time
            var finalUp = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up;

            // Paint all locations
            for (var i = 0; i < count; i++)
            {
                var collisionEvent = particleCollisionEvents[i];
                var finalPosition  = collisionEvent.intersection + collisionEvent.normal * offset;
                var finalNormal    = normal == NormalType.CollisionNormal ? collisionEvent.normal : -collisionEvent.velocity;
                var finalRotation  = finalNormal != Vector3.zero ? Quaternion.LookRotation(finalNormal, finalUp) : Quaternion.identity;

                hitCache.InvokePoints(gameObject, null, null, false, collisionEvent.colliderComponent as Collider, finalPosition, finalRotation, 1.0f);
            }
        }
        private void CalcHitData(Vector3 hitPoint, Vector3 hitNormal, Ray ray, out Vector3 finalPosition, out Quaternion finalRotation)
        {
            var finalUp     = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up;
            var finalNormal = normal == NormalType.HitNormal ? hitNormal : -ray.direction;

            finalPosition = hitPoint + hitNormal * offset;
            finalRotation = Quaternion.LookRotation(-finalNormal, finalUp);
        }
示例#5
0
        private void CheckCollision(Collision collision)
        {
            if (cooldown > 0.0f)
            {
                return;
            }

            var impulse = collision.impulse.magnitude / Time.fixedDeltaTime;

            // Only handle the collision if the impact was strong enough
            if (impulse >= impactMin)
            {
                cooldown = delay;

                // Calculate up vector ahead of time
                var finalUp   = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up;
                var contacts  = collision.contacts;
                var pressure  = Mathf.InverseLerp(impactMin, impactPressure, impulse);
                var finalRoot = root != null ? root : gameObject;

                for (var i = contacts.Length - 1; i >= 0; i--)
                {
                    var contact = contacts[i];

                    if (P3dHelper.IndexInMask(contact.otherCollider.gameObject.layer, layers) == true)
                    {
                        var finalPosition = contact.point + contact.normal * offset;
                        var finalRotation = Quaternion.LookRotation(-contact.normal, finalUp);

                        hitCache.InvokePoint(finalRoot, preview, priority, pressure, finalPosition, finalRotation);

                        if (raycastDistance > 0.0f)
                        {
                            var ray = new Ray(contact.point + contact.normal * raycastDistance, -contact.normal);
                            var hit = default(RaycastHit);

                            if (contact.otherCollider.Raycast(ray, out hit, raycastDistance * 2.0f) == true)
                            {
                                hitCache.InvokeRaycast(finalRoot, preview, priority, pressure, hit, finalRotation);
                            }
                        }

                        if (onlyUseFirstContact == true)
                        {
                            break;
                        }
                    }
                }
            }
        }
        protected virtual void OnParticleCollision(GameObject hitGameObject)
        {
            // Get the collision events array
            var count = cachedParticleSystem.GetSafeCollisionEventSize();

            // Expand collisionEvents list to fit all particles
            for (var i = particleCollisionEvents.Count; i < count; i++)
            {
                particleCollisionEvents.Add(new ParticleCollisionEvent());
            }

            count = cachedParticleSystem.GetCollisionEvents(hitGameObject, particleCollisionEvents);

            // Calculate up vector ahead of time
            var finalUp = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up;

            // Paint all locations
            for (var i = 0; i < count; i++)
            {
                if (skip > 0)
                {
                    if (skipCounter++ > skip)
                    {
                        skipCounter = 0;
                    }
                    else
                    {
                        continue;
                    }
                }

                var collisionEvent = particleCollisionEvents[i];
                var finalPosition  = collisionEvent.intersection + collisionEvent.normal * offset;
                var finalNormal    = normal == NormalType.CollisionNormal ? -collisionEvent.normal : collisionEvent.velocity;
                var finalRotation  = finalNormal != Vector3.zero ? Quaternion.LookRotation(finalNormal, finalUp) : Quaternion.identity;
                var finalPressure  = pressure;

                if (pressureMinDistance != pressureMaxDistance)
                {
                    var distance = Vector3.Distance(transform.position, collisionEvent.intersection);

                    finalPressure *= Mathf.InverseLerp(pressureMinDistance, pressureMaxDistance, distance);
                }

                hitCache.InvokePoints(gameObject, preview, priority, collisionEvent.colliderComponent as Collider, finalPosition, finalRotation, finalPressure);
            }
        }
示例#7
0
        private void SubmitHit(bool preview)
        {
            if (pointA != null && pointB != null)
            {
                var vector      = pointB.position - pointA.position;
                var maxDistance = vector.magnitude;
                var ray         = new Ray(pointA.position, vector);
                var hit         = default(RaycastHit);

                if (Physics.Raycast(ray, out hit, maxDistance, layers) == true)
                {
                    var finalUp       = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up;
                    var finalPosition = hit.point + hit.normal * offset;
                    var finalNormal   = normal == NormalType.HitNormal ? hit.normal : -ray.direction;
                    var finalRotation = Quaternion.LookRotation(-finalNormal, finalUp);

                    switch (draw)
                    {
                    case DrawType.PointsIn3D:
                    {
                        SubmitPoint(preview, priority, pressure, finalPosition, finalRotation, this);
                    }
                    break;

                    case DrawType.PointsOnUV:
                    {
                        hitCache.InvokeCoord(gameObject, preview, priority, pressure, new P3dHit(hit), finalRotation);
                    }
                    break;

                    case DrawType.TrianglesIn3D:
                    {
                        hitCache.InvokeTriangle(gameObject, preview, priority, pressure, hit, finalRotation);
                    }
                    break;
                    }

                    fraction = (hit.distance + offset) / maxDistance;
                }
                else
                {
                    BreakHits(this);

                    fraction = 1.0f;
                }
            }
        }
示例#8
0
        private void CheckCollision(Collision collision)
        {
            if (cooldown > 0.0f)
            {
                return;
            }

            var speed = collision.relativeVelocity.magnitude;

            // Only handle the collision if the impact was strong enough
            if (speed >= speedMin)
            {
                cooldown = delay;

                // Calculate up vector ahead of time
                var finalUp  = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up;
                var contacts = collision.contacts;
                var pressure = Mathf.InverseLerp(speedMin, speedPressure, speed);

                for (var i = contacts.Length - 1; i >= 0; i--)
                {
                    var contact       = contacts[i];
                    var finalPosition = contact.point + contact.normal * offset;
                    var finalRotation = Quaternion.LookRotation(-contact.normal, finalUp);

                    hitCache.InvokePoints(gameObject, null, null, false, contact.otherCollider, finalPosition, finalRotation, 1.0f);

                    if (raycastDistance > 0.0f)
                    {
                        var ray = new Ray(contact.point + contact.normal * raycastDistance, -contact.normal);
                        var hit = default(RaycastHit);

                        if (contact.otherCollider.Raycast(ray, out hit, raycastDistance * 2.0f) == true)
                        {
                            hitCache.InvokeRaycast(gameObject, null, null, false, hit, pressure);
                        }
                    }

                    if (onlyUseFirstContact == true)
                    {
                        break;
                    }
                }
            }
        }
示例#9
0
        private void CheckCollision(Collision collision)
        {
            if (cooldown > 0.0f)
            {
                return;
            }

            var impulse = collision.impulse.magnitude / Time.fixedDeltaTime;

            // Only handle the collision if the impact was strong enough
            if (impulse >= pressureMin)
            {
                cooldown = delay;

                // Calculate up vector ahead of time
                var finalUp       = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up;
                var contacts      = collision.contacts;
                var finalPressure = pressureMultiplier;
                var finalRoot     = root != null ? root : gameObject;

                switch (pressureMode)
                {
                case PressureType.Constant:
                {
                    finalPressure *= pressureConstant;
                }
                break;

                case PressureType.ImpactSpeed:
                {
                    finalPressure *= Mathf.InverseLerp(pressureMin, pressureMax, impulse);
                }
                break;
                }

                for (var i = contacts.Length - 1; i >= 0; i--)
                {
                    var contact = contacts[i];

                    if (P3dHelper.IndexInMask(contact.otherCollider.gameObject.layer, layers) == true)
                    {
                        var finalPosition = contact.point + contact.normal * offset;
                        var finalRotation = Quaternion.LookRotation(-contact.normal, finalUp);

                        switch (emit)
                        {
                        case EmitType.PointsIn3D:
                        {
                            hitCache.InvokePoint(finalRoot, preview, priority, finalPressure, finalPosition, finalRotation);
                        }
                        break;

                        case EmitType.PointsOnUV:
                        {
                            var hit = default(RaycastHit);

                            if (TryGetRaycastHit(contact, ref hit) == true)
                            {
                                hitCache.InvokeCoord(finalRoot, preview, priority, finalPressure, new P3dHit(hit), finalRotation);
                            }
                        }
                        break;

                        case EmitType.TrianglesIn3D:
                        {
                            var hit = default(RaycastHit);

                            if (TryGetRaycastHit(contact, ref hit) == true)
                            {
                                hitCache.InvokeTriangle(gameObject, preview, priority, finalPressure, hit, finalRotation);
                            }
                        }
                        break;
                        }

                        if (onlyUseFirstContact == true)
                        {
                            break;
                        }
                    }
                }
            }
        }
示例#10
0
        protected virtual void OnParticleCollision(GameObject hitGameObject)
        {
            // Get the collision events array
            var count = cachedParticleSystem.GetSafeCollisionEventSize();

            // Expand collisionEvents list to fit all particles
            for (var i = particleCollisionEvents.Count; i < count; i++)
            {
                particleCollisionEvents.Add(new ParticleCollisionEvent());
            }

            count = cachedParticleSystem.GetCollisionEvents(hitGameObject, particleCollisionEvents);

            // Calculate up vector ahead of time
            var finalUp = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up;

            // Paint all locations
            for (var i = 0; i < count; i++)
            {
                if (skip > 0)
                {
                    if (skipCounter++ > skip)
                    {
                        skipCounter = 0;
                    }
                    else
                    {
                        continue;
                    }
                }

                var collisionEvent = particleCollisionEvents[i];
                var finalPosition  = collisionEvent.intersection + collisionEvent.normal * offset;
                var finalNormal    = normal == NormalType.CollisionNormal ? collisionEvent.normal : -collisionEvent.velocity;
                var finalRotation  = finalNormal != Vector3.zero ? Quaternion.LookRotation(-finalNormal, finalUp) : Quaternion.identity;
                var finalPressure  = pressureMultiplier;

                switch (pressureMode)
                {
                case PressureType.Distance:
                {
                    var distance = Vector3.Distance(transform.position, collisionEvent.intersection);

                    finalPressure *= Mathf.InverseLerp(pressureMin, pressureMax, distance);
                }
                break;

                case PressureType.Speed:
                {
                    var speed = Vector3.SqrMagnitude(collisionEvent.velocity);

                    if (speed > 0.0f)
                    {
                        speed = Mathf.Sqrt(speed);
                    }

                    finalPressure *= Mathf.InverseLerp(pressureMin, pressureMax, speed);
                }
                break;
                }
                if (pressureMin != pressureMax)
                {
                }

                hitCache.InvokePoint(gameObject, preview, priority, finalPressure, finalPosition, finalRotation);
            }
        }
示例#11
0
        protected virtual void OnParticleCollision(GameObject hitGameObject)
        {
            if (cachedParticleSystemSet == false)
            {
                cachedParticleSystem    = GetComponent <ParticleSystem>();
                cachedParticleSystemSet = true;
            }

            // Get the collision events array
            var count = cachedParticleSystem.GetSafeCollisionEventSize();

            // Expand collisionEvents list to fit all particles
            for (var i = particleCollisionEvents.Count; i < count; i++)
            {
                particleCollisionEvents.Add(new ParticleCollisionEvent());
            }

            count = cachedParticleSystem.GetCollisionEvents(hitGameObject, particleCollisionEvents);

            // Calculate up vector ahead of time
            var finalUp   = orientation == OrientationType.CameraUp ? P3dHelper.GetCameraUp(_camera) : Vector3.up;
            var finalRoot = root != null ? root : gameObject;

            // Paint all locations
            for (var i = 0; i < count; i++)
            {
                var collision = particleCollisionEvents[i];

                if (P3dHelper.IndexInMask(collision.colliderComponent.gameObject.layer, layers) == false)
                {
                    continue;
                }

                if (skip > 0)
                {
                    if (skipCounter++ > skip)
                    {
                        skipCounter = 0;
                    }
                    else
                    {
                        continue;
                    }
                }

                var finalPosition = collision.intersection + collision.normal * offset;
                var finalNormal   = normal == NormalType.CollisionNormal ? collision.normal : -collision.velocity;
                var finalRotation = finalNormal != Vector3.zero ? Quaternion.LookRotation(-finalNormal, finalUp) : Quaternion.identity;
                var finalPressure = pressureMultiplier;

                switch (pressureMode)
                {
                case PressureType.Constant:
                {
                    finalPressure *= pressureConstant;
                }
                break;

                case PressureType.Distance:
                {
                    var distance = Vector3.Distance(transform.position, collision.intersection);

                    finalPressure *= Mathf.InverseLerp(pressureMin, pressureMax, distance);
                }
                break;

                case PressureType.Speed:
                {
                    var speed = Vector3.SqrMagnitude(collision.velocity);

                    if (speed > 0.0f)
                    {
                        speed = Mathf.Sqrt(speed);
                    }

                    finalPressure *= Mathf.InverseLerp(pressureMin, pressureMax, speed);
                }
                break;
                }

                switch (emit)
                {
                case EmitType.PointsIn3D:
                {
                    hitCache.InvokePoint(finalRoot, preview, priority, finalPressure, finalPosition, finalRotation);
                }
                break;

                case EmitType.PointsOnUV:
                {
                    var hit = default(RaycastHit);

                    if (TryGetRaycastHit(collision, ref hit) == true)
                    {
                        hitCache.InvokeCoord(finalRoot, preview, priority, finalPressure, new P3dHit(hit), finalRotation);
                    }
                }
                break;

                case EmitType.TrianglesIn3D:
                {
                    var hit = default(RaycastHit);

                    if (TryGetRaycastHit(collision, ref hit) == true)
                    {
                        hitCache.InvokeTriangle(gameObject, preview, priority, finalPressure, hit, finalRotation);
                    }
                }
                break;
                }
            }
        }