void UpdateMoonData(List <MaterialProperty> allProps) { MaterialProperty heightProp = GetPropertyWithName(allProps, "_MoonHeight"); if (heightProp == null) { Debug.LogError("Missing moon height property"); return; } MaterialProperty angleProp = GetPropertyWithName(allProps, "_MoonAngle"); if (angleProp == null) { Debug.LogError("Missing moon angle property"); return; } // Convert our percent properties to spherical point. float moonYPosition = SphereUtility.PercentToHeight(heightProp.floatValue); float moonAngle = SphereUtility.PercentToRadAngle(angleProp.floatValue); Vector3 moonPoint = SphereUtility.SphericalToPoint(moonYPosition, moonAngle); float xRotation = 0; float yRotation = 0; SphereUtility.CalculateStarRotation(moonPoint, out xRotation, out yRotation); MaterialProperty moonPositionProp = GetPropertyWithName(allProps, "_MoonComputedPositionData"); if (moonPositionProp == null) { Debug.LogError("Can't update precomputed moon position without property"); return; } moonPositionProp.vectorValue = new Vector4(moonPoint.x, moonPoint.y, moonPoint.z, 0); MaterialProperty moonRotationProp = GetPropertyWithName(allProps, "_MoonComputedRotationData"); if (moonRotationProp == null) { Debug.LogError("Can't update precomputed moon rotation without property"); return; } moonRotationProp.vectorValue = new Vector4(xRotation, yRotation, 0, 0); }
public void UpdateMoonData() { // Convert our percent properties to spherical point. float moonYPosition = SphereUtility.PercentToHeight(verticalPosition); float moonAngle = SphereUtility.PercentToRadAngle(horizontalPosition); Vector3 moonPoint = SphereUtility.SphericalToPoint(moonYPosition, moonAngle); float xRotation = 0; float yRotation = 0; SphereUtility.CalculateStarRotation(moonPoint, out xRotation, out yRotation); Vector4 moonPositionData = new Vector4(moonPoint.x, moonPoint.y, moonPoint.z, 0); Vector4 moonRotationData = new Vector4(xRotation, yRotation, 0, 0); moonMaterial.SetVector("_MoonComputedPositionData", moonPositionData); moonMaterial.SetVector("_MoonComputedRotationData", moonRotationData); }
public override IEnumerator ComputeStarData() { SendProgress(0); // Create 1 large texture, then render smaller tiles into it. Texture2D tex = new Texture2D((int)imageSize, (int)imageSize, TextureFormat.RGBAFloat, false); // Random points on sphere surface to position stars. int tileSize = (int)imageSize / 2; List <StarPoint> starPoints = GenerateRandomStarsPoints(density, tileSize, tileSize); Vector2 origin = new Vector2(0, tileSize); SendProgress(0); Vector2 rotationOrigin = new Vector2(tileSize, tileSize); // Fill in the image. for (int yIndex = 0; yIndex < tileSize; yIndex++) { float yPercent = (float)yIndex / (float)(tileSize - 1); float yPosition = SphereUtility.PercentToHeight(yPercent); for (int xIndex = 0; xIndex < tileSize; xIndex++) { float anglePercent = (float)xIndex / (float)(tileSize - 1); float angle = SphereUtility.PercentToRadAngle(anglePercent); Vector3 currentSpot = SphereUtility.SphericalToPoint(yPosition, angle); // Closest star to current spot. StarPoint star = NearestStarPoint(currentSpot, starPoints); UnityEngine.Color c = new UnityEngine.Color( star.position.x, star.position.y, star.position.z, star.noise); // Noise value used to randomize each star. tex.SetPixel((int)origin.x + xIndex, (int)origin.y + yIndex, c); // Calculate the stars rotation. float xRotation; float yRotation; SphereUtility.CalculateStarRotation(star.position, out xRotation, out yRotation); UnityEngine.Color r = new UnityEngine.Color( xRotation, yRotation, 0, 1); tex.SetPixel((int)rotationOrigin.x + xIndex, (int)rotationOrigin.y + yIndex, r); } // Update the GUI progress bar. float totalProgress = (float)((yIndex + 1) * tileSize) / (float)(tileSize * tileSize); SendProgress(totalProgress); yield return(null); } tex.Apply(false); SendCompletion(tex, true); yield break; }