|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>PlayCanvas Tweening</title> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> |
| 7 | + <link rel="icon" type="image/png" href="../playcanvas-favicon.png" /> |
| 8 | + <script src="../../build/playcanvas.js"></script> |
| 9 | + <script src="../../build/playcanvas-extras.js"></script> |
| 10 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/18.6.4/tween.umd.js"></script> |
| 11 | + <style> |
| 12 | + body { |
| 13 | + margin: 0; |
| 14 | + overflow-y: hidden; |
| 15 | + } |
| 16 | + </style> |
| 17 | +</head> |
| 18 | + |
| 19 | +<body> |
| 20 | + <!-- The canvas element --> |
| 21 | + <canvas id="application-canvas"></canvas> |
| 22 | + |
| 23 | + <!-- The script --> |
| 24 | + <script> |
| 25 | + var canvas = document.getElementById("application-canvas"); |
| 26 | + |
| 27 | + // Create the application and start the update loop |
| 28 | + var app = new pc.Application(canvas); |
| 29 | + app.start(); |
| 30 | + |
| 31 | + // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size |
| 32 | + app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW); |
| 33 | + app.setCanvasResolution(pc.RESOLUTION_AUTO); |
| 34 | + |
| 35 | + window.addEventListener("resize", function () { |
| 36 | + app.resizeCanvas(canvas.width, canvas.height); |
| 37 | + }); |
| 38 | + |
| 39 | + var miniStats = new pcx.MiniStats(app); |
| 40 | + |
| 41 | + // Utility function to create a text element-based entity |
| 42 | + var createText = function (fontAsset, message, x, y, z, rot) { |
| 43 | + var text = new pc.Entity(); |
| 44 | + text.addComponent("element", { |
| 45 | + anchor: [0.5, 0.5, 0.5, 0.5], |
| 46 | + fontAsset: fontAsset, |
| 47 | + fontSize: 0.5, |
| 48 | + pivot: [1, 0.5], |
| 49 | + text: message, |
| 50 | + type: pc.ELEMENTTYPE_TEXT |
| 51 | + }); |
| 52 | + text.setLocalPosition(x, y, z); |
| 53 | + text.setLocalEulerAngles(0, 0, rot); |
| 54 | + app.root.addChild(text); |
| 55 | + }; |
| 56 | + |
| 57 | + // A list of assets that need to be loaded |
| 58 | + var assetManifest = [ |
| 59 | + { |
| 60 | + type: "font", |
| 61 | + url: "../assets/fonts/arial.json" |
| 62 | + }, |
| 63 | + { |
| 64 | + type: "script", |
| 65 | + url: "../assets/scripts/animation/tween.js" |
| 66 | + } |
| 67 | + ]; |
| 68 | + |
| 69 | + // Load all assets and then run the example |
| 70 | + var assetsToLoad = assetManifest.length; |
| 71 | + assetManifest.forEach(function (entry) { |
| 72 | + app.assets.loadFromUrl(entry.url, entry.type, function (err, asset) { |
| 73 | + if (!err && asset) { |
| 74 | + assetsToLoad--; |
| 75 | + entry.asset = asset; |
| 76 | + if (assetsToLoad === 0) { |
| 77 | + run(); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + function run() { |
| 84 | + var easingFunctions = [ |
| 85 | + 'Linear', |
| 86 | + 'Quadratic', |
| 87 | + 'Cubic', |
| 88 | + 'Quartic', |
| 89 | + 'Quintic', |
| 90 | + 'Sinusoidal', |
| 91 | + 'Exponential', |
| 92 | + 'Circular', |
| 93 | + 'Elastic', |
| 94 | + 'Back', |
| 95 | + 'Bounce' |
| 96 | + ]; |
| 97 | + var points = []; |
| 98 | + var colors = []; |
| 99 | + |
| 100 | + for (var i = 0; i < easingFunctions.length; i++) { |
| 101 | + // Create an entity with a sphere model component |
| 102 | + var sphere = new pc.Entity(); |
| 103 | + |
| 104 | + sphere.addComponent("model", { |
| 105 | + type: "sphere" |
| 106 | + }); |
| 107 | + sphere.model.material.diffuse.set(1, 0, 0); |
| 108 | + sphere.model.material.specular.set(0.6, 0.6, 0.6); |
| 109 | + sphere.model.material.shininess = 20; |
| 110 | + |
| 111 | + sphere.addComponent("script"); |
| 112 | + sphere.script.create("tween", { |
| 113 | + attributes: { |
| 114 | + tweens: [{ |
| 115 | + autoPlay: true, // Start this tween immediately |
| 116 | + delay: 0, // No delay on start |
| 117 | + duration: 2000, // 2 seconds |
| 118 | + easingFunction: i, |
| 119 | + easingType: 2, // InOut type |
| 120 | + end: new pc.Vec4(10, -i, 0, 0), |
| 121 | + path: 'localPosition', // Update the entity's local position |
| 122 | + repeat: -1, // Repeat infinitely |
| 123 | + repeatDelay: 0, // No delay between repeats |
| 124 | + start: new pc.Vec4(0, -i, 0, 0), |
| 125 | + yoyo: true // Ping pong between start and end values |
| 126 | + }] |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + sphere.setLocalScale(0.8, 0.8, 0.8); |
| 131 | + app.root.addChild(sphere); |
| 132 | + |
| 133 | + // Add a line for the path of the sphere |
| 134 | + points.push(new pc.Vec3(0, -i, 0), new pc.Vec3(10, -i, 0)); |
| 135 | + colors.push(pc.Color.WHITE, pc.Color.WHITE); |
| 136 | + |
| 137 | + // Create a text label for the sphere |
| 138 | + createText(assetManifest[0].asset, easingFunctions[i], -0.5, -i, 0, 0); |
| 139 | + } |
| 140 | + |
| 141 | + // Create an entity with a directional light component |
| 142 | + var light = new pc.Entity(); |
| 143 | + light.addComponent("light", { |
| 144 | + type: "directional" |
| 145 | + }); |
| 146 | + light.setLocalEulerAngles(70, 30, 0); |
| 147 | + app.root.addChild(light); |
| 148 | + |
| 149 | + // Create an entity with a camera component |
| 150 | + var camera = new pc.Entity(); |
| 151 | + camera.addComponent("camera", { |
| 152 | + clearColor: new pc.Color(0.4, 0.45, 0.5) |
| 153 | + }); |
| 154 | + camera.translate(5, -5.5, 20); |
| 155 | + app.root.addChild(camera); |
| 156 | + |
| 157 | + app.on('update', function (dt) { |
| 158 | + app.renderLines(points, colors); |
| 159 | + }); |
| 160 | + } |
| 161 | + </script> |
| 162 | +</body> |
| 163 | +</html> |
0 commit comments