java - 在加工中制作箱内灯
问题描述
我是处理中的新编码员,所以请温柔。通常,我的代码更长更复杂,但我为您准备了简单的代码,我可以将其转换为我的代码。所以,代码是:
void setup()
{
size(1200, 800, P3D);
camZ = (height/2) / tan((PI*60.0)/360.0);
noStroke();
}
float camZ = 0;
void draw()
{
background(0);
camera(0, 0.0, camZ, //default camera position
0, 0, -500, //where it is looking to
0, 1, 0); //eye openness
rotateX(rotX + distY);
rotateY(rotY + distX);
scale(100);
drawHouse();
if(keyPressed)
{
if(key == 'w')
camZ -= 5;
else if(key == 's')
camZ += 5;
}
}
void drawHouse()
{
beginShape(QUADS);
fill(255,0,0);
//+Z face
vertex(-1, -1, 1); //upper left corner
vertex(1, -1, 1); //upper right corner
vertex(1, 1, 1); //bottom right corner
vertex(-1, 1, 1); //bottom left corner
fill(0,255,0);
//-Z face
vertex(-1, -1, -1); //upper left corner
vertex(1, -1, -1); //upper right corner
vertex(1, 1, -1); //bottom right corner
vertex(-1, 1, -1); //bottom left corner
fill(0,0,255);
//+X face
vertex(1, -1, 1); //upper left corner
vertex(1, -1, -1);
vertex(1, 1, -1);
vertex(1, 1, 1);
fill(255);
//-X face
vertex(-1, -1, 1); //upper left corner
vertex(-1, -1, -1);
vertex(-1, 1, -1);
vertex(-1, 1, 1);
fill(208,13,211);
//-Y face
vertex(-1, -1, 1);
vertex(1, -1, 1);
vertex(1, -1, -1);
vertex(-1, -1, -1);
fill(250,150,18);
//+Y face
vertex(-1, 1, 1);
vertex(1, 1, 1);
vertex(1, 1, -1);
vertex(-1, 1, -1);
endShape();
}
float rotX = 0, rotY = 0;
float lastX, lastY;
float distX, distY;
void mousePressed()
{
lastX = mouseX;
lastY = mouseY;
}
void mouseDragged()
{
distX = radians(mouseX - lastX);
distY = radians(lastY - mouseY);
}
void mouseReleased()
{
rotX += distY;
rotY += distX;
distX = distY = 0;
}
所以,我想在代码中给出的盒子里点亮一盏灯。灯光应该只在盒子里面起作用。如何让这种情况发生?
解决方案
好问题!这让我很好奇,我不得不进行调查。恐怕这超出了我的舒适区,但我发现了一些可以帮助你的东西。
您的简化代码很棒,但我进一步简化它以使事情更加明显。首先是代码,然后是一些解释:
float camZ = 0;
float rotX = 0, rotY = 0;
float lastX, lastY;
float distX, distY;
void setup()
{
size(1200, 800, P3D);
camZ = (height/2) / tan((PI*60.0)/360.0);
noStroke();
}
void draw()
{
background(0);
lights(); // We want to see what's outside the cube, too.
camera(0, 0.0, camZ, //default camera position
0, 0, -500, //where it is looking to
0, 1, 0); //eye openness
rotateX(rotX + distY);
rotateY(rotY + distX);
scale(100);
pointLight(255, 0, 0, 0, 0, 0); // this light is inside the cube. You cannot see it from outside (it's red, too)
box(2); //I made a white cube so the lightning will be more obvious
if (keyPressed)
{
if (key == 'w')
camZ -= 5;
else if (key == 's')
camZ += 5;
}
}
void mousePressed()
{
lastX = mouseX;
lastY = mouseY;
}
void mouseDragged()
{
distX = radians(mouseX - lastX);
distY = radians(lastY - mouseY);
}
void mouseReleased()
{
rotX += distY;
rotY += distX;
distX = distY = 0;
}
你试过运行它吗?从外面看,盒子是灰白色的。但是,如果您将相机移到盒子内,您会看到墙壁有红色调。这是正在发生的事情:
我使用lights()来照亮草图。如果您想更改它的值,它基本上是一个默认的ambientLight - 这对于这个演示并不重要。如果您对该行进行注释,您会注意到在立方体之外看不到任何东西。
我在立方体的中间添加了一个点光源。点光源就像一个小灯泡:它是全向的。
我的点光源是红色的,它只会照亮红色表面(在这种情况下,白墙看起来是红色的,因为白色在 RGB 中有红色部分)。
我希望这能让你走上正确的道路。祝你好运!
推荐阅读
- mysql - Mysql - 按大于或小于当前时间的日期对记录进行分组 now() mysql
- sql - SQL WHERE + MAX 查询
- php - 分页不适用于 CPT/Taxonomy 的自定义 URL 结构
- typescript - styled-component 组件中的 props 没有类型
- google-cloud-platform - 为什么我在尝试在 Google 目录服务中创建用户时会看到 403 Not Authorized to access this resource/api 错误?
- php - 启用 Intranet 以检查用户对共享文件的访问权限
- python - 有没有办法创建一个在 tf.estimator.train_and_evaluate 评估步骤中持续存在的变量?
- python - 如何从多数组数组中提取数组?
- javascript - 你能在同一个控制器中调用两个 Promise 吗?
- wix - 如何使用 WixSharp 注册 ActiveX 控件?