草庐IT

程序员教你用代码制作3d爱心跳动特效,正好拿去送给女神给她个惊喜

晚安独角兽 2023-04-14 原文

HTML+CSS+JavaScript实现

先点赞后观看,养成好习惯

效果图

注:任意浏览器都可以,建议使用谷歌浏览器 

代码部分

代码如下仅供参考
可以直接拿去复制粘贴

(function () {
  const _face = new THREE.Triangle();

  const _color = new THREE.Vector3();

  class MeshSurfaceSampler {

    constructor(mesh) {

      let geometry = mesh.geometry;

      if (!geometry.isBufferGeometry || geometry.attributes.position.itemSize !== 3) {

        throw new Error('THREE.MeshSurfaceSampler: Requires BufferGeometry triangle mesh.');

      }

      if (geometry.index) {

        console.warn('THREE.MeshSurfaceSampler: Converting geometry to non-indexed BufferGeometry.');
        geometry = geometry.toNonIndexed();

      }

      this.geometry = geometry;
      this.randomFunction = Math.random;
      this.positionAttribute = this.geometry.getAttribute('position');
      this.colorAttribute = this.geometry.getAttribute('color');
      this.weightAttribute = null;
      this.distribution = null;

    }

    setWeightAttribute(name) {

      this.weightAttribute = name ? this.geometry.getAttribute(name) : null;
      return this;

    }

    build() {

      const positionAttribute = this.positionAttribute;
      const weightAttribute = this.weightAttribute;
      const faceWeights = new Float32Array(positionAttribute.count / 3); // Accumulate weights for each mesh face.

      for (let i = 0; i < positionAttribute.count; i += 3) {

        let faceWeight = 1;

        if (weightAttribute) {

          faceWeight = weightAttribute.getX(i) + weightAttribute.getX(i + 1) + weightAttribute.getX(i + 2);

        }

        _face.a.fromBufferAttribute(positionAttribute, i);

        _face.b.fromBufferAttribute(positionAttribute, i + 1);

        _face.c.fromBufferAttribute(positionAttribute, i + 2);

        faceWeight *= _face.getArea();
        faceWeights[i / 3] = faceWeight;

      } // Store cumulative total face weights in an array, where weight index
      // corresponds to face index.


      this.distribution = new Float32Array(positionAttribute.count / 3);
      let cumulativeTotal = 0;

      for (let i = 0; i < faceWeights.length; i++) {

        cumulativeTotal += faceWeights[i];
        this.distribution[i] = cumulativeTotal;

      }

      return this;

    }

    setRandomGenerator(randomFunction) {

      this.randomFunction = randomFunction;
      return this;

    }

    sample(targetPosition, targetNormal, targetColor) {

      const cumulativeTotal = this.distribution[this.distribution.length - 1];
      const faceIndex = this.binarySearch(this.randomFunction() * cumulativeTotal);
      return this.sampleFace(faceIndex, targetPosition, targetNormal, targetColor);

    }

    binarySearch(x) {

      const dist = this.distribution;
      let start = 0;
      let end = dist.length - 1;
      let index = - 1;

      while (start <= end) {

        const mid = Math.ceil((start + end) / 2);

        if (mid === 0 || dist[mid - 1] <= x && dist[mid] > x) {

          index = mid;
          break;

        } else if (x < dist[mid]) {

          end = mid - 1;

        } else {

          start = mid + 1;

        }

      }

      return index;

    }

    sampleFace(faceIndex, targetPosition, targetNormal, targetColor) {

      let u = this.randomFunction();
      let v = this.randomFunction();

      if (u + v > 1) {

        u = 1 - u;
        v = 1 - v;

      }

      _face.a.fromBufferAttribute(this.positionAttribute, faceIndex * 3);

      _face.b.fromBufferAttribute(this.positionAttribute, faceIndex * 3 + 1);

      _face.c.fromBufferAttribute(this.positionAttribute, faceIndex * 3 + 2);

      targetPosition.set(0, 0, 0).addScaledVector(_face.a, u).addScaledVector(_face.b, v).addScaledVector(_face.c, 1 - (u + v));

      if (targetNormal !== undefined) {

        _face.getNormal(targetNormal);

      }

      if (targetColor !== undefined && this.colorAttribute !== undefined) {

        _face.a.fromBufferAttribute(this.colorAttribute, faceIndex * 3);

        _face.b.fromBufferAttribute(this.colorAttribute, faceIndex * 3 + 1);

        _face.c.fromBufferAttribute(this.colorAttribute, faceIndex * 3 + 2);

        _color.set(0, 0, 0).addScaledVector(_face.a, u).addScaledVector(_face.b, v).addScaledVector(_face.c, 1 - (u + v));

        targetColor.r = _color.x;
        targetColor.g = _color.y;
        targetColor.b = _color.z;
      }
      return this;

    }

  }

  THREE.MeshSurfaceSampler = MeshSurfaceSampler;

})();

运行步骤,在桌面新建个文本文档,将代码全部复制到文档后保存,然后重命名将.txt文件修改成.html文件最后点击文件就能运行成功啦,运行成功了快快发给爱慕女孩,说不定就能收获到爱情啦。(如果有不想动手的小伙伴可以私聊博主获取哦!!!!)

获取途径:https://download.csdn.net/download/javayoungcoolboy/86936685

也可通过公众号:晚安独角兽 免费获取

 

有关程序员教你用代码制作3d爱心跳动特效,正好拿去送给女神给她个惊喜的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  3. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  4. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  5. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  6. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  7. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  8. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  9. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  10. ruby-on-rails - 浏览 Ruby 源代码 - 2

    我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru

随机推荐