Exemplo n.º 1
0
 private static void SoundProof(ref ONSPPropagationMaterial material)
 {
     material.absorption.points = new List <Point> {
         new Point(1000f, 1.0f)
     };
     material.scattering.points = new List <Point> {
         new Point(1000f, 0.0f)
     };
     material.transmission.points = new List <Point>();
 }
    private void GetSpectra(out SpectrumDrawer absorption,
                            out SpectrumDrawer scattering,
                            out SpectrumDrawer transmission)
    {
        ONSPPropagationMaterial material = target as ONSPPropagationMaterial;

        absorption   = new SpectrumDrawer("Absorption", material.absorption, AxisScale.Sqr);
        scattering   = new SpectrumDrawer("Scattering", material.scattering, AxisScale.Lin);
        transmission = new SpectrumDrawer("Transmission", material.transmission, AxisScale.Sqr);
    }
Exemplo n.º 3
0
    private static void Grass(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .11f), new Point(250f, .26f), new Point(500f, .60f), new Point(1000f, .69f), new Point(2000f, .92f), new Point(4000f, .99f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .30f), new Point(250f, .30f), new Point(500f, .40f), new Point(1000f, .50f), new Point(2000f, .60f), new Point(4000f, .70f)
        };

        material.transmission.points = new List <Point>();
    }
Exemplo n.º 4
0
    private static void Snow(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .45f), new Point(250f, .75f), new Point(500f, .90f), new Point(1000f, .95f), new Point(2000f, .95f), new Point(4000f, .95f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .20f), new Point(250f, .30f), new Point(500f, .40f), new Point(1000f, .50f), new Point(2000f, .60f), new Point(4000f, .75f)
        };

        material.transmission.points = new List <Point>();
    }
Exemplo n.º 5
0
    private static void Curtain(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .07f), new Point(250f, .31f), new Point(500f, .49f), new Point(1000f, .75f), new Point(2000f, .70f), new Point(4000f, .60f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .15f), new Point(500f, .2f), new Point(1000f, .3f), new Point(2000f, .4f), new Point(4000f, .5f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .42f), new Point(250f, .39f), new Point(500f, .21f), new Point(1000f, .14f), new Point(2000f, .079f), new Point(4000f, .045f)
        };
    }
Exemplo n.º 6
0
    private static void ConcreteBlockPainted(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .05f), new Point(500f, .06f), new Point(1000f, .07f), new Point(2000f, .09f), new Point(4000f, .08f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .11f), new Point(500f, .13f), new Point(1000f, .15f), new Point(2000f, .16f), new Point(4000f, .20f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .02f), new Point(250f, .01f), new Point(500f, .0063f), new Point(1000f, .0035f), new Point(2000f, .00011f), new Point(4000f, .00063f)
        };
    }
Exemplo n.º 7
0
    private static void CeramicTile(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .01f), new Point(250f, .01f), new Point(500f, .01f), new Point(1000f, .01f), new Point(2000f, .02f), new Point(4000f, .02f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .12f), new Point(500f, .14f), new Point(1000f, .16f), new Point(2000f, .18f), new Point(4000f, .20f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .004f), new Point(250f, .0079f), new Point(500f, .0056f), new Point(1000f, .0016f), new Point(2000f, .0014f), new Point(4000f, .0005f)
        };
    }
Exemplo n.º 8
0
    private static void CarpetHeavyPadded(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .08f), new Point(250f, .24f), new Point(500f, .57f), new Point(1000f, .69f), new Point(2000f, .71f), new Point(4000f, .73f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .15f), new Point(500f, .20f), new Point(1000f, .25f), new Point(2000f, .35f), new Point(4000f, .50f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .004f), new Point(250f, .0079f), new Point(500f, .0056f), new Point(1000f, .0016f), new Point(2000f, .0014f), new Point(4000f, .0005f)
        };
    }
