Skip to content

Commit

Permalink
Chunk opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
sa2urami committed Dec 13, 2024
1 parent ea82415 commit af82c41
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions prismarine-viewer/examples/Cube.comp.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ struct Cube {
struct Chunk {
x: i32,
z: i32,
opacity: i32,
}


Expand Down
5 changes: 3 additions & 2 deletions prismarine-viewer/examples/Cube.frag.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
fn main(
@location(0) fragUV: vec2f,
@location(1) @interpolate(flat) TextureIndex: f32,
@location(2) @interpolate(flat) ColorBlend: vec3f
@location(2) @interpolate(flat) ColorBlend: vec3f,
@location(3) @interpolate(flat) ChunkOpacity: f32
) -> @location(0) vec4f {
let textureSize: vec2<f32> = vec2<f32>(textureDimensions(myTexture));
let tilesPerTexture: vec2<f32> = textureSize / tileSize;
let pixelColor = textureSample(myTexture, mySampler, fragUV / tilesPerTexture + vec2f(trunc(TextureIndex % tilesPerTexture.y), trunc(TextureIndex / tilesPerTexture.x)) / tilesPerTexture);
// return vec4f(pixelColor.rgb * ColorBlend / 255, pixelColor.a); // Set alpha to 1.0 for full opacity
return vec4f(pixelColor.rgb * ColorBlend / 255, 1.0); // Set alpha to 1.0 for full opacity
return vec4f(pixelColor.rgb * ColorBlend / 255, 1.0 * ChunkOpacity); // Set alpha to 1.0 for full opacity
// only gray:
// let t = textureSample(myTexture, mySampler, fragUV / tilesPerTexture + vec2f(trunc(TextureIndex % tilesPerTexture.y), trunc(TextureIndex / tilesPerTexture.x)) / tilesPerTexture);
// // return vec4f(pixelColor.rgb * ColorBlend / 255, pixelColor.a); // Set alpha to 1.0 for full opacity
Expand Down
5 changes: 4 additions & 1 deletion prismarine-viewer/examples/Cube.vert.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct Cube {
struct Chunk{
x : i32,
z : i32,
opacity: i32
}


Expand All @@ -22,7 +23,8 @@ struct VertexOutput {
@builtin(position) Position: vec4f,
@location(0) fragUV: vec2f,
@location(1) @interpolate(flat) TextureIndex: f32,
@location(2) @interpolate(flat) ColorBlend: vec3f
@location(2) @interpolate(flat) ColorBlend: vec3f,
@location(3) @interpolate(flat) ChunkOpacity: f32
}
@group(1) @binding(0) var<storage, read> cubes: array<Cube>;
@group(0) @binding(0) var<uniform> ViewProjectionMatrix: mat4x4<f32>;
Expand Down Expand Up @@ -94,6 +96,7 @@ fn main(
var output: VertexOutput;
output.Position = ViewProjectionMatrix * (position * normal + cube_position);
output.fragUV = Uv;
output.ChunkOpacity = f32(chunk.opacity) / 255;
output.TextureIndex = f32(textureIndex);
output.ColorBlend = colorBlend;
return output;
Expand Down
1 change: 1 addition & 0 deletions prismarine-viewer/examples/CubeSort.comp.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct Cube {
struct Chunk {
x: i32,
z: i32,
opacity: i32
}

struct Depth {
Expand Down
5 changes: 3 additions & 2 deletions prismarine-viewer/examples/webgpuRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,13 +902,14 @@ export class WebgpuRenderer {
}
const chunksCount = chunks.length

const chunksBuffer = new Int32Array(chunksCount * 2)
const chunksBuffer = new Int32Array(chunksCount * 3)
let totalFromChunks = 0
for (let i = 0; i < chunksCount; i++) {
const offset = i * 2
const offset = i * 3
const { x, z, length } = chunks[i]!
chunksBuffer[offset] = x
chunksBuffer[offset + 1] = z
chunksBuffer[offset + 2] = 255
const cubesCount = length
totalFromChunks += cubesCount
}
Expand Down

0 comments on commit af82c41

Please sign in to comment.