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


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


p5.j​​s中的setCamera()函数用于将渲染器的当前相机设置为给定的p5.Camera对象。这可用于切换到多个摄像机。

用法:

setCamera( cam )

参数:该函数接受如上所述和以下描述的单个参数:

  • cam:函数会将相机更改为p5.Camera对象。

以下示例说明了p5.js中的setCamera()函数:

javascript



let cameras = []; 
let currCameraIndex = 0; 
  
function setup() { 
  createCanvas(600, 400, WEBGL); 
  helpText = createP( 
    "Click on the buttons to switch to the"+ 
    " next camera of the sketch"
  ); 
  helpText.position(20, 0); 
  
  // Button to switch to the next camera 
  // in the scene 
  newCameraBtn = createButton("Switch to Next Camera"); 
  newCameraBtn.position(20, 40); 
  newCameraBtn.mouseClicked(switchActiveCamera); 
  
  // Create 5 cameras and store into array 
  for (let i = 0; i < 5; i++) { 
    cameras[i] = createCamera(); 
  
    // Randomly set the position the camera 
    // is looking at using setPosition() 
    randomX = floor(random(-100, 100)); 
    randomY = floor(random(-100, 100)); 
  
    cameras[i].setPosition(randomX, randomY, 350); 
  } 
} 
  
function switchActiveCamera() { 
  // Increment the camera index 
  if (currCameraIndex < 4) currCameraIndex += 1; 
  else currCameraIndex = 0; 
  
  // Set the camera from the camera array 
  // to that index 
  setCamera(cameras[currCameraIndex]); 
} 
  
function draw() { 
  clear(); 
  orbitControl(); 
  normalMaterial(); 
  
  // Create three boxes at three positions 
  translate(-150, 0); 
  box(65); 
  translate(150, 0); 
  box(65); 
  translate(150, 0); 
  box(65); 
}

输出:

setCamera-switch

在线编辑: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5/setCamera




相关用法


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