public override void Register(GridGraphRules rules)
        {
            if (!angleToPenalty.IsCreated)
            {
                angleToPenalty = new NativeArray <float>(32, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            }
            for (int i = 0; i < angleToPenalty.Length; i++)
            {
                angleToPenalty[i] = Mathf.Max(0, curve.Evaluate(90.0f * i / (angleToPenalty.Length - 1)) * penaltyScale);
            }

            rules.AddJobSystemPass(Pass.BeforeConnections, context => {
                new JobPenaltyAngle {
                    angleToPenalty = angleToPenalty,
                    up             = context.data.up,
                    nodeNormals    = context.data.nodeNormals,
                    penalty        = context.data.nodePenalties,
                }.Schedule(context.tracker);
            });
        }
Exemple #2
0
        public override void Register(GridGraphRules rules)
        {
            if (!elevationToPenalty.IsCreated)
            {
                elevationToPenalty = new NativeArray <float>(64, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
            }
            for (int i = 0; i < elevationToPenalty.Length; i++)
            {
                elevationToPenalty[i] = Mathf.Max(0, penaltyScale * curve.Evaluate(i * 1.0f / (elevationToPenalty.Length - 1)));
            }

            var clampedElevationRange = new Vector2(math.max(0, elevationRange.x), math.max(1, elevationRange.y));

            rules.AddJobSystemPass(Pass.BeforeConnections, context => {
                //var elevationRangeScale = Matrix4x4.TRS(new Vector3(0, -clampedElevationRange.x, 0), Quaternion.identity, new Vector3(1, 1/(clampedElevationRange.y - clampedElevationRange.x), 1));
                var elevationRangeScale = Matrix4x4.Scale(new Vector3(1, 1 / (clampedElevationRange.y - clampedElevationRange.x), 1)) * Matrix4x4.Translate(new Vector3(0, -clampedElevationRange.x, 0));
                new JobElevationPenalty {
                    elevationToPenalty = elevationToPenalty,
                    nodePositions      = context.data.nodePositions,
                    worldToGraph       = elevationRangeScale * context.data.transform.matrix.inverse,
                    penalty            = context.data.nodePenalties,
                }.Schedule(context.tracker);
            });
        }
Exemple #3
0
        public override void Register(GridGraphRules rules)
        {
            if (texture == null)
            {
                return;
            }

            if (!texture.isReadable)
            {
                Debug.LogError("Texture for the texture rule on a grid graph is not marked as readable.", texture);
                return;
            }

            if (colors.IsCreated)
            {
                colors.Dispose();
            }
            colors = new NativeArray <Color32>(texture.GetPixels32(), Allocator.Persistent).Reinterpret <int>();

            // Make sure this is done outside the delegate, just in case the texture is later resized
            var textureSize = new int2(texture.width, texture.height);

            float4 channelPenaltiesCombined      = float4.zero;
            bool4  channelDeterminesWalkability  = false;
            float4 channelPositionScalesCombined = float4.zero;

            for (int i = 0; i < 4; i++)
            {
                channelPenaltiesCombined[i]      = channels[i] == ChannelUse.Penalty || channels[i] == ChannelUse.WalkablePenalty ? channelScales[i] : 0;
                channelDeterminesWalkability[i]  = channels[i] == ChannelUse.Walkable || channels[i] == ChannelUse.WalkablePenalty;
                channelPositionScalesCombined[i] = channels[i] == ChannelUse.Position ? channelScales[i] : 0;
            }

            channelPositionScalesCombined /= 255.0f;
            channelPenaltiesCombined      /= 255.0f;

            if (math.any(channelPositionScalesCombined))
            {
                rules.AddJobSystemPass(Pass.BeforeCollision, context => {
                    new JobTexturePosition {
                        colorData            = colors,
                        nodePositions        = context.data.nodePositions,
                        nodeNormals          = context.data.nodeNormals,
                        bounds               = context.data.bounds,
                        colorDataSize        = textureSize,
                        scale                = scalingMode == ScalingMode.FixedScale ? 1.0f / math.max(0.01f, nodesPerPixel) : textureSize / new float2(context.graph.width, context.graph.depth),
                        channelPositionScale = channelPositionScalesCombined,
                        graphToWorld         = context.data.transform.matrix,
                    }.Schedule(context.tracker);
                });
            }

            rules.AddJobSystemPass(Pass.BeforeConnections, context => {
                new JobTexturePenalty {
                    colorData                    = colors,
                    penalty                      = context.data.nodePenalties,
                    walkable                     = context.data.nodeWalkable,
                    nodeNormals                  = context.data.nodeNormals,
                    bounds                       = context.data.bounds,
                    colorDataSize                = textureSize,
                    scale                        = scalingMode == ScalingMode.FixedScale ? 1.0f / math.max(0.01f, nodesPerPixel) : textureSize / new float2(context.graph.width, context.graph.depth),
                    channelPenalties             = channelPenaltiesCombined,
                    channelDeterminesWalkability = channelDeterminesWalkability,
                }.Schedule(context.tracker);
            });
        }