private void UpdateShader() { var uniformScale = UniformScale; var centrePosition = transform.position; var observerPosition = SGT_Helper.GetPosition(starObserver); var distance = AtmosphereHeightAtPoint(observerPosition); var altitude01 = SGT_Helper.Remap(0.0f, 1.0f, distance, atmosphereSkyAltitude, 1.0f); var invScale = SGT_MatrixHelper.Scaling(SGT_Helper.Reciprocal(new Vector3(1.0f, 1.0f - surfaceOblateness, 1.0f))); var rot = SGT_MatrixHelper.Rotation(transform.rotation); var invRot = SGT_MatrixHelper.Rotation(Quaternion.Inverse(transform.rotation)); var ellipsoid2sphere = rot * invScale * invRot; var rel = ellipsoid2sphere.MultiplyPoint(observerPosition - centrePosition); var dist = rel.magnitude; float maxSurfaceDepth, maxAtmosphereDepth; SGT_Helper.CalculateHorizonAtmosphereDepth(surfaceRadius * uniformScale, AtmosphereEquatorialRadius * uniformScale, dist, out maxSurfaceDepth, out maxAtmosphereDepth); // Update shader variables for (var i = 0; i < surfaceMaterials.Length; i++) { surfaceMaterials[i].SetTexture("surfaceTexture", surfaceTexture.GetTexture((CubemapFace)i)); } SGT_ShaderHelper.SetTexture(surfaceMaterials, "atmosphereTexture", atmosphereSurfaceTexture); SGT_ShaderHelper.SetVector(surfaceMaterials, "centrePosition", centrePosition); SGT_ShaderHelper.SetFloat(surfaceMaterials, "atmosphereRadius", AtmosphereEquatorialRadius * uniformScale); SGT_ShaderHelper.SetFloat(surfaceMaterials, "atmosphereFalloff", atmosphereSurfaceFalloff); SGT_ShaderHelper.SetFloat(surfaceMaterials, "maxDepth", maxSurfaceDepth * (1.0f - atmosphereFog)); // FOG SGT_ShaderHelper.SetFloat(surfaceMaterials, "surfaceRadius", surfaceRadius * uniformScale); SGT_ShaderHelper.SetFloat(surfaceMaterials, "atmosphereHeight", atmosphereHeight * uniformScale); SGT_ShaderHelper.SetMatrix(surfaceMaterials, "ellipsoid2sphere", ellipsoid2sphere); atmosphereMaterial.SetTexture("atmosphereTexture", atmosphereTexture); atmosphereMaterial.SetVector("centrePosition", centrePosition); atmosphereMaterial.SetFloat("atmosphereFalloff", Mathf.Lerp(atmosphereSkyFalloff, atmosphereFalloff, altitude01)); atmosphereMaterial.SetFloat("maxDepth", maxAtmosphereDepth); atmosphereMaterial.SetMatrix("ellipsoid2sphere", ellipsoid2sphere); }
public void Regenerate() { if (nebulaModified == false) { CheckForModifications(); } if (nebulaModified == true) { Validate(); nebulaModified = false; var particles = new ParticleList(); SGT_Helper.BeginRandomSeed(nebulaSeed); { for (var y = 0; y < nebulaResolution; y++) { for (var x = 0; x < nebulaResolution; x++) { if (nebulaTexture != null) { var u = (float)x / (float)(nebulaResolution - 1); var v = (float)y / (float)(nebulaResolution - 1); var noiseU = Mathf.Clamp01(u + Random.Range(-heightNoise, heightNoise)); var noiseV = Mathf.Clamp01(v + Random.Range(-heightNoise, heightNoise)); var jitterU = Mathf.Clamp01(u + Random.Range(-particleJitter, particleJitter)); var jitterV = Mathf.Clamp01(v + Random.Range(-particleJitter, particleJitter)); var colour = nebulaTexture.GetPixelBilinear(noiseU, noiseV); var height = 0.0f; var max = Mathf.Max(Mathf.Max(colour.r, colour.g), colour.b); var rot = Random.value * 360.0f; var offset = heightOffset * nebulaSize; for (var i = nebulaMirror == true ? 0 : 1; i < 2; i++) { if (Random.value < max) { switch (heightSource) { case TextureHeightSource.Alpha: height = colour.a; break; case TextureHeightSource.Red: height = colour.r; break; case TextureHeightSource.Green: height = colour.g; break; case TextureHeightSource.Blue: height = colour.b; break; case TextureHeightSource.RGB: height = (colour.r + colour.g + colour.b) / 3.0f; break; case TextureHeightSource.Min: height = Mathf.Min(Mathf.Min(colour.r, colour.g), colour.b); break; case TextureHeightSource.Max: height = max; break; } if (max > 0.0f) { var p = new Particle(); var posX = SGT_Helper.Remap(0.0f, 1.0f, jitterU, -nebulaSize, nebulaSize); var posZ = SGT_Helper.Remap(0.0f, 1.0f, jitterV, -nebulaSize, nebulaSize); var posY = (heightInvert == true ? 1.0f - height : height) * nebulaSize * heightScale + offset; p.Position = new Vector3(posX, i == 0 ? -posY : posY, posZ); p.Size = (nebulaSize / (float)nebulaResolution) * particleScale * 2.0f; p.Colour = colour; p.Angle = rot; particles.Add(p); } } } } } } } SGT_Helper.EndRandomSeed(); CreateMesh(particles); } }
private Mesh GenerateAsteroidMesh(int asteroidCount) { var indices = new int[asteroidCount * 6]; var positions = new Vector3[asteroidCount * 4]; var normals = new Vector3[asteroidCount * 4]; var colours = new Color[asteroidCount * 4]; var uv0s = new Vector2[asteroidCount * 4]; var uv1s = new Vector2[asteroidCount * 4]; var size = (ringRadius + ringWidth + asteroidRadiusMax) * 2.0f; var bounds = new Bounds(Vector3.zero, new Vector3(size, asteroidRadiusMax * 2.0f, size)); var uvStep = new Vector2(1.0f / (float)asteroidTextureTilesX, 1.0f / (float)asteroidTextureTilesY); var ringRadiusInner = RingRadiusInner; var ringRadiusOuter = RingRadiusOuter; for (var i = 0; i < asteroidCount; i++) { // Index data var indicesIndex = i * 6; var vertexIndex = i * 4; indices[indicesIndex + 0] = vertexIndex + 0; indices[indicesIndex + 1] = vertexIndex + 1; indices[indicesIndex + 2] = vertexIndex + 2; indices[indicesIndex + 3] = vertexIndex + 3; indices[indicesIndex + 4] = vertexIndex + 2; indices[indicesIndex + 5] = vertexIndex + 1; // Calculate asteroid values var radius = Random.Range(asteroidRadiusMin, asteroidRadiusMax); var radius2 = radius / SGT_Helper.InscribedBox; var textureCellX = (float)Random.Range(0, asteroidTextureTilesX); var textureCellY = (float)Random.Range(0, asteroidTextureTilesY); var uvMin = new Vector2(uvStep.x * textureCellX, uvStep.y * textureCellY); var uvMax = uvStep + uvMin; var roll = Random.Range(-Mathf.PI, Mathf.PI); var right = SGT_Helper.Rotate(Vector2.right * SGT_Helper.InscribedBox, roll); var up = SGT_Helper.Rotate(Vector2.up * SGT_Helper.InscribedBox, roll); var angle = Random.Range(-Mathf.PI, Mathf.PI); var distance01 = GenerateDistance01(); var distance = SGT_Helper.Remap(0.0f, 1.0f, distance01, ringRadiusInner, ringRadiusOuter); var rps = SGT_Helper.Clamp(Mathf.Lerp(orbitRateInner, orbitRateOuter, distance01) + Random.Range(-orbitRateDeviation, orbitRateDeviation), orbitRateInner, orbitRateOuter); var height = Random.value; var spinRate = Random.value; var position = new Vector3(angle, distance, rps); var colour = new Color(height, spinRate, 0.0f); var uv1 = new Vector2(radius, radius2); // Write star values into vertex data positions[vertexIndex + 0] = position; positions[vertexIndex + 1] = position; positions[vertexIndex + 2] = position; positions[vertexIndex + 3] = position; colours[vertexIndex + 0] = colour; colours[vertexIndex + 1] = colour; colours[vertexIndex + 2] = colour; colours[vertexIndex + 3] = colour; normals[vertexIndex + 0] = SGT_Helper.NewVector3(-right + up, 0.0f); normals[vertexIndex + 1] = SGT_Helper.NewVector3(right + up, 0.0f); normals[vertexIndex + 2] = SGT_Helper.NewVector3(-right - up, 0.0f); normals[vertexIndex + 3] = SGT_Helper.NewVector3(right - up, 0.0f); uv0s[vertexIndex + 0] = new Vector2(uvMin.x, uvMax.y); uv0s[vertexIndex + 1] = new Vector2(uvMax.x, uvMax.y); uv0s[vertexIndex + 2] = new Vector2(uvMin.x, uvMin.y); uv0s[vertexIndex + 3] = new Vector2(uvMax.x, uvMin.y); uv1s[vertexIndex + 0] = uv1; uv1s[vertexIndex + 1] = uv1; uv1s[vertexIndex + 2] = uv1; uv1s[vertexIndex + 3] = uv1; } var asteroidMesh = new Mesh(); asteroidMesh.hideFlags = HideFlags.DontSave; asteroidMesh.vertices = positions; asteroidMesh.triangles = indices; asteroidMesh.normals = normals; asteroidMesh.colors = colours; asteroidMesh.uv = uv0s; asteroidMesh.uv1 = uv1s; asteroidMesh.bounds = bounds; return(asteroidMesh); }