Scene.render() is not a function

Hi. I am new to Babylon.js

Nice to see you guys!

I just copied an example on Playground and pasted it on my editor to run it on local, but it keeps saying “scene.render() is not a function”

I saw this “[HELP] Scene.Render is not a function” post where it required const scene = await createScene() but this way, I just get “await must be used with async”

Can someone please explain what is happening here?

Thanks a lot!

code I copied

const canvas = document.getElementById("renderCanvas");
const engine = new BABYLON.Engine(canvas, true);

const createScene = async function () {
  const scene = new BABYLON.Scene(engine);
  
  const camera = new BABYLON.UniversalCamera("camera1", new BABYLON.Vector3(0, 20, 0), scene);
  camera.setTarget(new BABYLON.Vector3(0, 5, 0))
  camera.attachControl(canvas, true);
  
  const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
  
  // import boxes
  const data = await BABYLON.SceneLoader.ImportMeshAsync("", "https://raw.githubusercontent.com/griga/babylonjs-resources/master/scenes/", "hover-instances.babylon", scene);
  const sourceMesh = data.meshes[0];
  
  // coloring part
  const sourceMat = sourceMesh.material;
  const color = sourceMat.albedoColor;
  const instanceCount = sourceMesh.instances.length;
  
  sourceMesh.registerInstancedBuffer("color", 4); // 4 is the stride size eg. 4 floats here
  
  const vcolor = new BABYLON.Color4(color.r, color.g, color.b, 0.1);
  const vcolorHover = new BABYLON.Color4(1, 0, 0, 0.1);
  
  sourceMesh.instancedBuffers.color = vcolor;
  
  for (let index = 0; index < instanceCount; index++) {
    sourceMesh.instances[index].instancedBuffers.color = vcolor;
  }
  
  // hovering part
  let hoveredMeshes = [];
  scene.onBeforeRenderObservable.add(() => {
    const pickResult = scene.pick(scene.pointerX, scene.pointerY, (mesh) => mesh.isPickable && mesh.isVisible && mesh.isReady(), false, camera);
    
    if (pickResult.hit) {
      if (!hoveredMeshes.includes(pickResult.pickedMesh.id)) {
        hoveredMeshes.push(pickResult.pickedMesh.id);
        setInstancedMeshesColor(hoveredMeshes, vcolorHover, scene);
      }
    } else if (hoveredMeshes.length > 0) {
      setInstancedMeshesColor(hoveredMeshes, vcolor, scene);
      hoveredMeshes = []
    }
  });
  
  return scene;
};

function setInstancedMeshesColor(hoveredMeshes, color, scene) {
  hoveredMeshes.forEach(meshId => {
    scene.getMeshByID(meshId).instancedBuffers.color = color;
  });
};

const scene = createScene();

engine.runRenderLoop(function () {
  scene.render();
})

window.addEventListener("resize", function () {
  engine.resize();
})

Hello and welcome!

In order to use the code from Playground examples the simplest way is to press the button “Download”.


It will give you zip file with all needed code and assets (if any).
You will notice that there are some differences between your code and downloaded version.
I include this version below.
PG-example.zip (5.9 KB)

2 Likes

Thank you so much for your help!

I just downloaded Sphere example and ran it on local server, but still got scene.render is not a function error :frowning:

Could there be anything else I should do for setting up?

Also, once I get it working, can I have the script code separately?

Thanks again for the fast reply!!

I made short video about it :slight_smile:
(For this example you even don’t need local server)

Will you get any errors if you repeat steps in the video?

1 Like

Try logging the scene object to the console. See if it is undefined. If so then you have not included the choir library with you playground. This may be due to network errors. Try including the babylonjs with a script tag directing to a file.

haha yeah! I got it working just by clicking the index file. I don’t know why, but running it on local server did not work.

Thank you for helping me out!! I really appreciate your help. Great video!

I got it working just by clicking the index file. I don’t know why, but running it on local server did not work though.

Thanks for your comment!!