小猫抓毛线球特效详解编程语言
编程语言 详解 特效
2023-06-13 09:20:36 时间
效果图:
代码如下,复制即可使用:
!DOCTYPE html html head meta charset="UTF-8" title Cat vs ball of wool /title link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css" style @import url(http://fonts.googleapis.com/css?family=Open+Sans:800); #world{ position: absolute; width:100%; height: 100%; background-color: #6ecccc; overflow:hidden; #credits{ position:absolute; width:100%; margin: auto; bottom:0; margin-bottom:20px; font-family:Open Sans, sans-serif; color:#328685; font-size:0.7em; text-transform: uppercase; text-align : center; #credits a { color:#328685; #credits a:hover { color:#630d15; /style
div id="world"/ script src=http://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js /script script src=http://cdnjs.cloudflare.com/ajax/libs/gsap/1.16.1/TweenMax.min.js /script script src=https://s3-us-west-2.amazonaws.com/s.cdpn.io/264161/OrbitControls.js /script script src=https://s3-us-west-2.amazonaws.com/s.cdpn.io/264161/Cat.js /script script //THREEJS RELATED VARIABLES var scene, camera, fieldOfView, aspectRatio, nearPlane, farPlane, gobalLight, shadowLight, backLight, renderer, container, controls; //SCREEN MOUSE VARIABLES var HEIGHT, WIDTH, windowHalfX, windowHalfY, mousePos = { x: 0, y: 0 }, oldMousePos = {x:0, y:0}, ballWallDepth = 28;
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true }); renderer.setSize(WIDTH, HEIGHT); renderer.shadowMapEnabled = true; container = document.getElementById(world); container.appendChild(renderer.domElement); window.addEventListener(resize, handleWindowResize, false); document.addEventListener(mousemove, handleMouseMove, false); document.addEventListener(touchmove, handleTouchMove, false); controls = new THREE.OrbitControls(camera, renderer.domElement); controls.minPolarAngle = -Math.PI / 2; controls.maxPolarAngle = Math.PI / 2; controls.noZoom = true; controls.noPan = true; //*/
event.preventDefault(); mousePos = {x:event.touches[0].pageX, y:event.touches[0].pageY}; function createLights() { globalLight = new THREE.HemisphereLight(0xffffff, 0xffffff, .5) shadowLight = new THREE.DirectionalLight(0xffffff, .9); shadowLight.position.set(200, 200, 200); shadowLight.castShadow = true; shadowLight.shadowDarkness = .2; shadowLight.shadowMapWidth = shadowLight.shadowMapHeight = 2048; backLight = new THREE.DirectionalLight(0xffffff, .4); backLight.position.set(-100, 100, 100); backLight.castShadow = true; backLight.shadowDarkness = .1; backLight.shadowMapWidth = shadowLight.shadowMapHeight = 2048; scene.add(globalLight); scene.add(shadowLight); scene.add(backLight); function createFloor(){ floor = new THREE.Mesh(new THREE.PlaneBufferGeometry(1000,1000), new THREE.MeshBasicMaterial({color: 0x6ecccc})); floor.rotation.x = -Math.PI/2; floor.position.y = 0; floor.receiveShadow = true; scene.add(floor); function createHero() { hero = new Cat(); scene.add(hero.threeGroup); function createBall() { ball = new Ball(); scene.add(ball.threeGroup); // BALL RELATED CODE
for (var i=0; i woolNodes; i++ ){ var v = new THREE.Vector3(0, -i*woolSegLength, 0); stringGeom.vertices.push(v); var woolV = new WoolVert(); woolV.x = woolV.oldx = v.x; woolV.y = woolV.oldy = v.y; woolV.z = 0; woolV.fx = woolV.fy = 0; woolV.isRootNode = (i==0); woolV.vertex = v; if (i 0) woolV.attach(this.verts[(i - 1)]); this.verts.push(woolV); this.string = new THREE.Line(stringGeom, stringMat); // body var bodyGeom = new THREE.SphereGeometry(this.ballRay, 5,4); this.body = new THREE.Mesh(bodyGeom, redMat); this.body.position.y = -woolSegLength*woolNodes; var wireGeom = new THREE.TorusGeometry( this.ballRay, .5, 3, 10, Math.PI*2 ); this.wire1 = new THREE.Mesh(wireGeom, redMat); this.wire1.position.x = 1; this.wire1.rotation.x = -Math.PI/4; this.wire2 = this.wire1.clone(); this.wire2.position.y = 1; this.wire2.position.x = -1; this.wire1.rotation.x = -Math.PI/4 + .5; this.wire1.rotation.y = -Math.PI/6; this.wire3 = this.wire1.clone(); this.wire3.rotation.x = -Math.PI/2 + .3; this.wire4 = this.wire1.clone(); this.wire4.position.x = -1; this.wire4.rotation.x = -Math.PI/2 + .7; this.wire5 = this.wire1.clone(); this.wire5.position.x = 2; this.wire5.rotation.x = -Math.PI/2 + 1; this.wire6 = this.wire1.clone(); this.wire6.position.x = 2; this.wire6.position.z = 1; this.wire6.rotation.x = 1; this.wire7 = this.wire1.clone(); this.wire7.position.x = 1.5; this.wire7.rotation.x = 1.1; this.wire8 = this.wire1.clone(); this.wire8.position.x = 1; this.wire8.rotation.x = 1.3; this.wire9 = this.wire1.clone(); this.wire9.scale.set(1.2,1.1,1.1); this.wire9.rotation.z = Math.PI/2; this.wire9.rotation.y = Math.PI/2; this.wire9.position.y = 1; this.body.add(this.wire1); this.body.add(this.wire2); this.body.add(this.wire3); this.body.add(this.wire4); this.body.add(this.wire5); this.body.add(this.wire6); this.body.add(this.wire7); this.body.add(this.wire8); this.body.add(this.wire9); this.threeGroup.add(this.string); this.threeGroup.add(this.body); this.threeGroup.traverse( function ( object ) { if ( object instanceof THREE.Mesh ) { object.castShadow = true; object.receiveShadow = true; }}); The next part of the code is largely inspired by this codepen : thanks to dissimulate for his great work Copyright (c) 2013 dissimulate at Codepen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
nx = this.x + ((this.x - this.oldx)*.9) + this.fx; ny = this.y + ((this.y - this.oldy)*.9) + this.fy; this.oldx = this.x; this.oldy = this.y; this.x = nx; this.y = ny; this.vertex.x = this.x; this.vertex.y = this.y; this.vertex.z = this.z; this.fy = this.fx = 0 WoolVert.prototype.attach = function(point) { this.constraints.push(new Constraint(this, point)); WoolVert.prototype.add_force = function(x, y) { this.fx += x; this.fy += y; Constraint = function(p1, p2) { this.p1 = p1; this.p2 = p2; this.length = woolSegLength; Ball.prototype.update = function(posX, posY, posZ){ var i = accuracy; while (i--) { var nodesCount = woolNodes; while (nodesCount--) { var v = this.verts[nodesCount]; if (v.isRootNode) { v.x = posX; v.y = posY; v.z = posZ; else { var constraintsCount = v.constraints.length; while (constraintsCount--) { var c = v.constraints[constraintsCount]; var diff_x = c.p1.x - c.p2.x, diff_y = c.p1.y - c.p2.y, dist = Math.sqrt(diff_x * diff_x + diff_y * diff_y), diff = (c.length - dist) / dist; var px = diff_x * diff * .5; var py = diff_y * diff * .5; c.p1.x += px; c.p1.y += py; c.p2.x -= px; c.p2.y -= py; c.p1.z = c.p2.z = posZ; if (nodesCount == woolNodes-1){ this.body.position.x = this.verts[nodesCount].x; this.body.position.y = this.verts[nodesCount].y; this.body.position.z = this.verts[nodesCount].z; this.body.rotation.z += (v.y = this.ballRay)? (v.oldx-v.x)/10 : Math.min(Math.max( diff_x/2, -.1 ), .1); if (v.y this.ballRay) { v.y = this.ballRay; nodesCount = woolNodes; while (nodesCount--) this.verts[nodesCount].update(); this.string.geometry.verticesNeedUpdate = true;
Ball.prototype.receivePower = function(tp){ this.verts[woolNodes-1].add_force(tp.x, tp.y); // Enf of the code inspired by dissmulate
( mousePos.x / window.innerWidth ) * 2 - 1, - ( mousePos.y / window.innerHeight ) * 2 + 1, 0.1 ); vector.unproject( camera ); var dir = vector.sub( camera.position ).normalize(); var distance = (ballWallDepth - camera.position.z) / dir.z; var pos = camera.position.clone().add( dir.multiplyScalar( distance ) ); return pos; function render(){ if (controls) controls.update(); renderer.render(scene, camera); window.addEventListener(load, init, false); function init(event){ initScreenAnd3D(); createLights(); createFloor() createHero(); createBall(); loop(); /script /body /html
如有错误,欢迎联系我改正,非常感谢!!!
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/12328.html
cgojava相关文章
- 用一个示例入门solidity编程语言
- Jquery数字切换特效详解编程语言
- JS实现回到顶部特效详解编程语言
- java 发起http post请求详解编程语言
- Android非UI线程更新UI的几种方法详解编程语言
- Oracle删除重复记录详解编程语言
- node.js:exopress-连接mysql报错:Access denied for user ‘root’@’localhost’ (using password: NO)问题解决。详解编程语言
- JS实现逼真的雪花飘落特效详解编程语言
- Python学习:5、if语句,while语句,函数详解编程语言
- java虚拟机如何操作缓冲区详解编程语言
- 提高Web性能的前端优化技巧总结详解编程语言
- NIO之阻塞IO与非阻塞IO详解编程语言
- Tomcat 的 JDBC 连接池详解编程语言
- apache poi合并单元格设置边框详解编程语言
- MongoDB JAVA API Filters详解编程语言
- java中split以”.” 、”/”、“|”分隔详解编程语言
- java List排序问题详解编程语言
- ABAP ODATA服务发布详解编程语言
- Go-字符串拼接-性能测试总结详解编程语言
- SAP 录屏BDC使用—实例详解编程语言
- javascript tweenmax时间轴动画控制详解编程语言
- 如何通过输入输出参数查找类方法[表SEOSUBCO]详解编程语言
- 如何在SM30维护表时自动写入表字段的默认值-事件(EVENT)详解编程语言
- BAPI_GOODSMVT_CREATE的参数GOODSMVT_CODE的说明详解编程语言
- abap 获取生产订单标准工时和实际报工工时的报表详解编程语言
- Zookeeper——Zookeeper简单使用详解编程语言