private float[] GetTextureMix(FootStepObject footStepObj)
        {
            // returns an array containing the relative mix of textures
            // on the main terrain at this world position.

            // The number of values in the array will equal the number
            // of textures added to the terrain.

            UpdateTerrainInfo(footStepObj.terrain);

            // calculate which splat map cell the worldPos falls within (ignoring y)
            var worldPos = footStepObj.sender.position;
            int mapX     = (int)(((worldPos.x - terrainPos.x) / terrainData.size.x) * terrainData.alphamapWidth);
            int mapZ     = (int)(((worldPos.z - terrainPos.z) / terrainData.size.z) * terrainData.alphamapHeight);

            // get the splat data for this cell as a 1x1xN 3d array (where N = number of textures)
            if (!terrainCollider.bounds.Contains(worldPos))
            {
                return(new float[0]);
            }
            float[,,] splatmapData = terrainData.GetAlphamaps(mapX, mapZ, 1, 1);

            // extract the 3D array data to a 1D array:
            float[] cellMix = new float[splatmapData.GetUpperBound(2) + 1];

            for (int n = 0; n < cellMix.Length; n++)
            {
                cellMix[n] = splatmapData[0, 0, n];
            }
            return(cellMix);
        }
示例#2
0
        /// <summary>
        /// Step on Terrain
        /// </summary>
        /// <param name="footStepObject"></param>
        public void StepOnTerrain(FootStepObject footStepObject)
        {
            if (currentStep != null && currentStep == footStepObject.sender)
            {
                return;
            }
            currentStep  = footStepObject.sender;
            surfaceIndex = GetMainTexture(footStepObject);

            if (surfaceIndex != -1)
            {
#if UNITY_2018_3_OR_NEWER
                var name = (terrainData != null && terrainData.terrainLayers.Length > 0) ? (terrainData.terrainLayers[surfaceIndex]).diffuseTexture.name : "";
#else
                var name = (terrainData != null && terrainData.splatPrototypes.Length > 0) ? (terrainData.splatPrototypes[surfaceIndex]).texture.name : "";
#endif
                footStepObject.name = name;
                PlayFootFallSound(footStepObject, spawnParticle, spawnStepMark, volume);

                if (debugTextureName)
                {
                    Debug.Log(terrain.name + " " + name);
                }
            }
        }
        private int GetMainTexture(FootStepObject footStepObj)
        {
            // returns the zero-based index of the most dominant texture
            // on the main terrain at this world position.
            float[] mix = GetTextureMix(footStepObj);

            if (mix == null)
            {
                return(-1);
            }

            float maxMix   = 0;
            int   maxIndex = 0;

            // loop through each mix value and find the maximum
            for (int n = 0; n < mix.Length; n++)
            {
                if (mix[n] > maxMix)
                {
                    maxIndex = n;
                    maxMix   = mix[n];
                }
            }
            return(maxIndex);
        }
示例#4
0
        /// <summary>
        /// Step on Terrain
        /// </summary>
        /// <param name="footStepObject"></param>
        public void StepOnTerrain(FootStepObject footStepObject)
        {
            if (currentStep != null && currentStep == footStepObject.sender)
            {
                return;
            }
            currentStep = footStepObject.sender;

            if (terrainData)
            {
                surfaceIndex = GetMainTexture(footStepObject.sender.position);
            }

            //bool valid = (terrainData != null && terrainData.splatPrototypes != null && terrainData.splatPrototypes.Length > 0
            //&& terrainData.splatPrototypes.Length <= surfaceIndex && terrainData.splatPrototypes[surfaceIndex].texture != null);
            var name = (terrainData != null && terrainData.splatPrototypes.Length > 0) ? (terrainData.splatPrototypes[surfaceIndex]).texture.name : "";

            footStepObject.name = name;
            PlayFootFallSound(footStepObject);

            if (debugTextureName)
            {
                Debug.Log(name);
            }
        }
 /// <summary>
 /// Step on Mesh
 /// </summary>
 /// <param name="footStepObject"></param>
 public void StepOnMesh(FootStepObject footStepObject)
 {
     if (currentStep != null && currentStep == footStepObject.sender)
     {
         return;
     }
     currentStep = footStepObject.sender;
     PlayFootFallSound(footStepObject);
     if (debugTextureName)
     {
         Debug.Log(footStepObject.name);
     }
 }
