|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>PlayCanvas Shader Node Material</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 | + <style> |
| 11 | + body { |
| 12 | + margin: 0; |
| 13 | + overflow-y: hidden; |
| 14 | + } |
| 15 | + </style> |
| 16 | +</head> |
| 17 | + |
| 18 | +<body> |
| 19 | + <!-- The canvas element --> |
| 20 | + <canvas id="application-canvas"></canvas> |
| 21 | + <!-- shaders --> |
| 22 | + <script id="wobbleVS" type="x-shader/x-vertex"> |
| 23 | + vec3 wobbleVS(in vec3 wp, in vec3 wn, in float t) |
| 24 | + { |
| 25 | + return vec3(sin(t*5.0+dot(wp.xy,vec2(1,1)))*0.3)*normalize(wn); |
| 26 | + } |
| 27 | + </script> |
| 28 | + |
| 29 | + <!-- The script --> |
| 30 | + <script> |
| 31 | + var entity, light, camera; |
| 32 | + var time = 0; |
| 33 | + |
| 34 | + var canvas = document.getElementById("application-canvas"); |
| 35 | + |
| 36 | + // Create the application and start the update loop |
| 37 | + var app = new pc.Application(canvas); |
| 38 | + |
| 39 | + // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size |
| 40 | + app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW); |
| 41 | + app.setCanvasResolution(pc.RESOLUTION_AUTO); |
| 42 | + |
| 43 | + window.addEventListener("resize", function () { |
| 44 | + app.resizeCanvas(canvas.width, canvas.height); |
| 45 | + }); |
| 46 | + |
| 47 | + var miniStats = new pcx.MiniStats(app); |
| 48 | + |
| 49 | + app.scene.ambientLight = new pc.Color(0.2, 0.2, 0.2); |
| 50 | + |
| 51 | + // Create an Entity with a camera component |
| 52 | + camera = new pc.Entity(); |
| 53 | + camera.addComponent("camera", { |
| 54 | + clearColor: new pc.Color(0.4, 0.45, 0.5) |
| 55 | + }); |
| 56 | + camera.translate(0, 7, 25); |
| 57 | + app.root.addChild(camera); |
| 58 | + |
| 59 | + // Create an entity with a directional light component |
| 60 | + var light = new pc.Entity(); |
| 61 | + light.addComponent("light", { |
| 62 | + type: "directional", |
| 63 | + color: new pc.Color(1, 0.8, 0.25) |
| 64 | + }); |
| 65 | + app.root.addChild(light); |
| 66 | + light.setLocalEulerAngles(85, -100, 0); |
| 67 | + |
| 68 | + var url = "../assets/models/statue.glb"; |
| 69 | + app.assets.loadFromUrl(url, "container", function (err, asset) { |
| 70 | + entity = new pc.Entity(); |
| 71 | + |
| 72 | + var modelComponent = entity.addComponent("model", { |
| 73 | + type: "asset", |
| 74 | + asset: asset.resource.model |
| 75 | + }); |
| 76 | + app.root.addChild(entity); |
| 77 | + |
| 78 | + var model = modelComponent.model; |
| 79 | + var diffuseMap = model.getMaterials()[0].diffuseMap; |
| 80 | + |
| 81 | + //start building shader graph |
| 82 | + pc.shadergraph.start(); |
| 83 | + |
| 84 | + //create a simple PS shader graph - just a 2D texture sample |
| 85 | + var texSampleNode = pc.shadergraph.textureSample2D('diffSamp', diffuseMap, pc.shadergraph.uv0); |
| 86 | + |
| 87 | + //hook up PS outputs |
| 88 | + pc.shadergraph.connectFragOut(texSampleNode); |
| 89 | + |
| 90 | + //create a custom vertex offset shader graph node |
| 91 | + var wobbleVSNode = pc.shadergraph.customNode('wobbleVS', document.getElementById("wobbleVS").textContent); |
| 92 | + |
| 93 | + //hook up custom node inputs |
| 94 | + pc.shadergraph.connectCustom(wobbleVSNode, 'wp', pc.shadergraph.worldPosVS); |
| 95 | + pc.shadergraph.connectCustom(wobbleVSNode, 'wn', pc.shadergraph.worldNormVS); |
| 96 | + pc.shadergraph.connectCustom(wobbleVSNode, 't', pc.shadergraph.param('float', 'uTime', 0.0)); |
| 97 | + |
| 98 | + //hook up VS output |
| 99 | + pc.shadergraph.connectVertexOffset(wobbleVSNode); |
| 100 | + |
| 101 | + //end graph and assign built graph to material |
| 102 | + var material = pc.shadergraph.end(); |
| 103 | + |
| 104 | + //assign material to all model meshInstances |
| 105 | + model.meshInstances.forEach(function (meshInstance) { |
| 106 | + meshInstance.material = material; |
| 107 | + }); |
| 108 | + |
| 109 | + //initialize shader and update uniforms |
| 110 | + material.initShader(app.graphicsDevice); |
| 111 | + material.updateUniforms(); |
| 112 | + |
| 113 | + //update time parameter every frame |
| 114 | + app.on("update", function (dt) { |
| 115 | + time += dt; |
| 116 | + material.setParameter('IN_uTime_'+material.id, time); |
| 117 | + }); |
| 118 | + |
| 119 | + app.start(); |
| 120 | + }); |
| 121 | + </script> |
| 122 | +</body> |
| 123 | +</html> |
0 commit comments