当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


p5.js Camera setPosition()用法及代码示例

p5.j​​s中p5.Camera的setPosition()方法用于将摄像机的位置设置为世界空间中的给定点。更改位置时,它可保持当前相机方向。

用法:

setPosition( x, y, z )

参数:此方法接受上述和以下所述的三个参数:

  • x:它是一个数字,表示该点在世界空间中的x位置。
  • y:它是一个数字,表示该点在世界空间中的y位置。
  • z:该数字表示点在世界空间中的z位置。

以下示例说明了p5.js中的setPosition()方法:

例:



Javascript

let currCamera; 
  
function setup() { 
  createCanvas(500, 500, WEBGL); 
  helpText = createP( 
    "Move the sliders to change the " + 
    "position of the camera"
  ); 
  helpText.position(20, 0); 
  
  // Create the camera 
  currCamera = createCamera(); 
  
  // Create three sliders for changing the 
  // position of the camera 
  xPosSlider = createSlider(-360, 360, 0); 
  xPosSlider.position(20, 40); 
  
  yPosSlider = createSlider(-360, 360, 0); 
  yPosSlider.position(20, 70); 
  
  zPosSlider = createSlider(0, 800, 600); 
  zPosSlider.position(20, 100); 
} 
  
function draw() { 
  clear(); 
  lights(); 
  normalMaterial(); 
  debugMode(); 
  
  // Get the x, y, z values from the 
  // sliders 
  let currX = xPosSlider.value(); 
  let currY = yPosSlider.value(); 
  let currZ = zPosSlider.value(); 
  
  // Set the position of the camera 
  // to these points in the world space 
  currCamera.setPosition(currX, currY, currZ); 
  
  cylinder(90); 
}

输出:

在线编辑: https://editor.p5js.org/

环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

参考: https://p5js.org/reference/#/p5.Camera/setPosition

相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 p5.Camera setPosition() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。