diff --git a/src/Sky.hx b/src/Sky.hx index 24671f21..9cb60b19 100644 --- a/src/Sky.hx +++ b/src/Sky.hx @@ -1,5 +1,6 @@ package src; +import hxd.Pixels; import shaders.Skybox; import h3d.shader.pbr.PropsValues; import h3d.shader.AmbientLight; @@ -38,7 +39,7 @@ class Sky extends Object { skyMesh.material.blendMode = None; // var pbrprops = skyMesh.material.mainPass.getShader(PropsValues); // pbrprops.emissiveValue = 1; - // pbrprops.roughnessValue = 0; + // pbrprops.roughnessValue = 0;` // pbrprops.occlusionValue = 0; // pbrprops.metalnessValue = 1; @@ -76,10 +77,14 @@ class Sky extends Object { var line = StringTools.trim(lines[i]); var filenames = ResourceLoader.getFullNamesOf(dmlDirectory + '/' + line); if (filenames.length == 0) { - skyboxImages.push(new BitmapData(128, 128)); + var pixels = Texture.fromColor(0).capturePixels(0, 0); + skyboxImages.push(pixels); + // var tex = new h3d.mat.Texture(); + // skyboxImages.push(new BitmapData(128, 128)); } else { - var image = ResourceLoader.getResource(filenames[0], ResourceLoader.getImage, this.imageResources).toBitmap(); - skyboxImages.push(image); + var image = ResourceLoader.getResource(filenames[0], ResourceLoader.getImage, this.imageResources).toTexture(); + var pixels = image.capturePixels(0, 0); + skyboxImages.push(pixels); } } var maxwidth = 0; @@ -104,7 +109,7 @@ class Sky extends Object { var cubemaptexture = new Texture(maxheight, maxwidth, [Cube]); for (i in 0...6) { - cubemaptexture.uploadBitmap(skyboxImages[skyboxIndices[i]], 0, i); + cubemaptexture.uploadPixels(skyboxImages[skyboxIndices[i]], 0, i); } return cubemaptexture; } diff --git a/src/Util.hx b/src/Util.hx index bb856a0d..16988326 100644 --- a/src/Util.hx +++ b/src/Util.hx @@ -38,46 +38,130 @@ class Util { return new Vector(lerp(v1.x, v2.x, t), lerp(v1.y, v2.y, t), lerp(v1.z, v2.z, t), lerp(v1.w, v2.w, t)); } - public static function rotateImage(bitmap:BitmapData, angle:Float) { - bitmap.lock(); - var curpixels = bitmap.getPixels().clone(); + public static function rotateImage(bitmap:hxd.Pixels, angle:Float) { + var curpixels = bitmap.clone(); if (angle == Math.PI / 2) for (x in 0...curpixels.width) { for (y in 0...curpixels.height) { - bitmap.setPixel(x, y, curpixels.getPixel(y, curpixels.height - x - 1)); + var psrc = ((y + (curpixels.height - x - 1) * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset; + var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset; + + switch (curpixels.format) { + case R8: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + case BGRA | RGBA | ARGB: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1)); + bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2)); + bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3)); + case RG8: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1)); + default: + null; + } + + // bitmap.setPixel(x, y, curpixels.getPixel(y, curpixels.height - x - 1)); } } if (angle == -Math.PI / 2) for (x in 0...curpixels.width) { for (y in 0...curpixels.height) { - bitmap.setPixel(x, y, curpixels.getPixel(curpixels.width - y - 1, x)); + var psrc = ((curpixels.width - y - 1) + x * curpixels.width) * @:privateAccess curpixels.bytesPerPixel + curpixels.offset; + + var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset; + + switch (curpixels.format) { + case R8: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + case BGRA | RGBA | ARGB: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1)); + bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2)); + bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3)); + case RG8: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1)); + default: + null; + } } } if (angle == Math.PI) for (x in 0...curpixels.width) { for (y in 0...curpixels.height) { - bitmap.setPixel(x, y, curpixels.getPixel(curpixels.width - x - 1, curpixels.height - y - 1)); + var psrc = ((curpixels.width - x - 1) + + (curpixels.height - y - 1) * curpixels.width) * @:privateAccess curpixels.bytesPerPixel + + curpixels.offset; + + var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset; + + switch (curpixels.format) { + case R8: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + case BGRA | RGBA | ARGB: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1)); + bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2)); + bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3)); + case RG8: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1)); + default: + null; + } } } - bitmap.unlock(); } - public static function flipImage(bitmap:BitmapData, hflip:Bool, vflip:Bool) { - bitmap.lock(); - var curpixels = bitmap.getPixels().clone(); + public static function flipImage(bitmap:hxd.Pixels, hflip:Bool, vflip:Bool) { + var curpixels = bitmap.clone(); if (hflip) for (x in 0...curpixels.width) { for (y in 0...curpixels.height) { - bitmap.setPixel(x, y, curpixels.getPixel(curpixels.height - x - 1, y)); + var psrc = ((curpixels.width - x - 1) + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel + curpixels.offset; + + var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset; + + switch (curpixels.format) { + case R8: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + case BGRA | RGBA | ARGB: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1)); + bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2)); + bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3)); + case RG8: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1)); + default: + null; + } } } if (vflip) for (x in 0...curpixels.width) { for (y in 0...curpixels.height) { - bitmap.setPixel(x, y, curpixels.getPixel(x, curpixels.width - y - 1)); + var psrc = (x + (curpixels.width - y - 1) * curpixels.width) * @:privateAccess curpixels.bytesPerPixel + curpixels.offset; + + var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset; + + switch (curpixels.format) { + case R8: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + case BGRA | RGBA | ARGB: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1)); + bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2)); + bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3)); + case RG8: + bitmap.bytes.set(pdest, curpixels.bytes.get(psrc)); + bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1)); + default: + null; + } } } - bitmap.unlock(); } public static function splitIgnoreStringLiterals(str:String, splitter:String, strLiteralToken = '"') {