|
| 1 | +import './index.less'; |
| 2 | +import React, { Component } from 'react'; |
| 3 | + |
| 4 | +class Index extends Component { |
| 5 | + constructor(props) { |
| 6 | + super(props); |
| 7 | + this.state = {}; |
| 8 | + } |
| 9 | + componentDidMount() { |
| 10 | + var SEPARATION = 100, |
| 11 | + AMOUNTX = 50, |
| 12 | + AMOUNTY = 50; |
| 13 | + |
| 14 | + var container; |
| 15 | + var camera, scene, renderer; |
| 16 | + |
| 17 | + var particles, |
| 18 | + count = 0; |
| 19 | + |
| 20 | + var mouseX = 0, |
| 21 | + mouseY = 0; |
| 22 | + |
| 23 | + var windowHalfX = window.innerWidth / 2; |
| 24 | + var windowHalfY = window.innerHeight / 2; |
| 25 | + |
| 26 | + var THREE = window.THREE |
| 27 | + // setTimeout(() => { |
| 28 | + init(); |
| 29 | + animate(); |
| 30 | + // }, 100); |
| 31 | + |
| 32 | + function init() { |
| 33 | + container = document.createElement('div'); |
| 34 | + container.style.position = 'fixed'; |
| 35 | + container.style.top = '0'; |
| 36 | + container.style.left = '0'; |
| 37 | + container.style.zIndex = '-1'; |
| 38 | + container.style.opacity = '1'; |
| 39 | + document.body.appendChild(container); |
| 40 | + |
| 41 | + camera = new THREE.PerspectiveCamera( |
| 42 | + 75, |
| 43 | + window.innerWidth / window.innerHeight, |
| 44 | + 1, |
| 45 | + 10000, |
| 46 | + ); |
| 47 | + camera.position.z = 1000; |
| 48 | + |
| 49 | + scene = new THREE.Scene(); |
| 50 | + |
| 51 | + var numParticles = AMOUNTX * AMOUNTY; |
| 52 | + |
| 53 | + var positions = new Float32Array(numParticles * 3); |
| 54 | + var scales = new Float32Array(numParticles); |
| 55 | + |
| 56 | + var i = 0, |
| 57 | + j = 0; |
| 58 | + |
| 59 | + for (var ix = 0; ix < AMOUNTX; ix++) { |
| 60 | + for (var iy = 0; iy < AMOUNTY; iy++) { |
| 61 | + positions[i] = ix * SEPARATION - (AMOUNTX * SEPARATION) / 2; // x |
| 62 | + positions[i + 1] = 0; // y |
| 63 | + positions[i + 2] = iy * SEPARATION - (AMOUNTY * SEPARATION) / 2; // z |
| 64 | + |
| 65 | + scales[j] = 1; |
| 66 | + |
| 67 | + i += 3; |
| 68 | + j++; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + var geometry = new THREE.BufferGeometry(); |
| 73 | + geometry.addAttribute( |
| 74 | + 'position', |
| 75 | + new THREE.BufferAttribute(positions, 3), |
| 76 | + ); |
| 77 | + geometry.addAttribute('scale', new THREE.BufferAttribute(scales, 1)); |
| 78 | + |
| 79 | + var material = new THREE.ShaderMaterial({ |
| 80 | + uniforms: { |
| 81 | + color: { value: new THREE.Color(0x1890ff) }, |
| 82 | + }, |
| 83 | + vertexShader: |
| 84 | + 'attribute float scale;void main() {vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );gl_PointSize = scale * ( 300.0 / - mvPosition.z );gl_Position = projectionMatrix * mvPosition;}', |
| 85 | + fragmentShader: |
| 86 | + 'uniform vec3 color;void main() {if ( length( gl_PointCoord - vec2( 0.5, 0.5 ) ) > 0.475 ) discard;gl_FragColor = vec4( color, 1.0 );}', |
| 87 | + }); |
| 88 | + |
| 89 | + particles = new THREE.Points(geometry, material); |
| 90 | + scene.add(particles); |
| 91 | + |
| 92 | + renderer = new THREE.WebGLRenderer({ antialias: true }); |
| 93 | + renderer.setClearColor(0xffffff, 1.0); |
| 94 | + renderer.setPixelRatio(window.devicePixelRatio); |
| 95 | + renderer.setSize(window.innerWidth, window.innerHeight); |
| 96 | + container.appendChild(renderer.domElement); |
| 97 | + |
| 98 | + document.addEventListener('mousemove', onDocumentMouseMove, false); |
| 99 | + document.addEventListener('touchstart', onDocumentTouchStart, false); |
| 100 | + document.addEventListener('touchmove', onDocumentTouchMove, false); |
| 101 | + |
| 102 | + window.addEventListener('resize', onWindowResize, false); |
| 103 | + } |
| 104 | + |
| 105 | + function onWindowResize() { |
| 106 | + windowHalfX = window.innerWidth / 2; |
| 107 | + windowHalfY = window.innerHeight / 2; |
| 108 | + |
| 109 | + camera.aspect = window.innerWidth / window.innerHeight; |
| 110 | + camera.updateProjectionMatrix(); |
| 111 | + |
| 112 | + renderer.setSize(window.innerWidth, window.innerHeight); |
| 113 | + } |
| 114 | + |
| 115 | + function onDocumentMouseMove(event) { |
| 116 | + mouseX = event.clientX - windowHalfX; |
| 117 | + mouseY = event.clientY - windowHalfY; |
| 118 | + } |
| 119 | + |
| 120 | + function onDocumentTouchStart(event) { |
| 121 | + if (event.touches.length === 1) { |
| 122 | + event.preventDefault(); |
| 123 | + |
| 124 | + mouseX = event.touches[0].pageX - windowHalfX; |
| 125 | + mouseY = event.touches[0].pageY - windowHalfY; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + function onDocumentTouchMove(event) { |
| 130 | + if (event.touches.length === 1) { |
| 131 | + event.preventDefault(); |
| 132 | + |
| 133 | + mouseX = event.touches[0].pageX - windowHalfX; |
| 134 | + mouseY = event.touches[0].pageY - windowHalfY; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + function animate() { |
| 139 | + requestAnimationFrame(animate); |
| 140 | + |
| 141 | + render(); |
| 142 | + } |
| 143 | + |
| 144 | + function render() { |
| 145 | + camera.position.x += (mouseX - camera.position.x) * 0.05; |
| 146 | + camera.position.y += (-mouseY - camera.position.y) * 0.05; |
| 147 | + camera.lookAt(scene.position); |
| 148 | + |
| 149 | + var positions = particles.geometry.attributes.position.array; |
| 150 | + var scales = particles.geometry.attributes.scale.array; |
| 151 | + |
| 152 | + var i = 0, |
| 153 | + j = 0; |
| 154 | + |
| 155 | + for (var ix = 0; ix < AMOUNTX; ix++) { |
| 156 | + for (var iy = 0; iy < AMOUNTY; iy++) { |
| 157 | + positions[i + 1] = |
| 158 | + Math.sin((ix + count) * 0.3) * 50 + |
| 159 | + Math.sin((iy + count) * 0.5) * 50; |
| 160 | + |
| 161 | + scales[j] = |
| 162 | + (Math.sin((ix + count) * 0.3) + 1) * 8 + |
| 163 | + (Math.sin((iy + count) * 0.5) + 1) * 8; |
| 164 | + |
| 165 | + i += 3; |
| 166 | + j++; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + particles.geometry.attributes.position.needsUpdate = true; |
| 171 | + particles.geometry.attributes.scale.needsUpdate = true; |
| 172 | + |
| 173 | + renderer.render(scene, camera); |
| 174 | + |
| 175 | + count += 0.1; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + render() { |
| 180 | + return <div> 波浪效果 </div>; |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +export default Index; |
0 commit comments