コード例 #1
0
        protected ImpactTagMask?getOtherObjectTagMask(IImpactObject impactObject, Vector3 point, int otherPhysicsMaterialID, bool hasOtherObject)
        {
            if (hasOtherObject)
            {
                IImpactMaterial m = impactObject.GetPrimaryMaterial(point);

                if (m != null)
                {
                    return(m.MaterialTagsMask);
                }
                else if (ImpactManagerInstance.UseMaterialMapping && ImpactManagerInstance.TryGetImpactMaterialFromMapping(otherPhysicsMaterialID, out m))
                {
                    return(m.MaterialTagsMask);
                }
            }
            else
            {
                IImpactMaterial m;
                if (ImpactManagerInstance.UseMaterialMapping && ImpactManagerInstance.TryGetImpactMaterialFromMapping(otherPhysicsMaterialID, out m))
                {
                    return(m.MaterialTagsMask);
                }
            }

            return(null);
        }
コード例 #2
0
        public override int GetMaterialCompositionNonAlloc(Vector3 point, ImpactMaterialComposition[] results)
        {
            if (!hasTerrain)
            {
                Debug.LogError($"Cannot get material composition for ImpactTerrain {gameObject.name} because it has no TerrainData.");
                return(0);
            }

            Vector2Int alphamapIndices = getAlphamapIndicesAtPoint(point);
            int        finalLength     = Mathf.Min(results.Length, compositionBuffer.Length);

            int   count = 0;
            float compositionValueTotal = 0;

            //Clear influence buffer
            for (int i = 0; i < compositionBuffer.Length; i++)
            {
                compositionBuffer[i].CompositionValue = 0;
                compositionBuffer[i].Material         = null;
            }

            //Get the composition of all impact materials, combining when needed (since you can have multiple textures mapped to the same impact material)
            for (int i = 0; i < compositionBuffer.Length; i++)
            {
                IImpactMaterial m    = TerrainMaterials[i];
                float           comp = cachedAlphamaps[alphamapIndices.y, alphamapIndices.x, i];

                int existingIndex = compositionBuffer.IndexOf(p => p.Material == m);
                if (existingIndex > -1)
                {
                    compositionBuffer[existingIndex].CompositionValue += comp;
                }
                else
                {
                    compositionBuffer[i].CompositionValue = comp;
                    compositionBuffer[i].Material         = m;
                }

                if (count < finalLength)
                {
                    compositionValueTotal += comp;
                    count++;
                }
            }

            //Sort composition buffer by composition value
            Array.Sort(compositionBuffer, (a, b) => { return(b.CompositionValue.CompareTo(a.CompositionValue)); });

            //Populate final composition results
            for (int i = 0; i < finalLength; i++)
            {
                //Adjust composition value so results will always add up to 1, this is for cases where results.length < compositionBuffer.Length
                compositionBuffer[i].CompositionValue = Mathf.Clamp01(compositionBuffer[i].CompositionValue / compositionValueTotal);
                results[i] = compositionBuffer[i];
            }

            return(finalLength);
        }
コード例 #3
0
        /// <summary>
        /// Process interaction data using the Impact Material and an optional Impact Object that the interaction originated from.
        /// </summary>
        /// <param name="interactionData">The interaction data to process.</param>
        /// <param name="impactMaterial">The Impact Material to get interaction results from.</param>
        /// <param name="impactObject">An optional Impact Object that the interaction originated from.</param>
        public void ProcessInteraction <T>(T interactionData, IImpactMaterial impactMaterial, IImpactObject impactObject) where T : IInteractionData
        {
            int count = impactMaterial.GetInteractionResultsNonAlloc(interactionData, InteractionResultBuffer);

            for (int i = 0; i < count; i++)
            {
                InteractionResultBuffer[i].Process(impactObject);
            }
        }
コード例 #4
0
        /// <summary>
        /// Try to get an Impact Material from the Material Mapping using the given Physics Material instance ID.
        /// </summary>
        /// <param name="physicsMaterialInstanceId">The instance ID of the physics material (3D or 2D).</param>
        /// <param name="impactMaterial">The material that was found in the mapping, if one was found. Null otherwise.</param>
        /// <returns>True if a matching map was found, False otherwise.</returns>
        public bool TryGetImpactMaterialFromMapping(int physicsMaterialInstanceId, out IImpactMaterial impactMaterial)
        {
            if (!_usePhysicMaterialMapping)
            {
                impactMaterial = null;
                return(false);
            }

            return(materialMapDictionary.TryGetValue(physicsMaterialInstanceId, out impactMaterial));
        }
コード例 #5
0
        /// <summary>
        /// Try to get an Impact Material from the Material Mapping using the given Collider2D.
        /// </summary>
        /// <param name="collider2d">The collider that has the Physics Material 2D to get a mapping for.</param>
        /// <param name="impactMaterial">The material that was found in the mapping, if one was found. Null otherwise.</param>
        /// <returns>True if a matching map was found, False otherwise.</returns>
        public bool TryGetImpactMaterialFromMapping(Collider2D collider2d, out IImpactMaterial impactMaterial)
        {
            if (!_usePhysicMaterialMapping || collider2d.sharedMaterial == null)
            {
                impactMaterial = null;
                return(false);
            }

            return(materialMapDictionary.TryGetValue(collider2d.sharedMaterial.GetInstanceID(), out impactMaterial));
        }
