private void AddSingleBlends(SurfaceBlends.NormalizedBlends blendResults)
        {
            float totalWeight = 0;

            for (int i = 0; i < blendResults.result.Count; i++) ////int blendCount = Mathf.Min(blendResults.result.Count, maxOutputCount + 1);
            {
                var blend = blendResults.result[i];

                AddBlend(blend, false, 1, ref totalWeight);
            }

            outputs.SortDescending();
            NormalizeOutputs(totalWeight);
        }
Exemplo n.º 2
0
        //Methods
        private void Add(SurfaceData sd, SurfaceBlends.NormalizedBlends blendResults, Color tint, float weightMultiplier, ref float totalWeight)
        {
            if (weightMultiplier <= 0.0000000001f)
            {
                return;
            }

            for (int i = 0; i < blendResults.result.Count; i++)
            {
                var blend = blendResults.result[i];
                blend.color *= tint;
                sd.AddBlend(blend, false, weightMultiplier, ref totalWeight);
            }
        }
        private void AddNonTerrainSurfaceTypes(Collider collider, Vector3 worldPosition, int triangleIndex = -1)
        {
            //Markers
            var  marker     = collider.GetComponent <Marker>();
            bool anyMarkers = !object.Equals(marker, null); // != null;

            if (anyMarkers)
            {
                //Vibration Sound Marker
                if (marker.GetMarker(out VibrationMarker vibrationMarker))
                {
                    outputs.vibrationSound = vibrationMarker.vibrationSound;
                }
            }

            //MeshRenderers
            bool hasMR = Marker.GetMR(anyMarkers, marker, collider.gameObject, out MeshRenderer mr); //!mr.Equals(null))//mr != null) //var mr = ; //This is (I think) more performant // collider.GetComponent<MeshRenderer>();

            if (hasMR)
            {
                //The collider is a non-convex meshCollider. We can find the triangle index.
                if (triangleIndex != -1 && collider is MeshCollider mc && !mc.convex)
                {
                    var mesh      = Marker.GetMF(anyMarkers, marker, collider.gameObject).sharedMesh; //could use the marker to find this faster
                    int subMeshID = anyMarkers ? Utility.GetSubmesh(marker.submeshes, triangleIndex) : Utility.GetSubmesh(mesh, triangleIndex);

                    SurfaceBlends.NormalizedBlends blendResults = null;
                    if (anyMarkers)
                    {
                        //Blend Map Overrides
                        if (marker.GetMarker(out SurfaceBlendMapMarker blendMap))
                        {
                            if (blendMap.TryAddBlends(this, mesh, subMeshID, worldPosition, triangleIndex, out float totalWeight))
                            {
                                outputs.SortDescending();
                                NormalizeOutputs(totalWeight);
                                return;
                            }
                        }

                        //Blend Overrides
                        if (blendResults == null && marker.GetMarker(out SurfaceBlendOverridesMarker blendOverrides))
                        {
                            if (blendOverrides.GetOverride(subMeshID, out BlendOverride o))
                            {
                                blendResults = o.blends.result;
                            }
                        }
                    }

                    List <Material> GetMaterials()
                    {
                        if (anyMarkers)
                        {
                            return(marker.sharedMaterials); //If it has a marker, it doesn't need to spend time copying the list every time
                        }
                        materials.Clear();
                        mr.GetSharedMaterials(materials);
                        return(materials);
                    }

                    if (blendResults == null)
                    {
                        List <Material> materials = GetMaterials();
                        if (materialBlendLookup.TryGetValue(materials[subMeshID], out SurfaceBlends.NormalizedBlends value))
                        {
                            blendResults = value;
                        }
                    }

                    if (blendResults != null)
                    {
                        AddSingleBlends(blendResults);
                        return;
                    }
                    else
                    {
                        List <Material> materials = GetMaterials();

                        //Adds based on keywords
                        if (TryGetStringSurfaceType(materials[subMeshID].name, out int stID, out SurfaceType st, out SurfaceType.SubType subType, false))
                        {
                            AddSingleOutput(stID, st, subType);
                        }
                        return;
                    }
                }
            }

            //Single Markers
            if (anyMarkers)
            {
                if (marker.GetMarker(out SurfaceTypeMarker typeMarker))
                {
                    //Single Type Marker
                    if (TryGetStringSurfaceType(typeMarker.lowerReference, out int stID, out SurfaceType st, out SurfaceType.SubType subType, true))
                    {
                        AddSingleOutput(stID, st, subType);
                    }
                    return;
                }
                else if (marker.GetMarker(out SurfaceBlendMarker blendMarker))
                {
                    //Single Blend Marker
                    AddSingleBlends(blendMarker.blends.result);
                    return;
                }
            }

            //Defaults to the first material (For most colliders it can't be discerned which specific material it is)
            if (hasMR)
            {
                var mat = mr.sharedMaterial;

                //Blend Lookup
                if (materialBlendLookup.TryGetValue(mat, out SurfaceBlends.NormalizedBlends value))
                {
                    AddSingleBlends(value);
                    return;
                }

                //Name
                if (TryGetStringSurfaceType(mat.name, out int stID, out SurfaceType st, out SurfaceType.SubType subType, false))
                {
                    AddSingleOutput(stID, st, subType);
                }
                return;
            }
        }