private void BuildHeightmap()
        {
#if UNITY_EDITOR
            SgtHelper.MakeTextureReadable(heightmap);
#endif
            ClearHeightmap();

            if (heightmap != null)
            {
                var pixels = heightmap.GetPixels();
                var total  = pixels.Length;

                heightmapData = new NativeArray <float>(total, Allocator.Persistent);
                heightmapSize = new int2(heightmap.width, heightmap.height);

                for (var i = 0; i < total; i++)
                {
                    heightmapData[i] = pixels[i].a;
                }

                var t = (heightmapSize.y - 1) * heightmapSize.x;

                for (var x = 1; x < heightmapSize.x; x++)
                {
                    heightmapData[x]     = heightmapData[0];
                    heightmapData[x + t] = heightmapData[t];
                }
            }
        }
        public void RebuildMesh()
        {
            generatedMesh = SgtHelper.Destroy(generatedMesh);

            if (OriginalMesh != null && Heightmap != null)
            {
        #if UNITY_EDITOR
                SgtHelper.MakeTextureReadable(Heightmap);
        #endif
                // Duplicate original
                generatedMesh = Instantiate(OriginalMesh);
        #if UNITY_EDITOR
                generatedMesh.hideFlags = HideFlags.DontSave;
        #endif
                generatedMesh.name = OriginalMesh.name + " (Displaced)";

                // Displace vertices
                var positions = OriginalMesh.vertices;

                for (var i = 0; i < positions.Length; i++)
                {
                    var direction = positions[i].normalized;

                    positions[i] = direction * GetSurfaceHeightLocal(direction);
                }

                generatedMesh.vertices = positions;

                generatedMesh.RecalculateBounds();
                generatedMesh.RecalculateNormals();
                generatedMesh.RecalculateTangents();
            }

            ApplyMesh();
        }
Ejemplo n.º 3
0
        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 PrepareTexture()
        {
#if UNITY_EDITOR
            SgtHelper.MakeTextureReadable(heightmap);
#endif
            MarkAsDirty();
        }
Ejemplo n.º 5
0
        public void Rebuild()
        {
            dirtyMesh     = false;
            generatedMesh = SgtHelper.Destroy(generatedMesh);

            if (mesh != null)
            {
                generatedMesh = Instantiate(mesh);

                generatedMesh.GetVertices(generatedPositions);
                generatedMesh.GetUVs(0, generatedCoords);

                var count = generatedMesh.vertexCount;

                lastHeightmap = MaterialHeightmap;
#if UNITY_EDITOR
                SgtHelper.MakeTextureReadable(lastHeightmap);
#endif

                for (var i = 0; i < count; i++)
                {
                    var height = radius;
                    var vector = generatedPositions[i].normalized;
                    var coord  = generatedCoords[i];

                    if (vector.y > 0.0f)
                    {
                        coord.z = -vector.x * 0.5f;
                        coord.w = vector.z * 0.5f;
                    }
                    else
                    {
                        coord.z = vector.x * 0.5f;
                        coord.w = vector.z * 0.5f;
                    }

                    generatedCoords[i] = coord;

                    generatedPositions[i] = vector * Sample(vector);
                }

                generatedMesh.bounds = new Bounds(Vector3.zero, Vector3.one * (radius + displacement) * 2.0f);

                generatedMesh.SetVertices(generatedPositions);
                generatedMesh.SetUVs(0, generatedCoords);

                generatedMesh.RecalculateNormals();
                generatedMesh.RecalculateTangents();

                if (meshCollider != null)
                {
                    meshCollider.sharedMesh = null;
                    meshCollider.sharedMesh = generatedMesh;
                }
            }
        }
Ejemplo n.º 6
0
        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);
            }
        }
