private void UpdateShader(SimpleRainDrawerContainer dc, int index) { float progress = GetProgress(dc); dc.Drawer.RenderQueue = RenderQueue + index; dc.Drawer.NormalMap = Variables.NormalMap; dc.Drawer.ReliefTexture = Variables.OverlayTexture; dc.Drawer.OverlayColor = new Color( Variables.OverlayColor.r, Variables.OverlayColor.g, Variables.OverlayColor.b, Variables.OverlayColor.a * Variables.AlphaOverLifetime.Evaluate(progress) * Alpha ); dc.Drawer.DistortionStrength = Variables.DistortionValue * Variables.DistortionOverLifetime.Evaluate(progress) * Alpha; dc.Drawer.ReliefValue = Variables.ReliefValue * Variables.ReliefOverLifetime.Evaluate(progress) * Alpha; dc.Drawer.Blur = Variables.Blur * Variables.BlurOverLifetime.Evaluate(progress) * Alpha; dc.Drawer.Darkness = Variables.Darkness * Alpha; dc.transform.localScale = dc.startSize * Variables.SizeOverLifetime.Evaluate(progress); // old //dc.transform.localPosition = dc.startPos + Vector3.up * Variables.PosYOverLifetime.Evaluate(progress); Vector3 gforced = RainDropTools.GetGForcedScreenMovement(this.camera.transform, this.GForceVector); gforced = gforced.normalized; dc.transform.localPosition += new Vector3(-gforced.x, -gforced.y, 0f) * 0.01f * Variables.PosYOverLifetime.Evaluate(progress); dc.transform.localPosition += progress * new Vector3(GlobalWind.x, GlobalWind.y, 0f); dc.transform.localPosition = new Vector3(dc.transform.localPosition.x, dc.transform.localPosition.y, 0f); dc.Drawer.ShaderType = this.ShaderType; dc.Drawer.Show(); }
IEnumerator Wait(float atLeast = 0.5f, float step = 0.1f, int rndMax = 20, Action callBack = null) { float elapsed = 0f; while (elapsed < atLeast) { elapsed += Time.deltaTime; yield return(null); } while (RainDropTools.Random(0, rndMax) != 0) { elapsed = 0f; while (elapsed < step) { elapsed += Time.deltaTime; yield return(null); } } if (callBack != null) { callBack(); } yield break; }
private void InitializeDrawer(FlowRainDrawerContainer dc) { dc.TimeElapsed = 0f; dc.lifetime = RainDropTools.Random(Variables.LifetimeMin, Variables.LifetimeMax); dc.fluctuationRate = RainDropTools.Random(Variables.fluctuationRateMin, Variables.fluctuationRateMax); dc.acceleration = RainDropTools.Random(Variables.AccelerationMin, Variables.AccelerationMax); dc.transform.localPosition = RainDropTools.GetSpawnLocalPos(this.transform, camera, 0f, Variables.SpawnOffsetY); dc.startPos = dc.transform.localPosition; dc.acceleration = RainDropTools.Random(Variables.AccelerationMin, Variables.AccelerationMax); Material mat = RainDropTools.CreateRainMaterial(ShaderType, RenderQueue); RainDropTools.ApplyRainMaterialValue( mat, ShaderType, Variables.NormalMap, Variables.OverlayTexture, Variables.DistortionValue, Variables.OverlayColor, Variables.ReliefValue, Variables.Blur, Variables.Darkness ); dc.Drawer.lifeTime = dc.lifetime; dc.Drawer.vertexDistance = 0.01f; dc.Drawer.angleDivisions = 20; dc.Drawer.material = mat; dc.Drawer.widthCurve = Variables.TrailWidth; dc.Drawer.widthMultiplier = RainDropTools.Random(Variables.SizeMinX, Variables.SizeMaxX); dc.Drawer.textureMode = LineTextureMode.Stretch; dc.Drawer.vertexDistance = (1f * this.Distance * RainDropTools.GetCameraOrthographicSize(this.camera).y) / (Variables.Resolution * 10f); dc.Drawer.Clear(); dc.Drawer.enabled = false; }
/// <summary> /// Get an element from a weighted KeyValuePair list /// </summary> /// <typeparam name="T1"></typeparam> /// <typeparam name="T2"></typeparam> /// <param name="list"></param> /// <returns></returns> public static KeyValuePair <T1, T2> GetWeightedElement <T1, T2>(List <KeyValuePair <T1, T2> > list) where T2 : IComparable { if (list.Count == 0) { return(list.FirstOrDefault()); } float totalweight = (float)list.Sum(t => Convert.ToDouble(t.Value)); float choice = RainDropTools.Random(0f, totalweight); float sum = 0; foreach (var obj in list) { for (float i = sum; i < Convert.ToDouble(obj.Value) + sum; i++) { if (i >= choice) { return(obj); } } sum += (float)Convert.ToDouble(obj.Value); } return(list.First()); }
/// <summary> /// Creates the controller. /// </summary> /// <returns>The controller.</returns> FrictionFlowRainController CreateController() { Transform tr = RainDropTools.CreateHiddenObject("Controller", this.transform); FrictionFlowRainController con = tr.gameObject.AddComponent <FrictionFlowRainController> (); con.Variables = Variables; con.Alpha = 0f; con.NoMoreRain = false; con.camera = GetComponentInParent <Camera> (); return(con); }
private void Shuffle <T>(List <T> list) { int cnt = list.Count; while (cnt > 1) { cnt--; int k = RainDropTools.Random(0, cnt + 1); T value = list[k]; list[k] = list[cnt]; list[cnt] = value; } }
private void UpdateTransform(FrictionFlowRainDrawerContainer dc) { float progress = GetProgress(dc); float t = dc.TimeElapsed; Vector3 nextPos = GetNextPositionWithFriction( dc: dc, downValue: ((1 / 2f) * t * t * dc.acceleration * 0.1f) + (Variables.InitialVelocity * t * 0.01f), resolution: 150, widthResolution: 8, dt: Time.deltaTime); nextPos = new Vector3(nextPos.x, nextPos.y, 0f); nextPos += progress * new Vector3(GlobalWind.x, GlobalWind.y, 0f); dc.Drawer.vertexDistance = (1f * this.Distance * RainDropTools.GetCameraOrthographicSize(this.camera).y) / (Variables.Resolution * 10f); dc.transform.localPosition = nextPos; }
private void InitializeDrawer(SimpleRainDrawerContainer dc) { dc.TimeElapsed = 0f; dc.lifetime = RainDropTools.Random(Variables.LifetimeMin, Variables.LifetimeMax); dc.transform.localPosition = RainDropTools.GetSpawnLocalPos(this.transform, camera, 0f, Variables.SpawnOffsetY); dc.startPos = dc.transform.localPosition; dc.startSize = new Vector3( RainDropTools.Random(Variables.SizeMinX, Variables.SizeMaxX), RainDropTools.Random(Variables.SizeMinY, Variables.SizeMaxY), 1f ); dc.transform.localEulerAngles += Vector3.forward * (Variables.AutoRotate ? UnityEngine.Random.Range(0f, 179.9f) : 0f); dc.Drawer.NormalMap = Variables.NormalMap; dc.Drawer.ReliefTexture = Variables.OverlayTexture; dc.Drawer.Darkness = Variables.Darkness; dc.Drawer.Hide(); }
private void UpdateTransform(FlowRainDrawerContainer dc) { Action initRnd = () => { dc.rnd1 = RainDropTools.Random(-0.1f * Variables.Amplitude, 0.1f * Variables.Amplitude); dc.posXDt = 0f; }; if (dc.posXDt == 0f) { StartCoroutine( Wait( 0.01f, 0.01f, (int)(1f / dc.fluctuationRate * 100), () => { initRnd(); } ) ); } dc.posXDt += 0.01f * Variables.Smooth * Time.deltaTime; if (dc.rnd1 == 0f) { initRnd(); } float t = dc.TimeElapsed; Vector3 downward = RainDropTools.GetGForcedScreenMovement(this.camera.transform, this.GForceVector); downward = -downward.normalized; Vector3 nextPos = new Vector3( Vector3.Slerp(dc.transform.localPosition, dc.transform.localPosition + downward * dc.rnd1, dc.posXDt).x, dc.startPos.y - downward.y * (1 / 2f) * t * t * dc.acceleration - Variables.InitialVelocity * t, 0.001f // TODO: Work around ); dc.transform.localPosition = nextPos; dc.transform.localPosition += GetProgress(dc) * new Vector3(GlobalWind.x, GlobalWind.y, 0f); }
private void CheckSpawnTime() { if (interval == 0f) { interval = Variables.Duration / RainDropTools.Random(Variables.EmissionRateMin, Variables.EmissionRateMax); } timeElapsed += Time.deltaTime; if (timeElapsed >= interval) { int spawnNum = (int)Mathf.Min((timeElapsed / interval), Variables.MaxRainSpawnCount - drawers.FindAll(x => x.currentState == DrawState.Playing).Count()); for (int i = 0; i < spawnNum; i++) { Spawn(); } interval = Variables.Duration / RainDropTools.Random(Variables.EmissionRateMin, Variables.EmissionRateMax); timeElapsed = 0f; } }
bool CheckExistence() { if (!_trail) { Transform oldTrail = transform.FindChild(_name); if (oldTrail) { _trail = oldTrail.gameObject; _meshFilter = _trail.GetComponent <MeshFilter>(); _meshRenderer = _trail.GetComponent <MeshRenderer>(); } else { _trail = RainDropTools.CreateHiddenObject(_name, this.transform).gameObject; } } if (!_meshFilter) { _meshFilter = _trail.AddComponent <MeshFilter>(); } if (!_meshRenderer) { _meshRenderer = _trail.AddComponent <MeshRenderer>(); } if (material == null) { return(false); } else { _meshRenderer.material = material; } return(true); }
public void Show() { if (changed) { DestroyImmediate(meshRenderer); DestroyImmediate(meshFilter); meshRenderer = null; meshFilter = null; material = null; mesh = null; changed = false; } if (NormalMap != null) { if (ShaderType == RainDropTools.RainDropShaderType.Cheap) { if (DistortionStrength == 0f) { Hide(); return; } } else { if (DistortionStrength == 0f && ReliefValue == 0f && OverlayColor.a == 0f && Blur == 0f) { Hide(); return; } } } else { Debug.LogError("Normal Map is null!"); Hide(); return; } if (material == null) { material = RainDropTools.CreateRainMaterial(ShaderType, RenderQueue); } if (meshFilter == null) { meshFilter = gameObject.AddComponent <MeshFilter>(); } if (meshRenderer == null) { meshRenderer = gameObject.AddComponent <MeshRenderer>(); } if (mesh == null) { mesh = RainDropTools.CreateQuadMesh(); } // Update shader if needed if (material.shader.name != RainDropTools.GetShaderName(ShaderType)) { material = RainDropTools.CreateRainMaterial(ShaderType, material.renderQueue); } if (material != null && mesh != null && meshFilter != null) { meshFilter.mesh = mesh; meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; meshRenderer.material = material; meshRenderer.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off; meshRenderer.enabled = true; RainDropTools.ApplyRainMaterialValue( material, ShaderType, NormalMap, ReliefTexture, DistortionStrength, OverlayColor, ReliefValue, Blur, Darkness ); } }
public RainDrawerContainer(string name, Transform parent) { transform = RainDropTools.CreateHiddenObject(name, parent); this.Drawer = transform.gameObject.AddComponent <T> (); }
/// <summary> /// Update rain variables /// </summary> /// <param name="dc">Dc.</param> private void UpdateInstance(StaticRainDrawerContainer dc) { AnimationCurve fadeCurve = Variables.FadeinCurve; // Update time if (!NoMoreRain) { dc.TimeElapsed = Mathf.Min(Variables.fadeTime, dc.TimeElapsed + Time.deltaTime); } else { dc.TimeElapsed = Mathf.Max(0f, dc.TimeElapsed - Time.deltaTime); } if (dc.TimeElapsed == 0f) { dc.Drawer.Hide(); dc.currentState = DrawState.Disabled; return; } else { dc.currentState = DrawState.Playing; } if (Variables.FullScreen) { Vector2 orthSize = RainDropTools.GetCameraOrthographicSize(this.camera); Vector3 targetScale = new Vector3( orthSize.x / 2f, orthSize.y / 2f, 0f ); if (VRMode) { targetScale += Vector3.one * 0.02f; } dc.transform.localScale = targetScale; dc.transform.localPosition = Vector3.zero; } else { dc.transform.localScale = new Vector3( Variables.SizeX, Variables.SizeY, 1f ); Vector3 p = camera.ScreenToWorldPoint( new Vector3( -Screen.width * Variables.SpawnOffsetX + Screen.width / 2, -Screen.height * Variables.SpawnOffsetY + Screen.height / 2, 0f )); dc.transform.localPosition = transform.InverseTransformPoint(p); dc.transform.localPosition -= Vector3.forward * dc.transform.localPosition.z; } float progress = GetProgress(dc); dc.Drawer.RenderQueue = RenderQueue; dc.Drawer.NormalMap = Variables.NormalMap; dc.Drawer.ReliefTexture = Variables.OverlayTexture; dc.Drawer.OverlayColor = new Color( Variables.OverlayColor.r, Variables.OverlayColor.g, Variables.OverlayColor.b, Variables.OverlayColor.a * fadeCurve.Evaluate(progress) * Alpha ); dc.Drawer.DistortionStrength = Variables.DistortionValue * fadeCurve.Evaluate(progress) * Alpha; dc.Drawer.ReliefValue = Variables.ReliefValue * fadeCurve.Evaluate(progress) * Alpha; dc.Drawer.Blur = Variables.Blur * fadeCurve.Evaluate(progress) * Alpha; dc.Drawer.Darkness = Variables.Darkness; dc.Drawer.ShaderType = ShaderType; dc.Drawer.Show(); }
private void UpdateShader(FlowRainDrawerContainer dc, int index) { float progress = GetProgress(dc); dc.Drawer.material.renderQueue = RenderQueue + index; // Update shader if needed if (dc.Drawer.material.shader.name != RainDropTools.GetShaderName(ShaderType)) { dc.Drawer.material = RainDropTools.CreateRainMaterial(ShaderType, RenderQueue + index); } float distortionValue = Variables.DistortionValue * Variables.DistortionOverLifetime.Evaluate(progress) * Alpha; float reliefValue = Variables.ReliefValue * Variables.ReliefOverLifetime.Evaluate(progress) * Alpha; float blurValue = Variables.Blur * Variables.BlurOverLifetime.Evaluate(progress) * Alpha; Color overlayColor = new Color( Variables.OverlayColor.r, Variables.OverlayColor.g, Variables.OverlayColor.b, Variables.OverlayColor.a * Variables.AlphaOverLifetime.Evaluate(progress) * Alpha ); switch (ShaderType) { case RainDropTools.RainDropShaderType.Expensive: if (distortionValue == 0f && reliefValue == 0f && overlayColor.a == 0f && blurValue == 0f) { dc.Drawer.enabled = false; return; } break; case RainDropTools.RainDropShaderType.Cheap: if (distortionValue == 0f) { dc.Drawer.enabled = false; return; } break; case RainDropTools.RainDropShaderType.NoDistortion: if (reliefValue == 0f && overlayColor.a == 0f) { dc.Drawer.enabled = false; return; } break; } RainDropTools.ApplyRainMaterialValue( dc.Drawer.material, ShaderType, Variables.NormalMap, Variables.OverlayTexture, distortionValue, overlayColor, reliefValue, blurValue, Variables.Darkness * Alpha ); dc.Drawer.enabled = true; }
private Vector3 GetNextPositionWithFriction(FrictionFlowRainDrawerContainer dc, float downValue, int resolution, int widthResolution, float dt) { dummy.parent = dc.Drawer.transform.parent; dummy.localRotation = dc.Drawer.transform.localRotation; dummy.localPosition = dc.Drawer.transform.localPosition; int texW = Variables.FrictionMap.width; int texH = Variables.FrictionMap.height; int iter = (int)(Mathf.Clamp(resolution * dt, 2, 5)); //Vector3 frictionWay = dc.Drawer.transform.localPosition; Dictionary <Vector3, float> widthPixels = new Dictionary <Vector3, float>(); // Get the gravity forced vector Vector3 downward = RainDropTools.GetGForcedScreenMovement(this.camera.transform, this.GForceVector); downward = downward.normalized; float angl = Mathf.Rad2Deg * Mathf.Atan2(downward.y, downward.x); //dummy.localPosition += downValue * new Vector3(downward.x, downward.y, 0f); dummy.localRotation = Quaternion.AngleAxis(angl + 90f, Vector3.forward); float step = downValue * (1f / iter) * 3f / widthResolution; int resol = Mathf.Clamp(2 * widthResolution, 2, 5); for (int i = 0; i < iter; i++) { dummy.localPosition += downValue * (1f / iter) * new Vector3(downward.x, downward.y, 0f); for (int j = 0; j <= resol; j++) { float ww = (j * step - (resol / 2f) * step); Vector3 downPos = dummy.TransformPoint(new Vector3(ww, 0f, 0f)); Vector3 downVector2viewPoint = this.camera.WorldToViewportPoint(downPos); // Get the pixel grayscale float pixel = Variables.FrictionMap.GetPixel( (int)(texW * downVector2viewPoint.x), (int)(texH * -downVector2viewPoint.y) ).grayscale; // If never added to the list, we add it if (!widthPixels.ContainsKey(downPos)) { widthPixels.Add(downPos, 1.0f - pixel); } } } Vector3 frictionWay = PickRandomWeightedElement(widthPixels).Key; frictionWay = dc.Drawer.transform.parent.InverseTransformPoint(frictionWay); dummy.parent = null; return(frictionWay); // OLD /*for (int i = 0; i < iter; i++) * { * float dv = downValue * ((float)i / iter); * widthPixels.Clear(); * * for (int j = 0; j <= 2*widthResolution; j++) * { * Vector3 downVector = frictionWay + (downward * dv); * * // TODO: Use normal vector of downward to search pixels * if (Mathf.Abs(downward.y) > Mathf.Abs(downward.x)) * downVector += Vector3.left * dv + Vector3.right * dv * ((float)j / widthResolution); * else * downVector += Vector3.up * dv + Vector3.down * dv * ((float)j / widthResolution); * // END OF todo * * Vector3 downVector2viewPoint = camera.WorldToViewportPoint(downVector); * float pixel = Variables.FrictionMap.GetPixel( * (int)(texW * downVector2viewPoint.x), * (int)(texH * -downVector2viewPoint.y) * ).grayscale; * if (!widthPixels.ContainsKey(downVector)) * widthPixels.Add(downVector, 1.0f-pixel); * } * * Vector3 keyVector = PickRandomWeightedElement(widthPixels).Key; * frictionWay = keyVector; * } * * return frictionWay;*/ }