Esempio n. 1
0
    public void LateUpdate()
    {
        if (thrusterObserver == null)
        {
            thrusterObserver = SGT_Helper.FindCamera();
        }
        if (thrusterFlameGameObject == null)
        {
            thrusterFlameGameObject = SGT_Helper.CreateGameObject("Flame", gameObject);
        }
        if (thrusterFlareGameObject == null)
        {
            thrusterFlareGameObject = SGT_Helper.CreateGameObject("Flare", gameObject);
        }

        SGT_Helper.SetParent(thrusterFlameGameObject, gameObject);
        SGT_Helper.SetLayer(thrusterFlameGameObject, gameObject.layer);
        SGT_Helper.SetTag(thrusterFlameGameObject, gameObject.tag);

        SGT_Helper.SetParent(thrusterFlareGameObject, gameObject);
        SGT_Helper.SetLayer(thrusterFlareGameObject, gameObject.layer);
        SGT_Helper.SetTag(thrusterFlareGameObject, gameObject.tag);

        if (thrusterPhysics == true && thrusterPhysicsRigidbody == null)
        {
            thrusterPhysicsRigidbody = SGT_Helper.GetComponentUpwards <Rigidbody>(gameObject);
        }

        var observerPosition = SGT_Helper.GetPosition(thrusterObserver);

        if (Application.isPlaying == true)
        {
            currentThrusterThrottle = Mathf.MoveTowards(currentThrusterThrottle, targetThrusterThrottle, thrusterTweenSpeed * Time.deltaTime);
        }
        else
        {
            currentThrusterThrottle = targetThrusterThrottle;
        }

        if (thrusterFlame == true)
        {
            if (thrusterFlameMesh == null)
            {
                thrusterFlameMesh = new SGT_Mesh();
            }

            // Offset flame
            SGT_Helper.SetLocalPosition(thrusterFlameGameObject.transform, thrusterFlameOffset);

            var finalFlameScale = thrusterFlameScale + thrusterFlameScaleChange * currentThrusterThrottle;

            // Hide/show flame
            if (finalFlameScale == Vector3.zero)
            {
                thrusterFlameMesh.MeshRendererEnabled = false;
            }
            else
            {
                if (Application.isPlaying == true)
                {
                    finalFlameScale *= Random.Range(1.0f - thrusterFlameScaleFlicker, 1.0f);
                }

                thrusterFlameMesh.MeshRendererEnabled = true;

                SGT_Helper.SetLocalScale(thrusterFlameGameObject.transform, finalFlameScale);

                // Roll flame to observer
                var pointDir = transform.InverseTransformPoint(observerPosition);
                var roll     = Mathf.Atan2(pointDir.y, pointDir.x) * Mathf.Rad2Deg;

                SGT_Helper.SetRotation(thrusterFlameGameObject.transform, transform.rotation * Quaternion.Euler(0.0f, 0.0f, roll));
            }

            thrusterFlameMesh.GameObject      = thrusterFlameGameObject;
            thrusterFlameMesh.HasMeshRenderer = true;
            thrusterFlameMesh.Update();
        }
        else
        {
            if (thrusterFlameMesh != null)
            {
                thrusterFlameMesh = thrusterFlameMesh.Clear();
            }
        }

        if (thrusterFlare == true)
        {
            if (thrusterFlareMesh == null)
            {
                thrusterFlareMesh = new SGT_Mesh();
            }

            // Offset flare
            SGT_Helper.SetLocalPosition(thrusterFlareGameObject.transform, thrusterFlareOffset);

            // Flare visible?
            var a               = thrusterFlareGameObject.transform.position;
            var b               = observerPosition;
            var direction       = (b - a).normalized;
            var distance        = (b - a).magnitude;
            var targetFlareSize = 0.0f;

            // If the ray hits something, then hide the flare
            if (Physics.Raycast(a, direction, distance, thrusterFlareRaycastMask) == true)
            {
                targetFlareSize = 0.0f;
            }
            else
            {
                targetFlareSize = 1.0f;
            }

            // Point flare at observer
            if (thrusterObserver != null)
            {
                SGT_Helper.SetRotation(thrusterFlareGameObject.transform, thrusterObserver.transform.rotation);
            }

            // Fade flare in/out based on raycast
            if (Application.isPlaying == true)
            {
                currentThrusterFlareScale = Mathf.MoveTowards(currentThrusterFlareScale, targetFlareSize, thrusterFlareScaleTweenSpeed * Time.deltaTime);
            }
            else
            {
                currentThrusterFlareScale = targetFlareSize;
            }

            var finalFlareScale = currentThrusterFlareScale * (thrusterFlareScale + thrusterFlareScaleChange * currentThrusterThrottle);

            // Hide/show flare
            if (finalFlareScale == Vector3.zero)
            {
                thrusterFlareMesh.MeshRendererEnabled = false;
            }
            else
            {
                if (Application.isPlaying == true)
                {
                    finalFlareScale *= Random.Range(1.0f - thrusterFlareScaleFlicker, 1.0f);
                }

                thrusterFlareMesh.MeshRendererEnabled = true;

                SGT_Helper.SetLocalScale(thrusterFlareGameObject.transform, finalFlareScale);
            }

            thrusterFlareMesh.GameObject      = thrusterFlareGameObject;
            thrusterFlareMesh.HasMeshRenderer = true;
            thrusterFlareMesh.Update();
        }
        else
        {
            if (thrusterFlareMesh != null)
            {
                thrusterFlareMesh = thrusterFlareMesh.Clear();
            }
        }

#if UNITY_EDITOR == true
        if (thrusterFlameMesh != null)
        {
            thrusterFlameMesh.HideInEditor();
        }
        if (thrusterFlareMesh != null)
        {
            thrusterFlareMesh.HideInEditor();
        }

        SGT_Helper.HideGameObject(thrusterFlameGameObject);
        SGT_Helper.HideGameObject(thrusterFlareGameObject);
#endif
    }