Exemplo n.º 9
0
    //***********************************************************************

    private static void AcousticTile(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .50f), new Point(250f, .70f), new Point(500f, .60f), new Point(1000f, .70f), new Point(2000f, .70f), new Point(4000f, .50f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .15f), new Point(500f, .20f), new Point(1000f, .20f), new Point(2000f, .25f), new Point(4000f, .30f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .05f), new Point(250f, .04f), new Point(500f, .03f), new Point(1000f, .02f), new Point(2000f, .005f), new Point(4000f, .002f)
        };
    }
Exemplo n.º 10
0
    private static void Foliage(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .03f), new Point(250f, .06f), new Point(500f, .11f), new Point(1000f, .17f), new Point(2000f, .27f), new Point(4000f, .31f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .20f), new Point(250f, .3f), new Point(500f, .4f), new Point(1000f, .5f), new Point(2000f, .7f), new Point(4000f, .8f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .9f), new Point(250f, .9f), new Point(500f, .9f), new Point(1000f, .8f), new Point(2000f, .5f), new Point(4000f, .3f)
        };
    }
Exemplo n.º 11
0
    private static void Glass(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .35f), new Point(250f, .25f), new Point(500f, .18f), new Point(1000f, .12f), new Point(2000f, .07f), new Point(4000f, .05f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .05f), new Point(250f, .05f), new Point(500f, .05f), new Point(1000f, .05f), new Point(2000f, .05f), new Point(4000f, .05f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .125f), new Point(250f, .089f), new Point(500f, .05f), new Point(1000f, .028f), new Point(2000f, .022f), new Point(4000f, .079f)
        };
    }
Exemplo n.º 12
0
    private static void WoodOnConcrete(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .04f), new Point(250f, .04f), new Point(500f, .07f), new Point(1000f, .06f), new Point(2000f, .06f), new Point(4000f, .07f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .10f), new Point(500f, .10f), new Point(1000f, .10f), new Point(2000f, .10f), new Point(4000f, .15f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .004f), new Point(250f, .0079f), new Point(500f, .0056f), new Point(1000f, .0016f), new Point(2000f, .0014f), new Point(4000f, .0005f)
        };
    }
Exemplo n.º 13
0
    private static void WoodFloor(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .15f), new Point(250f, .11f), new Point(500f, .10f), new Point(1000f, .07f), new Point(2000f, .06f), new Point(4000f, .07f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .10f), new Point(500f, .10f), new Point(1000f, .10f), new Point(2000f, .10f), new Point(4000f, .15f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .071f), new Point(250f, .025f), new Point(500f, .0158f), new Point(1000f, .0056f), new Point(2000f, .0035f), new Point(4000f, .0016f)
        };
    }
Exemplo n.º 14
0
    private static void WoodThin(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .42f), new Point(250f, .21f), new Point(500f, .10f), new Point(1000f, .08f), new Point(2000f, .06f), new Point(4000f, .06f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .10f), new Point(500f, .10f), new Point(1000f, .10f), new Point(2000f, .10f), new Point(4000f, .15f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .2f), new Point(250f, .125f), new Point(500f, .079f), new Point(1000f, .1f), new Point(2000f, .089f), new Point(4000f, .05f)
        };
    }
Exemplo n.º 15
0
    private static void WoodThick(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .19f), new Point(250f, .14f), new Point(500f, .09f), new Point(1000f, .06f), new Point(2000f, .06f), new Point(4000f, .05f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .10f), new Point(500f, .10f), new Point(1000f, .10f), new Point(2000f, .10f), new Point(4000f, .15f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .035f), new Point(250f, .028f), new Point(500f, .028f), new Point(1000f, .028f), new Point(2000f, .011f), new Point(4000f, .0071f)
        };
    }
Exemplo n.º 16
0
    private static void PlasterOnConcreteBlock(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .12f), new Point(250f, .09f), new Point(500f, .07f), new Point(1000f, .05f), new Point(2000f, .05f), new Point(4000f, .04f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .20f), new Point(250f, .25f), new Point(500f, .30f), new Point(1000f, .35f), new Point(2000f, .40f), new Point(4000f, .45f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .02f), new Point(250f, .01f), new Point(500f, .0063f), new Point(1000f, .0035f), new Point(2000f, .00011f), new Point(4000f, .00063f)
        };
    }
