Esempio n. 1
0
		/// <summary>
		/// Upload the alphamaps (terrain layers) into heightfield network.
		/// Note that this skips the base layer.
		/// </summary>
		/// <param name="session"></param>
		/// <param name="idt"></param>
		/// <returns></returns>
		public bool UploadAlphaMaps(HEU_SessionBase session, HEU_InputDataTerrain idt)
		{
			bool bResult = true;

			int alphaLayers = idt._terrainData.alphamapLayers;

			// Skip the base layer
			if (alphaLayers < 1)
			{
				return true;
			}

			int sizeX = idt._terrainData.alphamapWidth;
			int sizeY = idt._terrainData.alphamapHeight;
			int totalSize = sizeX * sizeY;

			float[,,] alphaMaps = idt._terrainData.GetAlphamaps(0, 0, sizeX, sizeY);

			float[][] alphaMapsConverted = new float[alphaLayers - 1][];

			// Convert the alphamap layers to double arrays.
			// Note that we're skipping the base alpha map.
			for (int m = 0; m < alphaLayers - 1; ++m)
			{
				alphaMapsConverted[m] = new float[totalSize];
				for (int j = 0; j < sizeY; j++)
				{
					for (int i = 0; i < sizeX; i++)
					{
						// Flip for coordinate system change
						float h = alphaMaps[i, (sizeY - j - 1), m + 1];

						alphaMapsConverted[m][i + j * sizeX] = h;
					}
				}
			}


			// Create volume layers for all non-base alpha maps and upload values.
			for (int m = 0; m < alphaLayers - 1; ++m)
			{
				string layerName = "unity_alphamap_" + m + 1;

				HAPI_NodeId alphaLayerID = HEU_Defines.HEU_INVALID_NODE_ID;
				if (!session.CreateHeightfieldInputVolumeNode(idt._heightfieldNodeID, out alphaLayerID, layerName,
					Mathf.RoundToInt(sizeX * idt._voxelSize), Mathf.RoundToInt(sizeY * idt._voxelSize), idt._voxelSize))
				{
					bResult = false;
					Debug.LogError("Failed to create input volume node for layer " + layerName);
					break;
				}

				if (!SetHeightFieldData(session, alphaLayerID, 0, alphaMapsConverted[m], layerName))
				{
					bResult = false;
					break;
				}

				if (!session.CommitGeo(alphaLayerID))
				{
					bResult = false;
					Debug.LogError("Failed to commit volume layer " + layerName);
					break;
				}

				// Connect to the merge node but starting from index 1 since index 0 is height layer
				if (!session.ConnectNodeInput(idt._mergeNodeID, m + 2, alphaLayerID, 0))
				{
					bResult = false;
					Debug.LogError("Unable to connect new volume node for layer " + layerName);
					break;
				}
			}

			return bResult;
		}
        /// <summary>
        /// Upload the alphamaps (TerrainLayers) into heightfield network.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="idt"></param>
        /// <param name="baseVolumeInfo">The valid base height HAPI_VolumeInfo</param>
        /// <param name="bMaskSet">This is set to true if a mask layer was uploaded</param>
        /// <returns>True if successfully uploaded all layers</returns>
        public bool UploadAlphaMaps(HEU_SessionBase session, HEU_InputDataTerrain idt, ref HAPI_VolumeInfo baseVolumeInfo, out bool bMaskSet)
        {
            bool bResult = true;

            bMaskSet = false;

            int alphaLayers = idt._terrainData.alphamapLayers;

            if (alphaLayers < 1)
            {
                return(bResult);
            }

            int sizeX     = idt._terrainData.alphamapWidth;
            int sizeY     = idt._terrainData.alphamapHeight;
            int totalSize = sizeX * sizeY;

            float[,,] alphaMaps = idt._terrainData.GetAlphamaps(0, 0, sizeX, sizeY);

            float[][] alphaMapsConverted = new float[alphaLayers][];

            // Convert the alphamap layers to double arrays.
            for (int m = 0; m < alphaLayers; ++m)
            {
                alphaMapsConverted[m] = new float[totalSize];
                for (int j = 0; j < sizeY; j++)
                {
                    for (int i = 0; i < sizeX; i++)
                    {
                        // Flip for coordinate system change
                        float h = alphaMaps[i, (sizeY - j - 1), m];

                        alphaMapsConverted[m][i + j * sizeX] = h;
                    }
                }
            }

            // Create volume layers for all alpha maps and upload values.
            bool bMaskLayer      = false;
            int  inputLayerIndex = 1;

            for (int m = 0; m < alphaLayers; ++m)
            {
#if UNITY_2018_3_OR_NEWER
                string layerName = idt._terrainData.terrainLayers[m].name;
#else
                string layerName = "unity_alphamap_" + m + 1;
#endif

                // The Unity layer name could contain '.terrainlayer' and spaces. Remove them because Houdini doesn't allow
                // spaces, and the extension isn't necessary.
                layerName = layerName.Replace(" ", "_");
                int extIndex = layerName.LastIndexOf(HEU_Defines.HEU_EXT_TERRAINLAYER);
                if (extIndex > 0)
                {
                    layerName = layerName.Remove(extIndex);
                }
                //Debug.Log("Processing terrain layer: " + layerName);

                HAPI_NodeId alphaLayerID = HEU_Defines.HEU_INVALID_NODE_ID;

                if (layerName.Equals(HEU_Defines.HAPI_HEIGHTFIELD_LAYERNAME_HEIGHT))
                {
                    // Skip height (base) layer (since it has been uploaded already)
                    continue;
                }
                else if (layerName.Equals(HEU_Defines.HAPI_HEIGHTFIELD_LAYERNAME_MASK))
                {
                    //Debug.Log("Mask layer found! Skipping creating the HF.");
                    bMaskSet     = true;
                    bMaskLayer   = true;
                    alphaLayerID = idt._maskNodeID;
                }
                else
                {
                    bMaskLayer = false;

                    if (!session.CreateHeightfieldInputVolumeNode(idt._heightfieldNodeID, out alphaLayerID, layerName,
                                                                  Mathf.RoundToInt(sizeX * idt._voxelSize), Mathf.RoundToInt(sizeY * idt._voxelSize), idt._voxelSize))
                    {
                        bResult = false;
                        Debug.LogError("Failed to create input volume node for layer " + layerName);
                        break;
                    }
                }

                //Debug.Log("Uploading terrain layer: " + layerName);

                if (!SetHeightFieldData(session, alphaLayerID, 0, alphaMapsConverted[m], layerName, ref baseVolumeInfo))
                {
                    bResult = false;
                    break;
                }

#if UNITY_2018_3_OR_NEWER
                SetTerrainLayerAttributesToHeightField(session, alphaLayerID, 0, idt._terrainData.terrainLayers[m]);
#endif

                if (!session.CommitGeo(alphaLayerID))
                {
                    bResult = false;
                    Debug.LogError("Failed to commit volume layer " + layerName);
                    break;
                }

                if (!bMaskLayer)
                {
                    // Connect to the merge node but starting from index 1 since index 0 is height layer
                    if (!session.ConnectNodeInput(idt._mergeNodeID, inputLayerIndex + 1, alphaLayerID, 0))
                    {
                        bResult = false;
                        Debug.LogError("Unable to connect new volume node for layer " + layerName);
                        break;
                    }

                    inputLayerIndex++;
                }
            }

            return(bResult);
        }