示例#6
0
        void StepMark(FootStepObject footStep)
        {
            RaycastHit hit;

            if (Physics.Raycast(footStep.sender.transform.position + new Vector3(0, 0.1f, 0), -footStep.sender.up, out hit, 1f, stepLayer))
            {
                if (stepMark)
                {
                    var angle = Quaternion.FromToRotation(footStep.sender.up, hit.normal);
                    var step  = Instantiate(stepMark, hit.point, angle * footStep.sender.rotation) as GameObject;
                    //step.transform.SetParent(footStep.ground.transform);
                    Destroy(step, timeToDestroy);
                }
            }
        }
 public void PlayFootFallSound(FootStepObject footStepObject, bool spawnParticle = true, bool spawnStepMark = true, float volume = 1f)
 {
     for (int i = 0; i < customSurfaces.Count; i++)
     {
         if (customSurfaces[i] != null && ContainsTexture(footStepObject.name, customSurfaces[i]))
         {
             customSurfaces[i].PlayRandomClip(footStepObject, spawnParticle, spawnStepMark, volume);
             return;
         }
     }
     if (defaultSurface != null)
     {
         defaultSurface.PlayRandomClip(footStepObject, spawnParticle, spawnStepMark, volume);
     }
 }
示例#8
0
 public void PlayFootFallSound(FootStepObject footStepObject)
 {
     for (int i = 0; i < customSurfaces.Count; i++)
     {
         if (customSurfaces[i] != null && ContainsTexture(footStepObject.name, customSurfaces[i]))
         {
             customSurfaces[i].PlayRandomClip(footStepObject);
             return;
         }
     }
     if (defaultSurface != null)
     {
         defaultSurface.PlayRandomClip(footStepObject);
     }
 }
        public void PlayRandomClip(FootStepObject footStepObject)
        {
            // if there are no clips to play return.
            if (audioClips == null || audioClips.Count == 0)
            {
                return;
            }

            // initialize variable if not already started
            if (randomSource == null)
            {
                randomSource = new vFisherYatesRandom();
            }

            // find a random clip and play it.
            GameObject audioObject = null;

            if (audioSource != null)
            {
                audioObject = Instantiate(audioSource.gameObject, footStepObject.sender.position, Quaternion.identity) as GameObject;
            }
            else
            {
                audioObject = new GameObject("audioObject");
                audioObject.transform.position = footStepObject.sender.position;
            }

            var source = audioObject.AddComponent <vAudioSurfaceControl>();

            if (audioMixerGroup != null)
            {
                source.outputAudioMixerGroup = audioMixerGroup;
            }
            int index = randomSource.Next(audioClips.Count);

            if (particleObject && footStepObject.ground && stepLayer.ContainsLayer(footStepObject.ground.gameObject.layer))
            {
                var obj = Instantiate(particleObject, footStepObject.sender.position, footStepObject.sender.rotation) as GameObject;
                obj.transform.SetParent(vObjectContainer.root, true);
            }
            if (useStepMark)
            {
                StepMark(footStepObject);
            }

            source.PlayOneShot(audioClips[index]);
        }
        /// <summary>
        /// Step on Terrain
        /// </summary>
        /// <param name="footStepObject"></param>
        public void StepOnTerrain(FootStepObject footStepObject)
        {
            if (currentStep != null && currentStep == footStepObject.sender)
            {
                return;
            }
            currentStep  = footStepObject.sender;
            surfaceIndex = GetMainTexture(footStepObject);

            if (surfaceIndex != -1)
            {
                var name = (terrainData != null && terrainData.splatPrototypes.Length > 0) ? (terrainData.splatPrototypes[surfaceIndex]).texture.name : "";
                footStepObject.name = name;
                PlayFootFallSound(footStepObject);

                if (debugTextureName)
                {
                    Debug.Log(terrain.name + " " + name);
                }
            }
        }
示例#11
0
        void OnTriggerEnter(Collider other)
        {
            if (_fT == null)
            {
                return;
            }

            if ((lastCollider == null || lastCollider != other) || footstepObj == null)
            {
                footstepObj  = new FootStepObject(transform, other);
                lastCollider = other;
            }

            if (footstepObj.isTerrain) //Check if trigger objet is a terrain
            {
                _fT.StepOnTerrain(footstepObj);
            }
            else
            {
                _fT.StepOnMesh(footstepObj);
            }
        }