Inheritance: MonoBehaviour
示例#1
0
    private void Initialize()
    {
        _particles = new Particle[particleAmount];
        _colliders = new ParticleCollider[particleAmount];
        for (int i = 0; i < particleAmount; i++)
        {
            float x = (i % particlesPerRow) + Random.Range(-0.1f, 0.1f);
            float y = (2 * Radius) + (float)((i / particlesPerRow) / particlesPerRow) * 1.1f;
            float z = ((i / particlesPerRow) % particlesPerRow) + Random.Range(-0.1f, 0.1f);

            GameObject       current         = Instantiate(Prefab);
            Particle         currentParticle = current.AddComponent <Particle>();
            ParticleCollider currentCollider = current.AddComponent <ParticleCollider>();
            _particles[i] = currentParticle;
            _colliders[i] = currentCollider;

            current.transform.localScale = Vector3.one * Radius;
            current.transform.position   = new Vector3(x, y, z);

            currentParticle.Go       = current;
            currentParticle.Position = current.transform.position;
            currentCollider.Position = current.transform.position;
            currentCollider.Right    = current.transform.right;
            currentCollider.Up       = current.transform.up;
            currentCollider.Scale    = current.transform.localScale;
        }
    }
示例#2
0
    private Vector3 DampenVelocity(ParticleCollider particleCollider, Vector3 velocity, Vector3 penetrationNormal, float drag)
    {
        Vector3 newVelocity = Vector3.Dot(velocity, penetrationNormal) * _damping * penetrationNormal +
                              Vector3.Dot(velocity, particleCollider.Right) *
                              drag * particleCollider.Right + Vector3.Dot(velocity, particleCollider.Up) *
                              Drag * particleCollider.Up;

        return(Vector3.Dot(newVelocity, Vector3.forward) * Vector3.forward +
               Vector3.Dot(newVelocity, Vector3.right) * Vector3.right +
               Vector3.Dot(newVelocity, Vector3.up) * Vector3.up);
    }
    private void TestSelection()
    {
        // Selection (accounts for transparency)
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            if (!Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit))
            {
                return;
            }

            ParticleCollider hitParticleCollider = hit.transform.gameObject.GetComponent <ParticleCollider>();

            if (hitParticleCollider != null)
            {
                Renderer     rend         = hit.transform.GetComponent <Renderer>();
                MeshCollider meshCollider = hit.collider as MeshCollider;
                meshCollider.convex = false;

                if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null)
                {
                    Debug.Log("HIT! " + hitParticleCollider.ParticleSys.name, this);
                    return;
                }

                Texture2D tex     = rend.material.mainTexture as Texture2D;
                Vector2   pixelUV = hit.textureCoord;

                // Apply texture sheet animation offset
                ParticleSystem.TextureSheetAnimationModule texModule = hitParticleCollider.ParticleSys.textureSheetAnimation;
                if (texModule.enabled)
                {
                    SubUVTextureInfo subUV = new SubUVTextureInfo(texModule, hitParticleCollider.Particle);
                    pixelUV.x = (subUV.currentColumn / subUV.columns) + (pixelUV.x / subUV.columns);
                    pixelUV.y = (subUV.currentRow / subUV.rows) + ((1 - pixelUV.y) / subUV.rows);
                    pixelUV.y = 1 - pixelUV.y;
                }

                pixelUV.x *= tex.width;
                pixelUV.y *= tex.height;

                Color hitColor = tex.GetPixelForced((int)pixelUV.x, (int)pixelUV.y);
                Debug.Log("Raycast hit color: " + hitColor, this);
                if (debugImg != null)
                {
                    debugImg.color = hitColor;
                }
                if (hitColor.a > hitTestAlphaCutoff)
                {
                    Debug.Log("HIT! " + hitParticleCollider.ParticleSys.name, this);
                }
            }
        }
    }
