i m new to playcanvas.i m recreating space shooter game in javascript.
first i created in playcanvas editor from scratch it works very well but when i tried to acheive same thing
through javascript and all i get is blank screen.
here is my code.
import React, { useRef, useEffect } from "react";
import BgImage from "../images/mars.jpeg";
import * as pc from "playcanvas";
const Game = () => {
const canvas = useRef(null);
useEffect(() => {
const app = new pc.Application(canvas.current, {});
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
window.addEventListener("resize", () => app.resizeCanvas());
app.start();
///Main Camera
const camera = new pc.Entity("main camera");
camera.enabled = true;
camera.addComponent("camera", {
projection: "orthographic",
clearColorBuffer: true,
clearDepthBuffer: true,
clearColor: new pc.Color(0, 0, 0),
frustumCulling: true,
orthoHeight: 10,
nearClip: 0.1,
farClip: 1000,
priority: 0,
rect: new pc.Vec4(0, 0, 1, 1),
});
app.root.addChild(camera);
camera.setLocalPosition(0, 10, 5);
camera.setLocalRotation(-90, -180, 0);
camera.setLocalScale(1, 1, 1);
///Main Camera ends
//bg material
const bgMaterial = new pc.StandardMaterial();
bgMaterial.depthTest = true;
bgMaterial.depthWrite = true;
bgMaterial.cull = pc.CULLFACE_BACK;
bgMaterial.shadingModel = pc.SPECULAR_BLINN;
const asset = new pc.Asset("bg-img", "texture", {
url: BgImage, //using react its an image
});
asset.preload = true;
app.assets.load(asset);
asset.on("load", (asset) => {
bgMaterial.diffuseMap = asset.resource;
bgMaterial.update();
});
//bg material ends
//background
const background = new pc.Entity("background");
background.addComponent("model", {
type: "plane",
material: bgMaterial,
});
background.setLocalPosition(0, -10, 0);
background.setLocalRotation(0, 0, 0);
background.setLocalScale(15, 1, 30);
app.root.addChild(background);
//background 1
const background1 = new pc.Entity("background");
background1.addComponent("model", {
type: "plane",
material: bgMaterial,
});
background1.setLocalPosition(0, 0, 1);
background1.setLocalRotation(0, 0, 0);
background1.setLocalScale(1, 1, 1);
app.root.addChild(background1);
//particle system
const particles = new pc.Entity("");
particles.addComponent("particlesystem", {
numParticles: 200,
lifetime: 100,
rate: 0.8,
loop: true,
preWarm: true,
intensity: 1,
blend: pc.BLEND_ADDITIVE,
emitterExtents: new pc.Vec3(10, 1, 1),
});
app.root.addChild(particles);
}, []);
return (
<div>
<canvas ref={canvas}></canvas>
</div>
);
};
export default Game;
////code ends
link to my game i created in playcanvas editor.
i want to acheive same thing with above code.
https://playcanvas.com/editor/scene/1235119