Exemplo n.º 17
0
    private static void Water(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .01f), new Point(250f, .01f), new Point(500f, .01f), new Point(1000f, .02f), new Point(2000f, .02f), new Point(4000f, .03f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .10f), new Point(500f, .10f), new Point(1000f, .07f), new Point(2000f, .05f), new Point(4000f, .05f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .03f), new Point(250f, .03f), new Point(500f, .03f), new Point(1000f, .02f), new Point(2000f, .015f), new Point(4000f, .01f)
        };
    }
Exemplo n.º 18
0
    private static void PlasterOnBrick(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .01f), new Point(250f, .02f), new Point(500f, .02f), new Point(1000f, .03f), new Point(2000f, .04f), new Point(4000f, .05f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .20f), new Point(250f, .25f), new Point(500f, .30f), new Point(1000f, .35f), new Point(2000f, .40f), new Point(4000f, .45f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .025f), new Point(250f, .019f), new Point(500f, .01f), new Point(1000f, .0045f), new Point(2000f, .0018f), new Point(4000f, .00089f)
        };
    }
Exemplo n.º 19
0
    private static void GypsumBoard(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .29f), new Point(250f, .10f), new Point(500f, .05f), new Point(1000f, .04f), new Point(2000f, .07f), new Point(4000f, .09f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .10f), new Point(250f, .11f), new Point(500f, .12f), new Point(1000f, .13f), new Point(2000f, .14f), new Point(4000f, .15f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .035f), new Point(250f, .0125f), new Point(500f, .0056f), new Point(1000f, .0025f), new Point(2000f, .0013f), new Point(4000f, .0032f)
        };
    }
Exemplo n.º 20
0
    private static void GlassHeavy(ref ONSPPropagationMaterial material)
    {
        material.absorption.points = new List <Point> {
            new Point(125f, .18f), new Point(250f, .06f), new Point(500f, .04f), new Point(1000f, .03f), new Point(2000f, .02f), new Point(4000f, .02f)
        };

        material.scattering.points = new List <Point> {
            new Point(125f, .05f), new Point(250f, .05f), new Point(500f, .05f), new Point(1000f, .05f), new Point(2000f, .05f), new Point(4000f, .05f)
        };

        material.transmission.points = new List <Point>()
        {
            new Point(125f, .056f), new Point(250f, .039f), new Point(500f, .028f), new Point(1000f, .02f), new Point(2000f, .032f), new Point(4000f, .014f)
        };
    }
Exemplo n.º 21
0
    public override void OnInspectorGUI()
    {
        ONSPPropagationMaterial material = target as ONSPPropagationMaterial;

        EditorGUI.BeginChangeCheck();

        Rect r = EditorGUILayout.GetControlRect();

        r.width -= rightMargin;

        ONSPPropagationMaterial.Preset newPreset =
            (ONSPPropagationMaterial.Preset)EditorGUI.EnumPopup(r, "Preset", material.preset);

        Event     e    = Event.current;
        EventType type = e.type;

        absorption.Draw(e);
        scattering.Draw(e);
        transmission.Draw(e);

        if (EditorGUI.EndChangeCheck())
        {
            string groupName = Undo.GetCurrentGroupName();

            Undo.RegisterCompleteObjectUndo(material, groupName);

            if (groupName == "Point Added")
            {
                Undo.CollapseUndoOperations(Undo.GetCurrentGroup() - 1);
            }

            if (material.preset != newPreset)
            {
                material.preset = newPreset;
            }
            else
            {
                material.preset = ONSPPropagationMaterial.Preset.Custom;
            }

            if (Application.isPlaying)
            {
                material.UploadMaterial();
            }
        }
    }
