Example #1
0
        public override void OnInspectorGUI()
        {
            // Get target
            debris = target as RayfireDebris;
            if (debris == null)
            {
                return;
            }

            // Space
            GUILayout.Space(8);

            if (Application.isPlaying == true)
            {
                if (GUILayout.Button("Emit", GUILayout.Height(25)))
                {
                    foreach (var targ in targets)
                    {
                        if (targ as RayfireDebris != null)
                        {
                            (targ as RayfireDebris).Emit();
                        }
                    }
                }
            }

            // Draw script UI
            DrawDefaultInspector();

            // Space
            GUILayout.Space(8);
        }
Example #2
0
        // Copy debris and dust
        public static void CopyParticles(RayfireRigid source, RayfireRigid target)
        {
            // Copy debris
            if (source.HasDebris == true)
            {
                for (int i = 0; i < source.debrisList.Count; i++)
                {
                    RayfireDebris targetDebris = target.gameObject.AddComponent <RayfireDebris>();
                    targetDebris.CopyFrom(source.debrisList[i]);

                    if (source.debrisList[i].children == null)
                    {
                        source.debrisList[i].children = new List <RayfireDebris>();
                    }
                    source.debrisList[i].children.Add(targetDebris);
                }
            }

            // Copy dust
            if (source.HasDust == true)
            {
                for (int i = 0; i < source.dustList.Count; i++)
                {
                    RayfireDust targetDust = target.gameObject.AddComponent <RayfireDust>();
                    targetDust.CopyFrom(source.dustList[i]);

                    if (source.dustList[i].children == null)
                    {
                        source.dustList[i].children = new List <RayfireDust>();
                    }
                    source.dustList[i].children.Add(targetDust);
                }
            }
        }
Example #3
0
        /// /////////////////////////////////////////////////////////
        /// Impact
        /// /////////////////////////////////////////////////////////

        // Create single debris particle system
        public static void CreateDebrisImpact(RayfireDebris debris, Vector3 impactPos, Vector3 impactNormal)
        {
            // Particle system
            ParticleSystem ps = debris.CreateParticleSystem(debris);

            // Align over impact
            debris.hostTm.position = impactPos;
            debris.hostTm.LookAt(impactPos + impactNormal);

            // Set amount
            debris.amountFinal = debris.emission.burstAmount;

            // Create debris
            debris.CreateDebris(debris.rigid.transForm, debris, null, -1, ps);
        }
Example #4
0
        // Create single debris particle system
        public static void CreateDebrisRigid(RayfireDebris target)
        {
            // No particles
            if (target.amountFinal < target.limitations.minParticles && target.emission.distanceRate == 0)
            {
                return;
            }

            // Particle system
            ParticleSystem ps = target.CreateParticleSystem(target);

            // Get emit material index
            int emitMatIndex = GetEmissionMatIndex(target.rigid.meshRenderer, target.emissionMaterial);

            // Create debris
            target.CreateDebris(target.rigid.transForm, target, target.rigid.meshFilter, emitMatIndex, ps);
        }
Example #5
0
        /// /////////////////////////////////////////////////////////
        /// Create common
        /// /////////////////////////////////////////////////////////

        // Create single debris particle system
        public void CreateDebris(Transform host, RayfireDebris scr, MeshFilter emitMeshFilter, int emitMatIndex, ParticleSystem ps)
        {
            // Set main module
            RFParticles.SetMain(ps.main, scr.emission.lifeMin, scr.emission.lifeMax, scr.emission.sizeMin, scr.emission.sizeMax,
                                scr.dynamic.gravityMin, scr.dynamic.gravityMax, scr.dynamic.speedMin, scr.dynamic.speedMax,
                                3.1f, scr.limitations.maxParticles, scr.emission.duration);

            // Emission over distance
            RFParticles.SetEmission(ps.emission, scr.emission.distanceRate, scr.amountFinal);

            // Emission from mesh or from impact point
            if (emitMeshFilter != null)
            {
                RFParticles.SetShapeMesh(ps.shape, emitMeshFilter.sharedMesh, emitMatIndex, emitMeshFilter.transform.localScale);
            }
            else
            {
                RFParticles.SetShapeObject(ps.shape);
            }

            // Inherit velocity
            RFParticles.SetVelocity(ps.inheritVelocity, scr.dynamic);

            // Size over lifetime
            RFParticles.SetSizeOverLifeTime(ps.sizeOverLifetime, scr.refScale);

            // Rotation by speed
            RFParticles.SetRotationBySpeed(ps.rotationBySpeed, scr.dynamic.rotationSpeed);

            // Collision
            RFParticles.SetCollisionDebris(ps.collision, scr.collision);

            // Noise
            RFParticles.SetNoise(ps.noise, scr.noise);

            // Renderer
            SetParticleRendererDebris(ps.GetComponent <ParticleSystemRenderer>(), scr);

            // Start playing
            ps.Play();
        }