Ejemplo n.º 7
0
        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();
            }
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        private bool GenerateAreas()
        {
#if UNITY_EDITOR
            SgtHelper.MakeTextureReadable(texture);
            SgtHelper.MakeTextureTruecolor(texture);
#endif
            if (texture != null && splats != null)
            {
                splatCount = splats.Count;
                size       = new int2(texture.width, texture.height);

                SgtHelper.UpdateNativeArray(ref weights, size.x * size.y * splatCount);

                tempColors.Clear();

                foreach (var splat in splats)
                {
                    tempColors.Add(splat.Color);
                }

                var data = texture.GetRawTextureData <byte>();
                var offS = SgtHelper.GetStride(texture.format);
                var offR = SgtHelper.GetOffset(texture.format, 0);
                var offG = SgtHelper.GetOffset(texture.format, 1);
                var offB = SgtHelper.GetOffset(texture.format, 2);

                for (var y = 0; y < size.y; y++)
                {
                    for (var x = 0; x < size.x; x++)
                    {
                        var index = x + y * size.x;
                        var r     = data[index * offS + offR];
                        var g     = data[index * offS + offG];
                        var b     = data[index * offS + offB];

                        for (var s = 0; s < splatCount; s++)
                        {
                            weights[index * splatCount + s] = GetDistance(tempColors[s], r, g, b);
                        }
                    }
                }

                return(true);
            }

            return(false);
        }
Ejemplo n.º 10
0
        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);
                }
            }
        }
Ejemplo n.º 11
0
        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);
                }
            }
        }
Ejemplo n.º 12
0
        private static bool PrepareTexture(Texture texture, bool repX, bool repY, ref sampler sam)
        {
            sam.set = false;
#if UNITY_EDITOR
            SgtHelper.MakeTextureReadable(texture);
#endif
            if (texture != null && texture is Texture2D)
            {
                sam.set  = true;
                sam.tex  = (Texture2D)texture;
                sam.repX = repX;
                sam.repY = repY;
                sam.w    = 0.5f / texture.width;
                sam.h    = 0.5f / texture.height;
            }

            return(sam.set);
        }
Ejemplo n.º 13
0
        protected override int BeginQuads()
        {
            SgtHelper.BeginRandomSeed(Seed);

            sourceTex2D = SourceTex as Texture2D;

            if (sourceTex2D != null && Samples > 0)
            {
#if UNITY_EDITOR
                SgtHelper.MakeTextureReadable(sourceTex2D);
                SgtHelper.MakeTextureTruecolor(sourceTex2D);
#endif
                halfSize = Size * 0.5f;

                return(StarCount);
            }

            return(0);
        }
        public void Rebuild()
        {
            dirty         = false;
            generatedMesh = SgtHelper.Destroy(generatedMesh);

            if (mesh != null)
            {
                generatedMesh = Instantiate(mesh);

                generatedMesh.GetVertices(generatedPositions);
                generatedMesh.GetUVs(0, generatedCoords);

                var count = generatedMesh.vertexCount;
#if UNITY_EDITOR
                SgtHelper.MakeTextureReadable(heightmap);
#endif
                for (var i = 0; i < count; i++)
                {
                    generatedCoords[i] = generatedCoords[i];

                    var height = radius;
                    var vector = generatedPositions[i].normalized;

                    generatedPositions[i] = vector * Sample(vector);
                }

                generatedMesh.SetVertices(generatedPositions);
                generatedMesh.SetUVs(0, generatedCoords);

                generatedMesh.RecalculateBounds();
                generatedMesh.RecalculateNormals();
                generatedMesh.RecalculateTangents();

                if (meshCollider != null)
                {
                    meshCollider.sharedMesh = null;
                    meshCollider.sharedMesh = generatedMesh;
                }
            }
        }
Ejemplo n.º 15
0
        public void RebuildMesh()
        {
            displacedMesh = SgtHelper.Destroy(displacedMesh);

            if (OriginalMesh != null && Heightmap != null)
            {
        #if UNITY_EDITOR
                SgtHelper.MakeTextureReadable(Heightmap);
        #endif
                // Duplicate original
                displacedMesh = Instantiate(OriginalMesh);
        #if UNITY_EDITOR
                displacedMesh.hideFlags = HideFlags.DontSave;
        #endif
                displacedMesh.name = OriginalMesh.name + " (Displaced)";

                // Displace vertices
                var positions = OriginalMesh.vertices;

                for (var i = 0; i < positions.Length; i++)
                {
                    var direction = positions[i].normalized;

                    positions[i] = direction * GetSurfaceHeightLocal(direction);
                }

                displacedMesh.vertices = positions;

                displacedMesh.RecalculateBounds();
            }

            // Apply displaced mesh
            if (meshFilter == null)
            {
                meshFilter = GetComponent <MeshFilter>();
            }

            meshFilter.sharedMesh = displacedMesh;
        }
