//CLM USED FOR EXPORT public unsafe void convertLayersToTexturingDataHandle(BTerrainLayerContainer input, List <Texture> tempAlphaTextures, BTerrainCompositeTexture output, int minXVert, int minZVert, int lod, int channelCount) { // BTerrainLayerContainer input = node.mLayerContainer; // BTerrainCompositeTexture output = node.getTextureData(lod).mCompositeTexture; Viewport vp = new Viewport(); vp.X = output.mXPixelOffset; vp.Y = output.mYPixelOffset; vp.Width = output.mWidth; vp.Height = output.mWidth; vp.MinZ = 0; vp.MaxZ = 1; BRenderDevice.getDevice().Viewport = vp; Microsoft.DirectX.Direct3D.Effect shader = TerrainGlobals.getTexturing().mCompositeShader; shader.Begin(0); shader.BeginPass(0); for (int i = 0; i < channelCount; i++) { for (int k = 0; k < output.mNumMips; k++) { //actually render Surface compositeTarget = null; float scale = 1; try { if (output.UsingAtlas == false) { compositeTarget = output.mTextures[i].GetSurfaceLevel(k); BRenderDevice.getDevice().SetRenderTarget(0, compositeTarget); } else { BTerrainCompositeAtlasTexture atlas = output.mAtlas; if (atlas.mTextures[i].Disposed == true) { atlas.reCreate(); } scale = atlas.mAtlasScale; compositeTarget = atlas.mTextures[i].GetSurfaceLevel(k); BRenderDevice.getDevice().SetRenderTarget(0, compositeTarget); } { BTerrainTexturingLayer.eLayerType lastType = BTerrainTexturingLayer.eLayerType.cLayer_Splat; float layerInc = 1.0f / (float)(cMaxNumBlends - 1);// (float)input.getNumLayers(); for (int j = 0; j < input.getNumLayers(); j++) { // BRenderDevice.getDevice().Clear(ClearFlags.ZBuffer | ClearFlags.Target, unchecked((int)0xFFFF0000), 1.0f, 0); //compose a splat shader.SetValue(mShaderContribOverride, 1); shader.SetValue(mShaderAlphaOverride, 1); if (input.giveLayer(j).mLayerType == BTerrainTexturingLayer.eLayerType.cLayer_Splat) { if (lastType != BTerrainTexturingLayer.eLayerType.cLayer_Splat) { shader.EndPass(); shader.BeginPass(0); lastType = BTerrainTexturingLayer.eLayerType.cLayer_Splat; } if (TerrainGlobals.getEditor().getRenderMode() == BTerrainEditor.eEditorRenderMode.cRenderTextureSelectRender) { if (TerrainGlobals.getEditor().getMode() != BTerrainEditor.eEditorMode.cModeTexEdit) { if (j == 0) { shader.SetValue(mShaderContribOverride, 0); shader.SetValue(mShaderAlphaOverride, 0); } else { continue; } } else { if (input.giveLayer(j).mActiveTextureIndex != TerrainGlobals.getTerrainFrontEnd().SelectedTextureIndex) { shader.SetValue(mShaderContribOverride, 0); } else { shader.SetValue(mShaderContribOverride, 1); } shader.SetValue(mShaderAlphaOverride, 0); } } float targetLayer = (float)(j * layerInc); shader.SetValue(mShaderNumLayersHandle, targetLayer); shader.SetValue(mShaderAlphaTexArrayHandle, tempAlphaTextures[j]); //lock in our target texture BTerrainActiveTextureContainer active = TerrainGlobals.getTexturing().getActiveTexture(input.giveLayer(j).mActiveTextureIndex); if (active == null) { compositeTarget.Dispose(); continue; } shader.SetValue(mShaderTexArrayHandle, active.mTexChannels[i].mTexture); float[] uvs = new float[2]; uvs[0] = active.mUScale; uvs[1] = active.mVScale; shader.SetValue(mShaderLayerUV, uvs); } else //compose a decal { if (lastType != BTerrainTexturingLayer.eLayerType.cLayer_Decal) { shader.EndPass(); shader.BeginPass(1); lastType = BTerrainTexturingLayer.eLayerType.cLayer_Decal; } bool doWhite = false; if (TerrainGlobals.getEditor().getRenderMode() == BTerrainEditor.eEditorRenderMode.cRenderTextureSelectRender) { if (TerrainGlobals.getEditor().getMode() == BTerrainEditor.eEditorMode.cModeTexEdit) { shader.SetValue(mShaderContribOverride, 0); } else if (TerrainGlobals.getEditor().getMode() == BTerrainEditor.eEditorMode.cModeDecalEdit) { doWhite = true; if (TerrainGlobals.getTexturing().getActiveDecalInstance(input.giveLayer(j).mActiveTextureIndex).mActiveDecalIndex != TerrainGlobals.getTerrainFrontEnd().SelectedDecalIndex) { shader.SetValue(mShaderContribOverride, 0); } else { shader.SetValue(mShaderContribOverride, 1); shader.SetValue(mShaderAlphaOverride, 1); } } } //Grab our decal instance BTerrainDecalInstance decal = getActiveDecalInstance(input.giveLayer(j).mActiveTextureIndex); if (decal == null) { compositeTarget.Dispose(); continue; } Vector4 selColor = new Vector4(1, 1, 1, 1); if (decal.mIsSelected) { selColor.Y = 0.75f; selColor.Z = 0.75f; } shader.SetValue(mShaderColorOverload, selColor); //grab the decal we care about BTerrainActiveDecalContainer active = getActiveDecal(decal.mActiveDecalIndex); if (active == null) { compositeTarget.Dispose(); continue; } if (doWhite) { shader.SetValue(mShaderAlphaTexArrayHandle, tempAlphaTextures[0]); } else { shader.SetValue(mShaderAlphaTexArrayHandle, tempAlphaTextures[j]); } shader.SetValue(mShaderAlphaTexDecalHandle, active.mTexChannels[(int)BTerrainTexturing.eTextureChannels.cOpacity].mTexture); shader.SetValue(mShaderTexDecalHandle, active.mTexChannels[i].mTexture); float[] decalDat = new float[4]; decalDat[0] = decal.mRotation; //compute our U and V offset float vertsToHighResPixelSpaceRatio = BTerrainTexturing.getTextureWidth() / BTerrainQuadNode.cMaxWidth; decalDat[1] = (decal.mTileCenter.X - (minXVert * vertsToHighResPixelSpaceRatio)) / BTerrainTexturing.getTextureWidth(); decalDat[2] = (decal.mTileCenter.Y - (minZVert * vertsToHighResPixelSpaceRatio)) / BTerrainTexturing.getTextureHeight(); decalDat[3] = 0; shader.SetValue(mShaderLayerDecalData, decalDat); float[] uvs = new float[2]; uvs[0] = decal.mUScale; uvs[1] = decal.mVScale; shader.SetValue(mShaderLayerUV, uvs); } shader.CommitChanges(); BRenderDevice.getDevice().Viewport = vp; BRenderDevice.getDevice().DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2); } shader.EndPass(); shader.BeginPass(0); } } catch (Direct3DXException e) { CoreGlobals.getErrorManager().SendToErrorWarningViewer("An error has occured during the compositing process"); } finally { if (compositeTarget != null) { compositeTarget.Dispose(); } } } } shader.EndPass(); shader.End(); }
unsafe static public bool setMaskAlphaToLayer(BTerrainQuadNode node, UInt32 *mskImg, uint mskImgWidth, uint mskImgHeight, float alphaScalar, int terrainGridX, int terrainGridZ, char index) { bool changed = false; uint dstImgWidth = BTerrainTexturing.getAlphaTextureWidth(); uint dstImgHeight = BTerrainTexturing.getAlphaTextureWidth(); float vertsToPixelsRatio = BTerrainTexturing.getAlphaTextureWidth() / (float)BTerrainQuadNode.getMaxNodeWidth(); int minxvert = node.getDesc().mMinXVert; int minzvert = node.getDesc().mMinZVert; int maxxvert = node.getDesc().mMaxXVert; int maxzvert = node.getDesc().mMaxZVert; BTerrainLayerContainer layers = node.mLayerContainer; int layerIndex = layers.giveLayerIndex(index, BTerrainTexturingLayer.eLayerType.cLayer_Splat); if (TerrainGlobals.getEditor().mEraseTextureInstead&& layerIndex == 0) { return(false); } // layers.removeRedundantLayers();//CLM is this still needed?!??! if (layerIndex == -1) { if (TerrainGlobals.getEditor().mEraseTextureInstead) //we're erasing, and the texture doesn't exist.. { return(false); } //i don't exist yet. newSplatLayerEverywhere(index); layerIndex = layers.giveLayerIndex(index, BTerrainTexturingLayer.eLayerType.cLayer_Splat); BTerrainTexturingLayer tLayer = layers.giveLayer(layerIndex); for (int x = 0; x < BTerrainTexturing.getAlphaTextureWidth(); x++) { for (int z = 0; z < BTerrainTexturing.getAlphaTextureWidth(); z++) { float curWeight = TerrainGlobals.getTerrain().getSoftSelectionWeight(terrainGridX, terrainGridZ); if (!BMathLib.compare(curWeight, 0.0f)) { int cX = (int)((x * vertsToPixelsRatio) + minxvert); int cZ = (int)((z * vertsToPixelsRatio) + minzvert); curWeight = BMathLib.Clamp(curWeight, 0, 1); int dIP = x + ((int)BTerrainTexturing.getAlphaTextureHeight()) * z; byte alphaMapVal = giveBrushAlphaValue(mskImg, mskImgWidth, mskImgHeight, minxvert, minzvert, maxxvert, maxzvert, cX, cZ, terrainGridX, terrainGridZ); byte val = (byte)(alphaMapVal * alphaScalar * curWeight); tLayer.mAlphaLayer[dIP] = val; } } } changed = true; } else { BTerrainTexturingLayer tLayer = layers.giveLayer(layerIndex); //this layer already exists. //If a pixel exists above me, subtract me from it, rather than adding to me. for (int x = 0; x < BTerrainTexturing.getAlphaTextureWidth(); x++) { for (int z = 0; z < BTerrainTexturing.getAlphaTextureWidth(); z++) { //find the closest vert int cX = (int)((x * vertsToPixelsRatio) + minxvert); int cZ = (int)((z * vertsToPixelsRatio) + minzvert); int dIP = x + ((int)BTerrainTexturing.getAlphaTextureHeight()) * z; float curWeight = TerrainGlobals.getTerrain().getSoftSelectionWeight(terrainGridX, terrainGridZ); if (!BMathLib.compare(curWeight, 0.0f)) { changed = true; curWeight = BMathLib.Clamp(curWeight, 0, 1); byte alphaMapVal = giveBrushAlphaValue(mskImg, mskImgWidth, mskImgHeight, minxvert, minzvert, maxxvert, maxzvert, cX, cZ, terrainGridX, terrainGridZ); byte val = (byte)(alphaMapVal * alphaScalar * curWeight); valToLayerColumn(layers, tLayer, dIP, val, layerIndex); } } } } layers.removeBlankLayers(); return(changed); }
unsafe static void valToLayerColumn(BTerrainLayerContainer layers, BTerrainTexturingLayer tLayer, int dIP, byte val, int layerIndex) { if (val == 0) { return; } if (TerrainGlobals.getEditor().mEraseTextureInstead) { //if i'm erasing, ignore layers above me and just get rid of my values.. tLayer.mAlphaLayer[dIP] = (byte)(BMathLib.Clamp(tLayer.mAlphaLayer[dIP] - val, 0, 255)); } else { if (layerIndex + 1 >= layers.getNumLayers()) { //no layers above me tLayer.mAlphaLayer[dIP] = (byte)(BMathLib.Clamp(tLayer.mAlphaLayer[dIP] + val, 0, 255)); } else { float maxByte = 0; List <float> vList = new List <float>(); for (int k = layerIndex + 1; k < layers.getNumLayers(); k++) { if (layers.giveLayer(k).mLayerType == BTerrainTexturingLayer.eLayerType.cLayer_Splat && layers.giveLayer(k).mAlphaLayer[dIP] != byte.MinValue) { float v = layers.giveLayer(k).mAlphaLayer[dIP]; vList.Add(v); if (v > maxByte) { maxByte = v; } } } if (vList.Count == 0) { tLayer.mAlphaLayer[dIP] = (byte)(BMathLib.Clamp(tLayer.mAlphaLayer[dIP] + val, 0, 255)); return; } if (tLayer.mAlphaLayer[dIP] < maxByte) { tLayer.mAlphaLayer[dIP] = (byte)maxByte; } //normalize the contribs for everything above me floatN vec = new floatN((uint)(vList.Count)); vec.set(vList); vec = floatN.Normalize(vec); byte hVal = (byte)(val); uint T = 0; int remainder = 0; for (int k = layerIndex + 1; k < layers.getNumLayers(); k++) { if (layers.giveLayer(k).mLayerType == BTerrainTexturingLayer.eLayerType.cLayer_Splat && layers.giveLayer(k).mAlphaLayer[dIP] != byte.MinValue) { float amt = vec[T++]; int kQ = layers.giveLayer(k).mAlphaLayer[dIP]; int Del = (int)(((hVal * amt) + remainder)); int R = kQ - Del; if (R < 0) { remainder = Del - kQ; layers.giveLayer(k).mAlphaLayer[dIP] = 0; } else { layers.giveLayer(k).mAlphaLayer[dIP] = (byte)BMathLib.Clamp(R, 0, 255); remainder = 0; } } } tLayer.mAlphaLayer[dIP] = (byte)(BMathLib.Clamp(tLayer.mAlphaLayer[dIP] + remainder + val, 0, 255)); } } }
//---------------------------------------------- unsafe static public bool setIndexToMaskedArea(BTerrainQuadNode node, int minxvert, int minzvert, int maxxvert, int maxzvert, char index) { bool changed = false; uint dstImgWidth = BTerrainTexturing.getAlphaTextureWidth(); uint dstImgHeight = BTerrainTexturing.getAlphaTextureWidth(); float vertsToPixelsRatio = BTerrainTexturing.getAlphaTextureWidth() / (float)BTerrainQuadNode.getMaxNodeWidth(); BTerrainLayerContainer layers = node.mLayerContainer; int layerIndex = layers.giveLayerIndex(index, BTerrainTexturingLayer.eLayerType.cLayer_Splat); if (TerrainGlobals.getEditor().mEraseTextureInstead&& layerIndex == 0) { return(false); } //layers.removeRedundantLayers();//CLM is this still needed?!??! if (layerIndex == -1) { if (TerrainGlobals.getEditor().mEraseTextureInstead) //we're erasing, and the texture doesn't exist.. { return(false); } //i don't exist yet. newSplatLayerEverywhere(index); layerIndex = layers.giveLayerIndex(index, BTerrainTexturingLayer.eLayerType.cLayer_Splat); BTerrainTexturingLayer tLayer = layers.giveLayer(layerIndex); for (int x = 0; x < BTerrainTexturing.getAlphaTextureWidth(); x++) { for (int z = 0; z < BTerrainTexturing.getAlphaTextureWidth(); z++) { //find the closest vert int cX = (int)((x * vertsToPixelsRatio) + minxvert); int cZ = (int)((z * vertsToPixelsRatio) + minzvert); int vertIndex = (int)(cX * TerrainGlobals.getTerrain().getNumZVerts() + cZ); float curWeight = 1.0f; if (Masking.isPointSelected(vertIndex, ref curWeight)) { curWeight = BMathLib.Clamp(curWeight, 0, 1); int dIP = x + ((int)BTerrainTexturing.getAlphaTextureHeight()) * z; byte val = (byte)(curWeight * byte.MaxValue); tLayer.mAlphaLayer[dIP] = val; } } } changed = true; } else { BTerrainTexturingLayer tLayer = layers.giveLayer(layerIndex); //this layer already exists. //If a pixel exists above me, subtract me from it, rather than adding to me. for (int x = 0; x < BTerrainTexturing.getAlphaTextureWidth(); x++) { for (int z = 0; z < BTerrainTexturing.getAlphaTextureWidth(); z++) { //find the closest vert int cX = (int)((x * vertsToPixelsRatio) + minxvert); int cZ = (int)((z * vertsToPixelsRatio) + minzvert); int vertIndex = (int)(cX * TerrainGlobals.getTerrain().getNumZVerts() + cZ); float curWeight = 1.0f; if (Masking.isPointSelected(vertIndex, ref curWeight)) { changed = true; int dIP = x + ((int)BTerrainTexturing.getAlphaTextureHeight()) * z; curWeight = BMathLib.Clamp(curWeight, 0, 1); byte val = (byte)(curWeight * byte.MaxValue); valToLayerColumn(layers, tLayer, dIP, val, layerIndex); } } } } layers.removeBlankLayers(); return(changed); }