Example #6
0
        // Copy from
        public void CopyFrom(RayfireDebris source)
        {
            onDemolition = source.onDemolition;
            onActivation = source.onActivation;
            onImpact     = source.onImpact;

            debrisReference  = source.debrisReference;
            debrisMaterial   = source.debrisMaterial;
            emissionMaterial = source.emissionMaterial;

            emission.CopyFrom(source.emission);
            dynamic.CopyFrom(source.dynamic);
            noise.CopyFrom(source.noise);
            collision.CopyFrom(source.collision);
            limitations.CopyFrom(source.limitations);
            rendering.CopyFrom(source.rendering);

            // Hidden
            debrisMeshList = source.debrisMeshList;
            initialized    = source.initialized;
        }
Example #7
0
        /// /////////////////////////////////////////////////////////
        /// Renderer
        /// /////////////////////////////////////////////////////////

        // Set renderer
        public void SetParticleRendererDebris(ParticleSystemRenderer rend, RayfireDebris scr)
        {
            // Common vars
            rend.renderMode = ParticleSystemRenderMode.Mesh;
            rend.alignment  = ParticleSystemRenderSpace.World;



            // Set predefined meshes
            if (scr.debrisMeshList.Count > 0)
            {
                if (scr.debrisMeshList.Count <= 4)
                {
                    rend.SetMeshes(scr.debrisMeshList.ToArray());
                    rend.mesh = scr.debrisMeshList[0];
                }
                else
                {
                    List <Mesh> newList = new List <Mesh>();
                    for (int i = 0; i < 4; i++)
                    {
                        newList.Add(scr.debrisMeshList[Random.Range(0, scr.debrisMeshList.Count)]);
                    }
                    rend.SetMeshes(newList.ToArray());
                    rend.mesh = newList[0];
                }
            }

            // Set material
            rend.sharedMaterial = scr.debrisMaterial;

            // Shadow casting
            rend.shadowCastingMode = scr.rendering.castShadows == true
                ? UnityEngine.Rendering.ShadowCastingMode.On
                : UnityEngine.Rendering.ShadowCastingMode.Off;

            // Shadow receiving
            rend.receiveShadows = scr.rendering.receiveShadows;
        }
Example #8
0
        /// /////////////////////////////////////////////////////////
        /// Create common
        /// /////////////////////////////////////////////////////////

        // Create host and particle system
        public ParticleSystem CreateParticleSystem(RayfireDebris scr)
        {
            // Create root
            GameObject host = new GameObject(scr.name + "_debris");

            host.transform.position   = scr.transform.position;
            host.transform.rotation   = scr.transform.rotation;
            host.transform.parent     = scr.transform;
            host.transform.localScale = Vector3.one;

            // Particle system
            ParticleSystem ps = host.AddComponent <ParticleSystem>();

            scr.pSystem = ps;
            scr.hostTm  = host.transform;

            // Destroy after all particles death
            Destroy(host, scr.emission.lifeMax + ps.main.duration);

            // Stop for further properties set
            ps.Stop();

            return(ps);
        }
