|
| 1 | +// @config HIDDEN |
| 2 | +import { deviceType, rootPath, fileImport } from 'examples/utils'; |
| 3 | +import * as pc from 'playcanvas'; |
| 4 | + |
| 5 | +const { CameraControls } = await fileImport(`${rootPath}/static/scripts/esm/camera-controls.mjs`); |
| 6 | + |
| 7 | +const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas')); |
| 8 | +window.focus(); |
| 9 | + |
| 10 | +const assets = { |
| 11 | + helipad: new pc.Asset( |
| 12 | + 'morning-env-atlas', |
| 13 | + 'texture', |
| 14 | + { url: `${rootPath}/static/assets/cubemaps/morning-env-atlas.png` }, |
| 15 | + { type: pc.TEXTURETYPE_RGBP, mipmaps: false } |
| 16 | + ), |
| 17 | + model: new pc.Asset('model', 'container', { url: `${rootPath}/static/assets/models/TwoSidedPlane.glb` }) |
| 18 | +}; |
| 19 | + |
| 20 | +const gfxOptions = { |
| 21 | + deviceTypes: [deviceType] |
| 22 | +}; |
| 23 | + |
| 24 | +const device = await pc.createGraphicsDevice(canvas, gfxOptions); |
| 25 | +device.maxPixelRatio = Math.min(window.devicePixelRatio, 2); |
| 26 | + |
| 27 | +const createOptions = new pc.AppOptions(); |
| 28 | +createOptions.graphicsDevice = device; |
| 29 | + |
| 30 | +createOptions.componentSystems = [ |
| 31 | + pc.RenderComponentSystem, |
| 32 | + pc.CameraComponentSystem, |
| 33 | + pc.LightComponentSystem, |
| 34 | + pc.ScriptComponentSystem |
| 35 | +]; |
| 36 | +createOptions.resourceHandlers = [pc.TextureHandler, pc.ContainerHandler, pc.ScriptHandler]; |
| 37 | + |
| 38 | +const app = new pc.AppBase(canvas); |
| 39 | +app.init(createOptions); |
| 40 | + |
| 41 | +app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW); |
| 42 | +app.setCanvasResolution(pc.RESOLUTION_AUTO); |
| 43 | + |
| 44 | +const resize = () => app.resizeCanvas(); |
| 45 | +window.addEventListener('resize', resize); |
| 46 | +app.on('destroy', () => { |
| 47 | + window.removeEventListener('resize', resize); |
| 48 | +}); |
| 49 | + |
| 50 | +await new Promise((resolve) => { |
| 51 | + new pc.AssetListLoader(Object.values(assets), app.assets).load(resolve); |
| 52 | +}); |
| 53 | + |
| 54 | +app.start(); |
| 55 | + |
| 56 | +app.scene.skyboxMip = 1; |
| 57 | +app.scene.skyboxIntensity = 0.4; |
| 58 | +app.scene.envAtlas = assets.helipad.resource; |
| 59 | + |
| 60 | +const light = new pc.Entity(); |
| 61 | +light.addComponent('light', { |
| 62 | + type: 'directional', |
| 63 | + color: new pc.Color(1, 0.8, 0.25), |
| 64 | + intensity: 2 |
| 65 | +}); |
| 66 | +light.setLocalEulerAngles(45, 30, 0); |
| 67 | +app.root.addChild(light); |
| 68 | + |
| 69 | +const entity = assets.model.resource.instantiateRenderEntity(); |
| 70 | +app.root.addChild(entity); |
| 71 | + |
| 72 | +const camera = new pc.Entity(); |
| 73 | +camera.addComponent('camera', { |
| 74 | + toneMapping: pc.TONEMAP_ACES |
| 75 | +}); |
| 76 | +camera.addComponent('script'); |
| 77 | +camera.setPosition(0, 2, 6); |
| 78 | +app.root.addChild(camera); |
| 79 | + |
| 80 | +const cc = /** @type {CameraControls} */ (camera.script.create(CameraControls)); |
| 81 | +cc.focusPoint = new pc.Vec3(0, 0, 0); |
| 82 | + |
| 83 | +const lightMaterial = new pc.StandardMaterial(); |
| 84 | +lightMaterial.emissive = pc.Color.WHITE; |
| 85 | +lightMaterial.diffuse = pc.Color.BLACK; |
| 86 | +lightMaterial.useLighting = false; |
| 87 | +lightMaterial.update(); |
| 88 | + |
| 89 | +const omniLight = new pc.Entity(); |
| 90 | +omniLight.addComponent('light', { |
| 91 | + type: 'omni', |
| 92 | + color: new pc.Color(1, 1, 1), |
| 93 | + intensity: 4, |
| 94 | + range: 10, |
| 95 | + castShadows: false |
| 96 | +}); |
| 97 | +omniLight.addComponent('render', { |
| 98 | + type: 'sphere', |
| 99 | + material: lightMaterial, |
| 100 | + castShadows: false, |
| 101 | + receiveShadows: false |
| 102 | +}); |
| 103 | +omniLight.setLocalScale(0.1, 0.1, 0.1); |
| 104 | +app.root.addChild(omniLight); |
| 105 | + |
| 106 | +const orbitRadius = 2; |
| 107 | +let time = 0; |
| 108 | +app.on('update', (dt) => { |
| 109 | + time += dt * 0.5; |
| 110 | + omniLight.setPosition( |
| 111 | + Math.cos(time) * orbitRadius, |
| 112 | + Math.sin(time) * orbitRadius, |
| 113 | + 0 |
| 114 | + ); |
| 115 | +}); |
| 116 | + |
| 117 | +export { app }; |
0 commit comments