1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| <template> <view class="heart-box"> <canvas type="webgl" id="heart" /> </view> </template>
<script> import { STATIC_URL } from '@/utils/constant.js'; import * as THREE from '@/libs/three/three.weapp.js'; import { OBJLoader } from '@/libs/three/OBJLoader.js'; import { OrbitControls } from '@/libs/three/OrbitControls.js'; import { SimplexNoiseFactory } from '@/libs/three/simplex-noise.js'; import { TrackballControls } from '@/libs/three/TrackballControls.js'; import { MeshSurfaceSamplerFactory } from '@/libs/three/MeshSurfaceSampler.js'; import gsap from '@/libs/three/gsap.min.js'; export default { props: { isDynamicEffect: { type: Boolean, default: () => false, }, }, mounted() { uni.createSelectorQuery() .in(this) .select('#heart') .node() .exec((res) => { this.drawCanvas(res[0].node); }); }, methods: { drawCanvas(node) { const canvas = THREE.global.registerCanvas(node); const camera = new THREE.PerspectiveCamera(75,1,0.1,1000); const scene = new THREE.Scene(); const renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true }); renderer.setClearAlpha(0); renderer.setSize(1000, 1000); camera.position.z = 1; const controls = new TrackballControls(camera, renderer.domElement); controls.noPan = true; controls.maxDistance = 3; controls.minDistance = 0.7; const group = new THREE.Group(); scene.add(group); let heart = null; let sampler = null; let originHeart = null; new OBJLoader().load(`${STATIC_URL}/files/heart.obj`,obj => { heart = obj.children[0]; heart.geometry.rotateX(-Math.PI * 0.5); heart.geometry.scale(0.04, 0.04, 0.04); heart.geometry.translate(0, -0.4, 0); group.add(heart); heart.material = new THREE.MeshBasicMaterial({ color: 0xdc143c }); originHeart = Array.from(heart.geometry.attributes.position.array); sampler = new (MeshSurfaceSamplerFactory(THREE))(heart).build(); init(); renderer.setAnimationLoop(render, canvas); }); let positions = []; const geometry = new THREE.BufferGeometry(); const material = new THREE.LineBasicMaterial({ color: 0x00e924 }); const lines = new THREE.LineSegments(geometry, material); const simplex = new (SimplexNoiseFactory()); const pos = new THREE.Vector3(); class Grass { constructor () { sampler.sample(pos); this.pos = pos.clone(); this.scale = Math.random() * 0.01 + 0.001; this.one = null; this.two = null; } update (a) { const noise = simplex.noise4D(this.pos.x*1.5, this.pos.y*1.5, this.pos.z*1.5, a * 0.0005) + 1; this.one = this.pos.clone().multiplyScalar(1.01 + (noise * 0.15 * beat.a)); this.two = this.one.clone().add(this.one.clone().setLength(this.scale)); } } let spikes = []; function init (a) { positions = []; for (let i = 0; i < 20000; i++) { const g = new Grass(); spikes.push(g); } } const beat = {a:0} gsap.timeline({ repeat: -1, repeatDelay: 0.3 }).to(beat, { a: 1.2, duration: 0.6, ease: 'power2.in' }).to(beat, { a: 0.0, duration: 0.6, ease: 'power3.out' }); gsap.to(group.rotation, { y: Math.PI * 2, duration: 12, ease: 'none', repeat: -1 }); const _this = this; function render(a) { if (_this.isDynamicEffect) { positions = []; spikes.forEach(g => { g.update(a); positions.push(g.one.x, g.one.y, g.one.z); positions.push(g.two.x, g.two.y, g.two.z); }); geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(positions), 3)); const vs = heart.geometry.attributes.position.array; for (let i = 0; i < vs.length; i+=3) { const v = new THREE.Vector3(originHeart[i], originHeart[i+1], originHeart[i+2]); const noise = simplex.noise4D(originHeart[i]*1.5, originHeart[i+1]*1.5, originHeart[i+2]*1.5, a * 0.0005) + 1; v.multiplyScalar(1 + (noise * 0.15 * beat.a)); vs[i] = v.x; vs[i+1] = v.y; vs[i+2] = v.z; } heart.geometry.attributes.position.needsUpdate = true; } controls.update(); renderer.render(scene, camera); } }, }, } </script>
<style lang="scss" scoped> .heart-box { width: 80vh; height: 80vh; canvas { width: 100%; height: 100%; } } </style>
|