Exemplo n.º 22
0
    private int uploadMesh(IntPtr geometryHandle, GameObject meshObject, Matrix4x4 worldToLocal, bool ignoreStatic, ref int ignoredMeshCount)
    {
        // Get the child mesh objects.
        List <MeshMaterial>    meshes   = new List <MeshMaterial>();
        List <TerrainMaterial> terrains = new List <TerrainMaterial>();

        traverseMeshHierarchy(meshObject, null, includeChildMeshes, meshes, terrains, ignoreStatic, ref ignoredMeshCount);

#if INCLUDE_TERRAIN_TREES
        // TODO: expose tree material
        ONSPPropagationMaterial[] treeMaterials = new ONSPPropagationMaterial[1];
        treeMaterials[0] = gameObject.AddComponent <ONSPPropagationMaterial>();

#if true
        treeMaterials[0].SetPreset(ONSPPropagationMaterial.Preset.Foliage);
#else
        // Custom material that is highly transmissive
        treeMaterials[0].absorption.points = new List <ONSPPropagationMaterial.Point> {
            new ONSPPropagationMaterial.Point(125f, .03f),
            new ONSPPropagationMaterial.Point(250f, .06f),
            new ONSPPropagationMaterial.Point(500f, .11f),
            new ONSPPropagationMaterial.Point(1000f, .17f),
            new ONSPPropagationMaterial.Point(2000f, .27f),
            new ONSPPropagationMaterial.Point(4000f, .31f)
        };

        treeMaterials[0].scattering.points = new List <ONSPPropagationMaterial.Point> {
            new ONSPPropagationMaterial.Point(125f, .20f),
            new ONSPPropagationMaterial.Point(250f, .3f),
            new ONSPPropagationMaterial.Point(500f, .4f),
            new ONSPPropagationMaterial.Point(1000f, .5f),
            new ONSPPropagationMaterial.Point(2000f, .7f),
            new ONSPPropagationMaterial.Point(4000f, .8f)
        };

        treeMaterials[0].transmission.points = new List <ONSPPropagationMaterial.Point>()
        {
            new ONSPPropagationMaterial.Point(125f, .95f),
            new ONSPPropagationMaterial.Point(250f, .92f),
            new ONSPPropagationMaterial.Point(500f, .87f),
            new ONSPPropagationMaterial.Point(1000f, .81f),
            new ONSPPropagationMaterial.Point(2000f, .71f),
            new ONSPPropagationMaterial.Point(4000f, .67f)
        };
#endif
#endif

        //***********************************************************************
        // Count the number of vertices and indices.

        int  totalVertexCount   = 0;
        uint totalIndexCount    = 0;
        int  totalFaceCount     = 0;
        int  totalMaterialCount = 0;

        foreach (MeshMaterial m in meshes)
        {
            updateCountsForMesh(ref totalVertexCount, ref totalIndexCount, ref totalFaceCount, ref totalMaterialCount, m.meshFilter.sharedMesh);
        }

        for (int i = 0; i < terrains.Count; ++i)
        {
            TerrainMaterial t = terrains[i];
        }

        //***********************************************************************
        // Copy the mesh data.

        List <Vector3> tempVertices = new List <Vector3>();
        List <int>     tempIndices  = new List <int>();

        MeshGroup[] groups   = new MeshGroup[totalMaterialCount];
        float[]     vertices = new float[totalVertexCount * 3];
        int[]       indices  = new int[totalIndexCount];

        int vertexOffset = 0;
        int indexOffset  = 0;
        int groupOffset  = 0;

        foreach (MeshMaterial m in meshes)
        {
            MeshFilter meshFilter = m.meshFilter;

            // Compute the combined transform to go from mesh-local to geometry-local space.
            Matrix4x4 matrix = worldToLocal * meshFilter.gameObject.transform.localToWorldMatrix;

            uploadMeshFilter(tempVertices, tempIndices, groups, vertices, indices, ref vertexOffset, ref indexOffset, ref groupOffset, meshFilter.sharedMesh, m.materials, matrix);
        }


        // Upload mesh data
        return(ONSPPropagation.Interface.AudioGeometryUploadMeshArrays(geometryHandle,
                                                                       vertices, totalVertexCount,
                                                                       indices, indices.Length,
                                                                       groups, groups.Length));
    }
