小猫抓毛线球特效

小猫抓毛线球特效

小猫抓毛线球特效

 效果图:

 

代码如下,复制即可使用:

<!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>


  
</head>

<body>
      <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;


//3D OBJECTS VARIABLES

var hero;

//INIT THREE JS, SCREEN AND MOUSE EVENTS

function initScreenAnd3D() {
  
  HEIGHT = window.innerHeight;
  WIDTH = window.innerWidth;
  windowHalfX = WIDTH / 2;
  windowHalfY = HEIGHT / 2;

  scene = new THREE.Scene();
  aspectRatio = WIDTH / HEIGHT;
  fieldOfView = 50;
  nearPlane = 1;
  farPlane = 2000;
  camera = new THREE.PerspectiveCamera(
    fieldOfView,
    aspectRatio,
    nearPlane,
    farPlane
    );
  camera.position.x = 0;
  camera.position.z = 300;
  camera.position.y = 250;
  camera.lookAt(new THREE.Vector3(0, 60, 0));

  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;
  //*/


}

function handleWindowResize() {
  HEIGHT = window.innerHeight;
  WIDTH = window.innerWidth;
  windowHalfX = WIDTH / 2;
  windowHalfY = HEIGHT / 2;
  renderer.setSize(WIDTH, HEIGHT);
  camera.aspect = WIDTH / HEIGHT;
  camera.updateProjectionMatrix();
}

function handleMouseMove(event) {
  mousePos = {x:event.clientX, y:event.clientY};
} 

function handleTouchMove(event) {
  if (event.touches.length == 1) {
    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


var woolNodes = 10,
    woolSegLength = 2,
    gravity = -.8,
    accuracy =1;


Ball = function(){

    var redMat = new THREE.MeshLambertMaterial ({
        color: 0x630d15, 
        shading:THREE.FlatShading
    });

    var stringMat = new THREE.LineBasicMaterial({
        color: 0x630d15,
        linewidth: 3
    });

    this.threeGroup = new THREE.Group();
    this.ballRay = 8;

    this.verts = [];

    // string
    var stringGeom = new THREE.Geometry();

    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 :
http://codepen.io/dissimulate/pen/KrAwx?editors=001
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.
*/



WoolVert = function(){
    this.x = 0;
    this.y = 0;
    this.z = 0;
    this.oldx = 0;
    this.oldy = 0;
    this.fx = 0;
    this.fy = 0;
    this.isRootNode = false;
    this.constraints = [];
    this.vertex = null;
}


WoolVert.prototype.update = function(){
    var wind = 0;//.1+Math.random()*.5;
      this.add_force(wind, gravity);

      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


// Make everything work together :

var t=0;

function loop(){
  render();
  
  t+=.05;
  hero.updateTail(t);

  var ballPos = getBallPos();
  ball.update(ballPos.x,ballPos.y, ballPos.z);
  ball.receivePower(hero.transferPower);
  hero.interactWithBall(ball.body.position);

  requestAnimationFrame(loop);
}


function getBallPos(){
  var vector = new THREE.Vector3();

  vector.set(
      ( 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>

 如有错误,欢迎联系我改正,非常感谢!!!

posted @ 2018-04-03 16:15 Foryourfuture 阅读( ...) 评论( ...) 编辑 收藏
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值