Exemple #1
0
    private FlareState InitFlareData(URPFlareLauncher urpFlareLauncher)
    {
        FlareState state = new FlareState
        {
            sourceCoordinate    = Vector3.zero,
            flareWorldPosCenter = new Vector3[urpFlareLauncher.asset.spriteBlocks.Count],
            // edgeScale = 1,
            fadeoutScale = 0,
            fadeState    = 1
        };

        return(state);
    }
Exemple #2
0
 public void AddLight(URPFlareLauncher urpFlareLauncher)
 {
     if (DebugMode)
     {
         Debug.Log("Add Light " + urpFlareLauncher.gameObject.name + " to FlareList");
     }
     lightSource.Add(urpFlareLauncher);
     flareDatas.Add(InitFlareData(urpFlareLauncher));
     if (_totalMesh == null)
     {
         _totalMesh = new List <Mesh>();
     }
     _totalMesh.Add(new Mesh());
 }
    public void RemoveLight(URPFlareLauncher urpFlareLauncher)
    {
        if (DebugMode)
        {
            Debug.Log("Remove Light " + urpFlareLauncher.gameObject.name + " from FlareList");
        }
        int t = lightSource.IndexOf(urpFlareLauncher);

        lightSource.RemoveAt(t);
        flareDatas.RemoveAt(t);
        while (_totalMesh.Count > lightSource.Count)
        {
            _totalMesh.RemoveAt(0);
        }
    }
Exemple #4
0
    void CreateMesh()
    {
        if (_totalMesh.Count <= 0)
        {
            Debug.Log("Not enough source"); return;
        }

        List <Vector3> vertList   = new List <Vector3>();
        List <Vector2> uvList     = new List <Vector2>();
        List <int>     tri        = new List <int>();
        List <Color>   vertColors = new List <Color>();

        int count      = 0;
        var transform1 = _camera.transform;
        var position   = transform1.position;
        var center     = position + transform1.forward * 0.1f;

        for (int lightIndex = 0; lightIndex < lightSource.Count; lightIndex++)
        {
            _totalColor.Clear();
            _totalTriangle.Clear();
            _totalVert.Clear();
            _totalUv.Clear();
            vertList.Clear();
            uvList.Clear();
            tri.Clear();
            vertColors.Clear();
            if (flareDatas[lightIndex].fadeoutScale > 0)
            {
                URPFlareLauncher observer = lightSource[lightIndex];
                Texture2D        tex      = observer.tex;//observer.asset.flareSprite;
                float            angle    = (45 + Vector2.SignedAngle(Vector2.up, new Vector2(flareDatas[lightIndex].sourceCoordinate.x - _halfScreen.x, flareDatas[lightIndex].sourceCoordinate.y - _halfScreen.y))) / 180 * Mathf.PI;
                for (int i = 0; i < lightSource[lightIndex].asset.spriteBlocks.Count; i++)
                {
                    Rect    rect     = observer.asset.spriteBlocks[i].block;
                    Vector2 halfSize = new Vector2(
                        tex.width * rect.width / 2 * observer.asset.spriteBlocks[i].scale * (observer.asset.fadeWithScale ? (flareDatas[lightIndex].fadeoutScale * 0.5f + 0.5f) : 1),
                        tex.height * rect.height / 2 * observer.asset.spriteBlocks[i].scale * (observer.asset.fadeWithScale ? (flareDatas[lightIndex].fadeoutScale * 0.5f + 0.5f) : 1));
                    Vector3 flarePos = flareDatas[lightIndex].flareWorldPosCenter[i];
                    if (observer.asset.spriteBlocks[i].useRotation)
                    {
                        float magnitude = Mathf.Sqrt(halfSize.x * halfSize.x + halfSize.y * halfSize.y);
                        float cos       = magnitude * Mathf.Cos(angle);
                        float sin       = magnitude * Mathf.Sin(angle);
                        vertList.Add(_camera.ScreenToWorldPoint(new Vector3(flarePos.x - sin, flarePos.y + cos, flarePos.z)) - center);
                        vertList.Add(_camera.ScreenToWorldPoint(new Vector3(flarePos.x - cos, flarePos.y - sin, flarePos.z)) - center);
                        vertList.Add(_camera.ScreenToWorldPoint(new Vector3(flarePos.x + cos, flarePos.y + sin, flarePos.z)) - center);
                        vertList.Add(_camera.ScreenToWorldPoint(new Vector3(flarePos.x + sin, flarePos.y - cos, flarePos.z)) - center);
                    }
                    else
                    {
                        vertList.Add(_camera.ScreenToWorldPoint(new Vector3(flarePos.x - halfSize.x, flarePos.y + halfSize.y, flarePos.z)) - center);
                        vertList.Add(_camera.ScreenToWorldPoint(new Vector3(flarePos.x - halfSize.x, flarePos.y - halfSize.y, flarePos.z)) - center);
                        vertList.Add(_camera.ScreenToWorldPoint(new Vector3(flarePos.x + halfSize.x, flarePos.y + halfSize.y, flarePos.z)) - center);
                        vertList.Add(_camera.ScreenToWorldPoint(new Vector3(flarePos.x + halfSize.x, flarePos.y - halfSize.y, flarePos.z)) - center);
                    }
                    uvList.Add(rect.position);
                    uvList.Add(rect.position + new Vector2(rect.width, 0));
                    uvList.Add(rect.position + new Vector2(0, rect.height));
                    uvList.Add(rect.position + rect.size);

                    tri.Add(i * 4);
                    tri.Add(i * 4 + 3);
                    tri.Add(i * 4 + 1);
                    tri.Add(i * 4);
                    tri.Add(i * 4 + 2);
                    tri.Add(i * 4 + 3);

                    Color vertexAddColor = observer.asset.spriteBlocks[i].color;
                    Color lightColor     = observer.asset.spriteBlocks[i].useLightColor
                        ? observer.GetComponent <Light>().color
                        : new Color(1, 1, 1, 1);
                    lightColor *= observer.useLightIntensity ? observer.GetComponent <Light>().intensity : 1;

                    vertexAddColor *= new Vector4(lightColor.r, lightColor.g, lightColor.b,
                                                  (1.5f - Mathf.Abs(observer.asset.spriteBlocks[i].offset)) / 1.5f
                                                  * (1 - Mathf.Min(1, new Vector2(flarePos.x - _halfScreen.x, flarePos.y - _halfScreen.y).magnitude / new Vector2(_halfScreen.x, _halfScreen.y).magnitude))
                                                  ) * ((observer.asset.fadeWithAlpha ? flareDatas[lightIndex].fadeoutScale: 1));
                    vertexAddColor = vertexAddColor.linear;
                    vertColors.Add(vertexAddColor);
                    vertColors.Add(vertexAddColor);
                    vertColors.Add(vertexAddColor);
                    vertColors.Add(vertexAddColor);
                }
                _totalVert.AddRange(vertList);
                _totalUv.AddRange(uvList);
                _totalTriangle.AddRange(tri);
                _totalColor.AddRange(vertColors);
                _totalMesh[count].vertices  = _totalVert.ToArray();
                _totalMesh[count].uv        = _totalUv.ToArray();
                _totalMesh[count].triangles = _totalTriangle.ToArray();
                _totalMesh[count].colors    = _totalColor.ToArray();
                _propertyBlock.SetTexture(STATIC_BaseMap, observer.asset.flareSprite);

                Graphics.DrawMesh(_totalMesh[count], center, Quaternion.identity, material, 0, _camera, 0, _propertyBlock);

                count++;
            }
        }
    }