public void UpdateLightSprite(LightSprite2D id, MeshMode meshMode) { if (id.GetSprite() == null) { Free(); return; } if (meshModeMaterial != meshMode.materials) { meshModeMaterial = meshMode.materials; ClearMaterial(); } if (meshModeShader != meshMode.shader) { meshModeShader = meshMode.shader; ClearMaterial(); } Material[] material = GetMaterials(); if (material == null) { return; } float rotation = id.lightSpriteTransform.rotation; if (id.lightSpriteTransform.applyRotation) { rotation += id.transform.rotation.eulerAngles.z; } ////////////////////// Scale Vector2 scale = Vector2.zero; Sprite sprite = id.GetSprite(); Rect spriteRect = sprite.textureRect; scale.x = (float)sprite.texture.width / spriteRect.width; scale.y = (float)sprite.texture.height / spriteRect.height; Vector2 size = id.lightSpriteTransform.scale; size.x *= 2; size.y *= 2; size.x /= scale.x; size.y /= scale.y; size.x *= (float)sprite.texture.width / (sprite.pixelsPerUnit * 2); size.y *= (float)sprite.texture.height / (sprite.pixelsPerUnit * 2); if (id.spriteRenderer.flipX) { size.x = -size.x; } if (id.spriteRenderer.flipY) { size.y = -size.y; } ////////////////////// PIVOT Rect rect = spriteRect; Vector2 pivot = sprite.pivot; pivot.x /= spriteRect.width; pivot.y /= spriteRect.height; pivot.x -= 0.5f; pivot.y -= 0.5f; pivot.x *= size.x; pivot.y *= size.y; float pivotDist = Mathf.Sqrt(pivot.x * pivot.x + pivot.y * pivot.y); float pivotAngle = Mathf.Atan2(pivot.y, pivot.x); float rot = rotation * Mathf.Deg2Rad + Mathf.PI; Vector2 position = Vector2.zero; // Pivot Pushes Position position.x += Mathf.Cos(pivotAngle + rot) * pivotDist * id.transform.lossyScale.x; position.y += Mathf.Sin(pivotAngle + rot) * pivotDist * id.transform.lossyScale.y; position.x += id.transform.position.x; position.y += id.transform.position.y; position.x += id.lightSpriteTransform.position.x; position.y += id.lightSpriteTransform.position.y; Vector3 pos = position; pos.z = id.transform.position.z - 0.1f; transform.position = pos; Vector3 scale2 = id.transform.lossyScale; scale2.x *= size.x; scale2.y *= size.y; scale2.x /= 2; scale2.y /= 2; scale2.z = 1; transform.localScale = scale2; transform.rotation = Quaternion.Euler(0, 0, rotation); Rect uvRect = new Rect(); uvRect.x = rect.x / sprite.texture.width; uvRect.y = rect.y / sprite.texture.height; uvRect.width = rect.width / sprite.texture.width + uvRect.x; uvRect.height = rect.height / sprite.texture.height + uvRect.y; if (meshRenderer != null) { Color lightColor = id.color; lightColor.a = id.meshMode.alpha; for (int i = 0; i < materials.Length; i++) { if (materials[i] == null) { continue; } materials[i].SetColor("_TintColor", lightColor); materials[i].color = lightColor; materials[i].mainTexture = id.GetSprite().texture; } id.meshMode.sortingLayer.ApplyToMeshRenderer(meshRenderer); meshRenderer.sharedMaterials = materials; meshRenderer.enabled = true; Mesh mesh = GetMeshSprite(); Vector2[] uvs = mesh.uv; uvs[0].x = uvRect.x; uvs[0].y = uvRect.y; uvs[1].x = uvRect.width; uvs[1].y = uvRect.y; uvs[2].x = uvRect.width; uvs[2].y = uvRect.height; uvs[3].x = uvRect.x; uvs[3].y = uvRect.height; mesh.uv = uvs; meshFilter.mesh = mesh; } }
public void UpdateLight(Light2D id, MeshMode meshMode) // Camera { if (meshModeMaterial != meshMode.materials) { meshModeMaterial = meshMode.materials; ClearMaterial(); } if (meshModeShader != meshMode.shader) { meshModeShader = meshMode.shader; ClearMaterial(); } Material[] materials = GetMaterials(); if (materials == null) { return; } if (id.IsPixelPerfect()) { Camera camera = Camera.main; Vector2 cameraSize = LightingRender2D.GetSize(camera); Vector2 cameraPosition = LightingPosition.GetPosition2D(-camera.transform.position); transform.position = new Vector3(cameraPosition.x, cameraPosition.y, id.transform.position.z); transform.localScale = new Vector3(cameraSize.x, cameraSize.y, 1); } else { transform.position = id.transform.position; transform.localScale = new Vector3(id.size, id.size, 1); } transform.rotation = Quaternion.Euler(0, 0, 0); // transform.rotation = id.transform.rotation; // only if rotation enabled if (id.Buffer != null && meshRenderer != null) { Color lightColor = id.color; lightColor.a = id.meshMode.alpha; for (int i = 0; i < materials.Length; i++) { if (materials[i] == null) { continue; } materials[i].SetColor("_TintColor", lightColor); materials[i].color = lightColor; materials[i].mainTexture = id.Buffer.renderTexture.renderTexture; } id.meshMode.sortingLayer.ApplyToMeshRenderer(meshRenderer); meshRenderer.sharedMaterials = GetMaterials(); meshRenderer.enabled = true; meshFilter.mesh = GetMeshLight(); } }