コード例 #6
0
        /// <summary>
        /// Process a continuous interaction using the interaction data, an Impact Material, and an optional Impact Object that the interaction originated from.
        /// </summary>
        /// <param name="interactionData">The interaction data to process.</param>
        /// <param name="impactMaterial">The Impact Material to get interaction results from.</param>
        /// <param name="impactObject">An optional Impact Object that the interaction originated from.</param>
        public void ProcessContinuousInteraction <T>(T interactionData, IImpactMaterial material, IImpactObject impactObject) where T : IInteractionData
        {
            int resultCount = material.GetInteractionResultsNonAlloc(interactionData, InteractionResultBuffer);

            for (int i = 0; i < resultCount; i++)
            {
                IContinuousInteractionResult result = InteractionResultBuffer[i] as IContinuousInteractionResult;

                //result is not a continuous interaction, so simply Process it.
                if (result == null)
                {
                    InteractionResultBuffer[i].Process(impactObject);
                }
                //Otherwise update an existing continuous interaction or add a new one.
                else
                {
                    AddOrUpdateContinuousInteractionResult(impactObject, result);
                }
            }
        }
コード例 #7
0
        private static void triggerOnRaycastingObject <T>(T interactionData, IImpactObject impactObject, IImpactObject otherObject, int physicsMaterialId, bool useMaterialComposition) where T : IInteractionData
        {
            if (otherObject != null)
            {
                if (useMaterialComposition)
                {
                    int count = otherObject.GetMaterialCompositionNonAlloc(interactionData.Point, ImpactManagerInstance.MaterialCompositionBuffer);
                    for (int i = 0; i < count; i++)
                    {
                        ImpactMaterialComposition comp = ImpactManagerInstance.MaterialCompositionBuffer[i];
                        if (comp.CompositionValue > 0)
                        {
                            IInteractionData newInteractionData = interactionData.Clone();
                            newInteractionData.CompositionValue = comp.CompositionValue;
                            newInteractionData.TagMask          = comp.Material.MaterialTagsMask;
                            ImpactManagerInstance.ProcessInteraction(newInteractionData, impactObject);
                        }
                    }
                }
                else
                {
                    IImpactMaterial material = otherObject.GetPrimaryMaterial(interactionData.Point);
                    if (material != null || (ImpactManagerInstance.UseMaterialMapping && ImpactManagerInstance.TryGetImpactMaterialFromMapping(physicsMaterialId, out material)))
                    {
                        interactionData.TagMask = material.MaterialTagsMask;
                    }

                    ImpactManagerInstance.ProcessInteraction(interactionData, impactObject);
                }
            }
            else if (ImpactManagerInstance.UseMaterialMapping)
            {
                IImpactMaterial material;
                if (ImpactManagerInstance.TryGetImpactMaterialFromMapping(physicsMaterialId, out material))
                {
                    interactionData.TagMask = material.MaterialTagsMask;
                }

                ImpactManagerInstance.ProcessInteraction(interactionData, impactObject);
            }
        }
コード例 #8
0
 /// <summary>
 /// Initialize the material composition data.
 /// </summary>
 /// <param name="material">The Impact Material the composition represents.</param>
 /// <param name="compositionValue">The 0 to 1 value representing the influence the material has.</param>
 public ImpactMaterialComposition(IImpactMaterial material, float compositionValue)
 {
     Material         = material;
     CompositionValue = compositionValue;
 }
コード例 #9
0
        /// <summary>
        /// Process a continuous interaction using the interaction data, an Impact Material, and an optional Impact Object that the interaction originated from.
        /// </summary>
        /// <param name="interactionData">The interaction data to process.</param>
        /// <param name="impactMaterial">The Impact Material to get interaction results from.</param>
        /// <param name="impactObject">An optional Impact Object that the interaction originated from.</param>
        public static void ProcessContinuousInteraction <T>(T interactionData, IImpactMaterial material, IImpactObject impactObject) where T : IInteractionData
        {
            ImpactManager instance = GetInstance();

            instance.ProcessContinuousInteraction(interactionData, material, impactObject);
        }
コード例 #10
0
        /// <summary>
        /// Try to get an Impact Material from the Material Mapping using the given Physics Material instance ID.
        /// </summary>
        /// <param name="physicsMaterialInstanceId">The instance ID of the physics material (3D or 2D).</param>
        /// <param name="impactMaterial">The material that was found in the mapping, if one was found. Null otherwise.</param>
        /// <returns>True if a matching map was found, False otherwise.</returns>
        public static bool TryGetImpactMaterialFromMapping(int physicsMaterialInstanceId, out IImpactMaterial impactMaterial)
        {
            ImpactManager instance = GetInstance();

            return(instance.TryGetImpactMaterialFromMapping(physicsMaterialInstanceId, out impactMaterial));
        }
コード例 #11
0
        /// <summary>
        /// Try to get an Impact Material from the Material Mapping using the given Collider2D.
        /// </summary>
        /// <param name="collider2d">The collider that has the Physics Material 2D to get a mapping for.</param>
        /// <param name="impactMaterial">The material that was found in the mapping, if one was found. Null otherwise.</param>
        /// <returns>True if a matching map was found, False otherwise.</returns>
        public static bool TryGetImpactMaterialFromMapping(Collider2D collider2d, out IImpactMaterial impactMaterial)
        {
            ImpactManager instance = GetInstance();

            return(instance.TryGetImpactMaterialFromMapping(collider2d, out impactMaterial));
        }