Getting tile data from overworld
This commit is contained in:
		@@ -25,8 +25,8 @@ void mapInit() {
 | 
			
		||||
void mapUpdate() {
 | 
			
		||||
  // Copy tile ids.
 | 
			
		||||
  memoryCopyRangeSafe(
 | 
			
		||||
    &MAP_DATA.tileIds,
 | 
			
		||||
    &OVERWORLD.tileIds[0],
 | 
			
		||||
    MAP_DATA.tileIds,
 | 
			
		||||
    OVERWORLD.tileIds,
 | 
			
		||||
    &OVERWORLD.tileIds[
 | 
			
		||||
      OVERWORLD.mapWidth * OVERWORLD.mapHeight * OVERWORLD.mapLayerCount
 | 
			
		||||
    ],
 | 
			
		||||
 
 | 
			
		||||
@@ -7,8 +7,10 @@
 | 
			
		||||
#include "../fragments/packed.glsl"
 | 
			
		||||
#include "../fragments/quad.glsl"
 | 
			
		||||
 | 
			
		||||
#define MAP_TILE_PACKED_SIZE 16
 | 
			
		||||
 | 
			
		||||
layout(std140) uniform b_Map {
 | 
			
		||||
  uvec4 mapTileIds[OVERWORLD_TILE_COUNT_MAX / PACKED_U8_PER_UVEC4];
 | 
			
		||||
  uvec4 mapTileIds[OVERWORLD_TILE_COUNT_MAX / MAP_TILE_PACKED_SIZE];
 | 
			
		||||
  uint mapSize;
 | 
			
		||||
  uvec3 _padding0;
 | 
			
		||||
};
 | 
			
		||||
@@ -42,5 +44,6 @@ uint mapGetLayer(uint instanceIndex) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint mapGetTileId(uint instanceIndex) {
 | 
			
		||||
  
 | 
			
		||||
  uvec4 v4 = mapTileIds[packedArrayGetU8IndexFromUVEC4Array(instanceIndex)];
 | 
			
		||||
  return packedArrayGetU8FromUVEC4ArrayValue(instanceIndex, v4);
 | 
			
		||||
}
 | 
			
		||||
@@ -4,13 +4,33 @@
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#define PACKED_U8_PER_UI 4
 | 
			
		||||
#define PACKED_U8_PER_UVEC4 PACKED_U8_PER_UI / 4
 | 
			
		||||
#define PACKED_U8_PER_UVEC4 PACKED_U8_PER_UI * 4
 | 
			
		||||
 | 
			
		||||
uint packedGetU8(uint position, uint data) {
 | 
			
		||||
  return (data >> (position * 8u)) & 0xFFu;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint packedGetU8FromVEC4(uint position, vec4 data) {
 | 
			
		||||
  return packedGetU8(position, uint(data[position / 4u]));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int packedGetI8(uint position, uint data) {
 | 
			
		||||
  int shift = int(position * 8u);
 | 
			
		||||
  return int(data << (24 - shift)) >> 24;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint packedArrayGetU8IndexFromUVEC4Array(uint u8ArrayIndex) {
 | 
			
		||||
  // Given a uint8_t array is uploaded, this will return the index to get the
 | 
			
		||||
  // appropriate uvec4 from a uvec4 array that will be at the right index.
 | 
			
		||||
  return u8ArrayIndex / uint(PACKED_U8_PER_UVEC4);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint packedArrayGetU8FromUVEC4ArrayValue(uint u8ArrayIndex, uvec4 data) {
 | 
			
		||||
  // Given a value from a uint8_t array, this will return the value at the
 | 
			
		||||
  // appropriate index. You must first get the uvec4 from the array using
 | 
			
		||||
  // packedArrayGetU8IndexFromUVEC4Array.
 | 
			
		||||
  uint subIndex = (u8ArrayIndex % uint(PACKED_U8_PER_UVEC4)) / uint(PACKED_U8_PER_UI);
 | 
			
		||||
  uint shiftAmount = (u8ArrayIndex % uint(PACKED_U8_PER_UI)) * 8u;
 | 
			
		||||
  uint value = (data[subIndex] >> shiftAmount) & 0xFFu;
 | 
			
		||||
  return value;
 | 
			
		||||
}
 | 
			
		||||
@@ -16,12 +16,13 @@ out vec4 FragColor;
 | 
			
		||||
 | 
			
		||||
void main() {
 | 
			
		||||
  uint layer = mapGetLayer(v_InstanceIndex);
 | 
			
		||||
  uint tileId = mapGetTileId(v_InstanceIndex);
 | 
			
		||||
 | 
			
		||||
  if(layer == 0u) {
 | 
			
		||||
    FragColor = vec4(1, 0, 0, 0.5);
 | 
			
		||||
  if(tileId == 0u) {
 | 
			
		||||
    FragColor = vec4(1, 0, 0, 1);
 | 
			
		||||
  } else if(tileId == 1u) {
 | 
			
		||||
    FragColor = vec4(0, 1, 0, 1);
 | 
			
		||||
  } else {
 | 
			
		||||
    FragColor = vec4(0, 1, 0, 0.5);
 | 
			
		||||
    FragColor = vec4(0, 0, 1, 1);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // FragColor = vec4(1, 0, 0, 1);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user