Exemplo n.º 23
0
    private int uploadMesh(IntPtr geometryHandle, GameObject meshObject, Matrix4x4 worldToLocal, bool ignoreStatic, ref int ignoredMeshCount)
    {
        // Get the child mesh objects.
        List <MeshMaterial>    meshes   = new List <MeshMaterial>();
        List <TerrainMaterial> terrains = new List <TerrainMaterial>();

        traverseMeshHierarchy(meshObject, null, includeChildMeshes, meshes, terrains, ignoreStatic, ref ignoredMeshCount);

#if INCLUDE_TERRAIN_TREES
        // TODO: expose tree material
        ONSPPropagationMaterial[] treeMaterials = new ONSPPropagationMaterial[1];
        treeMaterials[0] = gameObject.AddComponent <ONSPPropagationMaterial>();

#if true
        treeMaterials[0].SetPreset(ONSPPropagationMaterial.Preset.Foliage);
#else
        // Custom material that is highly transmissive
        treeMaterials[0].absorption.points = new List <ONSPPropagationMaterial.Point> {
            new ONSPPropagationMaterial.Point(125f, .03f),
            new ONSPPropagationMaterial.Point(250f, .06f),
            new ONSPPropagationMaterial.Point(500f, .11f),
            new ONSPPropagationMaterial.Point(1000f, .17f),
            new ONSPPropagationMaterial.Point(2000f, .27f),
            new ONSPPropagationMaterial.Point(4000f, .31f)
        };

        treeMaterials[0].scattering.points = new List <ONSPPropagationMaterial.Point> {
            new ONSPPropagationMaterial.Point(125f, .20f),
            new ONSPPropagationMaterial.Point(250f, .3f),
            new ONSPPropagationMaterial.Point(500f, .4f),
            new ONSPPropagationMaterial.Point(1000f, .5f),
            new ONSPPropagationMaterial.Point(2000f, .7f),
            new ONSPPropagationMaterial.Point(4000f, .8f)
        };

        treeMaterials[0].transmission.points = new List <ONSPPropagationMaterial.Point>()
        {
            new ONSPPropagationMaterial.Point(125f, .95f),
            new ONSPPropagationMaterial.Point(250f, .92f),
            new ONSPPropagationMaterial.Point(500f, .87f),
            new ONSPPropagationMaterial.Point(1000f, .81f),
            new ONSPPropagationMaterial.Point(2000f, .71f),
            new ONSPPropagationMaterial.Point(4000f, .67f)
        };
#endif
#endif

        //***********************************************************************
        // Count the number of vertices and indices.

        int  totalVertexCount   = 0;
        uint totalIndexCount    = 0;
        int  totalFaceCount     = 0;
        int  totalMaterialCount = 0;

        foreach (MeshMaterial m in meshes)
        {
            updateCountsForMesh(ref totalVertexCount, ref totalIndexCount, ref totalFaceCount, ref totalMaterialCount, m.meshFilter.sharedMesh);
        }

        for (int i = 0; i < terrains.Count; ++i)
        {
            TerrainMaterial t       = terrains[i];
            TerrainData     terrain = t.terrain.terrainData;

#if UNITY_2019_3_OR_NEWER
            int w = terrain.heightmapResolution;
            int h = terrain.heightmapResolution;
#else
            int w = terrain.heightmapWidth;
            int h = terrain.heightmapHeight;
#endif
            int wRes        = (w - 1) / terrainDecimation + 1;
            int hRes        = (h - 1) / terrainDecimation + 1;
            int vertexCount = wRes * hRes;
            int indexCount  = (wRes - 1) * (hRes - 1) * 6;

            totalMaterialCount++;
            totalVertexCount += vertexCount;
            totalIndexCount  += (uint)indexCount;
            totalFaceCount   += indexCount / 3;

#if INCLUDE_TERRAIN_TREES
            TreePrototype[] treePrototypes = terrain.treePrototypes;
            t.treePrototypeMeshes = new Mesh[treePrototypes.Length];

            // assume the sharedMesh with the lowest vertex is the lowest LOD
            for (int j = 0; j < treePrototypes.Length; ++j)
            {
                GameObject   prefab         = treePrototypes[j].prefab;
                MeshFilter[] meshFilters    = prefab.GetComponentsInChildren <MeshFilter>();
                int          minVertexCount = int.MaxValue;
                int          index          = -1;
                for (int k = 0; k < meshFilters.Length; ++k)
                {
                    int count = meshFilters[k].sharedMesh.vertexCount;
                    if (count < minVertexCount)
                    {
                        minVertexCount = count;
                        index          = k;
                    }
                }

                t.treePrototypeMeshes[j] = meshFilters[index].sharedMesh;
            }

            TreeInstance[] trees = terrain.treeInstances;
            foreach (TreeInstance tree in trees)
            {
                updateCountsForMesh(ref totalVertexCount, ref totalIndexCount, ref totalFaceCount, ref totalMaterialCount, t.treePrototypeMeshes[tree.prototypeIndex]);
            }

            terrains[i] = t;
#endif
        }

        //***********************************************************************
        // Copy the mesh data.

        List <Vector3> tempVertices = new List <Vector3>();
        List <int>     tempIndices  = new List <int>();

        MeshGroup[] groups   = new MeshGroup[totalMaterialCount];
        float[]     vertices = new float[totalVertexCount * 3];
        int[]       indices  = new int[totalIndexCount];

        int vertexOffset = 0;
        int indexOffset  = 0;
        int groupOffset  = 0;

        foreach (MeshMaterial m in meshes)
        {
            MeshFilter meshFilter = m.meshFilter;

            // Compute the combined transform to go from mesh-local to geometry-local space.
            Matrix4x4 matrix = worldToLocal * meshFilter.gameObject.transform.localToWorldMatrix;

            uploadMeshFilter(tempVertices, tempIndices, groups, vertices, indices, ref vertexOffset, ref indexOffset, ref groupOffset, meshFilter.sharedMesh, m.materials, matrix);
        }

        foreach (TerrainMaterial t in terrains)
        {
            TerrainData terrain = t.terrain.terrainData;

            // Compute the combined transform to go from mesh-local to geometry-local space.
            Matrix4x4 matrix = worldToLocal * t.terrain.gameObject.transform.localToWorldMatrix;

#if UNITY_2019_3_OR_NEWER
            int w = terrain.heightmapResolution;
            int h = terrain.heightmapResolution;
#else
            int w = terrain.heightmapWidth;
            int h = terrain.heightmapHeight;
#endif
            float[,] tData = terrain.GetHeights(0, 0, w, h);

            Vector3 meshScale = terrain.size;
            meshScale = new Vector3(meshScale.x / (w - 1) * terrainDecimation, meshScale.y, meshScale.z / (h - 1) * terrainDecimation);
            int wRes          = (w - 1) / terrainDecimation + 1;
            int hRes          = (h - 1) / terrainDecimation + 1;
            int vertexCount   = wRes * hRes;
            int triangleCount = (wRes - 1) * (hRes - 1) * 2;

            // Initialize the group.
            groups[groupOffset].faceType    = FaceType.TRIANGLES;
            groups[groupOffset].faceCount   = (UIntPtr)triangleCount;
            groups[groupOffset].indexOffset = (UIntPtr)indexOffset;

            if (t.materials != null && 0 < t.materials.Length)
            {
                t.materials[0].StartInternal();
                groups[groupOffset].material = t.materials[0].materialHandle;
            }
            else
            {
                groups[groupOffset].material = IntPtr.Zero;
            }

            // Build vertices and UVs
            for (int y = 0; y < hRes; y++)
            {
                for (int x = 0; x < wRes; x++)
                {
                    int     offset = (vertexOffset + y * wRes + x) * 3;
                    Vector3 v      = matrix.MultiplyPoint3x4(Vector3.Scale(meshScale, new Vector3(y, tData[x * terrainDecimation, y * terrainDecimation], x)));
                    vertices[offset + 0] = v.x;
                    vertices[offset + 1] = v.y;
                    vertices[offset + 2] = v.z;
                }
            }

            // Build triangle indices: 3 indices into vertex array for each triangle
            for (int y = 0; y < hRes - 1; y++)
            {
                for (int x = 0; x < wRes - 1; x++)
                {
                    // For each grid cell output two triangles
                    indices[indexOffset + 0] = (vertexOffset + (y * wRes) + x);
                    indices[indexOffset + 1] = (vertexOffset + (y * wRes) + x + 1);
                    indices[indexOffset + 2] = (vertexOffset + ((y + 1) * wRes) + x);

                    indices[indexOffset + 3] = (vertexOffset + ((y + 1) * wRes) + x);
                    indices[indexOffset + 4] = (vertexOffset + (y * wRes) + x + 1);
                    indices[indexOffset + 5] = (vertexOffset + ((y + 1) * wRes) + x + 1);
                    indexOffset += 6;
                }
            }

            vertexOffset += vertexCount;
            groupOffset++;

#if INCLUDE_TERRAIN_TREES
            TreeInstance[] trees = terrain.treeInstances;
            foreach (TreeInstance tree in trees)
            {
                Vector3   pos = Vector3.Scale(tree.position, terrain.size);
                Matrix4x4 treeLocalToWorldMatrix = t.terrain.gameObject.transform.localToWorldMatrix;
                treeLocalToWorldMatrix.SetColumn(3, treeLocalToWorldMatrix.GetColumn(3) + new Vector4(pos.x, pos.y, pos.z, 0.0f));
                // TODO: tree rotation
                Matrix4x4 treeMatrix = worldToLocal * treeLocalToWorldMatrix;
                uploadMeshFilter(tempVertices, tempIndices, groups, vertices, indices, ref vertexOffset, ref indexOffset, ref groupOffset, t.treePrototypeMeshes[tree.prototypeIndex], treeMaterials, treeMatrix);
            }
#endif
        }

        // Upload mesh data
        return(ONSPPropagation.Interface.AudioGeometryUploadMeshArrays(geometryHandle,
                                                                       vertices, totalVertexCount,
                                                                       indices, indices.Length,
                                                                       groups, groups.Length));
    }
