예제 #1
0
        public override void Apply(ref GSpawnFilterArgs args)
        {
            Quaternion currentRotationY = Quaternion.Euler(0, args.Rotation.eulerAngles.y, 0);
            Quaternion rotationXZ       = Quaternion.FromToRotation(Vector3.up, args.SurfaceNormal);

            args.Rotation = rotationXZ * currentRotationY;
        }
예제 #2
0
        public override void Apply(ref GSpawnFilterArgs args)
        {
            Vector3 scale = new Vector3(
                Mathf.Clamp(args.Scale.x, MinScale.x, MaxScale.x),
                Mathf.Clamp(args.Scale.y, MinScale.y, MaxScale.y),
                Mathf.Clamp(args.Scale.z, MinScale.z, MaxScale.z));

            args.Scale = scale;
        }
예제 #3
0
        public static GSpawnFilterArgs Create()
        {
            GSpawnFilterArgs args = new GSpawnFilterArgs();

            args.Position        = Vector3.zero;
            args.Rotation        = Quaternion.identity;
            args.Scale           = Vector3.one;
            args.Color           = Color.white;
            args.SurfaceNormal   = Vector3.up;
            args.SurfaceTexcoord = Vector2.zero;
            args.ShouldExclude   = false;
            return(args);
        }
예제 #4
0
        public override void Apply(ref GSpawnFilterArgs args)
        {
            float h = 0;

            if (Space == Space.World)
            {
                h = args.Position.y;
            }
            else
            {
                h = args.Terrain.transform.InverseTransformPoint(args.Position).y;
            }

            args.ShouldExclude = h <MinHeight || h> MaxHeight;
        }
