import * as THREE from 'three';
const db1 = document.getElementById("debug1");
const camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.01, 50);
camera.position.set(-2, 2, 2);
/* camera.position.z = 1; */
const scene = new THREE.Scene();
const geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
const material = new THREE.MeshBasicMaterial({
transparent: true
});
material.onBeforeCompile = function(m) {
m.fragmentShader = `
uniform mat4 inverse_view_proj;
uniform float screen_width;
uniform float screen_height;
float a;
void main() {
// Convert screen coordinates to normalized device coordinates (NDC)
vec4 ndc = vec4(
(gl_FragCoord.x / screen_width - 0.5) * 2.0,
(gl_FragCoord.y / screen_height - 0.5) * 2.0,
(gl_FragCoord.z - 0.5) * 2.0,
1.0);
// Convert NDC throuch inverse clip coordinates to view coordinates
vec4 clip = inverse_view_proj * ndc;
vec3 vertex = (clip / clip.w).xyz;
// From attempt 1
float z = gl_FragCoord.z / gl_FragCoord.w;
vec3 test = vec3(0.0,0.0,(gl_FragCoord.z / gl_FragCoord.w));
// change alpha at a certain z value
if( ndc.z >= 0.0) {
a = 1.0;
} else {
a = 0.5;
}
// Results using >= 0.0:
// ndc.z: a = 1.0 at all times
// clip.z: a = 1.0 at all times
// vertex.z: a = 0.5 at all times
// test.z: a = 1.0 at all times (test.z appears to be around 5.0)
// Results using >= 1.0:
// ndc.z: a = 0.5 at all times
// clip.z: a = 0.5 at all times
// vertex.z: a = 0.5 at all times
// test.z: a = 1.0 at all times
// Which leads me to think that the z for all of the above
// clip, vertex and ndc are not being re-evaluated each render
// and is wrong code to get the correct z
// Set FragColor
gl_FragColor = vec4( vec3( 1.0 ), a );
}`;
};
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
const...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.