Esempio n. 2
0
    private void CreateMesh(ParticleList particles)
    {
        SGT_Helper.DestroyObject(generatedMesh);

        particleCount = particles.Count;
        generatedMesh = new Mesh();

        var positions = new Vector3[particleCount * 4];
        var indices   = new int[particleCount * 6];
        var uv0s      = new Vector2[particleCount * 4];
        var uv1s      = new Vector2[particleCount * 4];
        var normals   = new Vector3[particleCount * 4];
        var colours   = new Color[particleCount * 4];

        for (var i = 0; i < particleCount; i++)
        {
            var particle = particles[i];

            var i0 = i * 6;
            var i1 = i0 + 1;
            var i2 = i1 + 1;
            var i3 = i2 + 1;
            var i4 = i3 + 1;
            var i5 = i4 + 1;

            var v0 = i * 4;
            var v1 = v0 + 1;
            var v2 = v1 + 1;
            var v3 = v2 + 1;

            // Index data
            indices[i0] = v0;
            indices[i1] = v1;
            indices[i2] = v2;
            indices[i3] = v3;
            indices[i4] = v2;
            indices[i5] = v1;

            var right = SGT_Helper.Rotate(Vector2.right * SGT_Helper.InscribedBox, particle.Angle);
            var up    = SGT_Helper.Rotate(Vector2.up * SGT_Helper.InscribedBox, particle.Angle);
            var uv1   = new Vector2(particle.Size * 0.5f, 0.0f);

            // Write star values into vertex data
            positions[v0] = particle.Position;
            positions[v1] = particle.Position;
            positions[v2] = particle.Position;
            positions[v3] = particle.Position;

            normals[v0] = SGT_Helper.NewVector3(-right + up, 0.0f);
            normals[v1] = SGT_Helper.NewVector3(right + up, 0.0f);
            normals[v2] = SGT_Helper.NewVector3(-right - up, 0.0f);
            normals[v3] = SGT_Helper.NewVector3(right - up, 0.0f);

            colours[v0] = particle.Colour;
            colours[v1] = particle.Colour;
            colours[v2] = particle.Colour;
            colours[v3] = particle.Colour;

            uv0s[v0] = new Vector2(0.0f, 1.0f);
            uv0s[v1] = new Vector2(1.0f, 1.0f);
            uv0s[v2] = new Vector2(0.0f, 0.0f);
            uv0s[v3] = new Vector2(1.0f, 0.0f);

            uv1s[v0] = uv1;
            uv1s[v1] = uv1;
            uv1s[v2] = uv1;
            uv1s[v3] = uv1;
        }

        generatedMesh.name      = "Dust";
        generatedMesh.vertices  = positions;
        generatedMesh.normals   = normals;
        generatedMesh.colors    = colours;
        generatedMesh.uv        = uv0s;
        generatedMesh.uv2       = uv1s;
        generatedMesh.triangles = indices;
        generatedMesh.bounds    = new Bounds(Vector3.zero, new Vector3(100000000.0f, 100000000.0f, 100000000.0f));         // NOTE: using infinity here causes an error

        dustMesh.SharedMesh = generatedMesh;
        dustMesh.Update();
    }