예제 #5
0
        private void HandleSpawnGrass(GStylizedTerrain terrain, GFoliagePainterArgs args)
        {
            int        grassIndex   = -1;
            Vector3    randomPos    = Vector3.zero;
            Vector3    rayOrigin    = Vector3.zero;
            Vector3    rayDirection = Vector3.down;
            float      sqrtTwo      = Mathf.Sqrt(2);
            Ray        ray          = new Ray();
            RaycastHit samplePoint;
            Vector3    bary0  = Vector3.zero;
            Vector3    bary1  = Vector3.zero;
            Vector2    maskUv = Vector2.zero;
            Vector2    samplePointTexcoord = Vector2.zero;
            Color      maskColor           = Color.white;
            Texture2D  clonedMask          = null;
            Texture2D  terrainMask         = null;

            if (args.Mask != null)
            {
                clonedMask = GCommon.CloneAndResizeTexture(args.Mask, 256, 256);
            }
            if (args.EnableTerrainMask)
            {
                terrainMask = terrain.TerrainData.Mask.MaskMap;
            }

            int prototypeCount = terrain.TerrainData.Foliage.Grasses.Prototypes.Count;
            int sampleCount    = args.Density;
            List <GGrassInstance> newInstances = new List <GGrassInstance>();

            for (int i = 0; i < sampleCount; ++i)
            {
                grassIndex = args.GrassIndices[Random.Range(0, args.GrassIndices.Count)];
                if (grassIndex < 0 || grassIndex >= prototypeCount)
                {
                    continue;
                }
                randomPos = args.HitPoint + Random.insideUnitSphere * args.Radius * sqrtTwo;
                rayOrigin.Set(
                    randomPos.x,
                    10000,
                    randomPos.z);
                ray.origin    = rayOrigin;
                ray.direction = rayDirection;
                if (terrain.Raycast(ray, out samplePoint, float.MaxValue))
                {
                    GUtilities.CalculateBarycentricCoord(
                        new Vector2(samplePoint.point.x, samplePoint.point.z),
                        new Vector2(args.WorldPointCorners[0].x, args.WorldPointCorners[0].z),
                        new Vector2(args.WorldPointCorners[1].x, args.WorldPointCorners[1].z),
                        new Vector2(args.WorldPointCorners[2].x, args.WorldPointCorners[2].z),
                        ref bary0);
                    GUtilities.CalculateBarycentricCoord(
                        new Vector2(samplePoint.point.x, samplePoint.point.z),
                        new Vector2(args.WorldPointCorners[0].x, args.WorldPointCorners[0].z),
                        new Vector2(args.WorldPointCorners[2].x, args.WorldPointCorners[2].z),
                        new Vector2(args.WorldPointCorners[3].x, args.WorldPointCorners[3].z),
                        ref bary1);
                    if (bary0.x >= 0 && bary0.y >= 0 && bary0.z >= 0)
                    {
                        maskUv = bary0.x * Vector2.zero + bary0.y * Vector2.up + bary0.z * Vector2.one;
                    }
                    else if (bary1.x >= 0 && bary1.y >= 0 && bary1.z >= 0)
                    {
                        maskUv = bary1.x * Vector2.zero + bary1.y * Vector2.one + bary1.z * Vector2.right;
                    }
                    else
                    {
                        continue;
                    }

                    //sample mask
                    if (clonedMask != null)
                    {
                        maskColor = clonedMask.GetPixelBilinear(maskUv.x, maskUv.y);
                        if (Random.value > maskColor.grayscale)
                        {
                            continue;
                        }
                    }
                    //sample terrain mask
                    if (args.EnableTerrainMask)
                    {
                        samplePointTexcoord = samplePoint.textureCoord;
                        maskColor           = terrainMask.GetPixelBilinear(samplePointTexcoord.x, samplePointTexcoord.y);
                        if (Random.value < maskColor.r)
                        {
                            continue;
                        }
                    }

                    //apply filter
                    GSpawnFilterArgs filterArgs = GSpawnFilterArgs.Create();
                    filterArgs.Terrain         = terrain;
                    filterArgs.Position        = samplePoint.point;
                    filterArgs.SurfaceNormal   = samplePoint.normal;
                    filterArgs.SurfaceTexcoord = samplePoint.textureCoord;

                    List <Type> suitableFilter = SuitableFilterTypes;
                    if (args.Filters != null)
                    {
                        for (int fIndex = 0; fIndex < args.Filters.Length; ++fIndex)
                        {
                            if (args.Filters[fIndex] != null &&
                                args.Filters[fIndex].Ignore != true)
                            {
                                if (suitableFilter.Contains(args.Filters[fIndex].GetType()))
                                {
                                    args.Filters[fIndex].Apply(ref filterArgs);
                                }
                            }
                            if (filterArgs.ShouldExclude)
                            {
                                break;
                            }
                        }
                    }

                    //spawn
                    if (filterArgs.ShouldExclude)
                    {
                        continue;
                    }

                    GGrassInstance grass = GGrassInstance.Create(grassIndex);
                    grass.Position = terrain.WorldPointToNormalized(filterArgs.Position);
                    grass.Rotation = filterArgs.Rotation;
                    grass.Scale    = filterArgs.Scale;
                    newInstances.Add(grass);
                }
            }

            terrain.TerrainData.Foliage.AddGrassInstances(newInstances);
            if (clonedMask != null)
            {
                Object.DestroyImmediate(clonedMask);
            }
        }
