import java.nio.Buffer; import java.nio.ByteBuffer; import java.util.Random; /** * 317HD / RLHD Material definition and 64-layer 128x128 RGBA texture array generator. * Layer indices 0..49 correspond to enhanced RS2 cache textures; * Layer indices 50..63 correspond to dedicated 317HD procedural PBR materials. */ public final class HdMaterial { public static final int LAYER_COUNT = 64; public static final int TEX_SIZE = 128; // Dedicated 317HD Material Layer IDs (50..63) public static final int NONE = -1; public static final int HD_GRASS_1 = 50; public static final int HD_GRASS_2 = 51; public static final int HD_GRASS_3 = 52; public static final int HD_COBBLESTONE_1 = 53; public static final int HD_COBBLESTONE_2 = 54; public static final int HD_GRAVEL = 55; public static final int HD_STONE_PAVERS_1 = 56; public static final int HD_STONE_PAVERS_2 = 57; public static final int HD_DIRT_1 = 58; public static final int HD_DIRT_2 = 59; public static final int HD_SAND_1 = 60; public static final int HD_WOOD_FLOOR = 61; public static final int HD_WATER_WAVE_1 = 62; public static final int HD_WATER_WAVE_2 = 63; public final int layerId; public final float specularStrength; public final float specularGloss; public final float normalStrength; public final float uvScale; public final boolean isWater; public final boolean isEmissive; public static final HdMaterial[] MATERIALS = new HdMaterial[LAYER_COUNT]; static { // Initialize default cache texture slots (0..49) for (int i = 0; i < 50; i++) { boolean water = (i == 1 || i == 17 || i == 24 || i == 34); boolean emissive = (i == 31 || i == 40); float spec = 0.12f; float gloss = 14.0f; float norm = 0.75f; if (water) { spec = 0.90f; gloss = 72.0f; norm = 1.0f; } else if (i == 2 || i == 11 || i == 23) { // Castle stone masonry walls spec = 0.25f; gloss = 18.0f; norm = 1.15f; } else if (i == 5 || i == 16) { // Roof shingles spec = 0.28f; gloss = 22.0f; norm = 1.05f; } else if (i == 3 || i == 4 || i == 0) { // Wood planks spec = 0.16f; gloss = 12.0f; norm = 0.85f; } MATERIALS[i] = new HdMaterial(i, spec, gloss, norm, 1.0f, water, emissive); } // Initialize 317HD Ground & Architectural Materials (50..63) MATERIALS[HD_GRASS_1] = new HdMaterial(HD_GRASS_1, 0.08f, 10.0f, 0.70f, 1.0f, false, false); MATERIALS[HD_GRASS_2] = new HdMaterial(HD_GRASS_2, 0.08f, 10.0f, 0.75f, 1.0f, false, false); MATERIALS[HD_GRASS_3] = new HdMaterial(HD_GRASS_3, 0.10f, 12.0f, 0.70f, 1.0f, false, false); MATERIALS[HD_COBBLESTONE_1] = new HdMaterial(HD_COBBLESTONE_1, 0.28f, 22.0f, 0.75f, 1.0f, false, false); MATERIALS[HD_COBBLESTONE_2] = new HdMaterial(HD_COBBLESTONE_2, 0.26f, 20.0f, 0.70f, 1.0f, false, false); MATERIALS[HD_GRAVEL] = new HdMaterial(HD_GRAVEL, 0.20f, 16.0f, 0.65f, 1.0f, false, false); MATERIALS[HD_STONE_PAVERS_1] = new HdMaterial(HD_STONE_PAVERS_1, 0.35f, 28.0f, 1.10f, 1.0f, false, false); MATERIALS[HD_STONE_PAVERS_2] = new HdMaterial(HD_STONE_PAVERS_2, 0.30f, 24.0f, 1.05f, 1.0f, false, false); MATERIALS[HD_DIRT_1] = new HdMaterial(HD_DIRT_1, 0.10f, 10.0f, 0.90f, 1.0f, false, false); MATERIALS[HD_DIRT_2] = new HdMaterial(HD_DIRT_2, 0.14f, 12.0f, 0.95f, 1.0f, false, false); MATERIALS[HD_SAND_1] = new HdMaterial(HD_SAND_1, 0.20f, 18.0f, 0.80f, 1.0f, false, false); MATERIALS[HD_WOOD_FLOOR] = new HdMaterial(HD_WOOD_FLOOR, 0.22f, 18.0f, 0.85f, 1.0f, false, false); MATERIALS[HD_WATER_WAVE_1] = new HdMaterial(HD_WATER_WAVE_1, 0.95f, 80.0f, 1.0f, 1.0f, true, false); MATERIALS[HD_WATER_WAVE_2] = new HdMaterial(HD_WATER_WAVE_2, 0.95f, 80.0f, 1.0f, 1.0f, true, false); } public HdMaterial(int layerId, float specularStrength, float specularGloss, float normalStrength, float uvScale, boolean isWater, boolean isEmissive) { this.layerId = layerId; this.specularStrength = specularStrength; this.specularGloss = specularGloss; this.normalStrength = normalStrength; this.uvScale = uvScale; this.isWater = isWater; this.isEmissive = isEmissive; } public static HdMaterial get(int layerId) { if (layerId >= 0 && layerId < LAYER_COUNT) { return MATERIALS[layerId]; } return MATERIALS[HD_COBBLESTONE_1]; } /** * Populates the 64-layer 128x128 RGBA8 diffuse+heightfield and Sobel normal+specular buffers. */ public static void populateTextureBuffers(ByteBuffer diffuseBuf, ByteBuffer normalBuf) { final int size = TEX_SIZE; final int layers = LAYER_COUNT; final int layerPixels = size * size; float[] heightField = new float[layerPixels]; int[] rgbField = new int[layerPixels]; boolean[] cutoutField = new boolean[layerPixels]; // 1. Build Layers 0..49 from RS2 Cache Textures + 317HD Architectural Enhancements final int layerBytes = size * size * 4; for (int texId = 0; texId < 50; texId++) { buildCacheTextureLayer(texId, rgbField, heightField, cutoutField); writeLayerToBuffers(texId, texId * layerBytes, rgbField, heightField, cutoutField, get(texId).normalStrength, diffuseBuf, normalBuf); } // 2. Build Layers 50..63 (Dedicated 317HD Procedural PBR Materials) for (int matId = 50; matId < layers; matId++) { buildProceduralHdLayer(matId, rgbField, heightField, cutoutField); writeLayerToBuffers(matId, matId * layerBytes, rgbField, heightField, cutoutField, get(matId).normalStrength, diffuseBuf, normalBuf); } ((Buffer) diffuseBuf).position(0); ((Buffer) normalBuf).position(0); } /** * Populates a single 128x128 RGBA8 diffuse+heightfield and Sobel normal+specular layer at offset 0. * Useful on memory-constrained platforms (e.g., Android) to stream layers via glTexSubImage3D. */ public static void populateSingleLayer(int layerId, ByteBuffer diffuseLayerBuf, ByteBuffer normalLayerBuf) { final int layerPixels = TEX_SIZE * TEX_SIZE; float[] heightField = new float[layerPixels]; int[] rgbField = new int[layerPixels]; boolean[] cutoutField = new boolean[layerPixels]; if (layerId < 50) { buildCacheTextureLayer(layerId, rgbField, heightField, cutoutField); } else { buildProceduralHdLayer(layerId, rgbField, heightField, cutoutField); } writeLayerToBuffers(layerId, 0, rgbField, heightField, cutoutField, get(layerId).normalStrength, diffuseLayerBuf, normalLayerBuf); ((Buffer) diffuseLayerBuf).position(0); ((Buffer) normalLayerBuf).position(0); } private static void buildCacheTextureLayer(int texId, int[] rgbField, float[] heightField, boolean[] cutoutField) { int[] srcPixels = null; try { srcPixels = Texture.method371(texId); } catch (Throwable ignored) { } Background bg = (Texture.aBackgroundArray1474s != null && texId < Texture.aBackgroundArray1474s.length) ? Texture.aBackgroundArray1474s[texId] : null; int[] palette = (Texture.anIntArrayArray1483 != null && texId < Texture.anIntArrayArray1483.length) ? Texture.anIntArrayArray1483[texId] : null; // Upsample 64x64 cache textures with bilinear filtering while preserving cutout transparency if (bg != null && palette != null && bg.aByteArray1450 != null && bg.anInt1452 == 64 && bg.anInt1453 == 64) { byte[] raw = bg.aByteArray1450; for (int y = 0; y < 128; y++) { int sy0 = y >> 1; int sy1 = (sy0 + 1) & 63; int wy1 = (y & 1) * 128; int wy0 = 256 - wy1; for (int x = 0; x < 128; x++) { int sx0 = x >> 1; int sx1 = (sx0 + 1) & 63; int wx1 = (x & 1) * 128; int wx0 = 256 - wx1; int i00 = raw[(sy0 << 6) + sx0] & 0xFF; int i10 = raw[(sy0 << 6) + sx1] & 0xFF; int i01 = raw[(sy1 << 6) + sx0] & 0xFF; int i11 = raw[(sy1 << 6) + sx1] & 0xFF; int idx = (y << 7) | x; if (i00 == 0 || i10 == 0 || i01 == 0 || i11 == 0) { int c = palette[i00]; cutoutField[idx] = (i00 == 0 || c == 0); rgbField[idx] = c; heightField[idx] = cutoutField[idx] ? 0f : 0.5f; continue; } int c00 = palette[i00]; int c10 = palette[i10]; int c01 = palette[i01]; int c11 = palette[i11]; int r = (((c00 >> 16 & 0xFF) * wx0 + (c10 >> 16 & 0xFF) * wx1) * wy0 + ((c01 >> 16 & 0xFF) * wx0 + (c11 >> 16 & 0xFF) * wx1) * wy1) >> 16; int g = (((c00 >> 8 & 0xFF) * wx0 + (c10 >> 8 & 0xFF) * wx1) * wy0 + ((c01 >> 8 & 0xFF) * wx0 + (c11 >> 8 & 0xFF) * wx1) * wy1) >> 16; int b = (((c00 & 0xFF) * wx0 + (c10 & 0xFF) * wx1) * wy0 + ((c01 & 0xFF) * wx0 + (c11 & 0xFF) * wx1) * wy1) >> 16; cutoutField[idx] = false; rgbField[idx] = (r << 16) | (g << 8) | b; float lum = (r * 0.299f + g * 0.587f + b * 0.114f) / 255.0f; heightField[idx] = Math.max(0.08f, Math.min(0.95f, lum)); } } } else if (srcPixels != null && srcPixels.length >= 16384) { for (int i = 0; i < 16384; i++) { int c = srcPixels[i]; if (c == 0) { cutoutField[i] = true; rgbField[i] = 0; heightField[i] = 0f; } else { cutoutField[i] = false; rgbField[i] = c & 0xFFFFFF; int r = (c >> 16) & 0xFF; int g = (c >> 8) & 0xFF; int b = c & 0xFF; float lum = (r * 0.299f + g * 0.587f + b * 0.114f) / 255.0f; heightField[i] = Math.max(0.08f, Math.min(0.95f, lum)); } } } else { for (int i = 0; i < 16384; i++) { cutoutField[i] = false; rgbField[i] = 0x808080; heightField[i] = 0.5f; } } // Dilate opaque neighbor RGB into transparent cutout pixels (4 passes) so GL_LINEAR never bleeds black for (int pass = 0; pass < 4; pass++) { for (int y = 0; y < 128; y++) { int ym1 = (y - 1) & 127; int yp1 = (y + 1) & 127; for (int x = 0; x < 128; x++) { int idx = (y << 7) | x; if (rgbField[idx] != 0) continue; int xm1 = (x - 1) & 127; int xp1 = (x + 1) & 127; int n1 = rgbField[(ym1 << 7) | x]; int n2 = rgbField[(yp1 << 7) | x]; int n3 = rgbField[(y << 7) | xm1]; int n4 = rgbField[(y << 7) | xp1]; if (n1 != 0) rgbField[idx] = n1; else if (n2 != 0) rgbField[idx] = n2; else if (n3 != 0) rgbField[idx] = n3; else if (n4 != 0) rgbField[idx] = n4; } } } } private static void buildProceduralHdLayer(int matId, int[] rgbField, float[] heightField, boolean[] cutoutField) { for (int i = 0; i < 16384; i++) { cutoutField[i] = false; } if (matId == HD_GRASS_1 || matId == HD_GRASS_2 || matId == HD_GRASS_3) { int variant = matId - HD_GRASS_1; Random rnd = new Random(5001L + variant * 313L); float[] noise = new float[16384]; for (int i = 0; i < 16384; i++) { noise[i] = rnd.nextFloat(); } for (int y = 0; y < 128; y++) { int ym1 = (y - 1) & 127; int yp1 = (y + 1) & 127; double ny = y * 2.0 * Math.PI / 128.0; for (int x = 0; x < 128; x++) { int xm1 = (x - 1) & 127; int xp1 = (x + 1) & 127; double nx = x * 2.0 * Math.PI / 128.0; float fine = (noise[(y << 7) | x] * 0.40f + noise[(ym1 << 7) | x] * 0.15f + noise[(yp1 << 7) | x] * 0.15f + noise[(y << 7) | xm1] * 0.15f + noise[(y << 7) | xp1] * 0.15f); float patch = (float) (Math.sin(nx * 2.0 + variant) * Math.cos(ny * 2.0 - variant) * 0.5 + 0.5); float blade = fine * 0.72f + patch * 0.28f; float h = Math.max(0.24f, Math.min(0.68f, 0.28f + blade * 0.34f)); int modV = clampByte((int) (222 + blade * 28)); int idx = (y << 7) | x; rgbField[idx] = (modV << 16) | (modV << 8) | modV; heightField[idx] = h; } } } else if (matId == HD_COBBLESTONE_1 || matId == HD_COBBLESTONE_2 || matId == HD_GRAVEL) { int gridN = (matId == HD_GRAVEL) ? 13 : (matId == HD_COBBLESTONE_1 ? 10 : 11); double cellSize = 128.0 / gridN; int numCells = gridN * gridN; double[] cx = new double[numCells]; double[] cy = new double[numCells]; int[] stoneTintR = new int[numCells]; int[] stoneTintG = new int[numCells]; int[] stoneTintB = new int[numCells]; Random rnd = new Random(11700L + matId * 97L); for (int gy = 0; gy < gridN; gy++) { for (int gx = 0; gx < gridN; gx++) { int ci = gy * gridN + gx; cx[ci] = (gx + 0.5 + (rnd.nextDouble() - 0.5) * 0.65) * cellSize; cy[ci] = (gy + 0.5 + (rnd.nextDouble() - 0.5) * 0.65) * cellSize; int shade = 224 + rnd.nextInt(20) - 10; stoneTintR[ci] = shade; stoneTintG[ci] = shade; stoneTintB[ci] = shade; } } double stoneRadius = cellSize * 0.54; for (int y = 0; y < 128; y++) { for (int x = 0; x < 128; x++) { double minD = 999.0; double secD = 999.0; int closestCell = 0; for (int c = 0; c < numCells; c++) { double dx = Math.abs(x - cx[c]); if (dx > 64.0) dx = 128.0 - dx; double dy = Math.abs(y - cy[c]); if (dy > 64.0) dy = 128.0 - dy; double d = Math.sqrt(dx * dx + dy * dy); if (d < minD) { secD = minD; minD = d; closestCell = c; } else if (d < secD) { secD = d; } } double edge = secD - minD; int idx = (y << 7) | x; if (edge < 1.35) { float t = (float) (edge / 1.35); int v = clampByte((int) (182 + t * 28)); rgbField[idx] = (v << 16) | (v << 8) | v; heightField[idx] = 0.28f + t * 0.22f; } else { double normD = Math.min(1.0, minD / stoneRadius); float dome = (float) Math.cos(normD * Math.PI * 0.42); float scale = 0.90f + dome * 0.14f; int r = clampByte((int) (stoneTintR[closestCell] * scale)); int g = clampByte((int) (stoneTintG[closestCell] * scale)); int b = clampByte((int) (stoneTintB[closestCell] * scale)); rgbField[idx] = (r << 16) | (g << 8) | b; heightField[idx] = Math.max(0.55f, Math.min(0.92f, 0.58f + dome * 0.32f)); } } } } else if (matId == HD_STONE_PAVERS_1 || matId == HD_STONE_PAVERS_2) { if (matId == HD_STONE_PAVERS_2) { // Smooth, honed indoor architectural stone/marble floor (no sub-tile mortar grid or cobble bumps, // preserving crisp 1x1 castle checkerboard squares and authentic tile colors) Random rnd = new Random(6401L); for (int y = 0; y < 128; y++) { for (int x = 0; x < 128; x++) { int idx = (y << 7) | x; int fine = (int) ((rnd.nextFloat() - 0.5f) * 6.0f); int v = clampByte(244 + fine); rgbField[idx] = (v << 16) | (v << 8) | v; heightField[idx] = 0.75f + fine * 0.002f; } } } else { int tileW = 32; int tileH = 32; for (int y = 0; y < 128; y++) { int row = y / tileH; int shiftX = (row % 2 == 1) ? (tileW / 2) : 0; int cy = y % tileH; for (int x = 0; x < 128; x++) { int cx = (x + shiftX) % tileW; int idx = (y << 7) | x; int grain = (((x * 17 + y * 31) ^ (x * 5)) & 15) - 8; if (cx == 0 || cy == 0) { rgbField[idx] = 0xC8C8C8; heightField[idx] = 0.45f; } else { int v = clampByte(238 + grain / 2); rgbField[idx] = (v << 16) | (v << 8) | v; heightField[idx] = 0.80f + grain * 0.003f; } } } } } else if (matId == HD_DIRT_1 || matId == HD_DIRT_2) { int variant = matId - HD_DIRT_1; Random rnd = new Random(7301L + variant * 443L); float[] dNoise = new float[16384]; for (int i = 0; i < 16384; i++) { dNoise[i] = rnd.nextFloat(); } float[] blurred = new float[16384]; for (int pass = 0; pass < 2; pass++) { float[] src = (pass == 0) ? dNoise : blurred; for (int y = 0; y < 128; y++) { int ym1 = (y - 1) & 127, yp1 = (y + 1) & 127; for (int x = 0; x < 128; x++) { int xm1 = (x - 1) & 127, xp1 = (x + 1) & 127; blurred[(y << 7) | x] = src[(y << 7) | x] * 0.40f + (src[(ym1 << 7) | x] + src[(yp1 << 7) | x] + src[(y << 7) | xm1] + src[(y << 7) | xp1]) * 0.15f; } } } for (int y = 0; y < 128; y++) { double ny = y * 2.0 * Math.PI / 128.0; for (int x = 0; x < 128; x++) { double nx = x * 2.0 * Math.PI / 128.0; float soilLobe = (float) (Math.sin(nx + variant) * Math.cos(ny - variant) * 0.5 + 0.5); float fineGrain = (dNoise[(y << 7) | x] - 0.5f) * 14.0f; float soilVal = blurred[(y << 7) | x] * 0.70f + soilLobe * 0.30f; // Hue-neutral detail multiplier so gray rock/stone never shifts toward muddy brown int v = clampByte((int) (220 + soilVal * 26 + fineGrain)); int idx = (y << 7) | x; rgbField[idx] = (v << 16) | (v << 8) | v; heightField[idx] = Math.max(0.20f, Math.min(0.75f, 0.32f + soilVal * 0.38f)); } } } else if (matId == HD_SAND_1) { Random rnd = new Random(9901L); for (int y = 0; y < 128; y++) { double ny = y * 2.0 * Math.PI / 128.0; for (int x = 0; x < 128; x++) { double nx = x * 2.0 * Math.PI / 128.0; double gentleDune = Math.sin(nx * 3.0 + Math.sin(ny * 2.0) * 0.8) * 0.5 + 0.5; float fineGrain = (rnd.nextFloat() - 0.5f) * 10.0f; int v = clampByte((int) (228 + gentleDune * 16 + fineGrain)); int idx = (y << 7) | x; rgbField[idx] = (v << 16) | (v << 8) | v; heightField[idx] = (float) (0.35 + gentleDune * 0.30); } } } else if (matId == HD_WATER_WAVE_1 || matId == HD_WATER_WAVE_2) { int variant = matId - HD_WATER_WAVE_1; Random rnd = new Random(8821L + variant * 7919L); float[] wNoise = new float[16384]; for (int i = 0; i < 16384; i++) { wNoise[i] = rnd.nextFloat(); } float[] blurred = new float[16384]; for (int pass = 0; pass < 2; pass++) { float[] src = (pass == 0) ? wNoise : blurred; for (int y = 0; y < 128; y++) { int ym1 = (y - 1) & 127, yp1 = (y + 1) & 127; for (int x = 0; x < 128; x++) { int xm1 = (x - 1) & 127, xp1 = (x + 1) & 127; blurred[(y << 7) | x] = src[(y << 7) | x] * 0.40f + (src[(ym1 << 7) | x] + src[(yp1 << 7) | x] + src[(y << 7) | xm1] + src[(y << 7) | xp1]) * 0.15f; } } } for (int y = 0; y < 128; y++) { double ny = y * 2.0 * Math.PI / 128.0; for (int x = 0; x < 128; x++) { double nx = x * 2.0 * Math.PI / 128.0; double wave = Math.sin(nx * 2.0 + ny * 1.0 + variant) * 0.35 + Math.sin(-nx * 1.0 + ny * 3.0 - variant * 1.5) * 0.30 + Math.cos(nx * 3.0 - ny * 2.0 + variant * 2.0) * 0.20; float noiseVal = blurred[(y << 7) | x]; float h = (float) (0.50 + wave * 0.22 + (noiseVal - 0.5) * 0.28); int idx = (y << 7) | x; rgbField[idx] = 0x2A6296; heightField[idx] = Math.max(0.10f, Math.min(0.90f, h)); } } } else { for (int y = 0; y < 128; y++) { for (int x = 0; x < 128; x++) { int idx = (y << 7) | x; int px = x % 32; int grain = ((y * 13 + x * 7) & 7) - 4; if (px == 0) { rgbField[idx] = 0x705840; heightField[idx] = 0.20f; } else { int v = clampByte(224 + grain); rgbField[idx] = (v << 16) | (clampByte(v - 12) << 8) | clampByte(v - 24); heightField[idx] = 0.65f; } } } } } private static void writeLayerToBuffers(int layerId, int baseOffset, int[] rgbField, float[] heightField, boolean[] cutoutField, float normalStrength, ByteBuffer diffuseBuf, ByteBuffer normalBuf) { final int size = TEX_SIZE; final int mask = size - 1; ((Buffer) diffuseBuf).position(baseOffset); ((Buffer) normalBuf).position(baseOffset); for (int y = 0; y < size; y++) { int ym1 = (y - 1) & mask; int yp1 = (y + 1) & mask; for (int x = 0; x < size; x++) { int xm1 = (x - 1) & mask; int xp1 = (x + 1) & mask; int idx = (y << 7) | x; int c = rgbField[idx]; int r = (c >> 16) & 0xFF; int g = (c >> 8) & 0xFF; int b = c & 0xFF; int a; if (layerId == 24 || layerId == 17) { if (cutoutField[idx]) { a = 0; } else { a = Math.max(0, Math.min(255, (int) (heightField[idx] * 255.0f))); } } else if (cutoutField[idx]) { a = 0; } else if (layerId < 50) { a = 255; } else { a = Math.max(8, Math.min(255, (int) (heightField[idx] * 255.0f))); } diffuseBuf.put((byte) r); diffuseBuf.put((byte) g); diffuseBuf.put((byte) b); diffuseBuf.put((byte) a); // Wrapped 3x3 Sobel normal calculation from heightField float hTL = heightField[(ym1 << 7) | xm1]; float hT = heightField[(ym1 << 7) | x]; float hTR = heightField[(ym1 << 7) | xp1]; float hL = heightField[(y << 7) | xm1]; float hR = heightField[(y << 7) | xp1]; float hBL = heightField[(yp1 << 7) | xm1]; float hB = heightField[(yp1 << 7) | x]; float hBR = heightField[(yp1 << 7) | xp1]; float dX = (hTR + 2.0f * hR + hBR) - (hTL + 2.0f * hL + hBL); float dY = (hBL + 2.0f * hB + hBR) - (hTL + 2.0f * hT + hTR); float nx = -dX * normalStrength * 2.2f; float ny = -dY * normalStrength * 2.2f; float nz = 1.0f; float invLen = (float) (1.0 / Math.sqrt(nx * nx + ny * ny + nz * nz)); nx *= invLen; ny *= invLen; nz *= invLen; int nR = clampByte((int) ((nx * 0.5f + 0.5f) * 255.0f)); int nG = clampByte((int) ((ny * 0.5f + 0.5f) * 255.0f)); int nB = clampByte((int) ((nz * 0.5f + 0.5f) * 255.0f)); int specMask = Math.max(0, Math.min(255, (int) (heightField[idx] * 255.0f))); normalBuf.put((byte) nR); normalBuf.put((byte) nG); normalBuf.put((byte) nB); normalBuf.put((byte) specMask); } } } private static int clampByte(int val) { return val < 0 ? 0 : (val > 255 ? 255 : val); } }