Esempio n. 3
0
    private void CreateMesh(ParticleList particles)
    {
        SGT_Helper.DestroyObject(generatedMesh);

        particleCount = particles.Count;
        generatedMesh = new Mesh();

        var positions = new Vector3[particleCount * 4];
        var indices   = new int[particleCount * 6];
        var uv0s      = new Vector2[particleCount * 4];
        var uv1s      = new Vector2[particleCount * 4];
        var normals   = new Vector3[particleCount * 4];
        var colours   = new Color[particleCount * 4];
        var bounds    = new Bounds();

        for (var i = 0; i < particleCount; i++)
        {
            var particle = particles[i];

            var i0 = i * 6;
            var i1 = i0 + 1;
            var i2 = i1 + 1;
            var i3 = i2 + 1;
            var i4 = i3 + 1;
            var i5 = i4 + 1;

            var v0 = i * 4;
            var v1 = v0 + 1;
            var v2 = v1 + 1;
            var v3 = v2 + 1;

            // Index data
            indices[i0] = v0;
            indices[i1] = v1;
            indices[i2] = v2;
            indices[i3] = v3;
            indices[i4] = v2;
            indices[i5] = v1;

            var right = SGT_Helper.Rotate(Vector2.right * SGT_Helper.InscribedBox, particle.Angle);
            var up    = SGT_Helper.Rotate(Vector2.up * SGT_Helper.InscribedBox, particle.Angle);
            var uv1   = new Vector2(particle.Size * 0.5f, 0.0f);

            bounds.Encapsulate(particle.Position);

            // Write star values into vertex data
            positions[v0] = particle.Position;
            positions[v1] = particle.Position;
            positions[v2] = particle.Position;
            positions[v3] = particle.Position;

            normals[v0] = SGT_Helper.NewVector3(-right + up, 0.0f);
            normals[v1] = SGT_Helper.NewVector3(right + up, 0.0f);
            normals[v2] = SGT_Helper.NewVector3(-right - up, 0.0f);
            normals[v3] = SGT_Helper.NewVector3(right - up, 0.0f);

            colours[v0] = particle.Colour;
            colours[v1] = particle.Colour;
            colours[v2] = particle.Colour;
            colours[v3] = particle.Colour;

            uv0s[v0] = new Vector2(0.0f, 1.0f);
            uv0s[v1] = new Vector2(1.0f, 1.0f);
            uv0s[v2] = new Vector2(0.0f, 0.0f);
            uv0s[v3] = new Vector2(1.0f, 0.0f);

            uv1s[v0] = uv1;
            uv1s[v1] = uv1;
            uv1s[v2] = uv1;
            uv1s[v3] = uv1;
        }

        bounds.Expand(particleScale);

        generatedMesh.hideFlags = HideFlags.DontSave;
        generatedMesh.name      = "Nebula";
        generatedMesh.bounds    = bounds;
        generatedMesh.vertices  = positions;
        generatedMesh.normals   = normals;
        generatedMesh.colors    = colours;
        generatedMesh.uv        = uv0s;
        generatedMesh.uv1       = uv1s;
        generatedMesh.triangles = indices;

        nebulaMesh.SharedMesh = generatedMesh;
        nebulaMesh.Update();
    }