예제 #6
0
        public void Paint(Pinwheel.Griffin.GStylizedTerrain terrain, GFoliagePainterArgs args)
        {
            if (args.GrassIndices.Count == 0)
            {
                return;
            }
            if (terrain.TerrainData == null)
            {
                return;
            }
            if (terrain.TerrainData.Foliage.Grasses == null)
            {
                return;
            }
            if (args.MouseEventType == GPainterMouseEventType.Up || args.ShouldCommitNow)
            {
                terrain.TerrainData.SetDirty(GTerrainData.DirtyFlags.Foliage);
                GRuntimeSettings.Instance.isEditingFoliage = false;
                return;
            }

            Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length];
            for (int i = 0; i < uvCorners.Length; ++i)
            {
                uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]);
            }

            Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners);

            if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1)))
            {
                return;
            }

            GRuntimeSettings.Instance.isEditingFoliage = true;
            Texture2D clonedMask  = null;
            Texture2D terrainMask = null;

            if (args.Mask != null)
            {
                clonedMask = GCommon.CloneAndResizeTexture(args.Mask, 256, 256);
            }
            if (args.EnableTerrainMask)
            {
                terrainMask = terrain.TerrainData.Mask.MaskMap;
            }

            int     multiplier  = args.ActionType == GPainterActionType.Normal ? 1 : -1;
            int     grassIndex  = -1;
            Vector3 terrainSize = new Vector3(
                terrain.TerrainData.Geometry.Width,
                terrain.TerrainData.Geometry.Height,
                terrain.TerrainData.Geometry.Length);
            Vector3 localPos  = Vector3.zero;
            Vector3 worldPos  = Vector3.zero;
            Vector3 bary0     = Vector3.zero;
            Vector3 bary1     = Vector3.zero;
            Vector2 maskUv    = Vector2.zero;
            Color   maskColor = Color.white;
            Vector3 scale     = Vector3.zero;

            GGrassPatch[] patches = terrain.TerrainData.Foliage.GrassPatches;
            for (int p = 0; p < patches.Length; ++p)
            {
                if (!patches[p].GetUvRange().Overlaps(dirtyRect))
                {
                    continue;
                }

                List <GGrassInstance> instances = patches[p].Instances;
                int instanceCount = instances.Count;
                for (int i = 0; i < instanceCount; ++i)
                {
                    grassIndex = args.GrassIndices[Random.Range(0, args.GrassIndices.Count)];
                    GGrassInstance grass = instances[i];
                    if (grass.PrototypeIndex != grassIndex)
                    {
                        continue;
                    }

                    localPos.Set(
                        grass.position.x * terrainSize.x,
                        grass.position.y * terrainSize.y,
                        grass.position.z * terrainSize.z);
                    worldPos = terrain.transform.TransformPoint(localPos);
                    GUtilities.CalculateBarycentricCoord(
                        new Vector2(worldPos.x, worldPos.z),
                        new Vector2(args.WorldPointCorners[0].x, args.WorldPointCorners[0].z),
                        new Vector2(args.WorldPointCorners[1].x, args.WorldPointCorners[1].z),
                        new Vector2(args.WorldPointCorners[2].x, args.WorldPointCorners[2].z),
                        ref bary0);
                    GUtilities.CalculateBarycentricCoord(
                        new Vector2(worldPos.x, worldPos.z),
                        new Vector2(args.WorldPointCorners[0].x, args.WorldPointCorners[0].z),
                        new Vector2(args.WorldPointCorners[2].x, args.WorldPointCorners[2].z),
                        new Vector2(args.WorldPointCorners[3].x, args.WorldPointCorners[3].z),
                        ref bary1);
                    if (bary0.x >= 0 && bary0.y >= 0 && bary0.z >= 0)
                    {
                        maskUv = bary0.x * Vector2.zero + bary0.y * Vector2.up + bary0.z * Vector2.one;
                    }
                    else if (bary1.x >= 0 && bary1.y >= 0 && bary1.z >= 0)
                    {
                        maskUv = bary1.x * Vector2.zero + bary1.y * Vector2.one + bary1.z * Vector2.right;
                    }
                    else
                    {
                        continue;
                    }

                    if (clonedMask != null)
                    {
                        maskColor = clonedMask.GetPixelBilinear(maskUv.x, maskUv.y);
                        if (Random.value > maskColor.grayscale)
                        {
                            continue;
                        }
                    }
                    //sample terrain mask
                    if (args.EnableTerrainMask)
                    {
                        maskColor = terrainMask.GetPixelBilinear(grass.position.x, grass.position.z);
                        if (Random.value < maskColor.r)
                        {
                            continue;
                        }
                    }

                    scale.Set(
                        Mathf.Max(0, grass.scale.x + multiplier * maskColor.grayscale * args.ScaleStrength * GUtilities.DELTA_TIME),
                        Mathf.Max(0, grass.scale.y + multiplier * maskColor.grayscale * args.ScaleStrength * GUtilities.DELTA_TIME),
                        Mathf.Max(0, grass.scale.z + multiplier * maskColor.grayscale * args.ScaleStrength * GUtilities.DELTA_TIME));

                    GSpawnFilterArgs filterArgs = GSpawnFilterArgs.Create();
                    filterArgs.Terrain  = terrain;
                    filterArgs.Position = worldPos;
                    filterArgs.Rotation = grass.Rotation;
                    filterArgs.Scale    = scale;
                    List <Type> suitableFilter = SuitableFilterTypes;
                    if (args.Filters != null)
                    {
                        for (int fIndex = 0; fIndex < args.Filters.Length; ++fIndex)
                        {
                            if (args.Filters[fIndex] != null &&
                                args.Filters[fIndex].Ignore != true)
                            {
                                if (suitableFilter.Contains(args.Filters[fIndex].GetType()))
                                {
                                    args.Filters[fIndex].Apply(ref filterArgs);
                                }
                            }
                            if (filterArgs.ShouldExclude)
                            {
                                break;
                            }
                        }
                    }

                    grass.scale  = filterArgs.Scale;
                    instances[i] = grass;
                }
                patches[p].Changed();
            }

            terrain.TerrainData.Foliage.SetGrassRegionDirty(dirtyRect);
            GUtilities.MarkCurrentSceneDirty();
        }
