* fix(client): restore authentic 317/377 minimap fallback walk radius in doWalkTo Restores the client-side pathfinding fallback search radius in `Game.doWalkTo` to match authentic 2006 RS2 / 317 client behavior when clicking unreachable tiles such as underground void or blocked terrain. Previously, the search radius was restricted to a single tile, causing minimap and ground clicks more than one tile into unwalkable areas to be silently ignored. ### Bug Fixes & Edge Cases - `2006Scape Client/src/main/java/Game.java` (`doWalkTo`): - Expanded the alternative-destination search loop from `k5 < 2` (1-tile radius) to `k5 < 11` (10-tile Chebyshev radius, matching authentic 317/377 clients) when `flag` is `true` and the exact target tile is unreachable. - Preserves the existing `i5 = 100` maximum path-cost threshold and `104x104` region boundary checks (`i6 >= 0 && l6 >= 0 && i6 < 104 && l6 < 104`), allowing players to walk to the nearest reachable tile when clicking black void on the minimap. Closes #380 * fix(firemaking): fix fire lighting race, object sync, level scaling, and log walk-on Fixes firemaking reliability, object lifecycle desyncs, and authentic 2006 RS2 movement and level-scaling behavior on the server. Previously, rapid firemaking could cancel the next attempt and still spawn phantom ashes, fires were spawned on hardcoded height 0 with mismatched ObjectManager/ObjectHandler state, and ground-log lighting triggered from an adjacent tile instead of walking onto the logs first. ### Bug Fixes & Edge Cases - `2006Scape Server/src/main/java/com/rs2/game/content/skills/firemaking/Firemaking.java`: - Removed the delayed `stopFiremaking(c)` call from the 2-tick post-lighting `turnPlayerTo` event so rapid consecutive firemaking attempts no longer cancel the next log's cycle. - Moved the `100 + Misc.random(98)` tick fire-despawn and ashes (`592`) event inside the lit branch so aborted/cancelled attempts never leave behind phantom ashes. - Spawned fires on `c.heightLevel` (instead of `0`) and removed `fireObj` directly from `GameEngine.objectManager.objects` via `updateObject(fireObj)` on burnout, eliminating `ObjectManager` stale-entry desyncs. - Replaced the redundant 4-iteration `firemakingItems` loop with `isFiremakingItem(...)`, gated colored firelighters (`7329`, `7330`, `7331`) to regular logs (`1511`), and ensured `stopFiremaking` / skilling interruptions call `container.stop()`. - `2006Scape Server/src/main/java/com/rs2/game/items/UseItem.java`: - Fixed the `||` / `&&` operator precedence bug on `firemakingItems` checks and added `break` after matching a firemaking item. - `2006Scape Server/src/main/java/com/rs2/net/packets/impl/ItemOnGroundItem.java`: - Added `isFiremakingItem(...)` and `isInRange(...)` so using a tinderbox or firelighter on ground logs requires standing on `(gItemX, gItemY)` rather than stopping 1 tile away. ### Authentic 2006 RS2 Behavior - `2006Scape Server/src/main/java/com/rs2/game/content/skills/firemaking/Firemaking.java`: - Moved `Region.getClipping` step-off (`West -> East -> South -> North`) and `walkTo` to execute when the fire catches rather than when lighting begins, and turned the player to `(x, y)` after stepping off. - Added `canLightLog(int level, LogData log)` using RS2 `interpolate(low, high, level)` rolls out of 256 (re-rolling every 3 ticks on failure with animation `733` and `SoundList.FIRE_LIGHT`). - `2006Scape Server/src/main/java/com/rs2/world/clip/Region.java` & `Firemaking.java`: - Added `Region.hasSceneryObject(x, y, z)` (`objectType >= 9 && objectType != 22`) and `Firemaking.isTileBlocked(x, y, height)` checking `ObjectManager`, `ObjectHandler`, static scenery objects, and tile clipping (`0x1280100`) before starting and before spawning the fire. Closes #295 Closes #543 * fix(objects): preserve cache-defined orientation when opening and closing chests Fixes chest and toggleable object rotation bugs where opening or closing an object not listed in `ObjectDefaults.GET_FACE` rotated it to face South (`0`), and `OpenObject` passed swapped `type`/`face` arguments to `Region.addObject`. Looking up the existing object's orientation from `Region` and `ObjectHandler` ensures all toggleable objects preserve their cache-defined facing across the world. ### Bug Fixes & Edge Cases - `2006Scape Server/src/main/java/com/rs2/game/objects/ObjectDefaults.java`: - Updated `getObjectFace(Player, int)` to check `Region.getObject(...)` and `GameEngine.objectHandler.objectExists(...)` for the current object's `objectFace` before falling back to the hardcoded `GET_FACE` / `switch` defaults. - `2006Scape Server/src/main/java/com/rs2/game/objects/impl/OpenObject.java`: - Preserved the object's `face` and `objectType` when toggling between closed and open IDs in `OBJECT_DATA`. - Removed the previous object from `Region` and `GameEngine.objectHandler.globalObjects` before adding and placing the toggled object, and fixed the swapped `type`/`face` parameter order in `Region.addObject(newId, x, y, h, type, face, false)`. Closes #549 * fix(desert): start desert heat timer on entry and remove shared static state Fixes instantaneous desert heat damage when entering the desert, eliminates per-tick `CycleEvent` accumulation, and removes shared `static` state across players. Previously, `player.lastDesert` defaulted to `0` on login and was never reset on exit, causing immediate damage on the first tick inside `Boundary.DESERT`, while `Player.process()` queued a new `CycleEvent` every tick. ### Bug Fixes & Edge Cases - `2006Scape Server/src/main/java/com/rs2/game/players/Player.java`: - Updated `process()` to invoke `DesertHeat.callHeat(this)` unconditionally so `lastDesert` resets whenever the player leaves `Boundary.DESERT`, moves off `heightLevel == 0`, or enters `Boundary.NO_HEAT`. - `2006Scape Server/src/main/java/com/rs2/game/content/traveling/DesertHeat.java`: - Initialized `player.lastDesert = now` on desert heat entry (`lastDesert == 0`) instead of applying damage on the first tick, and reset `lastDesert = 0` when outside desert heat. - Evaluated the `90s` base (`+10s` per equipped desert clothing piece) timer directly per tick in `callHeat(Player)` rather than spawning duplicate 1-tick `CycleEvent`s. - Replaced shared `static int DAMAGE` and `static int waterskin` fields with per-hit `1 + Misc.random(9)` (`1-10`) damage rolls and a local `checkWaterskin(Player)` loop. - Replaced hardcoded waterskin item IDs with `StaticItemList.WATERSKIN0` through `StaticItemList.WATERSKIN4` ordered from lowest to highest dose. Closes #596 * fix(shops): start restock timer on purchase from full stock Fixes instantaneous restocking of the first item purchased from a fully stocked shop and cleans up shop tick processing. Previously, `shopItemsRestock[shop_id][j]` remained `0` (or stale) when a player bought from full stock, causing `ShopHandler.process()` to immediately restock 1 item on the next tick. ### Bug Fixes & Edge Cases - `2006Scape Server/src/main/java/com/rs2/game/shops/ShopHandler.java`: - Updated `buyItem(int, int, int)` to set `shopItemsRestock[shop_id][j] = System.currentTimeMillis()` and reset `shopItemsDelay[shop_id][j] = 0` when a purchase reduces stock from at/above standard stock (`shopItemsSN`) to below `shopItemsSN`, without resetting an in-progress restock timer on subsequent purchases. - Fixed the off-by-one standard-stock boundary in `process()` (`j < shopItemsStandard[i]` instead of `<=`), removed the redundant `shopItemsDelay[i][j] = 1;` assignment, and gated `refreshshop(i)` on `shopUpdated` so unchanged shops are not broadcast every tick. - Cleared `shopItemsRestock[shopID][ArrayID] = 0` in `ResetItem(int, int)` when a shop slot is emptied. Closes #595 * feat(randomevents): migrate random events, lamps, and Thessalia to Kotlin plugins and add ::randomevent Rewrite all legacy Java random events, experience lamps, and Thessalia interactions into Kotlin plugins under plugins/game/content/randomevents/, plugins/game/content/items/lamps/, plugins/npc/thessalia/, and Revision 377 quest packages, while adding Zanaris Choir and developer testing commands. Core Java hooks now post RandomActionEvent and NpcDeathEvent, preserve legacy save migration via PlayerPluginData, and enforce authentic 2006 RS2 (Revision 377) mechanics, dialogues, and variable XP rate scaling. ### Command Infrastructure - `RandomActionEvent.java` & `NpcDeathEvent.java`: - Added core event classes posted from skilling/action/shop handlers (`Fishing`, `Woodcutting`, `Mining`, `Prayer`, `ShopAssistant.openShop`, `Cooking`, `Potatoes`, `Runecrafting`, `SoftClay`, `SearchForTraps`, `Pickpocket`, `Stalls`, `MagicTeleports`, `PlayerAssistant`, `NpcHandler`) and NPC death processing. - `ClickNPC.java` & `NpcHandler.java`: - Fixed `ClickNPC` to pass `npc.npcId` instead of `player.npcClickIndex` (which `firstClickNpc` resets to `0`) to `NpcFirstClickEvent`. - Added `NpcHandler.spawnNpcReturn` and guarded `npcs[i] == null` after `Player.postNpcTick` to prevent NPEs when tick plugins despawn NPCs. - `Player.java` & `PlayerSave.java`: - Removed obsolete random event fields from `Player` while migrating legacy `randomActions` and `recievedReward` save keys into `PlayerPluginData`. ### Migrated Command Plugins - `RandomToggle.kt` (`::random`, `::randomtoggle`, `::togglerandom`): - Migrated `randomEventsEnabled` toggling out of `Commands.java` into `plugins/commands/`. - `Teleport.kt` (`::teleport`, `::tele`): - Added `zanaris` (`2452, 4470, 0`) to `NAMED_LOCATIONS` and `VALID_CITIES_MESSAGES`. ### New Search Commands - `TriggerRandomEvent.kt` (`::randomevent <name>`, `::randomevents`, `::revent`): - Added developer command plugin to trigger `sandwich`, `chicken`, `forester`, `swarm`, `genie`, `frog`, `troll`, `golem`, `spirit`, `shade`, `zombie`, and `choir` on demand, with multi-line help and Zanaris location hint. ### Random Event & Lamp Plugins - `RandomEventSupport.kt`, `RandomEventDispatcherPlugin.kt`, `RandomEventNpcDeathPlugin.kt`, & `RandomEventFollowPlugin.kt`: - Implemented per-player session state, clipped adjacent tile spawning, active NPC slot tracking, unified non-combat NPC following/facing/despawn (`GENIE`, `SANDWICH_LADY`, `FROG_PRINCE`, `FROG_PRINCESS`, `ZANARIS_CHOIR`), and global guards (`randomEventsEnabled`, `!inFightCaves()`, `tutorialProgress >= 36`, `MONKEY_GREEGREE`). - `CombatRandoms.kt`: - Unified `EvilChicken`, `RiverTroll` (Fishing), `RockGolem` (Mining), `TreeSpirit` (Woodcutting), `Shade` & `Zombie` (Prayer bone burying), and `Swarm` with full `3..138` combat level coverage. - `GeniePlugin.kt`, `SandwichLadyPlugin.kt`, `SandwichLadyTalkPlugin.kt`, `FrogPlugin.kt`, `FreakyForesterPlugin.kt`, `FreakyForesterPortalPlugin.kt`, `ZanarisChoirPlugin.kt`, & `ZanarisChoirTalkPlugin.kt`: - Implemented authentic 2006 RS2 NPC spawns, dialogues (`Master`/`Mistress` Genie greeting, Kiss-the-Frog, Freaky Forester 1–4 tail pheasants, Sandwich Lady tray `16135`, Zanaris Choir), and `DialogueFactory.onAction` reward/despawn sequencing. - `LampInterfaceSupport.kt` & Revision 377 Lamp Plugins: - Replaced `ExperienceLamp.java` with config `261` (`1..21`) skill highlighting and `VARIABLE_XP_RATE` / `XP_RATE` scaling across Genie Lamp (`2528`, `10 * level`), One Small Favour (`4447`, level 30+, `10,000` XP), A Tail of Two Cats (`6543`, level 30+, `2,500` XP), Shadow of the Storm (`6796`, combat skills, `10,000` XP), and Recipe for Disaster (`7498`, level 50+, `20,000` XP). - `ThessaliaFirstClickPlugin.kt` & `ThessaliaSecondClickPlugin.kt`: - Migrated Thessalia (`548`) Talk-to shop/Frog Token (`6183`) exchange and Trade (`shop 12`) actions out of `DialogueHandler` and `Shops.java` into `plugins/npc/thessalia/`. ### Cleanups & Deprecations - Removed all 12 legacy Java classes under `com/rs2/game/content/randomevents/`, `ExperienceLamp.java`, and dead random-event/dialogue/button hooks across `ClickingButtons.java`, `ClickItem.java`, `DialogueHandler.java`, `NpcActions.java`, `ObjectsActions.java`, and `Shops.java`. * feat(audio): overhaul 377 music, audio playback, and GameOptionsPlugin Overhauls the 2006Scape Server and Client audio systems to match authentic 2006 RS2 (Revision 377) behavior, fixing MIDI deadlocks, volume persistence, title-screen music handoff, and varp-driven track unlocking. Consolidates all interface 904 Settings-tab buttons into a single Kotlin plugin and populates PlayList and Music with cache-verified 377 track and region mappings. ### Audio & Client Playback Fixes - `Class56_Sub1_Sub1.java` & `Class56.java` & `Class56_Sub1_Sub2.java` - Split `RECEIVER_LOCK` from `MIDI_LOCK` so Java Sound's `Sequencer.stop()` cannot deadlock against `Receiver.send(...)` when switching tracks. - Replaced `method835(0, i, -1L)` with `method840(i, -1L)` in `method827` to reset all 16 channel volumes and unconditionally apply `musicVolume` on track changes. - Added `setLoop(boolean loop, boolean restartIfStopped)` to toggle looping live via `config 19` and rewind/restart finished tracks when `LOOP` is enabled while unmuted. - `Game.java` & `DesktopHooks.java` & `SoundPlayer.java` - Stops title-screen `Scape Main` on login (`method55(false); currentSong = -1;`) so region music starts immediately. - Respects `!loggedIn || variousSettings[19] == 1` for track looping while keeping jingles one-shot. - Replaced unbounded per-sound thread creation in `SoundPlayer` with a 4-thread daemon executor and try-with-resources stream/line cleanup, and fixed `DesktopHooks` MIDI receiver/synthesizer cleanup. - `HdMaterial.java` - Added `populateSingleLayer(int layerId, ByteBuffer diffuseLayerBuf, ByteBuffer normalLayerBuf)` sharing `writeLayerToBuffers` for memory-constrained Android texture streaming. ### Server Music & Playlist Architecture - `PlayList.java` - Mapped all 383 clickable songs from `interface 962` to cache-verified `idx3` MIDI IDs and the 14 native RS2 music unlock varps (`20, 21, 22, 23, 24, 25, 298, 311, 346, 414, 464, 598, 662, 721`), fixing 25 legacy mis-mapped song IDs and `FAITHLESS` `childId` (`10129`). - Replaced `fixAllColors()` `sendColor` bursts with `sendMusicConfigs()` sending the 14 music varps plus `config 18` (`AUTO`/`MAN`) and `config 19` (`LOOP`), handling `Strange Place` (`464`, bit `17`) natively. - Fixed `AUTO` (`24125`), `MAN` (`24126`), and `LOOP` (`38197`) action button IDs in `handleButton`. - `Music.java` - Expanded `MUSIC_DATA` to 517 unique regions cross-checked against the 377 `map_index`, `LostCityRS`, and OSRS Wiki polygons, and fixed Ape Atoll `(43, 43)` to `303` (`Monkey Madness`) and 73 legacy mismatches. - Removed `player.musicOn` gates in `Music.playMusic`, `ChangeRegions`, `ClickingButtons`, and `PacketSender.login` so tracks still unlock and `PlayList` controls work while muted. - `PacketSender.java` & `Player.java` & `PlayerSave.java` - Flushed `sendSong` immediately and standardized `sendSound` on packet 174 (`id`, `volume`, `delay`). - Persisted `musicVolume`, `soundVolume`, `mouseButton`, `chatEffects`, and `acceptAid` in `PlayerSave` and synced configs `168`, `169`, `170`, `171`, `287`, and `427` on login. ### New & Migrated Plugins - `GameOptionsPlugin.kt` - Migrated `BrightnessButtons.java`, `MusicVolumeButtons.java`, `SoundVolumeButtons.java`, and `ToggleSplitChatButtons.java` plus `Mouse Buttons` (`3145`/`3146`), `Chat Effects` (`3147`/`3148`), and `Accept Aid` (`48176`/`48177`) into a single Kotlin plugin using named constants and lookup maps, removing legacy `500`–`503` configs and stale `74176`/`74180`/`74188` cases from `ClickingButtons.java`. - `UnlockSongs.kt` - Added developer command `::unlocksongs` (`::unlockmusic`, `::allsongs`) to unlock all playlist tracks and refresh music varps. * fix(client): replace ThreadFactory lambda in SoundPlayer for JDK 8 javac Replaces the `ThreadFactory` lambda in `SoundPlayer.SOUND_EXECUTOR` with an anonymous `ThreadFactory` class to resolve a JDK 8 `javac` definite-assignment bug (`variable r might not have been initialized`) hit by GitHub Actions CI on PR #686. ### Bug Fixes - `SoundPlayer.java` - Replaced `r -> { ... }` in `Executors.newFixedThreadPool(4, ...)` with an anonymous `ThreadFactory` implementation. * refactor(combat): replace DeprecatedItems.getItemName with ItemDefinition.lookup in AutocastCancelButtonPlugin Removes the deprecated `DeprecatedItems.getItemName(weaponId)` call from `AutocastCancelButtonPlugin.kt` in favor of `ItemDefinition.lookup(weaponId)?.name`, eliminating the Kotlin compiler deprecation warning. ### Cleanups & Deprecations - `AutocastCancelButtonPlugin.kt` - Replaced `DeprecatedItems.getItemName(weaponId)` with `ItemDefinition.lookup(weaponId)?.name ?: "Unarmed"` (preserving `"Unarmed"` when `weaponId == -1`). * build(maven): set explicit UTF-8 source and reporting encoding across modules Configures `project.build.sourceEncoding` and `project.reporting.outputEncoding` to `UTF-8` in the root and server POMs so resource filtering and Java compilation are platform-independent and free of platform-encoding warnings. ### Build Configuration - `pom.xml` & `2006Scape Server/pom.xml` - Added `<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>` and `<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>`. * style(client): fix space-before-tab indentation in Class56_Sub1_Sub2 Replace mixed space-and-tab indentation with spaces in Class56_Sub1_Sub2 to clear the git diff --check whitespace lint prior to merging PR #686. ### Cleanups & Deprecations - 2006Scape Client/src/main/java/Class56_Sub1_Sub2.java: - Normalized line 34 indentation inside setLoop(boolean, boolean) from space-before-tab to 8 spaces. * fix(audio): remove post-377 Stronghold of Security song entries from Music Remove the four legacy Stronghold of Security region music mappings from Music.java so MUSIC_DATA stays strictly aligned with Revision 377 (May 2006) tracks and PlayList idx3 entries. ### Cleanups & Deprecations - 2006Scape Server/src/main/java/com/rs2/game/content/music/Music.java: - Removed pre-existing post-377 Stronghold of Security entries { 29, 81, 537 }, { 31, 81, 558 }, { 33, 82, 559 }, and { 36, 81, 560 } from MUSIC_DATA.
579 lines
27 KiB
Java
579 lines
27 KiB
Java
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);
|
|
}
|
|
}
|