示例#4
0
    private bool CheckCollision(ParticleCollider particleCollider, Vector3 position, float radius, out Vector3 penetrationNormal, out Vector3 penetrationPosition, out float penetrationLength)
    {
        Vector3 colliderProjection = particleCollider.Position - position;

        penetrationNormal   = Vector3.Cross(particleCollider.Right, particleCollider.Up);
        penetrationLength   = Mathf.Abs(Vector3.Dot(colliderProjection, penetrationNormal)) - (radius / 2.0f);
        penetrationPosition = particleCollider.Position - colliderProjection;

        return(penetrationLength < 0.0f &&
               Mathf.Abs(Vector3.Dot(colliderProjection, particleCollider.Right)) < particleCollider.Scale.x &&
               Mathf.Abs(Vector3.Dot(colliderProjection, particleCollider.Up)) < particleCollider.Scale.y);
    }
        public void CreateParticleColliders()
        {
            ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particleSys.particleCount];
            int particleCount = particleSys.GetParticles(particles);

            for (int i = 0; i < particleCount; i++)
            {
                GameObject go = Instantiate(Resources.Load <GameObject>("ParticleCollider"));
                go.name = particleSys.name + " collider " + i;
                MeshFilter   meshFilter   = go.GetComponent <MeshFilter>();
                MeshCollider meshCollider = go.GetComponent <MeshCollider>();
                Renderer     rend         = go.GetComponent <Renderer>();
                rend.material = particleSystemRenderer.material;

                switch (particleSystemRenderer.renderMode)
                {
                case ParticleSystemRenderMode.Billboard:
                    rend.material.SetColor(TINT_COLOR, particleSystemRenderer.material.GetColor(TINT_COLOR));
                    break;

                case ParticleSystemRenderMode.Mesh:
                    meshFilter.mesh         = particleSystemRenderer.mesh;
                    meshCollider.sharedMesh = particleSystemRenderer.mesh;
                    break;

                default:
                    Debug.LogError("Unsupported render mode " + particleSystemRenderer.renderMode);
                    break;
                }

                ParticleCollider newParticleCollider = go.AddComponent <ParticleCollider>();
                newParticleCollider.Init(particles[i], particleSys);

                particleColliders.Add(newParticleCollider);
            }
        }
示例#6
0
        /// <summary>
        /// Return the currently selected menu button based on the previous
        /// menu parameters.
        /// </summary>
        /// <param name="current_selected">The index of the currently selected button.</param>
        /// <param name="buttons">The array of buttons.</param>
        /// <param name="increment">The key that increments the selection.</param>
        /// <param name="decrement">The key that decrements the selection.</param>
        /// <param name="wrap">
        /// Determines what happens when the selection becomes < 0 or greater than the
        /// length of the array. If true, then the selection is modded by the length of
        /// the array. If false it is clamped.
        /// </param>
        /// <returns></returns>
        public static int?GetMenuSelection(
            int?current_selected, SceneButton[] buttons,
            Keys increment, Keys decrement,
            bool wrap = true, int default_selection = 0)
        {
            // Local aliases for previous and current mouse and keyboard states.
            MouseState pms = Globals.previousMouseState;
            MouseState cms = Globals.currentMouseState;

            KeyboardState pks = Globals.previousKeyboardState;
            KeyboardState cks = Globals.currentKeyboardState;

            // If the mouse states are valid, and the positions are different (implying
            // the mouse has moved).
            if (pms != null && cms.Position != pms.Position)
            {
                ParticleCollider mouse_pos = new ParticleCollider(cms.Position);
                // Find the first button that collides with the mouse.
                for (int i = 0; i < buttons.Length; i++)
                {
                    if (CollisionChecker.Collision(buttons[i].CollisionMask, mouse_pos))
                    {
                        return(i);
                    }
                }
            }
            // Else if the keyboard states are valid.
            else if (pks != null)
            {
                // If the increment key was pressed once.
                if (
                    cks.IsKeyDown(increment) &&
                    !pks.IsKeyDown(increment))
                {
                    // Return the default value if current_selected == null.
                    if (!current_selected.HasValue)
                    {
                        return(default_selection);
                    }
                    int new_selection = current_selected.Value - 1;
                    // Clamp or wrap the selection.
                    if (wrap)
                    {
                        return(new_selection % buttons.Length);
                    }
                    return(wrap ? new_selection % buttons.Length
                                : (int)MathHelper.Clamp(new_selection, 0, buttons.Length));
                }
                // If the decrement key was pressed once.
                else if (
                    cks.IsKeyDown(decrement) &&
                    !pks.IsKeyDown(decrement))
                {
                    // Return the default value if current_selected == null.
                    if (!current_selected.HasValue)
                    {
                        return(default_selection);
                    }
                    int new_selection = current_selected.Value - 1;
                    // Clamp or wrap the selection.
                    if (wrap)
                    {
                        return(new_selection % buttons.Length);
                    }
                    return(wrap ? new_selection % buttons.Length
                                : (int)MathHelper.Clamp(new_selection, 0, buttons.Length));
                }
            }
            // Return the current_selected value or null.
            return(current_selected ?? null);
        }
示例#7
0
 private void Awake()
 {
     connectorSource  = GetComponent <ConnectorSource>();
     particleCollider = GetComponent <ParticleCollider>();
 }