예제 #7
0
        public void Paint(Pinwheel.Griffin.GStylizedTerrain terrain, GObjectPainterArgs args)
        {
            if (args.MouseEventType == GPainterMouseEventType.Up)
            {
                return;
            }

            Vector2[] uvCorners = new Vector2[args.WorldPointCorners.Length];
            for (int i = 0; i < uvCorners.Length; ++i)
            {
                uvCorners[i] = terrain.WorldPointToUV(args.WorldPointCorners[i]);
            }

            Rect dirtyRect = GUtilities.GetRectContainsPoints(uvCorners);

            if (!dirtyRect.Overlaps(new Rect(0, 0, 1, 1)))
            {
                return;
            }

            Texture2D clonedMask  = null;
            Texture2D terrainMask = null;

            if (args.Mask != null)
            {
                clonedMask = GCommon.CloneAndResizeTexture(args.Mask, 256, 256);
            }
            if (args.EnableTerrainMask)
            {
                terrainMask = terrain.TerrainData.Mask.MaskMap;
            }

            int multiplier = args.ActionType == GPainterActionType.Normal ? 1 : -1;

            int     prototypeIndex = -1;
            Vector2 terrainUv      = Vector2.zero;
            Vector3 worldPos       = Vector3.zero;
            Vector3 bary0          = Vector3.zero;
            Vector3 bary1          = Vector3.zero;
            Vector2 maskUv         = Vector2.zero;
            Color   maskColor      = Color.white;
            Vector3 scale          = Vector3.zero;

            for (int i = 0; i < args.PrototypeIndices.Count; ++i)
            {
                prototypeIndex = args.PrototypeIndices[i];
                if (prototypeIndex < 0 || prototypeIndex >= args.Prototypes.Count)
                {
                    continue;
                }
                GameObject g = args.Prototypes[prototypeIndex];
                if (g == null)
                {
                    continue;
                }
                GameObject  root          = GSpawner.GetRoot(terrain, g);
                Transform[] instances     = GUtilities.GetChildrenTransforms(root.transform);
                int         instanceCount = instances.Length;
                for (int j = 0; j < instanceCount; ++j)
                {
                    worldPos = instances[j].position;
                    GUtilities.CalculateBarycentricCoord(
                        new Vector2(worldPos.x, worldPos.z),
                        new Vector2(args.WorldPointCorners[0].x, args.WorldPointCorners[0].z),
                        new Vector2(args.WorldPointCorners[1].x, args.WorldPointCorners[1].z),
                        new Vector2(args.WorldPointCorners[2].x, args.WorldPointCorners[2].z),
                        ref bary0);
                    GUtilities.CalculateBarycentricCoord(
                        new Vector2(worldPos.x, worldPos.z),
                        new Vector2(args.WorldPointCorners[0].x, args.WorldPointCorners[0].z),
                        new Vector2(args.WorldPointCorners[2].x, args.WorldPointCorners[2].z),
                        new Vector2(args.WorldPointCorners[3].x, args.WorldPointCorners[3].z),
                        ref bary1);
                    if (bary0.x >= 0 && bary0.y >= 0 && bary0.z >= 0)
                    {
                        maskUv = bary0.x * Vector2.zero + bary0.y * Vector2.up + bary0.z * Vector2.one;
                    }
                    else if (bary1.x >= 0 && bary1.y >= 0 && bary1.z >= 0)
                    {
                        maskUv = bary1.x * Vector2.zero + bary1.y * Vector2.one + bary1.z * Vector2.right;
                    }
                    else
                    {
                        continue;
                    }

                    if (clonedMask != null)
                    {
                        maskColor = clonedMask.GetPixelBilinear(maskUv.x, maskUv.y);
                        if (Random.value > maskColor.grayscale)
                        {
                            continue;
                        }
                    }
                    if (args.EnableTerrainMask)
                    {
                        terrainUv = terrain.WorldPointToUV(worldPos);
                        maskColor = terrainMask.GetPixelBilinear(terrainUv.x, terrainUv.y);
                        if (Random.value < maskColor.r)
                        {
                            continue;
                        }
                    }

                    scale.Set(
                        Mathf.Max(0, instances[j].transform.localScale.x + multiplier * maskColor.grayscale * args.ScaleStrength * GUtilities.DELTA_TIME),
                        Mathf.Max(0, instances[j].transform.localScale.y + multiplier * maskColor.grayscale * args.ScaleStrength * GUtilities.DELTA_TIME),
                        Mathf.Max(0, instances[j].transform.localScale.z + multiplier * maskColor.grayscale * args.ScaleStrength * GUtilities.DELTA_TIME));

                    GSpawnFilterArgs filterArgs = GSpawnFilterArgs.Create();
                    filterArgs.Terrain  = terrain;
                    filterArgs.Position = worldPos;
                    filterArgs.Rotation = instances[j].transform.rotation;
                    filterArgs.Scale    = scale;
                    List <Type> suitableFilter = SuitableFilterTypes;
                    if (args.Filters != null)
                    {
                        for (int fIndex = 0; fIndex < args.Filters.Length; ++fIndex)
                        {
                            if (args.Filters[fIndex] != null &&
                                args.Filters[fIndex].Ignore != true)
                            {
                                if (suitableFilter.Contains(args.Filters[fIndex].GetType()))
                                {
                                    args.Filters[fIndex].Apply(ref filterArgs);
                                }
                            }
                            if (filterArgs.ShouldExclude)
                            {
                                break;
                            }
                        }
                    }

                    instances[j].transform.localScale = filterArgs.Scale;
                }
            }
        }
 public abstract void Apply(ref GSpawnFilterArgs args);
        public override void Apply(ref GSpawnFilterArgs args)
        {
            float angle = Vector3.Angle(args.SurfaceNormal, Vector3.up);

            args.ShouldExclude = angle <MinSlopeAngle || angle> MaxSlopeAngle;
        }
예제 #10
0
        public override void Apply(ref GSpawnFilterArgs args)
        {
            Vector3 scale = Vector3.Lerp(MinScale, MaxScale, Random.value);

            args.Scale = scale;
        }
예제 #11
0
 public override void Apply(ref GSpawnFilterArgs args)
 {
     args.Rotation = Quaternion.Euler(0, Random.Range(MinAngle, MaxAngle), 0);
 }