Billiard

<!-- 1. Constrained Execution Container -->
<div id="threejs-canvas-container" style="width: 100%; height: 600px; position: relative; border-radius: 8px; overflow: hidden;">
	<div id="notSupported" style="display:none; position:absolute; z-index:10; background:white; padding:10px;">Graphics hardware not supported.</div>
</div>

<!-- 2. Inline GLSL Shaders -->
<script id="vshader" type="x-shader/x-vertex">
	precision highp float;
	uniform mat4 modelViewMatrix;
	uniform mat4 projectionMatrix;
	uniform float time;
	attribute vec3 position;
	attribute vec2 uv;
	attribute vec3 translate;
	varying vec2 vUv;
	varying float vScale;

	void main() {
		vec4 mvPosition = modelViewMatrix * vec4( translate, 1.0 );
		vec3 trTime = vec3(translate.x + time, translate.y + time, translate.z + time);
		float scale =  sin( trTime.x * 2.1 ) + sin( trTime.y * 3.2 ) + sin( trTime.z * 4.3 );
		vScale = scale;
		scale = scale * 10.0 + 10.0;
		mvPosition.xyz += position * scale;
		vUv = uv;
		gl_Position = projectionMatrix * mvPosition;
	}
</script>

<script id="fshader" type="x-shader/x-fragment">
	precision highp float;
	uniform sampler2D map;
	varying vec2 vUv;
	varying float vScale;

	vec3 HUEtoRGB(float H){
		H = mod(H,1.0);
		float R = abs(H * 6.0 - 3.0) - 1.0;
		float G = 2.0 - abs(H * 6.0 - 2.0);
		float B = 2.0 - abs(H * 6.0 - 4.0);
		return clamp(vec3(R,G,B),0.0,1.0);
	}
	vec3 HSLtoRGB(vec3 HSL){
		vec3 RGB = HUEtoRGB(HSL.x);
		float C = (1.0 - abs(2.0 * HSL.z - 1.0)) * HSL.y;
		return (RGB - 0.5) * C + HSL.z;
	}

	void main() {
		vec4 diffuseColor = texture2D( map, vUv );
		gl_FragColor = vec4( diffuseColor.xyz * HSLtoRGB(vec3(vScale/5.0, 1.0, 0.5)), diffuseColor.w );
		if ( diffuseColor.w < 0.5 ) discard;
	}
</script>

<!-- 3. Absolute CDN Import Map -->
<script type="importmap">
	{
		"imports": {
			"three": "https://unpkg.com/three@0.160.0/build/three.module.js",
			"three/addons/": "https://unpkg.com/three@0.160.0/examples/jsm/"
		}
	}
</script>

<!-- 4. Scoped Initialization Script -->
<script type="module">
	import * as THREE from 'three';
	import Stats from 'three/addons/libs/stats.module.js';

	let container, stats;
	let camera, scene, renderer;
	let geometry, material, mesh;

	init();

	function init() {
		container = document.getElementById( 'threejs-canvas-container' );
        const width = container.clientWidth;
        const height = container.clientHeight;

		camera = new THREE.PerspectiveCamera( 50, width / height, 1, 5000 );
		camera.position.z = 1400;

		scene = new THREE.Scene();

		const circleGeometry = new THREE.CircleGeometry( 1, 6 );
		geometry = new THREE.InstancedBufferGeometry();
		geometry.index = circleGeometry.index;
		geometry.attributes = circleGeometry.attributes;

		const particleCount = 75000;
		const translateArray = new Float32Array( particleCount * 3 );

		for ( let i = 0, i3 = 0, l = particleCount; i < l; i ++, i3 += 3 ) {
			translateArray[ i3 + 0 ] = Math.random() * 2 - 1;
			translateArray[ i3 + 1 ] = Math.random() * 2 - 1;
			translateArray[ i3 + 2 ] = Math.random() * 2 - 1;
		}

		geometry.setAttribute( 'translate', new THREE.InstancedBufferAttribute( translateArray, 3 ) );

        // Upload circle.png to WP Media Library and replace this URL, or use a reliable CDN
        const textureLoader = new THREE.TextureLoader();
        textureLoader.crossOrigin = 'anonymous';
        const texture = textureLoader.load('https://unpkg.com/three@0.160.0/examples/textures/sprites/circle.png');

		material = new THREE.RawShaderMaterial( {
			uniforms: {
				'map': { value: texture },
				'time': { value: 0.0 }
			},
			vertexShader: document.getElementById( 'vshader' ).textContent,
			fragmentShader: document.getElementById( 'fshader' ).textContent,
			depthTest: true,
			depthWrite: true
		} );

		mesh = new THREE.Mesh( geometry, material );
		mesh.scale.set( 500, 500, 500 );
		scene.add( mesh );

		renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
		renderer.setPixelRatio( window.devicePixelRatio );
		renderer.setSize( width, height );
		renderer.setAnimationLoop( animate );
		container.appendChild( renderer.domElement );

		stats = new Stats();
        stats.dom.style.position = 'absolute';
		container.appendChild( stats.dom );

		window.addEventListener( 'resize', onWindowResize );
		return true;
	}

	function onWindowResize() {
        if(!container) return;
        const width = container.clientWidth;
        const height = container.clientHeight;
		camera.aspect = width / height;
		camera.updateProjectionMatrix();
		renderer.setSize( width, height );
	}

	function animate() {
		const time = performance.now() * 0.0005;
		material.uniforms[ 'time' ].value = time;
		mesh.rotation.x = time * 0.2;
		mesh.rotation.y = time * 0.4;
		renderer.render( scene, camera );
		stats.update();
	}
</script>

Publicado

em

por

Tags: