You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

250 lines
6.1 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - materials - shaders [lava]</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> - shader material demo. featuring lava shader by <a href="http://irrlicht.sourceforge.net/phpBB2/viewtopic.php?t=21057" target="_blank" rel="noopener">TheGameMaker</a></div>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform float time;
uniform float fogDensity;
uniform vec3 fogColor;
uniform sampler2D texture1;
uniform sampler2D texture2;
varying vec2 vUv;
void main( void ) {
vec2 position = - 1.0 + 2.0 * vUv;
vec4 noise = texture2D( texture1, vUv );
vec2 T1 = vUv + vec2( 1.5, - 1.5 ) * time * 0.02;
vec2 T2 = vUv + vec2( - 0.5, 2.0 ) * time * 0.01;
T1.x += noise.x * 2.0;
T1.y += noise.y * 2.0;
T2.x -= noise.y * 0.2;
T2.y += noise.z * 0.2;
float p = texture2D( texture1, T1 * 2.0 ).a;
vec4 color = texture2D( texture2, T2 * 2.0 );
vec4 temp = color * ( vec4( p, p, p, p ) * 2.0 ) + ( color * color - 0.1 );
if( temp.r > 1.0 ) { temp.bg += clamp( temp.r - 2.0, 0.0, 100.0 ); }
if( temp.g > 1.0 ) { temp.rb += temp.g - 1.0; }
if( temp.b > 1.0 ) { temp.rg += temp.b - 1.0; }
gl_FragColor = temp;
float depth = gl_FragCoord.z / gl_FragCoord.w;
const float LOG2 = 1.442695;
float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );
fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );
gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );
}
</script>
<script id="vertexShader" type="x-shader/x-vertex">
uniform vec2 uvScale;
varying vec2 vUv;
void main()
{
vUv = uvScale * uv;
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
gl_Position = projectionMatrix * mvPosition;
}
</script>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/libs/stats.module.js';
let camera, renderer, clock, scene;
let uniforms, stats;
const materials = [];
init();
animate();
function init() {
const container = document.getElementById( 'container' );
camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 3000 );
camera.position.z = 7;
scene = new THREE.Scene();
clock = new THREE.Clock();
const textureLoader = new THREE.TextureLoader();
uniforms = {
'fogDensity': { value: 0.001 },
'fogColor': { value: new THREE.Vector3( 0, 0, 0 ) },
'time': { value: 1.0 },
'uvScale': { value: new THREE.Vector2( 3.0, 1.0 ) },
'texture1': { value: textureLoader.load( 'textures/lava/cloud.png' ) },
'texture2': { value: textureLoader.load( 'textures/lava/lavatile.jpg' ) }
};
uniforms[ 'texture1' ].value.wrapS = uniforms[ 'texture1' ].value.wrapT = THREE.RepeatWrapping;
uniforms[ 'texture2' ].value.wrapS = uniforms[ 'texture2' ].value.wrapT = THREE.RepeatWrapping;
//
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
container.appendChild( renderer.domElement );
//
stats = new Stats();
container.appendChild( stats.dom );
onWindowResize();
window.addEventListener( 'resize', onWindowResize );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
addMeshes();
}
function removeAllMeshes() {
for ( var i = scene.children.length - 1; i >= 0; i -- ) {
const obj = scene.children[ i ];
scene.remove( obj );
obj.geometry.dispose();
obj.material.dispose();
}
}
function addMeshes() {
removeAllMeshes();
//reset pseudorandom number
THREE.MathUtils.seededRandom( 1 );
const projScreenMatrix = new THREE.Matrix4();
const frustum = new THREE.Frustum();
camera.updateMatrixWorld();
projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
frustum.setFromProjectionMatrix( projScreenMatrix );
const size = 0.65;
let meshesCount = 0;
while ( meshesCount < 2500 ) {
const material = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent
} );
const mesh = new THREE.Mesh( new THREE.TorusGeometry( size, 0.3, 30, 30 ), material );
mesh.position.x = THREE.MathUtils.seededRandom() * 20 - 10;
mesh.position.y = THREE.MathUtils.seededRandom() * 20 - 10;
mesh.position.z = THREE.MathUtils.seededRandom() * 20 - 10;
mesh.rotation.x = THREE.MathUtils.seededRandom() * 2 * Math.PI;
mesh.rotation.y = THREE.MathUtils.seededRandom() * 2 * Math.PI;
mesh.scale.x = mesh.scale.y = mesh.scale.z = THREE.MathUtils.seededRandom() * .2 + 0.1;
mesh.updateMatrixWorld();
if ( frustum.intersectsObject( mesh ) ) {
// mesh.rotation.x = 0.3;
materials.push( material );
scene.add( mesh );
meshesCount ++;
}
}
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
materials.forEach( ( material ) => {
material.needsUpdate = true;
} );
const delta = 5 * clock.getDelta();
uniforms[ 'time' ].value += 0.2 * delta;
renderer.render( scene, camera );
}
</script>
</body>
</html>