Skip to content

Commit

Permalink
Another fix for fragment shader
Browse files Browse the repository at this point in the history
  • Loading branch information
sa2urami committed Dec 19, 2024
1 parent fbe2641 commit 43cdd21
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
17 changes: 2 additions & 15 deletions prismarine-viewer/examples/Cube.frag.wgsl
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
@group(0) @binding(1) var mySampler: sampler;
@group(0) @binding(2) var myTexture: texture_2d<f32>;
@group(0) @binding(5) var<uniform> tileSize: vec2<f32>;

@fragment
fn main(
@location(0) fragUV: vec2f,
@location(2) @interpolate(flat) ColorBlend: vec3f,
@location(3) @interpolate(flat) ChunkOpacity: f32
@location(1) @interpolate(flat) ColorBlend: vec4f,
) -> @location(0) vec4f {
let pixelColor = textureSample(myTexture, mySampler, fragUV);
// return vec4f(pixelColor.rgb * ColorBlend / 255, pixelColor.a); // 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

// if (abs(t.x-t.y) <=0.03 || abs(t.x-t.z)<=0.03 ||abs(t.y-t.z) <=0.03)
// {
// return vec4f(t.rgb * ColorBlend / 255, 1.0);
// }
// else {
// return vec4f(t.rgb, 1.0);
// }
return vec4f(pixelColor.rgb, 1.0) * ColorBlend; // Set alpha to 1.0 for full opacity
}
17 changes: 7 additions & 10 deletions prismarine-viewer/examples/Cube.vert.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ struct CubeModel {
struct VertexOutput {
@builtin(position) Position: vec4f,
@location(0) fragUV: vec2f,
@location(2) @interpolate(flat) ColorBlend: vec3f,
@location(3) @interpolate(flat) ChunkOpacity: f32
@location(1) @interpolate(flat) ColorBlend: vec4f,
}
@group(1) @binding(0) var<storage, read> cubes: array<Cube>;
@group(0) @binding(0) var<uniform> ViewProjectionMatrix: mat4x4<f32>;
Expand Down Expand Up @@ -59,11 +58,9 @@ fn main(

let cube_position = vec4f(positionX, positionY, positionZ, 0.0);

let colorBlendR : f32 = f32(cube.cube[1] & 255);
let colorBlendG : f32 = f32((cube.cube[1] >> 8) & 255);
let colorBlendB : f32 = f32((cube.cube[1] >> 16) & 255);
let colorBlend = vec3f(colorBlendR, colorBlendG, colorBlendB);

var colorBlend = vec4f(unpack4xU8(cube.cube[1]));
colorBlend.a = f32(chunk.opacity);
colorBlend /= 255;
var normal : mat4x4<f32>;
var Uv = vec2(uv.x, (1.0 - uv.y));
normal = rotatations[normalIndex];
Expand Down Expand Up @@ -95,13 +92,13 @@ fn main(
}
}

let textureSize: vec2<f32> = vec2<f32>(textureDimensions(myTexture));
let tilesPerTexture: vec2<f32> = textureSize / tileSize;
let textureSize = vec2f(textureDimensions(myTexture));
let tilesPerTexture= textureSize / tileSize;
Uv = vec2(Uv / tilesPerTexture + vec2f(trunc(f32(textureIndex) % tilesPerTexture.y), trunc(f32(textureIndex) / tilesPerTexture.x)) / tilesPerTexture);

var output: VertexOutput;
output.Position = ViewProjectionMatrix * (position * normal + cube_position);
output.fragUV = Uv;
output.ChunkOpacity = f32(chunk.opacity) / 255;
output.ColorBlend = colorBlend;
return output;
}

0 comments on commit 43cdd21

Please sign in to comment.