123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - read float pixels</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="container"></div>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> read float pixels webgl example<br/>
- <span id="values"></span>
- </div>
- <script id="fragment_shader_screen" type="x-shader/x-fragment">
- varying vec2 vUv;
- uniform sampler2D tDiffuse;
- void main() {
- gl_FragColor = texture2D( tDiffuse, vUv );
- }
- </script>
- <script id="fragment_shader_pass_1" type="x-shader/x-fragment">
- varying vec2 vUv;
- uniform float time;
- void main() {
- float r = vUv.x;
- if( vUv.y < 0.5 ) r = 0.0;
- float g = vUv.y;
- if( vUv.x < 0.5 ) g = 0.0;
- gl_FragColor = vec4( r, g, time, 1.0 );
- }
- </script>
- <script id="vertexShader" type="x-shader/x-vertex">
- varying vec2 vUv;
- void main() {
- vUv = uv;
- gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
- }
- </script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- var container, stats;
- var cameraRTT, sceneRTT, sceneScreen, renderer, zmesh1, zmesh2;
- var mouseX = 0, mouseY = 0;
- var windowHalfX = window.innerWidth / 2;
- var windowHalfY = window.innerHeight / 2;
- var rtTexture, material, quad;
- var delta = 0.01;
- var valueNode;
- init();
- animate();
- function init() {
- container = document.getElementById( 'container' );
- cameraRTT = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 10000, 10000 );
- cameraRTT.position.z = 100;
- //
- sceneRTT = new THREE.Scene();
- sceneScreen = new THREE.Scene();
- var light = new THREE.DirectionalLight( 0xffffff );
- light.position.set( 0, 0, 1 ).normalize();
- sceneRTT.add( light );
- light = new THREE.DirectionalLight( 0xffaaaa, 1.5 );
- light.position.set( 0, 0, - 1 ).normalize();
- sceneRTT.add( light );
- rtTexture = new THREE.WebGLRenderTarget( window.innerWidth, window.innerHeight, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat, type: THREE.FloatType } );
- material = new THREE.ShaderMaterial( {
- uniforms: { "time": { value: 0.0 } },
- vertexShader: document.getElementById( 'vertexShader' ).textContent,
- fragmentShader: document.getElementById( 'fragment_shader_pass_1' ).textContent
- } );
- var materialScreen = new THREE.ShaderMaterial( {
- uniforms: { "tDiffuse": { value: rtTexture.texture } },
- vertexShader: document.getElementById( 'vertexShader' ).textContent,
- fragmentShader: document.getElementById( 'fragment_shader_screen' ).textContent,
- depthWrite: false
- } );
- var plane = new THREE.PlaneBufferGeometry( window.innerWidth, window.innerHeight );
- quad = new THREE.Mesh( plane, material );
- quad.position.z = - 100;
- sceneRTT.add( quad );
- var geometry = new THREE.TorusBufferGeometry( 100, 25, 15, 30 );
- var mat1 = new THREE.MeshPhongMaterial( { color: 0x555555, specular: 0xffaa00, shininess: 5 } );
- var mat2 = new THREE.MeshPhongMaterial( { color: 0x550000, specular: 0xff2200, shininess: 5 } );
- zmesh1 = new THREE.Mesh( geometry, mat1 );
- zmesh1.position.set( 0, 0, 100 );
- zmesh1.scale.set( 1.5, 1.5, 1.5 );
- sceneRTT.add( zmesh1 );
- zmesh2 = new THREE.Mesh( geometry, mat2 );
- zmesh2.position.set( 0, 150, 100 );
- zmesh2.scale.set( 0.75, 0.75, 0.75 );
- sceneRTT.add( zmesh2 );
- quad = new THREE.Mesh( plane, materialScreen );
- quad.position.z = - 100;
- sceneScreen.add( quad );
- renderer = new THREE.WebGLRenderer();
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.autoClear = false;
- container.appendChild( renderer.domElement );
- stats = new Stats();
- container.appendChild( stats.dom );
- valueNode = document.getElementById( 'values' );
- document.addEventListener( 'mousemove', onDocumentMouseMove, false );
- }
- function onDocumentMouseMove( event ) {
- mouseX = ( event.clientX - windowHalfX );
- mouseY = ( event.clientY - windowHalfY );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- var time = Date.now() * 0.0015;
- if ( zmesh1 && zmesh2 ) {
- zmesh1.rotation.y = - time;
- zmesh2.rotation.y = - time + Math.PI / 2;
- }
- if ( material.uniforms[ "time" ].value > 1 || material.uniforms[ "time" ].value < 0 ) {
- delta *= - 1;
- }
- material.uniforms[ "time" ].value += delta;
- renderer.clear();
- // Render first scene into texture
- renderer.setRenderTarget( rtTexture );
- renderer.clear();
- renderer.render( sceneRTT, cameraRTT );
- // Render full screen quad with generated texture
- renderer.setRenderTarget( null );
- renderer.render( sceneScreen, cameraRTT );
- var read = new Float32Array( 4 );
- renderer.readRenderTargetPixels( rtTexture, windowHalfX + mouseX, windowHalfY - mouseY, 1, 1, read );
- valueNode.innerHTML = 'r:' + read[ 0 ] + '<br/>g:' + read[ 1 ] + '<br/>b:' + read[ 2 ];
- }
- </script>
- </body>
- </html>
|