Exemplo n.º 24
0
    private static void traverseMeshHierarchy(GameObject obj, ONSPPropagationMaterial[] currentMaterials, bool includeChildren,
                                              List <MeshMaterial> meshMaterials, List <TerrainMaterial> terrainMaterials, bool ignoreStatic)
    {
        if (!obj.activeInHierarchy)
        {
            return;
        }

        MeshFilter[] meshes   = obj.GetComponents <MeshFilter>();
        Terrain[]    terrains = obj.GetComponents <Terrain>();
        ONSPPropagationMaterial[] materials = obj.GetComponents <ONSPPropagationMaterial>();

        // Initialize the current material array to a new array if there are any new materials.
        if (materials != null && materials.Length > 0)
        {
            // Determine the length of the material array.
            int maxLength = materials.Length;
            if (currentMaterials != null)
            {
                maxLength = Math.Max(maxLength, currentMaterials.Length);
            }

            ONSPPropagationMaterial[] newMaterials = new ONSPPropagationMaterial[maxLength];

            // Copy the previous materials into the new array.
            if (currentMaterials != null)
            {
                for (int i = materials.Length; i < maxLength; i++)
                {
                    newMaterials[i] = currentMaterials[i];
                }
            }
            currentMaterials = newMaterials;

            // Copy the current materials.
            for (int i = 0; i < materials.Length; i++)
            {
                currentMaterials[i] = materials[i];
            }
        }

        // Gather the meshes.
        foreach (MeshFilter meshFilter in meshes)
        {
            Mesh mesh = meshFilter.sharedMesh;
            if (mesh == null)
            {
                continue;
            }

            if (ignoreStatic && !mesh.isReadable)
            {
                Debug.Log("Mesh: " + meshFilter.gameObject.name + " not readable, cannot be static.");
                continue;
            }

            MeshMaterial m = new MeshMaterial();
            m.meshFilter = meshFilter;
            m.materials  = currentMaterials;
            meshMaterials.Add(m);
        }

        // Gather the terrains.
        foreach (Terrain terrain in terrains)
        {
            TerrainMaterial m = new TerrainMaterial();
            m.terrain   = terrain;
            m.materials = currentMaterials;
            terrainMaterials.Add(m);
        }

        // Traverse to the child objects.
        if (includeChildren)
        {
            foreach (Transform child in obj.transform)
            {
                if (child.GetComponent <ONSPPropagationGeometry>() == null) // skip children which have their own component
                {
                    traverseMeshHierarchy(child.gameObject, currentMaterials, includeChildren, meshMaterials, terrainMaterials, ignoreStatic);
                }
            }
        }
    }
