【问题标题】:How can I create this voronoi shader animation in spark AR?如何在 spark AR 中创建这个 voronoi 着色器动画?
【发布时间】:2020-04-05 19:48:54
【问题描述】:

我有这个代码示例用于从 github 生成这个纹理。我希望这些点在流畅的动画中移动,但我不知道从哪里开始。下面是生成纹理的JS代码:

//based on voronoi shaders from: https://thebookofshaders.com/

const Materials = require('Materials');
const R = require('Reactive');
const S = require('Shaders');
const Random = require('Random');
const Patches = require('Patches');
const Animation = require('Animation');
const Time = require('Time');
const Diagnostics = require('Diagnostics');

Diagnostics.log();

let points =[];
let nPoints = 10;

const uv = S.fragmentStage(S.vertexAttribute({'variableName': S.VertexAttribute.TEX_COORDS}));


for(let i=0;i<nPoints; i++){

let x = Random.random();
let y = Random.random();
points[i] = R.pack2(x, y);

}


let m_dist = 1.;
let m_point = R.pack2(0,0);


for (let i = 0; i < nPoints; i++) {

let newPoint = R.pack2( points[i].x, points[i].y);
let dist = R.distance(uv, newPoint);

let cond = R.step(m_dist,dist) ;

m_dist = R.add(R.mul(dist,cond), R.mul(m_dist,R.sub(1,cond)));

m_point = R.add(R.mul(points[i],cond), R.mul(m_point,R.sub(1,cond)));

}


let v = R.mul(m_dist, 3.);

let color = R.pack4(v,v,v,1.);

Materials.get('defaultMaterial0').setTexture(color, {textureSlotName: "diffuseTexture"});
Materials.get('test_mat').setTexture(color, {textureSlotName: "diffuseTexture"});

如何在连续流畅的动画中为点设置动画?

【问题讨论】:

    标签: javascript shader voronoi spark-ar-studio


    【解决方案1】:

    这很酷!我能够使用动画模块以流畅的运动获得动画点。我通过将每个点变成一个随机循环动画来做到这一点。将脚本顶部的 for 循环更改为此,它应该可以工作:

    //include the Animation module
    const Animation = require('Animation');
    
    for(let i=0;i<nPoints; i++){
      //create an animation for each point x and y position
      ///x position animation
      let dx = Animation.timeDriver({durationMilliseconds: Random.random()*10000+1000, loopCount:Infinity, mirror:true});
      let sx = Animation.samplers.easeInOutSine(Random.random(), Random.random());
      let ax = Animation.animate(dx, sx);
      ///y position animation
      let dy = Animation.timeDriver({durationMilliseconds: Random.random()*10000+1000, loopCount:Infinity, mirror:true});
      let sy = Animation.samplers.easeInOutSine(Random.random(), Random.random());
      let ay = Animation.animate(dy, sy);
      //start the animation
      dx.start();
      dy.start();
      //save the points positions into the points array
      points[i] = R.pack2(ax, ay);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-09-18
      • 1970-01-01
      • 2013-12-29
      • 2018-12-16
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      相关资源
      最近更新 更多