private static void SetDims() { XDIM = VOXELS.GetLength(0); YDIM = VOXELS.GetLength(1); ZDIM = VOXELS.GetLength(2); }
void copyTerrain(Terrain origTerrain, string newName, float xMin, float xMax, float zMin, float zMax, int heightmapResolution, int detailResolution, int alphamapResolution) { if (heightmapResolution < 33 || heightmapResolution > 4097) { Debug.Log("Invalid heightmapResolution " + heightmapResolution); return; } if (detailResolution < 0 || detailResolution > 4048) { Debug.Log("Invalid detailResolution " + detailResolution); return; } if (alphamapResolution < 16 || alphamapResolution > 2048) { Debug.Log("Invalid alphamapResolution " + alphamapResolution); return; } if (xMin < 0 || xMin > xMax || xMax > origTerrain.terrainData.size.x) { Debug.Log("Invalid xMin or xMax"); return; } if (zMin < 0 || zMin > zMax || zMax > origTerrain.terrainData.size.z) { Debug.Log("Invalid zMin or zMax"); return; } if (AssetDatabase.FindAssets(newName).Length != 0) { Debug.Log("Asset with name " + newName + " already exists"); return; } TerrainData td = new TerrainData(); GameObject gameObject = Terrain.CreateTerrainGameObject(td); Terrain newTerrain = gameObject.GetComponent <Terrain>(); if (!AssetDatabase.IsValidFolder("Assets/Resources")) { AssetDatabase.CreateFolder("Assets", "Resources"); } // Must do this before Splat AssetDatabase.CreateAsset(td, "Assets/Resources/" + newName + ".asset"); // Copy over all vars newTerrain.bakeLightProbesForTrees = origTerrain.bakeLightProbesForTrees; newTerrain.basemapDistance = origTerrain.basemapDistance; newTerrain.castShadows = origTerrain.castShadows; newTerrain.collectDetailPatches = origTerrain.collectDetailPatches; newTerrain.detailObjectDensity = origTerrain.detailObjectDensity; newTerrain.detailObjectDistance = origTerrain.detailObjectDistance; newTerrain.drawHeightmap = origTerrain.drawHeightmap; newTerrain.drawTreesAndFoliage = origTerrain.drawTreesAndFoliage; newTerrain.editorRenderFlags = origTerrain.editorRenderFlags; newTerrain.heightmapMaximumLOD = origTerrain.heightmapMaximumLOD; newTerrain.heightmapPixelError = origTerrain.heightmapPixelError; newTerrain.legacyShininess = origTerrain.legacyShininess; newTerrain.legacySpecular = origTerrain.legacySpecular; newTerrain.lightmapIndex = origTerrain.lightmapIndex; newTerrain.lightmapScaleOffset = origTerrain.lightmapScaleOffset; newTerrain.materialTemplate = origTerrain.materialTemplate; newTerrain.materialType = origTerrain.materialType; newTerrain.realtimeLightmapIndex = origTerrain.realtimeLightmapIndex; newTerrain.realtimeLightmapScaleOffset = origTerrain.realtimeLightmapScaleOffset; newTerrain.reflectionProbeUsage = origTerrain.reflectionProbeUsage; newTerrain.treeBillboardDistance = origTerrain.treeBillboardDistance; newTerrain.treeCrossFadeLength = origTerrain.treeCrossFadeLength; newTerrain.treeDistance = origTerrain.treeDistance; newTerrain.treeMaximumFullLODCount = origTerrain.treeMaximumFullLODCount; td.splatPrototypes = origTerrain.terrainData.splatPrototypes; td.treePrototypes = origTerrain.terrainData.treePrototypes; td.detailPrototypes = origTerrain.terrainData.detailPrototypes; // Get percent of original float xMinNorm = xMin / origTerrain.terrainData.size.x; float xMaxNorm = xMax / origTerrain.terrainData.size.x; float zMinNorm = zMin / origTerrain.terrainData.size.z; float zMaxNorm = zMax / origTerrain.terrainData.size.z; float dimRatio1, dimRatio2; // Height td.heightmapResolution = heightmapResolution; float[,] heights = origTerrain.terrainData.GetHeights( Mathf.FloorToInt(xMinNorm * origTerrain.terrainData.heightmapWidth), Mathf.FloorToInt(zMinNorm * origTerrain.terrainData.heightmapHeight), Mathf.FloorToInt((xMaxNorm - xMinNorm) * origTerrain.terrainData.heightmapWidth), Mathf.FloorToInt((zMaxNorm - zMinNorm) * origTerrain.terrainData.heightmapHeight)); float[,] newHeights = new float[heightmapResolution, heightmapResolution]; dimRatio1 = (float)heights.GetLength(0) / heightmapResolution; dimRatio2 = (float)heights.GetLength(1) / heightmapResolution; for (int i = 0; i < newHeights.GetLength(0); i++) { for (int j = 0; j < newHeights.GetLength(1); j++) { newHeights[i, j] = heights[Mathf.FloorToInt(i * dimRatio1), Mathf.FloorToInt(j * dimRatio2)]; } } td.SetHeightsDelayLOD(0, 0, newHeights); // Detail td.SetDetailResolution(detailResolution, 8); // Default? Haven't messed with resolutionPerPatch for (int layer = 0; layer < origTerrain.terrainData.detailPrototypes.Length; layer++) { int[,] detailLayer = origTerrain.terrainData.GetDetailLayer( Mathf.FloorToInt(xMinNorm * origTerrain.terrainData.detailWidth), Mathf.FloorToInt(zMinNorm * origTerrain.terrainData.detailHeight), Mathf.FloorToInt((xMaxNorm - xMinNorm) * origTerrain.terrainData.detailWidth), Mathf.FloorToInt((zMaxNorm - zMinNorm) * origTerrain.terrainData.detailHeight), layer); int[,] newDetailLayer = new int[detailResolution, detailResolution]; dimRatio1 = (float)detailLayer.GetLength(0) / detailResolution; dimRatio2 = (float)detailLayer.GetLength(1) / detailResolution; for (int i = 0; i < newDetailLayer.GetLength(0); i++) { for (int j = 0; j < newDetailLayer.GetLength(1); j++) { newDetailLayer[i, j] = detailLayer[Mathf.FloorToInt(i * dimRatio1), Mathf.FloorToInt(j * dimRatio2)]; } } td.SetDetailLayer(0, 0, layer, newDetailLayer); } // Splat td.alphamapResolution = alphamapResolution; float[,,] alphamaps = origTerrain.terrainData.GetAlphamaps( Mathf.FloorToInt(xMinNorm * origTerrain.terrainData.alphamapWidth), Mathf.FloorToInt(zMinNorm * origTerrain.terrainData.alphamapHeight), Mathf.FloorToInt((xMaxNorm - xMinNorm) * origTerrain.terrainData.alphamapWidth), Mathf.FloorToInt((zMaxNorm - zMinNorm) * origTerrain.terrainData.alphamapHeight)); // Last dim is always origTerrain.terrainData.splatPrototypes.Length so don't ratio float[,,] newAlphamaps = new float[alphamapResolution, alphamapResolution, alphamaps.GetLength(2)]; dimRatio1 = (float)alphamaps.GetLength(0) / alphamapResolution; dimRatio2 = (float)alphamaps.GetLength(1) / alphamapResolution; for (int i = 0; i < newAlphamaps.GetLength(0); i++) { for (int j = 0; j < newAlphamaps.GetLength(1); j++) { for (int k = 0; k < newAlphamaps.GetLength(2); k++) { newAlphamaps[i, j, k] = alphamaps[Mathf.FloorToInt(i * dimRatio1), Mathf.FloorToInt(j * dimRatio2), k]; } } } td.SetAlphamaps(0, 0, newAlphamaps); // Tree for (int i = 0; i < origTerrain.terrainData.treeInstanceCount; i++) { TreeInstance ti = origTerrain.terrainData.treeInstances[i]; if (ti.position.x < xMinNorm || ti.position.x >= xMaxNorm) { continue; } if (ti.position.z < zMinNorm || ti.position.z >= zMaxNorm) { continue; } ti.position = new Vector3(((ti.position.x * origTerrain.terrainData.size.x) - xMin) / (xMax - xMin), ti.position.y, ((ti.position.z * origTerrain.terrainData.size.z) - zMin) / (zMax - zMin)); newTerrain.AddTreeInstance(ti); } gameObject.transform.position = new Vector3(origTerrain.transform.position.x + xMin, origTerrain.transform.position.y, origTerrain.transform.position.z + zMin); gameObject.name = newName; // Must happen after setting heightmapResolution td.size = new Vector3(xMax - xMin, origTerrain.terrainData.size.y, zMax - zMin); AssetDatabase.SaveAssets(); }
public void recalculateFunction() { createNewBrushes(); minZ = double.PositiveInfinity; maxZ = double.NegativeInfinity; const int qjump = 1; mesh1 = new double[vals.GetLength(0), vals.GetLength(1)]; mesh2 = new double[vals.GetLength(1), vals.GetLength(2)]; mesh3 = new double[vals.GetLength(2), vals.GetLength(0)]; int tz = vals.GetLength(2) - 1; for (int x = 0; x < vals.GetLength(0); x += qjump) { for (int y = 0; y < vals.GetLength(1); y += qjump) { double zz = function(x, y, az, vals[x, y, tz - az]); mesh1[x, y] = zz; if (double.IsInfinity(zz) || double.IsNaN(zz)) { } else { if (minZ > zz) { minZ = zz; } if (maxZ < zz) { maxZ = zz; } } } } //Y-Z for (int y = 0; y < vals.GetLength(1); y += qjump) { for (int z = 0; z < vals.GetLength(2); z += qjump) { double zz = function(ax, y, z, vals[ax, y, tz - z]); mesh2[y, z] = zz; if (double.IsInfinity(zz) || double.IsNaN(zz)) { } else { if (minZ > zz) { minZ = zz; } if (maxZ < zz) { maxZ = zz; } } } } //Z-X for (int z = 0; z < vals.GetLength(2); z += qjump) { for (int x = 0; x < vals.GetLength(0); x += qjump) { double zz = function(x, ay, z, vals[x, ay, tz - z]); mesh3[z, x] = zz; if (double.IsInfinity(zz) || double.IsNaN(zz)) { } else { if (minZ > zz) { minZ = zz; } if (maxZ < zz) { maxZ = zz; } } } } for (int x = 0; x < vals.GetLength(0); x += qjump) { for (int y = 0; y < vals.GetLength(1); y += qjump) { if (double.IsInfinity(mesh1[x, y]) || double.IsNaN(mesh1[x, y])) { if (double.IsPositiveInfinity(mesh1[x, y])) { mesh1[x, y] = maxZ; } else { mesh1[x, y] = minZ; } } } } //Y-Z for (int y = 0; y < vals.GetLength(1); y += qjump) { for (int z = 0; z < vals.GetLength(2); z += qjump) { if (double.IsInfinity(mesh2[y, z]) || double.IsNaN(mesh2[y, z])) { if (double.IsPositiveInfinity(mesh2[y, z])) { mesh2[y, z] = maxZ; } else { mesh2[y, z] = minZ; } } } } //Z-X for (int z = 0; z < vals.GetLength(2); z += qjump) { for (int x = 0; x < vals.GetLength(0); x += qjump) { if (double.IsInfinity(mesh3[z, x]) || double.IsNaN(mesh3[z, x])) { if (double.IsPositiveInfinity(mesh3[z, x])) { mesh3[z, x] = maxZ; } else { mesh3[z, x] = minZ; } } } } functionNeedsCalculation = false; }
public static void WeldSplats (float[,,] splats, Terrain prevX, Terrain nextZ, Terrain nextX, Terrain prevZ, int margin) { if (margin == 0) return; int splatsSize = splats.GetLength(0); int numSplats = splats.GetLength(2); float[] nRow = new float[numSplats]; //prev x if (prevX != null && prevX.terrainData.alphamapResolution==splatsSize && prevX.terrainData.alphamapLayers==numSplats) { float[,,] nStrip = prevX.terrainData.GetAlphamaps(splatsSize-1,0,1,splatsSize); for (int z=0; z<splatsSize; z++) { for (int s=0; s<numSplats; s++) nRow[s] = nStrip[z,0,s]; float percentFromSide = Mathf.Min( Mathf.Clamp01(1f*z/margin), Mathf.Clamp01(1 - 1f*(z-(splatsSize-1-margin))/margin) ); float invPercentFromSide = 2000000000; if (percentFromSide > 0.0001f) invPercentFromSide = 1f/percentFromSide; for (int x=0; x<margin; x++) { float percent = 1-1f*x/margin; if (percentFromSide < 0.999f) percent = Mathf.Pow(percent, invPercentFromSide); percent = 3*percent*percent - 2*percent*percent*percent; for (int s=0; s<numSplats; s++) splats[z,x,s] = nRow[s]*percent + splats[z,x,s]*(1-percent); } } } //next x if (nextX != null && nextX.terrainData.alphamapResolution==splatsSize && nextX.terrainData.alphamapLayers==numSplats) { float[,,] nStrip = nextX.terrainData.GetAlphamaps(0,0,1,splatsSize); for (int z=0; z<splatsSize; z++) { for (int s=0; s<numSplats; s++) nRow[s] = nStrip[z,0,s]; float percentFromSide = Mathf.Min( Mathf.Clamp01(1f*z/margin), Mathf.Clamp01(1 - 1f*(z-(splatsSize-1-margin))/margin) ); float invPercentFromSide = 2000000000; if (percentFromSide > 0.0001f) invPercentFromSide = 1f/percentFromSide; for (int x=splatsSize-margin; x<splatsSize; x++) { float percent = 1 - 1f*(splatsSize-x-1)/margin; if (percentFromSide < 0.999f) percent = Mathf.Pow(percent, invPercentFromSide); percent = 3*percent*percent - 2*percent*percent*percent; for (int s=0; s<numSplats; s++) splats[z,x,s] = nRow[s]*percent + splats[z,x,s]*(1-percent); } } } //prev z if (prevZ != null && prevZ.terrainData.alphamapResolution==splatsSize && prevZ.terrainData.alphamapLayers==numSplats) { float[,,] nStrip = prevZ.terrainData.GetAlphamaps(0,splatsSize-1,splatsSize,1); for (int x=0; x<splatsSize; x++) { for (int s=0; s<numSplats; s++) nRow[s] = nStrip[0,x,s]; float percentFromSide = Mathf.Min( Mathf.Clamp01(1f*x/margin), Mathf.Clamp01(1 - 1f*(x-(splatsSize-1-margin))/margin) ); float invPercentFromSide = 2000000000; if (percentFromSide > 0.0001f) invPercentFromSide = 1f/percentFromSide; for (int z=0; z<margin; z++) { float percent = 1-1f*z/margin; if (percentFromSide < 0.999f) percent = Mathf.Pow(percent, invPercentFromSide); percent = 3*percent*percent - 2*percent*percent*percent; for (int s=0; s<numSplats; s++) splats[z,x,s] = nRow[s]*percent + splats[z,x,s]*(1-percent); } } } //next z if (nextZ != null && nextZ.terrainData.alphamapResolution==splatsSize && nextZ.terrainData.alphamapLayers==numSplats) { float[,,] nStrip = nextZ.terrainData.GetAlphamaps(0,0,splatsSize,1); for (int x=0; x<splatsSize; x++) { for (int s=0; s<numSplats; s++) nRow[s] = nStrip[0,x,s]; float percentFromSide = Mathf.Min( Mathf.Clamp01(1f*x/margin), Mathf.Clamp01(1 - 1f*(x-(splatsSize-1-margin))/margin) ); float invPercentFromSide = 2000000000; if (percentFromSide > 0.0001f) invPercentFromSide = 1f/percentFromSide; for (int z=splatsSize-margin; z<splatsSize; z++) { float percent = 1 - 1f*(splatsSize-z-1)/margin; if (percentFromSide < 0.999f) percent = Mathf.Pow(percent, invPercentFromSide); percent = 3*percent*percent - 2*percent*percent*percent; for (int s=0; s<numSplats; s++) splats[z,x,s] = nRow[s]*percent + splats[z,x,s]*(1-percent); } } } }
internal static void RemoveSplatTexture(TerrainData terrainData, int index) { Undo.RegisterCompleteObjectUndo(terrainData, "Remove texture"); int width = terrainData.alphamapWidth; int height = terrainData.alphamapHeight; float[,,] alphamap = terrainData.GetAlphamaps(0, 0, width, height); int alphaCount = alphamap.GetLength(2); int newAlphaCount = alphaCount - 1; float[,,] newalphamap = new float[height, width, newAlphaCount]; // move further alphamaps one index below for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { for (int a = 0; a < index; ++a) { newalphamap[y, x, a] = alphamap[y, x, a]; } for (int a = index + 1; a < alphaCount; ++a) { newalphamap[y, x, a - 1] = alphamap[y, x, a]; } } } // normalize weights in new alpha map for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { float sum = 0.0F; for (int a = 0; a < newAlphaCount; ++a) { sum += newalphamap[y, x, a]; } if (sum >= 0.01) { float multiplier = 1.0F / sum; for (int a = 0; a < newAlphaCount; ++a) { newalphamap[y, x, a] *= multiplier; } } else { // in case all weights sum to pretty much zero (e.g. // removing splat that had 100% weight), assign // everything to 1st splat texture (just like // initial terrain). for (int a = 0; a < newAlphaCount; ++a) { newalphamap[y, x, a] = (a == 0) ? 1.0f : 0.0f; } } } } // remove splat from terrain prototypes SplatPrototype[] splats = terrainData.splatPrototypes; SplatPrototype[] newSplats = new SplatPrototype[splats.Length - 1]; for (int a = 0; a < index; ++a) { newSplats[a] = splats[a]; } for (int a = index + 1; a < alphaCount; ++a) { newSplats[a - 1] = splats[a]; } terrainData.splatPrototypes = newSplats; // set new alphamaps terrainData.SetAlphamaps(0, 0, newalphamap); }
public List <NeuralNetworkYoloResult> Yolo(float[,,] inputs, float imageWidth, float imageHeight, float threshold, float nmsThreshold) { float[,] anchors = { { 116, 90, 156, 198, 373, 326 }, { 30, 61, 62, 45, 59, 119 }, { 10, 13, 16, 30, 33, 23 } }; int inputWidth = inputs.GetLength(1); int inputHeight = inputs.GetLength(0); object[] outputs = (object[])ForwardPropagation(inputs); List <NeuralNetworkBoundBox> boundBoxes = new List <NeuralNetworkBoundBox> (); int gridWidth = 0; int gridHeight = 0; int boxLength = 0; int classesLength = 0; for (int i = 0; i < outputs.Length; i++) { float[,,] outputValues = (float[, , ])outputs[i]; if (i == 0) { gridWidth = outputValues.GetLength(1); gridHeight = outputValues.GetLength(0); boxLength = outputValues.GetLength(2) / 3; classesLength = boxLength - 5; } for (int gridY = 0; gridY < gridHeight; gridY++) { for (int gridX = 0; gridX < gridWidth; gridX++) { for (int boxIndex = 0; boxIndex < 3; boxIndex++) { int boxOffset = boxIndex * boxLength; float score = outputValues[gridY, gridX, boxOffset + 4]; if (score <= threshold) { continue; } float x = (gridX + outputValues[gridY, gridX, boxOffset]) / gridWidth; float y = (gridY + outputValues[gridY, gridX, boxOffset + 1]) / gridHeight; float width = (float)(anchors[i, 2 * boxIndex + 0] * Math.Exp(outputValues[gridY, gridX, boxOffset + 2]) / inputWidth); float height = (float)(anchors[i, 2 * boxIndex + 1] * Math.Exp(outputValues[gridY, gridX, boxOffset + 3]) / inputHeight); float[] classes = new float[classesLength]; for (int n = 0; n < classes.Length; n++) { classes[n] = outputValues[gridY, gridX, boxOffset + 5 + n]; } float offsetX = (inputWidth - inputWidth) / 2 / inputWidth; float offsetY = (inputHeight - inputHeight) / 2 / inputHeight; float scaleX = (float)inputWidth / inputWidth; float scaleY = (float)inputHeight / inputHeight; float minX = (x - width / 2 - offsetX) / scaleX * imageWidth; float minY = (y - height / 2 - offsetY) / scaleY * imageHeight; float maxX = (x + width / 2 - offsetX) / scaleX * imageWidth; float maxY = (y + height / 2 - offsetY) / scaleY * imageHeight; boundBoxes.Add(new NeuralNetworkBoundBox(score, minX, minY, maxX, maxY, classes)); } } } } PrintBoxes("correct", boundBoxes); List <NeuralNetworkYoloResult> results = new List <NeuralNetworkYoloResult> (); if (boundBoxes.Count > 0) { for (int c = 0; c < classesLength; c++) { float[] values = new float[boundBoxes.Count]; for (int b = 0; b < boundBoxes.Count; b++) { values[b] = -boundBoxes[b].Classes[c]; } float[] sortedIndices = new float[values.Length]; Array.Copy(values, sortedIndices, values.Length); Array.Sort(sortedIndices); for (int s = 0; s < sortedIndices.Length; s++) { sortedIndices[s] = Array.IndexOf(values, sortedIndices[s]); } for (int s = 0; s < sortedIndices.Length; s++) { int index = (int)sortedIndices[s]; if (boundBoxes[index].Classes[c] == 0) { continue; } for (int j = s + 1; j < sortedIndices.Length; j++) { int indexJ = (int)sortedIndices[j]; if (BoundBoxIOU(boundBoxes[index], boundBoxes[indexJ]) >= nmsThreshold) { boundBoxes[indexJ].Classes[c] = 0; } } } } PrintBoxes("nms", boundBoxes); for (int i = 0; i < boundBoxes.Count; i++) { for (int c = 0; c < classesLength; c++) { if (boundBoxes[i].Classes[c] > threshold) { results.Add(new NeuralNetworkYoloResult(boundBoxes[i], c, boundBoxes[i].Classes[c] * 100)); } } } } return(results); }
public int GetOutputSize() { return(pooling.GetLength(0)); }
private static void ProcessWithDsl(Dsl.FunctionData funcData, string type, float[,] datas, float[, ,] alphamaps, Dictionary <int, int[, ]> details, DslExpression.DslCalculator calc, string proc, ref bool resetTrees) { if (null == funcData) { return; } if (null != funcData) { if (type == "height") { foreach (var comp in funcData.Statements) { var callData = comp as Dsl.CallData; if (null != callData) { string id = callData.GetId(); if (id == "resettrees") { resetTrees = bool.Parse(callData.GetParamId(0)); } else if (id == "rect") { int x = int.Parse(callData.GetParamId(0)); int y = int.Parse(callData.GetParamId(1)); int w = int.Parse(callData.GetParamId(2)); int h = int.Parse(callData.GetParamId(3)); ProcessHeights(datas, calc, proc, x, y, w, h); } else if (id == "circle") { int x = int.Parse(callData.GetParamId(0)); int y = int.Parse(callData.GetParamId(1)); int r = int.Parse(callData.GetParamId(2)); ProcessHeights(datas, calc, proc, x, y, r); } } } } else if (type == "alphamap") { int alphanum = alphamaps.GetLength(2); foreach (var comp in funcData.Statements) { var callData = comp as Dsl.CallData; if (null != callData) { string id = callData.GetId(); if (id == "resettrees") { resetTrees = bool.Parse(callData.GetParamId(0)); } else if (id == "rect") { int x = int.Parse(callData.GetParamId(0)); int y = int.Parse(callData.GetParamId(1)); int w = int.Parse(callData.GetParamId(2)); int h = int.Parse(callData.GetParamId(3)); ProcessAlphamaps(alphamaps, calc, proc, x, y, w, h); } else if (id == "circle") { int x = int.Parse(callData.GetParamId(0)); int y = int.Parse(callData.GetParamId(1)); int r = int.Parse(callData.GetParamId(2)); ProcessAlphamaps(alphamaps, calc, proc, x, y, r); } } } } else if (type == "detail") { foreach (var comp in funcData.Statements) { var callData = comp as Dsl.CallData; if (null != callData) { string id = callData.GetId(); if (id == "resettrees") { resetTrees = bool.Parse(callData.GetParamId(0)); } else if (id == "rect") { int x = int.Parse(callData.GetParamId(0)); int y = int.Parse(callData.GetParamId(1)); int w = int.Parse(callData.GetParamId(2)); int h = int.Parse(callData.GetParamId(3)); ProcessDetails(details, calc, proc, x, y, w, h); } else if (id == "circle") { int x = int.Parse(callData.GetParamId(0)); int y = int.Parse(callData.GetParamId(1)); int r = int.Parse(callData.GetParamId(2)); ProcessDetails(details, calc, proc, x, y, r); } } } } } }
public override object ForwardPropagation(object inputs) { float[,,] inputValues = (float[, , ])inputs; int inputWidth = inputValues.GetLength(1); int inputHeight = inputValues.GetLength(0); int inputChannel = inputValues.GetLength(2); int outputWidth = NeuralNetworkAPI.CalculateOutputLength(inputValues.GetLength(1), Width, StrideX, PaddingType); int outputHeight = NeuralNetworkAPI.CalculateOutputLength(inputValues.GetLength(0), Height, StrideY, PaddingType); int paddingX; int paddingY; switch (PaddingType) { case NeuralNetworkPaddingType.Valid: paddingX = 0; paddingY = 0; break; case NeuralNetworkPaddingType.Same: paddingX = (Width - 1) / 2; paddingY = (Height - 1) / 2; break; default: throw new NotImplementedException(PaddingType.ToString()); } int startX = 0 - paddingX; int startY = 0 - paddingY; int endX = inputWidth + paddingX - Width; int endY = inputHeight + paddingY - Height; float[,,] outputs = new float[outputHeight, outputWidth, inputChannel]; #if NET40_OR_GREATER Parallel.For(0, inputChannel, c => { int outputX = 0; int outputY = 0; for (int y = startY; y <= endY; y += StrideY, outputY++) { for (int x = startX; x <= endX; x += StrideX, outputX++) { bool first = true; float max = 0; int ex = x + Width; int eY = y + Height; for (int cy = y; cy < eY; cy++) { for (int cx = x; cx < ex; cx++) { float value; if (cy < 0 || cy >= endY || cx < 0 || cx >= endX) { value = 0; } else { value = inputValues[cy, cx, c]; } if (first || max < value) { first = false; max = value; } } } outputs[outputY, outputX, c] = max; } outputX = 0; } }); #else for (int c = 0; c < inputChannel; c++) { int outputX = 0; int outputY = 0; for (int y = startY; y <= endY; y += StrideY, outputY++) { for (int x = startX; x <= endX; x += StrideX, outputX++) { bool first = true; float max = 0; int ex = x + Width; int eY = y + Height; for (int cy = y; cy < eY; cy++) { for (int cx = x; cx < ex; cx++) { float value; if (cy < 0 || cy >= endY || cx < 0 || cx >= endX) { value = 0; } else { value = inputValues[cy, cx, c]; } if (first || max < value) { first = false; max = value; } } } outputs[outputY, outputX, c] = max; } outputX = 0; } } #endif return(Outputs = outputs); }
public void fillArray(Coordinates coords, float[,,] array) { TimeSpan total = new TimeSpan(); DateTime start2 = DateTime.Now; int x, y, z; for (y = 0; y < array.GetLength(1); y++) { Layer previous, next = null; int min, max, mid; float weight; max = layers.Count; min = 0; while (max - min > 1) { mid = (max + min) >> 1; if (layers[mid].startingHeight > coords.y + y) { max = mid; } else { min = mid; } } previous = layers[min]; if (max >= layers.Count) { weight = 1; } else { next = layers[max]; weight = Math.Min(1, (next.startingHeight - coords.y - y) / MERGE_DIST); } if (weight > 0.99f) { float[,,] layer = getFloat(); DateTime start = DateTime.Now; previous.terrainGenerator.fillArray(new Coordinates(coords.x, coords.y + y, coords.z), layer); total += (DateTime.Now.Subtract(start)); for (x = 0; x < array.GetLength(0); x++) { for (z = 0; z < array.GetLength(2); z++) { array[x, y, z] = layer[x, 0, z]; } } disposeFloat(layer); } else { float[,,] previousLayer = getFloat(); float[,,] nextLayer = getFloat(); DateTime start = DateTime.Now; previous.terrainGenerator.fillArray(new Coordinates(coords.x, coords.y + y, coords.z), previousLayer); next.terrainGenerator.fillArray(new Coordinates(coords.x, coords.y + y, coords.z), nextLayer); total += (DateTime.Now.Subtract(start)); for (x = 0; x < array.GetLength(0); x++) { for (z = 0; z < array.GetLength(2); z++) { array[x, y, z] = weight * previousLayer[x, 0, z] + (1 - weight) * nextLayer[x, 0, z]; } } disposeFloat(previousLayer); disposeFloat(nextLayer); } } Debug.Log("Perlin took " + total + " of " + DateTime.Now.Subtract(start2) + " to fill " + array.GetLength(0) + " | " + array.GetLength(1) + " | " + array.GetLength(2)); }
public void Noise(float[,,] noise, Vector3 offset, Vector3 scale, float noiseScale, bool add) { var xLength = noise.GetLength(0); var yLength = noise.GetLength(1); var zLength = noise.GetLength(2); for (int x = 0; x < xLength; x++) { var xOffset = offset.x + x * scale.x; var xCoords = Split(xOffset); var u = Fade(xCoords.remainder); int a = m_Permutation[xCoords.integer]; int b = m_Permutation[xCoords.integer + 1]; for (int y = 0; y < yLength; y++) { var yOffset = offset.y + y * scale.y; var yCoords = Split(yOffset); var v = Fade(yCoords.remainder); int aa = m_Permutation[a + yCoords.integer]; int ab = m_Permutation[a + yCoords.integer + 1]; int ba = m_Permutation[b + yCoords.integer]; int bb = m_Permutation[b + yCoords.integer + 1]; for (int z = 0; z < zLength; z++) { var zOffset = offset.z + z * scale.z; var zCoords = Split(zOffset); var w = Fade(zCoords.remainder); int aaa = m_Permutation[aa + zCoords.integer]; int aba = m_Permutation[ab + zCoords.integer]; int aab = m_Permutation[aa + zCoords.integer + 1]; int abb = m_Permutation[ab + zCoords.integer + 1]; int baa = m_Permutation[ba + zCoords.integer]; int bba = m_Permutation[bb + zCoords.integer]; int bab = m_Permutation[ba + zCoords.integer + 1]; int bbb = m_Permutation[bb + zCoords.integer + 1]; var xa = new Vector4( Grad(aaa, xCoords.remainder, yCoords.remainder, zCoords.remainder), Grad(aba, xCoords.remainder, yCoords.remainder - 1, zCoords.remainder), Grad(aab, xCoords.remainder, yCoords.remainder, zCoords.remainder - 1), Grad(abb, xCoords.remainder, yCoords.remainder - 1, zCoords.remainder - 1) ); var xb = new Vector4( Grad(baa, xCoords.remainder - 1, yCoords.remainder, zCoords.remainder), Grad(bba, xCoords.remainder - 1, yCoords.remainder - 1, zCoords.remainder), Grad(bab, xCoords.remainder - 1, yCoords.remainder, zCoords.remainder - 1), Grad(bbb, xCoords.remainder - 1, yCoords.remainder - 1, zCoords.remainder - 1) ); var xl = Vector4.Lerp(xa, xb, u); var ya = new Vector2(xl.x, xl.z); var yb = new Vector2(xl.y, xl.w); var yl = Vector2.Lerp(ya, yb, v); var value = (Mathf.Lerp(yl.x, yl.y, w) + 1) * 0.5f * noiseScale; if (add) { noise[x, y, z] += value; } else { noise[x, y, z] = value; } } } } }
} //--end of the method //--evaluate density (t,x) public float[,] P() { float[,] dens = new float[F.GetLength(0), F.GetLength(1)]; for (int t = 0; t < F.GetLength(0); t++) { for (int j = 0; j < F.GetLength(1); j++) { dens[t, j] = _dens(t, j); } } return(dens); } //--end of the method
// Step 2. Remove shared edges. // Input: List of triangles. // Output: List of edges that were not shared by other edges in the triangles. void StepTwo() { // Check every triangle against all other triangles. for (int m = 0; m < input_triangles.GetLength(0); ++m) { for (int n = 0; n < input_triangles.GetLength(0); ++n) { // Not the same triangle. if (m != n) { for (int i = 0; i < 3; ++i) { int j = i + 1; if (i == 2) { j = 0; } Edge tri1_edge = new Edge( input_triangles[m, i, 0], input_triangles[m, i, 1], input_triangles[m, i, 2], input_triangles[m, j, 0], input_triangles[m, j, 1], input_triangles[m, j, 2]); all_edges.Add(tri1_edge); // a --> b == (b - a) Edge tri2_edge01 = new Edge( input_triangles[n, 0, 0], input_triangles[n, 0, 1], input_triangles[n, 0, 2], input_triangles[n, 1, 0], input_triangles[n, 1, 1], input_triangles[n, 1, 2]); Edge tri2_edge12 = new Edge( input_triangles[n, 1, 0], input_triangles[n, 1, 1], input_triangles[n, 1, 2], input_triangles[n, 2, 0], input_triangles[n, 2, 1], input_triangles[n, 2, 2]); Edge tri2_edge20 = new Edge( input_triangles[n, 2, 0], input_triangles[n, 2, 1], input_triangles[n, 2, 2], input_triangles[n, 0, 0], input_triangles[n, 0, 1], input_triangles[n, 0, 2]); if (Edge.AreOverlappingEdges(tri1_edge, tri2_edge01)) { shared_edges.Add(tri1_edge); } if (Edge.AreOverlappingEdges(tri1_edge, tri2_edge12)) { shared_edges.Add(tri1_edge); } if (Edge.AreOverlappingEdges(tri1_edge, tri2_edge20)) { shared_edges.Add(tri1_edge); } } } } } outline_edges = UniqueEdgeList.SetDifference(all_edges, shared_edges); //shared_edges.Print(); //Debug.Log("OUTLINE EDGES"); //outline_edges.Print(); //all_edges.Print(); return; }
void InitializeViewMatrix() { myTexts = new Text[beltMatrix.GetLength(0), beltMatrix.GetLength(1), beltMatrix.GetLength(2)]; grid.constraintCount = beltMatrix.GetLength(0); grid.cellSize = Vector2.one * 500f / beltMatrix.GetLength(0); for (int y = 0; y < beltMatrix.GetLength(1); y++) { for (int x = 0; x < beltMatrix.GetLength(0); x++) { var obj = Instantiate(CellPrefab, CellParent); beltMatrix[x, y, 1] = obj.transform.position.x; beltMatrix[x, y, 2] = obj.transform.position.y; for (int i = 0; i < beltMatrix.GetLength(2); i++) { var text = Instantiate(TextPrefab, obj.transform); myTexts[x, y, i] = text.GetComponent <Text>(); } } } }
public static void ChangeTexture(int startX, int startY, int sizeX, int sizeY, TerrainTextureType tp, float randMin, float randMax) { Vector3 worldPos = Grid.ToWorld(startX, startY); worldPos -= new Vector3(0.5f, 0, 0.5f) * Grid.SCALE; sizeX *= 2; sizeY *= 2; /*if (sizeX == 2) * { * sizeX = 3; * worldPos -= new Vector3(0.25f, 0, 0) * Grid.SCALE; * } * if (sizeY == 2) * { * sizeY = 3; * worldPos -= new Vector3(0, 0, 0.25f) * Grid.SCALE; * }*/ int tex = 0; switch (tp) { case TerrainTextureType.Grass: tex = 0; break; case TerrainTextureType.Path: tex = 1; break; case TerrainTextureType.Field: tex = 5; break; case TerrainTextureType.Building: tex = 2; break; } // calculate which splat map cell the worldPos falls within (ignoring y) 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) float[,,] splatmapData = terrainData.GetAlphamaps(mapX, mapZ, sizeX, sizeY); float spW = splatmapData.GetLength(0); float spH = splatmapData.GetLength(1); float midX = spW / 2 - 0.5f; float midY = spH / 2 - 0.5f; float maxR = midX * midX + midY * midY; for (int x = 0; x < spW; x++) { for (int y = 0; y < spH; y++) { float randPx = Random.Range(randMin, randMax); for (int i = 0; i < splatmapData.GetLength(2); i++) { float r = (midX - x) * (midX - x) + (midY - y) * (midY - y); float alpha = 1f - (r / maxR); alpha += randPx; if (spW <= 2) { alpha = 0.5f; } if (i == 0) { alpha = 1f - alpha; } else if (i != tex) { alpha = 0; } splatmapData[x, y, i] = alpha; } } } terrainData.SetAlphamaps(mapX, mapZ, splatmapData); //terrain.Flush(); }