Ejemplo n.º 16
0
        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();
            }
        }
Ejemplo n.º 17
0
        protected override int BeginQuads()
        {
            SgtHelper.BeginRandomSeed(seed);

            if (starColors == null)
            {
                starColors = SgtHelper.CreateGradient(Color.white);
            }

            sourceTex2D = sourceTex as Texture2D;

            if (sourceTex2D != null && samples > 0)
            {
#if UNITY_EDITOR
                SgtHelper.MakeTextureReadable(sourceTex2D);
                SgtHelper.MakeTextureTruecolor(sourceTex2D);
#endif
                halfSize = size * 0.5f;

                return(starCount);
            }

            return(0);
        }
Ejemplo n.º 18
0
        private void Rebuild()
        {
            Clear();
#if UNITY_EDITOR
            SgtHelper.MakeTextureReadable(splat);
#endif
            if (splat != null)
            {
                var pixels = splat.GetPixels();
                var total  = pixels.Length;

                splatData = new NativeArray <float>(total, Allocator.Persistent);
                splatSize = new int2(splat.width, splat.height);

                switch (channel)
                {
                case ChannelType.Red:
                {
                    for (var i = 0; i < total; i++)
                    {
                        splatData[i] = pixels[i].r;
                    }
                }
                break;

                case ChannelType.Green:
                {
                    for (var i = 0; i < total; i++)
                    {
                        splatData[i] = pixels[i].g;
                    }
                }
                break;

                case ChannelType.Blue:
                {
                    for (var i = 0; i < total; i++)
                    {
                        splatData[i] = pixels[i].b;
                    }
                }
                break;

                case ChannelType.Alpha:
                {
                    for (var i = 0; i < total; i++)
                    {
                        splatData[i] = pixels[i].a;
                    }
                }
                break;
                }

                var t = (splatSize.y - 1) * splatSize.x;

                for (var x = 1; x < splatSize.x; x++)
                {
                    splatData[x]     = splatData[0];
                    splatData[x + t] = splatData[t];
                }
            }
            else
            {
                splatData = new NativeArray <float>(0, Allocator.Persistent);
                splatSize = int2.zero;
            }
        }
        public void UpdateTexture()
        {
            if (Source == null)
            {
                Source = CachedShadowRing.Texture as Texture2D;
            }

            if (Source != null)
            {
                var width = Source.width;
#if UNITY_EDITOR
                SgtHelper.MakeTextureReadable(Source);
#endif
                // Destroy if invalid
                if (generatedTexture != null)
                {
                    if (generatedTexture.width != width || generatedTexture.height != 1 || generatedTexture.format != Format)
                    {
                        generatedTexture = SgtHelper.Destroy(generatedTexture);
                    }
                }

                // Create?
                if (generatedTexture == null)
                {
                    generatedTexture = SgtHelper.CreateTempTexture2D("Ring Shadow (Generated)", width, 1, Format);

                    generatedTexture.wrapMode = TextureWrapMode.Clamp;

                    ApplyTexture();
                }

                if (bufferA == null || bufferA.Length != width)
                {
                    bufferA = new Color[width];
                    bufferB = new Color[width];
                }

                for (var x = 0; x < width; x++)
                {
                    bufferA[x] = bufferB[x] = Source.GetPixel(x, 0);
                }

                if (Invert == true)
                {
                    for (var x = 0; x < width; x++)
                    {
                        var a = bufferA[x];

                        bufferA[x] = bufferB[x] = new Color(1.0f - a.r, 1.0f - a.g, 1.0f - a.b, 1.0f - a.a);
                    }
                }

                if (ShareRGB == true)
                {
                    for (var x = 0; x < width; x++)
                    {
                        var a = bufferA[x].a;

                        bufferA[x] = bufferB[x] = new Color(a, a, a, a);
                    }
                }

                for (var i = 0; i < Iterations; i++)
                {
                    SwapBuffers();

                    for (var x = width - 2; x >= 1; x--)
                    {
                        WritePixel(x);
                    }
                }

                for (var x = 0; x < width; x++)
                {
                    generatedTexture.SetPixel(x, 0, bufferB[x]);
                }

                generatedTexture.SetPixel(0, 0, Color.white);
                generatedTexture.SetPixel(width - 1, 0, Color.white);

                generatedTexture.Apply();
            }
        }