|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <script src="../../build/output/playcanvas-latest.js"></script> |
| 5 | + <link href="../style.css" rel="stylesheet" /> |
| 6 | +</head> |
| 7 | + |
| 8 | +<body> |
| 9 | + <!-- The canvas element --> |
| 10 | + <canvas id="application-canvas"></canvas> |
| 11 | + |
| 12 | + <!-- The script --> |
| 13 | + <script> |
| 14 | + var canvas = document.getElementById("application-canvas"); |
| 15 | + |
| 16 | + // Create the app and start the update loop |
| 17 | + var app = new pc.Application(canvas); |
| 18 | + app.start(); |
| 19 | + |
| 20 | + // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size |
| 21 | + app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW); |
| 22 | + app.setCanvasResolution(pc.RESOLUTION_AUTO); |
| 23 | + |
| 24 | + app.scene.ambientLight = new pc.Color(0.2, 0.2, 0.2); |
| 25 | + |
| 26 | + var entity, light, camera; |
| 27 | + |
| 28 | + // Create a new layer to put in front of everything |
| 29 | + var layer = new pc.Layer({ |
| 30 | + name: "Front Layer" |
| 31 | + }); |
| 32 | + |
| 33 | + // get the world layer index |
| 34 | + var worldLayer = app.scene.layers.getLayerByName("World"); |
| 35 | + var idx = app.scene.layers.getTransparentIndex(worldLayer); |
| 36 | + |
| 37 | + // insert the new layer after the world layer |
| 38 | + app.scene.layers.insert(layer, idx+1); |
| 39 | + |
| 40 | + // Create an Entity with a camera component |
| 41 | + // Make sure it renders both World and Front Layer |
| 42 | + var camera = new pc.Entity(); |
| 43 | + camera.addComponent("camera", { |
| 44 | + clearColor: new pc.Color(0.4, 0.45, 0.5), |
| 45 | + layers: [worldLayer.id, layer.id] |
| 46 | + }); |
| 47 | + camera.translate(0, 0, 24); |
| 48 | + app.root.addChild(camera); |
| 49 | + |
| 50 | + // Create an Entity with a point light component |
| 51 | + // Make sure it lights both World and Front Layer |
| 52 | + var light = new pc.Entity(); |
| 53 | + light.addComponent("light", { |
| 54 | + type: "point", |
| 55 | + color: new pc.Color(1, 1, 1), |
| 56 | + range: 100, |
| 57 | + layers: [worldLayer.id, layer.id] |
| 58 | + }); |
| 59 | + light.translate(5, 0, 15); |
| 60 | + app.root.addChild(light); |
| 61 | + |
| 62 | + // red material is semi-transparent |
| 63 | + var red = new pc.StandardMaterial(); |
| 64 | + red.diffuse.set(1,0,0); |
| 65 | + red.blendType = pc.BLEND_NORMAL; |
| 66 | + red.opacity = 0.5; |
| 67 | + red.update(); |
| 68 | + |
| 69 | + // blue material does not test the existing depth buffer |
| 70 | + var blue = new pc.StandardMaterial(); |
| 71 | + blue.diffuse.set(0,0,1); |
| 72 | + blue.depthTest = false; |
| 73 | + blue.update(); |
| 74 | + |
| 75 | + // red box is rendered first in World layer |
| 76 | + var redBox = new pc.Entity(); |
| 77 | + redBox.addComponent('model', { |
| 78 | + type: 'box' |
| 79 | + }); |
| 80 | + redBox.model.material = red; |
| 81 | + redBox.setLocalScale(5,5,5); |
| 82 | + app.root.addChild(redBox); |
| 83 | + |
| 84 | + // blue box is rendered in the Front Layer which is after World |
| 85 | + // because it does not test for depth |
| 86 | + // and is in a later layer |
| 87 | + // it is visible even though it should be inside the red box |
| 88 | + var blueBox = new pc.Entity(); |
| 89 | + blueBox.addComponent('model', { |
| 90 | + type: 'box', |
| 91 | + layers: [layer.id] // try removing this line, the blue box will appear inside the red one |
| 92 | + }); |
| 93 | + blueBox.model.material = blue; |
| 94 | + blueBox.setLocalScale(2.5,2.5,2.5); |
| 95 | + app.root.addChild(blueBox); |
| 96 | + |
| 97 | + app.on("update", function (dt) { |
| 98 | + if (redBox) { |
| 99 | + redBox.rotate(0,10*dt,0); |
| 100 | + } |
| 101 | + if (blueBox) { |
| 102 | + blueBox.rotate(0,-10*dt,0); |
| 103 | + } |
| 104 | + |
| 105 | + blueBox.model.meshInstances[0].layer = 10; |
| 106 | + }); |
| 107 | + </script> |
| 108 | +</body> |
| 109 | +</html> |
0 commit comments