bool CheckIn(ref FlareState state, int lightIndex) { if (state.sourceCoordinate.x < _camera.pixelRect.xMin || state.sourceCoordinate.y < _camera.pixelRect.yMin || state.sourceCoordinate.x > _camera.pixelRect.xMax || state.sourceCoordinate.y > _camera.pixelRect.yMax || Vector3.Dot(lightSource[lightIndex].transform.position - _camera.transform.position, _camera.transform.forward) < 0.25f) { return(false); } else { var camPos = _camera.transform.position; var targetPos = lightSource[lightIndex].directionalLight ? -lightSource[lightIndex].transform.forward * 10000f : lightSource[lightIndex].transform.position; Ray ray = new Ray(camPos, targetPos - camPos); RaycastHit hit; Physics.Raycast(ray, out hit); if (Vector3.Distance(hit.point, camPos) < Vector3.Distance(targetPos, camPos)) { if (hit.point == Vector3.zero) { return(true); } return(false); } return(true); } }
bool CheckIn(ref FlareState state, int lightIndex) { if (state.sourceCoordinate.x < _camera.pixelRect.xMin || state.sourceCoordinate.y < _camera.pixelRect.yMin || state.sourceCoordinate.x > _camera.pixelRect.xMax || state.sourceCoordinate.y > _camera.pixelRect.yMax || Vector3.Dot(lightSource[lightIndex].transform.position - _camera.transform.position, _camera.transform.forward) < 0.25f) { return(false); } else { var direction = lightSource[lightIndex].directionalLight ? -lightSource[lightIndex].transform.forward : lightSource[lightIndex].transform.position - _camera.transform.position; float distance = lightSource[lightIndex].directionalLight ? float.PositiveInfinity : direction.magnitude; float angle = Vector3.Angle(lightSource[lightIndex].transform.forward, -direction); if (!lightSource[lightIndex].directionalLight && angle > lightSource[lightIndex].angle) { return(false); } if (Physics.Raycast(_camera.transform.position, direction, distance, blockLayers)) { return(false); } return(true); } }
void GetSourceCoordinate(ref FlareState state, int lightIndex) { Vector3 sourceScreenPos = _camera.WorldToScreenPoint( lightSource[lightIndex].directionalLight ?lightSource[lightIndex].transform.position - lightSource[lightIndex].transform.forward * 10000 :lightSource[lightIndex].transform.position ); state.sourceCoordinate = new Vector3(sourceScreenPos.x, sourceScreenPos.y, DISTANCE); }
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); }
void CalculateMeshData(ref FlareState state, int lightIndex) { Vector3[] oneFlareLine = new Vector3[lightSource[lightIndex].asset.spriteBlocks.Count]; bool[] useLightColor = new bool[lightSource[lightIndex].asset.spriteBlocks.Count]; for (int i = 0; i < lightSource[lightIndex].asset.spriteBlocks.Count; i++) { Vector2 realSourceCoordinateOffset = new Vector2(state.sourceCoordinate.x - _halfScreen.x, state.sourceCoordinate.y - _halfScreen.y); Vector2 realOffset = realSourceCoordinateOffset * lightSource[lightIndex].asset.spriteBlocks[i].offset; oneFlareLine[i] = new Vector3(_halfScreen.x + realOffset.x, _halfScreen.y + realOffset.y, state.sourceCoordinate.z); useLightColor[i] = lightSource[lightIndex].asset.spriteBlocks[i].useLightColor; } state.flareWorldPosCenter = oneFlareLine; }
bool CheckIn(ref FlareState state, int lightIndex) { if (state.sourceCoordinate.x < _camera.pixelRect.xMin || state.sourceCoordinate.y < _camera.pixelRect.yMin || state.sourceCoordinate.x > _camera.pixelRect.xMax || state.sourceCoordinate.y > _camera.pixelRect.yMax || Vector3.Dot(lightSource[lightIndex].transform.position - _camera.transform.position, _camera.transform.forward) < 0.25f) { return(false); } else { if (Physics.Raycast(_camera.transform.position, (lightSource[lightIndex].directionalLight ? -lightSource[lightIndex].transform.forward :lightSource[lightIndex].transform.position - _camera.transform.position))) { return(false); } return(true); } }
// 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); // } // } private void Update() { _halfScreen = new Vector2(_camera.scaledPixelWidth / 2 + _camera.pixelRect.xMin, _camera.scaledPixelHeight / 2 + _camera.pixelRect.yMin); foreach (Mesh mesh in _totalMesh) { mesh.Clear(); } _totalMesh.Clear(); for (int i = 0; i < lightSource.Count; i++) { _totalMesh.Add(new Mesh()); FlareState flareState = flareDatas[i]; GetSourceCoordinate(ref flareState, i); bool isIn = CheckIn(ref flareState, i); if (flareState.fadeoutScale > 0) { // if (flareData.fadeState == 3) // { // _totalMesh.Add(new Mesh()); // } if (!isIn) { flareState.fadeState = 2; } } if (flareState.fadeoutScale < 1) { if (isIn) { flareState.fadeState = 1; } } if (!isIn && flareState.fadeoutScale <= 0) { if (flareState.fadeState != 3) { flareState.fadeState = 3; } } else { CalculateMeshData(ref flareState, i); } switch (flareState.fadeState) { case 1: flareState.fadeoutScale += Time.deltaTime / fadeoutTime; flareState.fadeoutScale = Mathf.Clamp(flareState.fadeoutScale, 0, 1); flareDatas[i] = flareState; break; case 2: flareState.fadeoutScale -= Time.deltaTime / fadeoutTime; flareState.fadeoutScale = Mathf.Clamp(flareState.fadeoutScale, 0, 1); flareDatas[i] = flareState; break; case 3: // RemoveLight(lightSource[i]); break; default: flareDatas[i] = flareState; break; } } CreateMesh(); if (DebugMode) { Debug.Log("Lens Flare : " + lightSource.Count + " lights"); for (int i = 0; i < lightSource.Count; i++) { DebugDrawMeshPos(i); } } }