예제 #1
0
    /// Generates an acoustic mesh from a source object's mesh filter. Returns null if the
    /// generation failed.
    public static ResonanceAudioAcousticMesh GenerateFromMeshFilter(MeshFilter meshFilter,
                                                                    Shader surfaceMaterialShader)
    {
        var sourceObject       = meshFilter.gameObject;
        var sourceMesh         = meshFilter.sharedMesh;
        var sourceTriangles    = sourceMesh.triangles;
        int numTriangleIndices = sourceTriangles.Length;
        int numVertices        = sourceMesh.vertexCount;

        ResonanceAudioAcousticMesh acousticMesh = new ResonanceAudioAcousticMesh();

        int[]     triangles = null;
        Vector3[] vertices  = null;
        acousticMesh.InitializeMesh(numTriangleIndices, numVertices, out triangles, out vertices);

        // Duplicate the source object's mesh. The vertices are transformed to world space.
        acousticMesh.FillVerticesAndTrianglesFromMesh(sourceMesh, sourceObject.transform, ref vertices,
                                                      ref triangles);

        acousticMesh.mesh.vertices  = vertices;
        acousticMesh.mesh.triangles = triangles;
        acousticMesh.mesh.RecalculateNormals();

        acousticMesh.InitializeSubMeshMaterials();
        acousticMesh.InitializeVisualizationMaterial(surfaceMaterialShader);

        acousticMesh.sourceObject = sourceObject;
        return(acousticMesh);
    }
예제 #2
0
    /// Generates an acoustic mesh from a terrain.
    public static ResonanceAudioAcousticMesh GenerateFromTerrain(Terrain terrain,
                                                                 Shader surfaceMaterialShader)
    {
        var terrainData = terrain.terrainData;
        var heightMap   = terrainData.GetHeights(0, 0, terrainData.heightmapResolution,
                                                 terrainData.heightmapResolution);

        // First sub-sample the height map.
        int m;
        int n;
        int subSampledNumTriangleIndices;
        int subSampleStep;

        SubSampleHeightMap(heightMap.GetLength(0), heightMap.GetLength(1), out m, out n,
                           out subSampleStep, out subSampledNumTriangleIndices);

        ResonanceAudioAcousticMesh acousticMesh = new ResonanceAudioAcousticMesh();

        int[]     triangles;
        Vector3[] vertices;
        acousticMesh.InitializeMesh(subSampledNumTriangleIndices, subSampledNumTriangleIndices,
                                    out triangles, out vertices);

        // Create triangles and vertices from the height map. The vertices are transformed to world
        // space.
        acousticMesh.FillTrianglesAndVerticesFromHeightMap(
            terrain.transform.position, terrainData.size, heightMap, m, n, subSampleStep,
            ref triangles, ref vertices);

        acousticMesh.mesh.vertices  = vertices;
        acousticMesh.mesh.triangles = triangles;
        acousticMesh.mesh.RecalculateNormals();

        acousticMesh.InitializeSubMeshMaterials();
        acousticMesh.InitializeVisualizationMaterial(surfaceMaterialShader);

        acousticMesh.sourceObject = terrain.gameObject;
        return(acousticMesh);
    }
예제 #3
0
    /// Generates an acoustic mesh from a source object's mesh filter. Returns null if the
    /// generation failed.
    public static ResonanceAudioAcousticMesh GenerateFromMeshFilter(MeshFilter meshFilter,
                                                                    Shader surfaceMaterialShader)
    {
        var sourceObject = meshFilter.gameObject;
        var sourceMesh   = meshFilter.sharedMesh;

        if (sourceMesh == null)
        {
            Debug.LogWarning("GameObject: " + sourceObject.name + " has no mesh and will not be " +
                             "included in reverb baking.");
            return(null);
        }

        int numTriangleIndices = CountTriangleIndices(sourceMesh);
        int numVertices        = sourceMesh.vertexCount;

        ResonanceAudioAcousticMesh acousticMesh = new ResonanceAudioAcousticMesh();

        int[]     triangles = null;
        Vector3[] vertices  = null;
        acousticMesh.InitializeMesh(numTriangleIndices, numVertices, out triangles, out vertices);

        // Duplicate the source object's mesh. The vertices are transformed to world space.
        acousticMesh.FillVerticesAndTrianglesFromMesh(sourceMesh, sourceObject.transform, ref vertices,
                                                      ref triangles);

        acousticMesh.mesh.vertices  = vertices;
        acousticMesh.mesh.triangles = triangles;
        acousticMesh.mesh.RecalculateNormals();

        acousticMesh.InitializeSubMeshMaterials();
        acousticMesh.InitializeVisualizationMaterial(surfaceMaterialShader);

        acousticMesh.sourceObject = sourceObject;
        return(acousticMesh);
    }