public override void CalculateShadow(SgtLight light) { var direction = default(Vector3); var position = default(Vector3); var color = default(Color); SgtLight.Calculate(light, transform.position, null, null, ref position, ref direction, ref color); var dot = Vector3.Dot(direction, transform.up); var radiusXZ = (transform.lossyScale.x + transform.lossyScale.z) * 0.5f * RadiusMax; var radiusY = transform.lossyScale.y * RadiusMax; var radius = GetRadius(radiusY, radiusXZ, dot * Mathf.PI * 0.5f); var rotation = Quaternion.FromToRotation(direction, Vector3.back); var vector = rotation * transform.up; var spin = Quaternion.LookRotation(Vector3.forward, new Vector2(-vector.x, vector.y)); // Orient the shadow ellipse var scale = SgtHelper.Reciprocal3(new Vector3(radiusXZ, radius, 1.0f)); var shadowT = Matrix4x4.Translate(-transform.position); var shadowR = Matrix4x4.Rotate(spin * rotation); var shadowS = Matrix4x4.Scale(scale); cachedActive = true; cachedMatrix = shadowS * shadowR * shadowT; cachedRatio = SgtHelper.Divide(RadiusMax, RadiusMax - RadiusMin); cachedRadius = SgtHelper.UniformScale(transform.lossyScale) * RadiusMax; cachedTexture = generatedTexture; }
public override bool CalculateShadow(ref Matrix4x4 matrix, ref float ratio) { var light = default(SgtLight); if (SgtLight.Find(ref light) == true) { var direction = default(Vector3); var position = default(Vector3); var color = default(Color); SgtLight.Calculate(light.CachedLight, transform.position, null, null, ref position, ref direction, ref color); var dot = Vector3.Dot(direction, transform.up); var radiusXZ = (transform.lossyScale.x + transform.lossyScale.z) * 0.5f * RadiusMax; var radiusY = transform.lossyScale.y * RadiusMax; var radius = GetRadius(radiusY, radiusXZ, dot * Mathf.PI * 0.5f); var rotation = Quaternion.FromToRotation(direction, Vector3.back); var vector = rotation * transform.up; var spin = Quaternion.LookRotation(Vector3.forward, new Vector2(-vector.x, vector.y)); // Orient the shadow ellipse var scale = SgtHelper.Reciprocal3(new Vector3(radiusXZ, radius, 1.0f)); var shadowT = SgtHelper.Translation(-transform.position); var shadowR = SgtHelper.Rotation(spin * rotation); var shadowS = SgtHelper.Scaling(scale); matrix = shadowS * shadowR * shadowT; ratio = SgtHelper.Divide(RadiusMax, RadiusMax - RadiusMin); return(true); } return(false); }
public override bool CalculateShadow(ref Matrix4x4 matrix, ref float ratio) { var light = default(SgtLight); if (Texture != null && SgtLight.Find(ref light) == true) { var direction = default(Vector3); var position = default(Vector3); var color = default(Color); SgtLight.Calculate(light.CachedLight, transform.position, null, null, ref position, ref direction, ref color); var rotation = Quaternion.FromToRotation(direction, Vector3.back); var squash = Vector3.Dot(direction, transform.up); // Find how squashed the ellipse is based on light direction var width = transform.lossyScale.x * RadiusMax; var length = transform.lossyScale.z * RadiusMax; var axis = rotation * transform.up; // Find the transformed up axis var spin = Quaternion.LookRotation(Vector3.forward, new Vector2(-axis.x, axis.y)); // Orient the shadow ellipse var scale = SgtHelper.Reciprocal3(new Vector3(width, length * Mathf.Abs(squash), 1.0f)); var skew = Mathf.Tan(SgtHelper.Acos(-squash)); var shadowT = SgtHelper.Translation(-transform.position); var shadowR = SgtHelper.Rotation(spin * rotation); // Spin the shadow so lines up with its tilt var shadowS = SgtHelper.Scaling(scale); // Scale the ring into an oval var shadowK = SgtHelper.ShearingZ(new Vector2(0.0f, skew)); // Skew the shadow so it aligns with the ring plane matrix = shadowS * shadowK * shadowR * shadowT; ratio = SgtHelper.Divide(RadiusMax, RadiusMax - RadiusMin); return(true); } return(false); }
private void HandleCameraDraw(Camera camera) { if (SgtHelper.CanDraw(gameObject, camera) == false) { return; } if (depthTex != null) { #if UNITY_EDITOR SgtHelper.MakeTextureReadable(depthTex); #endif material.SetFloat(SgtShader._Sky, GetSky(camera.transform.position)); } var scale = SgtHelper.Divide(radius, meshRadius); var matrix = transform.localToWorldMatrix * Matrix4x4.Scale(Vector3.one * scale); if (cameraOffset != 0.0f) { var direction = Vector3.Normalize(camera.transform.position - transform.position); matrix = Matrix4x4.Translate(direction * cameraOffset) * matrix; } Graphics.DrawMesh(mesh, matrix, material, gameObject.layer, camera); }
private void CalculateOcclusion(int layers, Vector4 worldEye, Vector4 worldTgt, ref float occlusion) { if (SgtOcclusion.IsValid(occlusion, layers, gameObject) == true && OuterRadius > 0.0f && outerDepthTex != null) { var eye = cachedTransform.InverseTransformPoint(worldEye) / OuterRadius; var tgt = cachedTransform.InverseTransformPoint(worldTgt) / OuterRadius; var dir = Vector3.Normalize(tgt - eye); var len = Vector3.Magnitude(tgt - eye); var length = default(float); if (GetLength(eye, dir, len, ref length) == true) { var localPosition = cachedTransform.InverseTransformPoint(worldEye); var localDistance = localPosition.magnitude; var innerThickness = default(float); var outerThickness = default(float); var innerRatio = SgtHelper.Divide(innerMeshRadius, OuterRadius); var middleRatio = Mathf.Lerp(innerRatio, 1.0f, middle); var distance = SgtHelper.Divide(localDistance, OuterRadius); var outerDensity = 1.0f - outerFog; SgtHelper.CalculateHorizonThickness(innerRatio, middleRatio, distance, out innerThickness, out outerThickness); length *= SgtHelper.Reciprocal(outerThickness * outerDensity); var depth = outerDepthTex.GetPixelBilinear(length, length).a; depth = Mathf.Clamp01(depth + (1.0f - depth) * GetSky(worldEye, localDistance)); occlusion += (1.0f - occlusion) * depth; } } }
public override void CalculateShadow(SgtLight light) { if (Texture != null) { var direction = default(Vector3); var position = default(Vector3); var color = default(Color); SgtLight.Calculate(light, transform.position, null, null, ref position, ref direction, ref color); var rotation = Quaternion.FromToRotation(direction, Vector3.back); var squash = Vector3.Dot(direction, transform.up); // Find how squashed the ellipse is based on light direction var width = transform.lossyScale.x * RadiusMax; var length = transform.lossyScale.z * RadiusMax; var axis = rotation * transform.up; // Find the transformed up axis var spin = Quaternion.LookRotation(Vector3.forward, new Vector2(-axis.x, axis.y)); // Orient the shadow ellipse var scale = SgtHelper.Reciprocal3(new Vector3(width, length * Mathf.Abs(squash), 1.0f)); var skew = Mathf.Tan(SgtHelper.Acos(-squash)); var shadowT = Matrix4x4.Translate(-transform.position); var shadowR = Matrix4x4.Rotate(spin * rotation); // Spin the shadow so lines up with its tilt var shadowS = Matrix4x4.Scale(scale); // Scale the ring into an oval var shadowK = SgtHelper.ShearingZ(new Vector2(0.0f, skew)); // Skew the shadow so it aligns with the ring plane cachedActive = true; cachedMatrix = shadowS * shadowK * shadowR * shadowT; cachedRatio = SgtHelper.Divide(RadiusMax, RadiusMax - RadiusMin); cachedRadius = SgtHelper.UniformScale(transform.lossyScale) * RadiusMax; cachedTexture = Texture; } else { cachedActive = false; } }
public void UpdateModels() { var angleStep = SgtHelper.Divide(360.0f, Segments); for (var i = 0; i < Segments; i++) { var model = GetOrAddModel(i); var angle = angleStep * i; var rotation = Quaternion.Euler(0.0f, angle, 0.0f); model.SetMesh(Mesh); model.SetMaterial(material); model.SetRotation(rotation); } // Remove any excess if (models != null) { var min = Mathf.Max(0, Segments); for (var i = models.Count - 1; i >= min; i--) { SgtRingModel.Pool(models[i]); models.RemoveAt(i); } } }
private float GetSky(Vector3 eye) { var localCameraPosition = transform.InverseTransformPoint(eye); var localDistance = localCameraPosition.magnitude; var scaleDistance = SgtHelper.Divide(localDistance, radius); return(sky * depthTex.GetPixelBilinear(1.0f - scaleDistance, 0.0f).a); }
private void UpdateMaterialNonSerialized() { var scale = SgtHelper.Divide(OuterMeshRadius, OuterRadius); var worldToLocal = SgtHelper.Scaling(scale) * transform.worldToLocalMatrix; innerMaterial.SetMatrix("_WorldToLocal", worldToLocal); outerMaterial.SetMatrix("_WorldToLocal", worldToLocal); }
private void UpdateMaterialNonSerialized() { var scale = SgtHelper.Divide(OuterMeshRadius, OuterRadius); var worldToLocal = Matrix4x4.Scale(new Vector3(scale, scale, scale)) * cachedTransform.worldToLocalMatrix; innerMaterial.SetMatrix(SgtShader._WorldToLocal, worldToLocal); outerMaterial.SetMatrix(SgtShader._WorldToLocal, worldToLocal); }
public static void CalculateAtmosphereThicknessAtHorizon(float groundRadius, float skyRadius, float distance, out float groundThickness, out float skyThickness) { var horizonDistance = DistanceToHorizon(groundRadius, distance); var maxSkyThickness = Mathf.Sin(Mathf.Acos(SgtHelper.Divide(groundRadius, skyRadius))) * skyRadius; var maxGroundThickness = Mathf.Min(horizonDistance, maxSkyThickness); groundThickness = Mathf.Max(maxGroundThickness, skyRadius - groundRadius); skyThickness = maxSkyThickness + maxGroundThickness; }
private void CameraPreRender(Camera camera) { if (model != null) { model.Restore(camera); } // Write camera-dependant shader values if (innerMaterial != null && outerMaterial != null) { var localPosition = cachedTransform.InverseTransformPoint(camera.transform.position); var localDistance = localPosition.magnitude; var height01 = Mathf.InverseLerp(OuterRadius, InnerMeshRadius, localDistance); var innerThickness = default(float); var outerThickness = default(float); var innerRatio = SgtHelper.Divide(InnerMeshRadius, OuterRadius); var middleRatio = Mathf.Lerp(innerRatio, 1.0f, Middle); var distance = SgtHelper.Divide(localDistance, OuterRadius); var innerDensity = 1.0f - InnerFog; var outerDensity = 1.0f - OuterFog; SgtHelper.CalculateHorizonThickness(innerRatio, middleRatio, distance, out innerThickness, out outerThickness); innerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(innerThickness * innerDensity)); outerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(outerThickness * outerDensity)); if (OuterDepthTex != null) { #if UNITY_EDITOR SgtHelper.MakeTextureReadable(OuterDepthTex); #endif outerMaterial.SetFloat(SgtShader._Sky, GetSky(camera) * OuterDepthTex.GetPixelBilinear(height01 / outerDensity, 0.0f).a); } var scale = SgtHelper.Divide(OuterMeshRadius, OuterRadius); var worldToLocal = Matrix4x4.Scale(new Vector3(scale, scale, scale)) * cachedTransform.worldToLocalMatrix; // cachedTransform might not be set here, so use the property innerMaterial.SetMatrix(SgtShader._WorldToLocal, worldToLocal); outerMaterial.SetMatrix(SgtShader._WorldToLocal, worldToLocal); // Write lights and shadows SgtHelper.SetTempMaterial(innerMaterial, outerMaterial); var mask = 1 << gameObject.layer; var lights = SgtLight.Find(Lit, mask); SgtShadow.Find(Lit, mask, lights); SgtShadow.FilterOutSphere(transform.position); SgtShadow.Write(Lit, 2); SgtLight.FilterOut(transform.position); SgtLight.Write(Lit, transform.position, cachedTransform, null, ScatteringStrength, 2); } }
public void UpdateModel() { if (model == null) { model = SgtCloudsphereModel.Create(this); } var scale = SgtHelper.Divide(Radius, MeshRadius); model.SetMesh(Mesh); model.SetMaterial(material); model.SetScale(scale); }
public void UpdateModel() { if (model == null) { model = SgtAtmosphereModel.Create(this); } var scale = SgtHelper.Divide(OuterRadius, OuterMeshRadius); model.SetMesh(OuterMesh); model.SetMaterial(outerMaterial); model.SetScale(scale); }
private void HandleCameraDraw(Camera camera) { if (SgtHelper.CanDraw(gameObject, camera) == false) { return; } // Write camera-dependent shader values if (innerMaterial != null && outerMaterial != null) { var localPosition = CachedTransform.InverseTransformPoint(camera.transform.position); var localDistance = localPosition.magnitude; var innerThickness = default(float); var outerThickness = default(float); var innerRatio = SgtHelper.Divide(innerMeshRadius, OuterRadius); var middleRatio = Mathf.Lerp(innerRatio, 1.0f, middle); var distance = SgtHelper.Divide(localDistance, OuterRadius); var innerDensity = 1.0f - innerFog; var outerDensity = 1.0f - outerFog; SgtHelper.CalculateHorizonThickness(innerRatio, middleRatio, distance, out innerThickness, out outerThickness); innerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(innerThickness * innerDensity)); outerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(outerThickness * outerDensity)); if (outerDepthTex != null) { #if UNITY_EDITOR SgtHelper.MakeTextureReadable(outerDepthTex); #endif outerMaterial.SetFloat(SgtShader._Sky, GetSky(localDistance)); } UpdateMaterialNonSerialized(); } var scale = SgtHelper.Divide(OuterRadius, outerMeshRadius); var matrix = CachedTransform.localToWorldMatrix * Matrix4x4.Scale(Vector3.one * scale); if (cameraOffset != 0.0f) { var direction = Vector3.Normalize(camera.transform.position - cachedTransform.position); matrix = Matrix4x4.Translate(direction * cameraOffset) * matrix; } Graphics.DrawMesh(outerMesh, matrix, outerMaterial, gameObject.layer, camera); }
private void CameraPreRender(Camera camera) { if (model != null) { model.Restore(camera); } // Write camera-dependant shader values if (innerMaterial != null && outerMaterial != null) { var cameraPosition = camera.transform.position; var localCameraPosition = transform.InverseTransformPoint(cameraPosition); var localDistance = localCameraPosition.magnitude; var clampedSky = Mathf.InverseLerp(OuterRadius, InnerMeshRadius, localDistance); var innerAtmosphereDepth = default(float); var outerAtmosphereDepth = default(float); var radiusRatio = SgtHelper.Divide(InnerMeshRadius, OuterRadius); var scaleDistance = SgtHelper.Divide(localDistance, OuterRadius); var innerDensity = 1.0f - InnerFog; var outerDensity = 1.0f - OuterFog; SgtHelper.CalculateAtmosphereThicknessAtHorizon(radiusRatio, 1.0f, scaleDistance, out innerAtmosphereDepth, out outerAtmosphereDepth); SgtHelper.SetTempMaterial(innerMaterial, outerMaterial); if (scaleDistance > 1.0f) { SgtHelper.EnableKeyword("SGT_A"); // Outside } else { SgtHelper.DisableKeyword("SGT_A"); // Outside } innerMaterial.SetFloat("_HorizonLengthRecip", SgtHelper.Reciprocal(innerAtmosphereDepth * innerDensity)); outerMaterial.SetFloat("_HorizonLengthRecip", SgtHelper.Reciprocal(outerAtmosphereDepth * outerDensity)); if (OuterDepthTex != null) { #if UNITY_EDITOR SgtHelper.MakeTextureReadable(OuterDepthTex); #endif outerMaterial.SetFloat("_Sky", GetSky(camera) * OuterDepthTex.GetPixelBilinear(clampedSky / outerDensity, 0.0f).a); } UpdateMaterialNonSerialized(); } }
private void CameraPreRender(Camera camera) { if (material != null) { var cameraPosition = camera.transform.position; var localCameraPosition = transform.InverseTransformPoint(cameraPosition); var localDistance = localCameraPosition.magnitude; var scaleDistance = SgtHelper.Divide(localDistance, Radius); if (scaleDistance > 1.0f) { SgtHelper.EnableKeyword("SGT_A", material); // Outside } else { SgtHelper.DisableKeyword("SGT_A", material); // Outside if (DepthTex != null) { #if UNITY_EDITOR SgtHelper.MakeTextureReadable(DepthTex); #endif material.SetFloat("_Sky", Sky * DepthTex.GetPixelBilinear(1.0f - scaleDistance, 0.0f).a); } } // Write these once to save CPU if (renderedThisFrame == false) { renderedThisFrame = true; // Write lights and shadows SgtHelper.SetTempMaterial(material); SgtLight.Write(Lit, transform.position, transform, null, ScatteringStrength, 2); SgtShadow.Write(Lit, gameObject, 2); // Write matrices var localToWorld = transform.localToWorldMatrix * SgtHelper.Scaling(Radius * 2.0f); // Double mesh radius so the max thickness caps at 1.0 material.SetMatrix("_WorldToLocal", localToWorld.inverse); material.SetMatrix("_LocalToWorld", localToWorld); } } }
public static Vector4 CalculateSpriteUV(Sprite s) { var uv = default(Vector4); if (s != null) { var r = s.textureRect; var t = s.texture; uv.x = SgtHelper.Divide(r.xMin, t.width); uv.y = SgtHelper.Divide(r.yMin, t.height); uv.z = SgtHelper.Divide(r.xMax, t.width); uv.w = SgtHelper.Divide(r.yMax, t.height); } return(uv); }
public void UpdateMaterials() { if (innerMaterial == null) { innerMaterial = SgtHelper.CreateTempMaterial("Corona Inner (Generated)", SgtHelper.ShaderNamePrefix + "CoronaInner"); CachedSharedMaterial.Material = innerMaterial; } if (outerMaterial == null) { outerMaterial = SgtHelper.CreateTempMaterial("Corona Outer (Generated)", SgtHelper.ShaderNamePrefix + "CoronaOuter"); if (model != null) { model.SetMaterial(outerMaterial); } } var color = SgtHelper.Brighten(Color, Brightness); var innerRatio = SgtHelper.Divide(InnerMeshRadius, OuterRadius); innerMaterial.renderQueue = outerMaterial.renderQueue = RenderQueue; innerMaterial.SetColor(SgtShader._Color, color); outerMaterial.SetColor(SgtShader._Color, color); innerMaterial.SetTexture(SgtShader._DepthTex, InnerDepthTex); outerMaterial.SetTexture(SgtShader._DepthTex, OuterDepthTex); innerMaterial.SetFloat(SgtShader._InnerRatio, innerRatio); innerMaterial.SetFloat(SgtShader._InnerScale, 1.0f / (1.0f - innerRatio)); if (OuterSoftness > 0.0f) { SgtHelper.EnableKeyword("SGT_A", outerMaterial); // Softness outerMaterial.SetFloat(SgtShader._SoftParticlesFactor, SgtHelper.Reciprocal(OuterSoftness)); } else { SgtHelper.DisableKeyword("SGT_A", outerMaterial); // Softness } UpdateMaterialNonSerialized(); }
private void CameraPreRender(Camera camera) { if (material != null) { var cameraPosition = camera.transform.position; var localCameraPosition = transform.InverseTransformPoint(cameraPosition); var localDistance = localCameraPosition.magnitude; var scaleDistance = SgtHelper.Divide(localDistance, Radius); if (DepthTex != null) { #if UNITY_EDITOR SgtHelper.MakeTextureReadable(DepthTex); #endif material.SetFloat(SgtShader._Sky, Sky * DepthTex.GetPixelBilinear(1.0f - scaleDistance, 0.0f).a); } // Write these once to save CPU if (renderedThisFrame == false) { renderedThisFrame = true; // Write lights and shadows SgtHelper.SetTempMaterial(material); var mask = 1 << gameObject.layer; var lights = SgtLight.Find(Lit, mask, transform.position); SgtLight.FilterOut(transform.position); SgtShadow.Find(Lit, mask, lights); SgtShadow.FilterOutSphere(transform.position); SgtShadow.Write(Lit, 2); SgtLight.Write(Lit, transform.position, transform, null, ScatteringStrength, 2); // Write matrices var scale = Radius; var localToWorld = transform.localToWorldMatrix * Matrix4x4.Scale(new Vector3(scale, scale, scale)); // Double mesh radius so the max thickness caps at 1.0 material.SetMatrix(SgtShader._WorldToLocal, localToWorld.inverse); material.SetMatrix(SgtShader._LocalToWorld, localToWorld); } } }
protected virtual void OnDrawGizmosSelected() { var mask = 1 << gameObject.layer; var lights = SgtLight.Find(true, mask); if (SgtHelper.Enabled(this) == true && lights.Count > 0) { CalculateShadow(lights[0]); if (cachedActive == true) { Gizmos.matrix = cachedMatrix.inverse; var distA = 0.0f; var distB = 1.0f; var scale = 1.0f * Mathf.Deg2Rad; var inner = SgtHelper.Divide(RadiusMin, RadiusMax); for (var i = 1; i < 10; i++) { var posA = new Vector3(0.0f, 0.0f, distA); var posB = new Vector3(0.0f, 0.0f, distB); Gizmos.color = new Color(1.0f, 1.0f, 1.0f, Mathf.Pow(0.75f, i) * 0.125f); for (var a = 1; a <= 360; a++) { posA.x = posB.x = Mathf.Sin(a * scale); posA.y = posB.y = Mathf.Cos(a * scale); Gizmos.DrawLine(posA, posB); posA.x = posB.x = posA.x * inner; posA.y = posB.y = posA.y * inner; Gizmos.DrawLine(posA, posB); } distA = distB; distB = distB * 2.0f; } } } }
private void HandleCameraDraw(Camera camera) { if (SgtHelper.CanDraw(gameObject, camera) == false) { return; } var scale = SgtHelper.Divide(radius, meshRadius); var matrix = cachedTransform.localToWorldMatrix * Matrix4x4.Scale(Vector3.one * scale); if (cameraOffset != 0.0f) { var direction = Vector3.Normalize(camera.transform.position - cachedTransform.position); matrix = Matrix4x4.Translate(direction * cameraOffset) * matrix; } Graphics.DrawMesh(mesh, matrix, generatedMaterial, gameObject.layer, camera); }
protected virtual void Update() { if (Point != null && Title != null) { var currentPosition = Point.Position; if (expectedPositionSet == false) { expectedPositionSet = true; expectedPosition = currentPosition; } var delta = SgtHelper.Divide(SgtPosition.Distance(ref expectedPosition, ref currentPosition), Time.deltaTime); Title.text = "Speed = " + System.Math.Round(delta).ToString("0") + "m/s"; expectedPosition = currentPosition; } }
protected virtual void OnDrawGizmosSelected() { if (SgtHelper.Enabled(this) == true) { var matrix = default(Matrix4x4); var ratio = default(float); if (CalculateShadow(ref matrix, ref ratio) == true) { Gizmos.matrix = matrix.inverse; var distA = 0.0f; var distB = 1.0f; var scale = 1.0f * Mathf.Deg2Rad; var inner = SgtHelper.Divide(RadiusMin, RadiusMax); for (var i = 1; i < 10; i++) { var posA = new Vector3(0.0f, 0.0f, distA); var posB = new Vector3(0.0f, 0.0f, distB); Gizmos.color = new Color(1.0f, 1.0f, 1.0f, Mathf.Pow(0.75f, i) * 0.125f); for (var a = 1; a <= 360; a++) { posA.x = posB.x = Mathf.Sin(a * scale); posA.y = posB.y = Mathf.Cos(a * scale); Gizmos.DrawLine(posA, posB); posA.x = posB.x = posA.x * inner; posA.y = posB.y = posA.y * inner; Gizmos.DrawLine(posA, posB); } distA = distB; distB = distB * 2.0f; } } } }
protected virtual void LateUpdate() { var scale = SgtHelper.Divide(outerMeshRadius, OuterRadius); var worldToLocal = Matrix4x4.Scale(new Vector3(scale, scale, scale)) * cachedTransform.worldToLocalMatrix; innerMaterial.SetMatrix(SgtShader._WorldToLocal, worldToLocal); outerMaterial.SetMatrix(SgtShader._WorldToLocal, worldToLocal); // Write lights and shadows SgtHelper.SetTempMaterial(innerMaterial, outerMaterial); var mask = 1 << gameObject.layer; var lights = SgtLight.Find(lit, mask, cachedTransform.position); SgtShadow.Find(lit, mask, lights); SgtShadow.FilterOutSphere(cachedTransform.position); SgtShadow.Write(lit, 2); SgtLight.FilterOut(cachedTransform.position); SgtLight.Write(lit, cachedTransform.position, cachedTransform, null, scatteringStrength, 2); }
protected virtual void Update() { if (LightningSpawner == null) { Pool(this); } else { if (Application.isPlaying == true) { Age += Time.deltaTime; } if (Age >= Life) { SgtComponentPool <SgtLightning> .Add(this); } else if (material != null) { material.SetFloat(SgtShader._Age, SgtHelper.Divide(Age, Life)); } } }
private void CameraPreRender(Camera camera) { if (model != null) { model.Restore(camera); } // Write camera-dependant shader values if (innerMaterial != null && outerMaterial != null) { var localPosition = cachedTransform.InverseTransformPoint(camera.transform.position); var localDistance = localPosition.magnitude; var height01 = Mathf.InverseLerp(OuterRadius, InnerMeshRadius, localDistance); var innerThickness = default(float); var outerThickness = default(float); var innerRatio = SgtHelper.Divide(InnerMeshRadius, OuterRadius); var middleRatio = Mathf.Lerp(innerRatio, 1.0f, Middle); var distance = SgtHelper.Divide(localDistance, OuterRadius); var innerDensity = 1.0f - InnerFog; var outerDensity = 1.0f - OuterFog; SgtHelper.CalculateHorizonThickness(innerRatio, middleRatio, distance, out innerThickness, out outerThickness); innerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(innerThickness * innerDensity)); outerMaterial.SetFloat(SgtShader._HorizonLengthRecip, SgtHelper.Reciprocal(outerThickness * outerDensity)); if (OuterDepthTex != null) { #if UNITY_EDITOR SgtHelper.MakeTextureReadable(OuterDepthTex); #endif outerMaterial.SetFloat(SgtShader._Sky, Sky * OuterDepthTex.GetPixelBilinear(height01 / outerDensity, 0.0f).a); } UpdateMaterialNonSerialized(); } }
private void TryUpdate() { if (point != null) { var currentPosition = point.Position; if (expectedPositionSet == false) { expectedPosition = currentPosition; expectedPositionSet = true; } var distance = SgtPosition.Distance(ref expectedPosition, ref currentPosition); var delta = SgtHelper.Divide(distance, Time.deltaTime); var text = string.Format(format, System.Math.Round(delta)); if (onString != null) { onString.Invoke(text); } expectedPosition = currentPosition; } }
public void UpdateMesh() { if (mesh == null) { mesh = SgtHelper.CreateTempMesh("Plane"); if (models != null) { for (var i = models.Count - 1; i >= 0; i--) { var model = models[i]; if (model != null) { model.SetMesh(mesh); } } } } else { mesh.Clear(false); } if (PlaneDetail >= 2) { var detail = Mathf.Min(PlaneDetail, SgtHelper.QuadsPerMesh / 2); // Limit the amount of vertices that get made var positions = new Vector3[detail * 2 + 2]; var normals = new Vector3[detail * 2 + 2]; var coords1 = new Vector2[detail * 2 + 2]; var coords2 = new Vector2[detail * 2 + 2]; var indices = new int[detail * 6]; var angleStep = SgtHelper.Divide(Mathf.PI * 2.0f, detail); var coordStep = SgtHelper.Reciprocal(detail); for (var i = 0; i <= detail; i++) { var coord = coordStep * i; var angle = angleStep * i; var sin = Mathf.Sin(angle); var cos = Mathf.Cos(angle); var offV = i * 2; positions[offV + 0] = new Vector3(sin * RadiusMin, 0.0f, cos * RadiusMin); positions[offV + 1] = new Vector3(sin * RadiusMax, 0.0f, cos * RadiusMax); normals[offV + 0] = Vector3.up; normals[offV + 1] = Vector3.up; coords1[offV + 0] = new Vector2(0.0f, coord * RadiusMin); coords1[offV + 1] = new Vector2(1.0f, coord * RadiusMax); coords2[offV + 0] = new Vector2(RadiusMin, 0.0f); coords2[offV + 1] = new Vector2(RadiusMax, 0.0f); } for (var i = 0; i < detail; i++) { var offV = i * 2; var offI = i * 6; indices[offI + 0] = offV + 0; indices[offI + 1] = offV + 1; indices[offI + 2] = offV + 2; indices[offI + 3] = offV + 3; indices[offI + 4] = offV + 2; indices[offI + 5] = offV + 1; } mesh.vertices = positions; mesh.normals = normals; mesh.uv = coords1; mesh.uv2 = coords2; mesh.triangles = indices; } }
protected virtual void Update() { if (Target == null) { ClearDebris(); return; } var followerPosition = Target.position; var followerDensity = GetDensity(followerPosition); if (followerDensity > 0.0f) { var debrisCount = Debris != null ? Debris.Count : 0; if (debrisCount < SpawnLimit) { spawnCooldown -= Time.deltaTime; while (spawnCooldown <= 0.0f) { spawnCooldown += Random.Range(SpawnRateMin, SpawnRateMax); SpawnDebris(false); debrisCount += 1; if (debrisCount >= SpawnLimit) { break; } } } } if (Debris != null) { var distanceRange = HideDistance - ShowDistance; for (var i = Debris.Count - 1; i >= 0; i--) { var debris = Debris[i]; if (debris != null) { var targetScale = default(float); var distance = Vector3.Distance(followerPosition, debris.transform.position); // Fade its size in debris.Show = SgtHelper.Dampen(debris.Show, 1.0f, ShowSpeed, Time.deltaTime, 0.1f); if (distance < ShowDistance) { targetScale = 1.0f; } else if (distance > HideDistance) { targetScale = 0.0f; } else { targetScale = 1.0f - SgtHelper.Divide(distance - ShowDistance, distanceRange); } debris.transform.localScale = debris.Scale * debris.Show * Mathf.Max(minScale, targetScale); if (targetScale <= 0.0f) { Despawn(debris, i); } } else { Debris.RemoveAt(i); } } } }