|
| 1 | +// @config DESCRIPTION This example demonstrates the clear coat material. Visually, the Coated column should contain highlights from both the Base and Boating layers. |
| 2 | +import { deviceType, rootPath } from 'examples/utils'; |
| 3 | +import * as pc from 'playcanvas'; |
| 4 | + |
| 5 | +const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas')); |
| 6 | +window.focus(); |
| 7 | + |
| 8 | +const assets = { |
| 9 | + orbitCamera: new pc.Asset('script', 'script', { url: `${rootPath}/static/scripts/camera/orbit-camera.js` }), |
| 10 | + helipad: new pc.Asset( |
| 11 | + 'helipad-env-atlas', |
| 12 | + 'texture', |
| 13 | + { url: `${rootPath}/static/assets/cubemaps/morning-env-atlas.png` }, |
| 14 | + { type: pc.TEXTURETYPE_RGBP, mipmaps: false } |
| 15 | + ), |
| 16 | + model: new pc.Asset('model', 'container', { url: `${rootPath}/static/assets/models/PrimitiveModeNormalsTest.glb` }) |
| 17 | +}; |
| 18 | + |
| 19 | +const gfxOptions = { |
| 20 | + deviceTypes: [deviceType], |
| 21 | + glslangUrl: `${rootPath}/static/lib/glslang/glslang.js`, |
| 22 | + twgslUrl: `${rootPath}/static/lib/twgsl/twgsl.js` |
| 23 | +}; |
| 24 | + |
| 25 | +const device = await pc.createGraphicsDevice(canvas, gfxOptions); |
| 26 | +device.maxPixelRatio = Math.min(window.devicePixelRatio, 2); |
| 27 | + |
| 28 | +const createOptions = new pc.AppOptions(); |
| 29 | +createOptions.graphicsDevice = device; |
| 30 | +createOptions.mouse = new pc.Mouse(document.body); |
| 31 | +createOptions.touch = new pc.TouchDevice(document.body); |
| 32 | +createOptions.keyboard = new pc.Keyboard(document.body); |
| 33 | + |
| 34 | +createOptions.componentSystems = [ |
| 35 | + pc.RenderComponentSystem, |
| 36 | + pc.CameraComponentSystem, |
| 37 | + pc.LightComponentSystem, |
| 38 | + pc.ScriptComponentSystem |
| 39 | +]; |
| 40 | +createOptions.resourceHandlers = [pc.TextureHandler, pc.ContainerHandler, pc.ScriptHandler]; |
| 41 | + |
| 42 | +const app = new pc.AppBase(canvas); |
| 43 | +app.init(createOptions); |
| 44 | + |
| 45 | +// Set the canvas to fill the window and automatically change resolution to be the same as the canvas size |
| 46 | +app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW); |
| 47 | +app.setCanvasResolution(pc.RESOLUTION_AUTO); |
| 48 | + |
| 49 | +// Ensure canvas is resized when window changes size |
| 50 | +const resize = () => app.resizeCanvas(); |
| 51 | +window.addEventListener('resize', resize); |
| 52 | +app.on('destroy', () => { |
| 53 | + window.removeEventListener('resize', resize); |
| 54 | +}); |
| 55 | + |
| 56 | +const assetListLoader = new pc.AssetListLoader(Object.values(assets), app.assets); |
| 57 | +assetListLoader.load(() => { |
| 58 | + app.start(); |
| 59 | + |
| 60 | + // Setup skydome |
| 61 | + app.scene.envAtlas = assets.helipad.resource; |
| 62 | + app.scene.skyboxIntensity = 1; |
| 63 | + |
| 64 | + const testEntity = assets.model.resource.instantiateRenderEntity(); |
| 65 | + testEntity.setLocalEulerAngles(0, 90, 0); |
| 66 | + app.root.addChild(testEntity); |
| 67 | + |
| 68 | + // Create a camera with an orbit camera script |
| 69 | + const camera = new pc.Entity(); |
| 70 | + camera.addComponent('camera', { |
| 71 | + toneMapping: pc.TONEMAP_ACES |
| 72 | + }); |
| 73 | + camera.addComponent('script'); |
| 74 | + camera.script.create('orbitCamera', { |
| 75 | + attributes: { |
| 76 | + inertiaFactor: 0.2 |
| 77 | + } |
| 78 | + }); |
| 79 | + camera.script.create('orbitCameraInputMouse'); |
| 80 | + camera.script.create('orbitCameraInputTouch'); |
| 81 | + app.root.addChild(camera); |
| 82 | + camera.script.orbitCamera.yaw = 90; |
| 83 | + camera.script.orbitCamera.distance = 25; |
| 84 | + |
| 85 | + const directionalLight = new pc.Entity(); |
| 86 | + directionalLight.addComponent('light', { |
| 87 | + type: 'directional', |
| 88 | + color: pc.Color.YELLOW, |
| 89 | + castShadows: false, |
| 90 | + intensity: 1 |
| 91 | + }); |
| 92 | + directionalLight.setEulerAngles(45, 180, 0); |
| 93 | + app.root.addChild(directionalLight); |
| 94 | +}); |
| 95 | + |
| 96 | +export { app }; |
0 commit comments