Example #9
0
        // Copy debris and dust
        public static void CopyRootMeshParticles(RayfireRigid source, List <RayfireRigid> targets)
        {
            // Copy debris. only initialized debris in this list
            if (source.HasDebris == true)
            {
                for (int d = 0; d < source.debrisList.Count; d++)
                {
                    // Set max amount
                    int maxAmount = targets.Count;
                    if (source.debrisList[d].limitations.percentage < 100)
                    {
                        maxAmount = targets.Count * source.debrisList[d].limitations.percentage / 100;
                    }

                    // Copy component
                    for (int i = 0; i < targets.Count; i++)
                    {
                        // Max amount reached
                        if (maxAmount <= 0)
                        {
                            break;
                        }

                        // TODO consider size threshold

                        // Filter by percentage
                        if (Random.Range(0, 100) > source.debrisList[d].limitations.percentage)
                        {
                            continue;
                        }

                        // Copy
                        RayfireDebris targetDebris = targets[i].gameObject.AddComponent <RayfireDebris>();
                        targetDebris.CopyFrom(source.debrisList[d]);
                        targetDebris.rigid = targets[i];

                        // Collect debris for Rigid
                        if (targets[i].debrisList == null)
                        {
                            targets[i].debrisList = new List <RayfireDebris>();
                        }
                        targets[i].debrisList.Add(targetDebris);

                        // Collect debris for parent debris
                        if (source.debrisList[d].children == null)
                        {
                            source.debrisList[d].children = new List <RayfireDebris>();
                        }
                        source.debrisList[d].children.Add(targetDebris);

                        maxAmount--;
                    }

                    // Get amount list
                    SetDebrisFinalAmount(source.debrisList[d].children, source.debrisList[d].emission.burstType, source.debrisList[d].emission.burstAmount);
                }
            }

            // Copy dust
            if (source.HasDust == true)
            {
                for (int d = 0; d < source.dustList.Count; d++)
                {
                    // Set max amount
                    int maxAmount = targets.Count;
                    if (source.dustList[d].limitations.percentage < 100)
                    {
                        maxAmount = targets.Count * source.dustList[d].limitations.percentage / 100;
                    }

                    for (int i = 0; i < targets.Count; i++)
                    {
                        // Max amount reached
                        if (maxAmount <= 0)
                        {
                            break;
                        }

                        // Filter by percentage
                        if (Random.Range(0, 100) > source.dustList[d].limitations.percentage)
                        {
                            continue;
                        }

                        // Copy
                        RayfireDust targetDust = targets[i].gameObject.AddComponent <RayfireDust>();
                        targetDust.CopyFrom(source.dustList[d]);
                        targetDust.rigid = targets[i];

                        // Collect debris for Rigid
                        if (targets[i].dustList == null)
                        {
                            targets[i].dustList = new List <RayfireDust>();
                        }
                        targets[i].dustList.Add(targetDust);

                        // Collect debris for parent debris
                        if (source.dustList[d].children == null)
                        {
                            source.dustList[d].children = new List <RayfireDust>();
                        }
                        source.dustList[d].children.Add(targetDust);

                        maxAmount--;
                    }

                    // Get amount list
                    SetDustFinalAmount(source.dustList[d].children, source.dustList[d].emission.burstType, source.dustList[d].emission.burstAmount);
                }
            }
        }
Example #10
0
        // Set material properties
        public void SetMaterialProps(RayfireDebris debris)
        {
            // Properties was set. Exclude this step
            if (propertiesSet == true)
            {
                return;
            }

            // Do this method once
            propertiesSet = true;

            // No need to set collision properties. NO collision expected
            if (debris.collision.collidesWith == RFParticleCollisionWithType.Nothing)
            {
                return;
            }

            // own props should be used
            if (dampenType == RFParticleCollisionMatType.ByProperties && bounceType == RFParticleCollisionMatType.ByProperties)
            {
                return;
            }

            // Set collider to take properties
            Collider collider;

            if (debris.rigid == null || debris.rigid.physics.meshCollider == null)
            {
                collider = debris.GetComponent <Collider>();
            }
            else
            {
                collider = debris.rigid.physics.meshCollider;
            }

            // No collider
            if (collider == null)
            {
                return;
            }

            // No collider material
            if (collider.sharedMaterial == null)
            {
                return;
            }

            // Set dampen
            if (dampenType == RFParticleCollisionMatType.ByPhysicalMaterial)
            {
                dampenMin = collider.sharedMaterial.dynamicFriction;
                dampenMax = dampenMin * 0.05f + dampenMin;
            }

            // Set bounce
            if (bounceType == RFParticleCollisionMatType.ByPhysicalMaterial)
            {
                bounceMin = collider.sharedMaterial.bounciness;
                bounceMax = bounceMin * 0.05f + bounceMin;
            }
        }