Exemplo n.º 25
0
    //***********************************************************************

    public void SetPreset(Preset preset)
    {
        ONSPPropagationMaterial material = this;

        switch (preset)
        {
        case Preset.AcousticTile:                       AcousticTile(ref material);                             break;

        case Preset.Brick:                                      Brick(ref material);                                    break;

        case Preset.BrickPainted:                       BrickPainted(ref material);                             break;

        case Preset.Carpet:                                     Carpet(ref material);                                   break;

        case Preset.CarpetHeavy:                        CarpetHeavy(ref material);                              break;

        case Preset.CarpetHeavyPadded:          CarpetHeavyPadded(ref material);                break;

        case Preset.CeramicTile:                        CeramicTile(ref material);                              break;

        case Preset.Concrete:                           Concrete(ref material);                                 break;

        case Preset.ConcreteRough:                      ConcreteRough(ref material);                    break;

        case Preset.ConcreteBlock:                      ConcreteBlock(ref material);                    break;

        case Preset.ConcreteBlockPainted:       ConcreteBlockPainted(ref material);             break;

        case Preset.Curtain:                            Curtain(ref material);                                  break;

        case Preset.Foliage:                            Foliage(ref material);                                  break;

        case Preset.Glass:                                      Glass(ref material);                                    break;

        case Preset.GlassHeavy:                         GlassHeavy(ref material);                               break;

        case Preset.Grass:                                      Grass(ref material);                                    break;

        case Preset.Gravel:                                     Gravel(ref material);                                   break;

        case Preset.GypsumBoard:                        GypsumBoard(ref material);                              break;

        case Preset.PlasterOnBrick:                     PlasterOnBrick(ref material);                   break;

        case Preset.PlasterOnConcreteBlock:     PlasterOnConcreteBlock(ref material);   break;

        case Preset.Soil:                                       Soil(ref material);                                             break;

        case Preset.SoundProof:                         SoundProof(ref material);                               break;

        case Preset.Snow:                                       Snow(ref material);                                             break;

        case Preset.Steel:                                      Steel(ref material);                                    break;

        case Preset.Water:                                      Water(ref material);                                    break;

        case Preset.WoodThin:                           WoodThin(ref material);                                 break;

        case Preset.WoodThick:                          WoodThick(ref material);                                break;

        case Preset.WoodFloor:                          WoodFloor(ref material);                                break;

        case Preset.WoodOnConcrete:                     WoodOnConcrete(ref material);                   break;

        case Preset.Custom:
            break;

        default:
            break;
        }
    }