簡體   English   中英

如何使用Processing 2.0(Java)在元素周圍繪制發光的光暈?

[英]How to draw a glowing halo around elements using Processing 2.0 (Java)?

如果我想使用Processing 2.0(Java)創建“發光的光環效果”,該怎么做?

我可以將任何內置方法/轉換應用於對象以使其發光嗎?

如果不是,是否有任何視覺技巧可以達到相同的效果?

我至少在一般情況下不知道內置方式(您不提及代碼的組織方式)。 但是,這里有一個DIY方法的快速演示。 這個想法是您在對象后面繪制一系列逐漸變大和變暗的橢圓。 您可能需要根據自己的喜好調整硬編碼的數字,並且可能使它非線性透明(可能會變暗更長的時間?)。

Thing thing1;
Thing thing2;

void setup(){
  size(300,300);
  smooth();
  thing1 = new Thing(1*width/3,height/2,false);
  thing2 = new Thing(2*width/3,height/2,true);
}
void draw(){
  background(100);
  stroke(0);
  line(100,100,250,250);
  line(150,100,300,250);
  thing1.display();
  thing2.display();
}

class Thing
{
  PVector loc;
  boolean glowing;
  Thing(int x, int y, boolean glow){
    loc = new PVector(x,y);
    glowing = glow;
  }
  void display(){
    if(glowing){
      //float glowRadius = 100.0;
      float glowRadius = 100.0 + 15 * sin(frameCount/(3*frameRate)*TWO_PI); 
      strokeWeight(2);
      fill(255,0);
      for(int i = 0; i < glowRadius; i++){
        stroke(255,255.0*(1-i/glowRadius));
        ellipse(loc.x,loc.y,i,i);
      }
    }
    //irrelevant
    stroke(0);
    fill(0);
    ellipseMode(CENTER);
    ellipse(loc.x,loc.y,40,40);
    stroke(0,255,0);
    line(loc.x,loc.y+30,loc.x,loc.y-30);
    line(loc.x+30,loc.y,loc.x-30,loc.y);
  }
}

在此處輸入圖片說明

此外,如果對象不是圓對稱的,則可以執行以下操作。 它查看對象的像素,並在找到非空白像素的位置繪制一個幾乎透明的橢圓。 但是,這是一個相當混亂的解決方案。 跟隨對象的邊緣或采用其他方法可能更好。 我希望這能給您一些想法!

Thing thing1;

void setup(){
  size(300,300);
  background(0);
  thing1 = new Thing();
  thing1.display();
}
void draw(){}

class Thing
{
  PGraphics pg;
  Thing(){
    pg = createGraphics(200,200);
  }
  void display(){
    pg.beginDraw();
    pg.background(0,0);
    pg.strokeWeight(10);
    pg.stroke(255,100,0);
    pg.line(100,50,100,150);
    pg.line(75,50,125,50);
    pg.line(75,150,125,150);
    pg.line(30,100,170,100);
    pg.endDraw();
    PGraphics glow = createGraphics(200,200);
    glow.beginDraw();
    glow.image(pg,0,0);
    glow.loadPixels();
    glow.fill(255,3);
    glow.noStroke();
    //change the +=2 for different effects
    for(int x = 0; x < glow.width; x+=2){
      for(int y = 0; y < glow.height; y+=2){
        if(alpha(glow.pixels[y*glow.width+x]) != 0) glow.ellipse(x,y,40,40);
      }
    }

    glow.endDraw();
    //draw the glow:
    image(glow,50,50);
    //draw the sprite:
    image(pg,50,50);
  }
}

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM