|
| 1 | +import * as THREE from 'https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js'; |
| 2 | +import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/controls/OrbitControls.js'; |
| 3 | + |
| 4 | +// Get the container element |
| 5 | +const container = document.getElementById('scene-container'); |
| 6 | + |
| 7 | +// Create the scene |
| 8 | +let scene = new THREE.Scene(); |
| 9 | + |
| 10 | +// Create a camera |
| 11 | +let camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 1000); |
| 12 | +camera.position.set(0, 2, 5); |
| 13 | + |
| 14 | +// Create a renderer |
| 15 | +let renderer = new THREE.WebGLRenderer({ antialias: true }); |
| 16 | +renderer.setSize(container.clientWidth, container.clientHeight); |
| 17 | +container.appendChild(renderer.domElement); |
| 18 | + |
| 19 | +// Add orbit controls for mouse interaction |
| 20 | +let controls = new OrbitControls(camera, renderer.domElement); |
| 21 | + |
| 22 | +// Create a geometry |
| 23 | +let geometry = new THREE.SphereGeometry(2.3, 128, 128); |
| 24 | + |
| 25 | +// Create a shader material |
| 26 | +const vertexShader = ` |
| 27 | + varying vec3 vNormal; |
| 28 | + varying vec3 vPosition; |
| 29 | + uniform float time; |
| 30 | + uniform float noiseScale; |
| 31 | +
|
| 32 | + // Improved Perlin Noise Function |
| 33 | + vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } |
| 34 | + vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; } |
| 35 | + vec4 permute(vec4 x) { return mod289(((x*34.0)+1.0)*x); } |
| 36 | + vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; } |
| 37 | +
|
| 38 | + float snoise(vec3 v) { |
| 39 | + const vec2 C = vec2(1.0/6.0, 1.0/3.0); |
| 40 | + const vec4 D = vec4(0.0, 0.5, 1.0, 2.0); |
| 41 | + vec3 i = floor(v + dot(v, C.yyy) ); |
| 42 | + vec3 x0 = v - i + dot(i, C.xxx); |
| 43 | + vec3 g = step(x0.yzx, x0.xyz); |
| 44 | + vec3 l = 1.0 - g; |
| 45 | + vec3 i1 = min( g.xyz, l.zxy ); |
| 46 | + vec3 i2 = max( g.xyz, l.zxy ); |
| 47 | + vec3 x1 = x0 - i1 + C.xxx; |
| 48 | + vec3 x2 = x0 - i2 + C.yyy; |
| 49 | + vec3 x3 = x0 - D.yyy; |
| 50 | + i = mod289(i); |
| 51 | + vec4 p = permute( permute( permute( i.z + vec4(0.0, i1.z, i2.z, 1.0 )) + i.y + vec4(0.0, i1.y, i2.y, 1.0 )) + i.x + vec4(0.0, i1.x, i2.x, 1.0 )); |
| 52 | + float n_ = 0.142857142857; |
| 53 | + vec3 ns = n_ * D.wyz - D.xzx; |
| 54 | + vec4 j = p - 49.0 * floor(p * ns.z * ns.z); |
| 55 | + vec4 x_ = floor(j * ns.z); |
| 56 | + vec4 y_ = floor(j - 7.0 * x_ ); |
| 57 | + vec4 x = x_ *ns.x + ns.yyyy; |
| 58 | + vec4 y = y_ *ns.x + ns.yyyy; |
| 59 | + vec4 h = 1.0 - abs(x) - abs(y); |
| 60 | + vec4 b0 = vec4( x.xy, y.xy ); |
| 61 | + vec4 b1 = vec4( x.zw, y.zw ); |
| 62 | + vec4 s0 = floor(b0)*2.0 + 1.0; |
| 63 | + vec4 s1 = floor(b1)*2.0 + 1.0; |
| 64 | + vec4 sh = -step(h, vec4(0.0)); |
| 65 | + vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ; |
| 66 | + vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ; |
| 67 | + vec3 p0 = vec3(a0.xy,h.x); |
| 68 | + vec3 p1 = vec3(a0.zw,h.y); |
| 69 | + vec3 p2 = vec3(a1.xy,h.z); |
| 70 | + vec3 p3 = vec3(a1.zw,h.w); |
| 71 | + vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2,p2), dot(p3,p3))); |
| 72 | + p0 *= norm.x; |
| 73 | + p1 *= norm.y; |
| 74 | + p2 *= norm.z; |
| 75 | + p3 *= norm.w; |
| 76 | + vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0); |
| 77 | + m = m * m; |
| 78 | + return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3) ) ); |
| 79 | + } |
| 80 | +
|
| 81 | + void main() { |
| 82 | + vNormal = normal; |
| 83 | + vPosition = position; |
| 84 | + float speed = 0.5; // Adjust this value to change the speed |
| 85 | + vec3 newPosition = position + normal * snoise(position * noiseScale + time * speed) * 0.4; |
| 86 | + gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0); |
| 87 | + } |
| 88 | +`; |
| 89 | + |
| 90 | +const fragmentShader = ` |
| 91 | + varying vec3 vNormal; |
| 92 | + varying vec3 vPosition; |
| 93 | +
|
| 94 | + void main() { |
| 95 | + vec3 color1 = vec3(20.0 / 255.0, 181.0 / 255.0, 217.0 / 255.0); // #14B5D9 |
| 96 | + vec3 color2 = vec3(189.0 / 255.0, 36.0 / 255.0, 155.0 / 255.0); // #BD249B |
| 97 | + float h = 0.5 + 0.5 * vPosition.y; |
| 98 | + vec3 color = mix(color1, color2, h); |
| 99 | + gl_FragColor = vec4(color, 1.0); |
| 100 | + } |
| 101 | +`; |
| 102 | + |
| 103 | +const uniforms = { |
| 104 | + time: { value: 0.0 }, |
| 105 | + noiseScale: { value: 1.0 } |
| 106 | +}; |
| 107 | + |
| 108 | +const material = new THREE.ShaderMaterial({ |
| 109 | + uniforms: uniforms, |
| 110 | + vertexShader: vertexShader, |
| 111 | + fragmentShader: fragmentShader, |
| 112 | + wireframe: true |
| 113 | +}); |
| 114 | + |
| 115 | +const mesh = new THREE.Mesh(geometry, material); |
| 116 | +scene.add(mesh); |
| 117 | + |
| 118 | +// Add lighting |
| 119 | +const ambientLight = new THREE.AmbientLight(0x404040); |
| 120 | +scene.add(ambientLight); |
| 121 | + |
| 122 | +const directionalLight = new THREE.DirectionalLight(0xffffff, 1); |
| 123 | +directionalLight.position.set(5, 10, 7.5).normalize(); |
| 124 | +scene.add(directionalLight); |
| 125 | + |
| 126 | +let noiseSpeed = 0.02; // Change this value to control the speed |
| 127 | + |
| 128 | +// Handle window resize |
| 129 | +window.addEventListener('resize', () => { |
| 130 | + camera.aspect = container.clientWidth / container.clientHeight; |
| 131 | + camera.updateProjectionMatrix(); |
| 132 | + renderer.setSize(container.clientWidth, container.clientHeight); |
| 133 | +}); |
| 134 | + |
| 135 | +// Animation loop |
| 136 | +function animate() { |
| 137 | + requestAnimationFrame(animate); |
| 138 | + controls.update(); |
| 139 | + uniforms.time.value += noiseSpeed; |
| 140 | + renderer.render(scene, camera); |
| 141 | +} |
| 142 | + |
| 143 | +animate(); |
0 commit comments