将 P3D 对象放置在默认渲染器中

Placing a P3D object in the default renderer

我在 Processing 3 中使用默认渲染器创建了一个音频可视化器,现在我想在音频可视化器(在默认渲染器中创建)中实现一个独立的旋转 3D 立方体(使用 P3D)。这是 3D 立方体的代码:

import processing.opengl.*;

float y = 0.1;
float x = 0.1;
float z = 0.1;

void setup()
{
    size(800,600,P3D);
    smooth();
}

void draw()
{
    translate(400,300,0);
    rotateX(x);
    rotateY(y);
    rotateZ(z);
    background(255);
    fill(255,228,225);
    box(200);
    x += random(.1);
    y += random(.1);
    z += random(.1);
}

这是可视化工具中与 3D 立方体相关的片段:

void setup()
{
  size(800, 600);
  //fullScreen(2);
  minim = new Minim(this);
  player = minim.loadFile("/Users/samuel/Desktop/GT.mp3");
  meta = player.getMetaData();
  beat = new BeatDetect();
  player.loop();
  fft = new FFT(player.bufferSize(), player.sampleRate());
  fft.logAverages(60, 7);
  noStroke();
  w = width/fft.avgSize();
  player.play();
  background(0);
  smooth();
}

最后,我很好奇是否可以在不将可视化器的 size() 更改为 P3D 的情况下集成 3D 对象。

您可以使用 createGraphics() 函数创建渲染器,您可以将 P3D 传递到该渲染器以允许在 3D 中绘图。

但是,如果您的草图使用的是默认渲染器,则无法执行此操作。您必须在主渲染器中使用 P2DP3D 才能在任何 createGraphics() 渲染器中使用 P3D。来自 the reference:

It's important to consider the renderer used with createGraphics() in relation to the main renderer specified in size(). For example, it's only possible to use P2D or P3D with createGraphics() when one of them is defined in size(). Unlike Processing 1.0, P2D and P3D use OpenGL for drawing, and when using an OpenGL renderer it's necessary for the main drawing surface to be OpenGL-based. If P2D or P3D are used as the renderer in size(), then any of the options can be used with createGraphics(). If the default renderer is used in size(), then only the default or PDF can be used with createGraphics().

这是一个使用 P2D 渲染器作为主渲染器和 P3D 作为子渲染器的小示例:

PGraphics pg;

void setup() {
  size(200, 200, P2D);
  pg = createGraphics(100, 100, P3D);
}

void draw() {
  pg.beginDraw();
  pg.background(0);
  pg.noStroke();
  pg.translate(pg.width*0.5, pg.height*0.5);
  pg.lights();
  pg.sphere(25);
  pg.endDraw();

  background(0, 0, 255);
  image(pg, 50, 50); 
}