|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>PlayCanvas Clear Coat 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/output/playcanvas.js"></script> |
| 9 | + <style> |
| 10 | + body { |
| 11 | + margin: 0; |
| 12 | + overflow-y: hidden; |
| 13 | + } |
| 14 | + </style> |
| 15 | +</head> |
| 16 | + |
| 17 | +<body> |
| 18 | + <!-- The canvas element --> |
| 19 | + <canvas id="application-canvas"></canvas> |
| 20 | + |
| 21 | + <!-- The script --> |
| 22 | + <script> |
| 23 | + var canvas = document.getElementById("application-canvas"); |
| 24 | + |
| 25 | + // Create the application and start the update loop |
| 26 | + var app = new pc.Application(canvas); |
| 27 | + |
| 28 | + // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size |
| 29 | + app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW); |
| 30 | + app.setCanvasResolution(pc.RESOLUTION_AUTO); |
| 31 | + |
| 32 | + window.addEventListener("resize", function () { |
| 33 | + app.resizeCanvas(canvas.width, canvas.height); |
| 34 | + }); |
| 35 | + |
| 36 | + app.scene.gammaCorrection = pc.GAMMA_SRGB; |
| 37 | + app.scene.toneMapping = pc.TONEMAP_ACES; |
| 38 | + // Set the skybox to the 128x128 cubemap mipmap level |
| 39 | + app.scene.skyboxMip = 1; |
| 40 | + |
| 41 | + // Create an entity with a camera component |
| 42 | + var camera = new pc.Entity(); |
| 43 | + camera.addComponent("camera"); |
| 44 | + camera.translate(0, 0, 3); |
| 45 | + app.root.addChild(camera); |
| 46 | + |
| 47 | + // Create an entity with a directional light component |
| 48 | + var light = new pc.Entity(); |
| 49 | + light.addComponent("light", { |
| 50 | + type: "directional", |
| 51 | + color: new pc.Color(1, 0.8, 0.25) |
| 52 | + }); |
| 53 | + app.root.addChild(light); |
| 54 | + light.setLocalEulerAngles(85, -100, 0); |
| 55 | + |
| 56 | + // Load a cubemap asset. This DDS file was 'prefiltered' in the |
| 57 | + // PlayCanvas Editor and then downloaded. |
| 58 | + var cubemapAsset = new pc.Asset('helipad.dds', 'cubemap', { |
| 59 | + url: "../assets/cubemaps/helipad.dds" |
| 60 | + }); |
| 61 | + app.assets.add(cubemapAsset); |
| 62 | + app.assets.load(cubemapAsset); |
| 63 | + cubemapAsset.ready(function () { |
| 64 | + // Mark the cubemaps as RGBM format since it's an HDR cubemap |
| 65 | + cubemapAsset.resources.forEach(function (cubemap) { |
| 66 | + cubemap.rgbm = true; |
| 67 | + }); |
| 68 | + app.scene.setSkybox(cubemapAsset.resources); |
| 69 | + }); |
| 70 | + |
| 71 | + // function to create sphere |
| 72 | + var createSphere = function (x, y, z, material) { |
| 73 | + var sphere = new pc.Entity(); |
| 74 | + |
| 75 | + sphere.addComponent("model", { |
| 76 | + material: material, |
| 77 | + type: "sphere" |
| 78 | + }); |
| 79 | + sphere.setLocalPosition(x, y, z); |
| 80 | + sphere.setLocalScale(0.7, 0.7, 0.7); |
| 81 | + app.root.addChild(sphere); |
| 82 | + } |
| 83 | + |
| 84 | + // async load a textures, create materials and spheres, then start app |
| 85 | + app.assets.loadFromUrl("../assets/textures/flakes5n.png", "texture", function (err, asset) { |
| 86 | + var normalMap = asset.resource; |
| 87 | + app.assets.loadFromUrl("../assets/textures/flakes5c.png", "texture", function (err, asset) { |
| 88 | + var diffuseMap = asset.resource; |
| 89 | + app.assets.loadFromUrl("../assets/textures/flakes5o.png", "texture", function (err, asset) { |
| 90 | + var otherMap = asset.resource; |
| 91 | + |
| 92 | + var material = new pc.StandardMaterial(); |
| 93 | + material.diffuseMap = diffuseMap; |
| 94 | + material.metalnessMap = otherMap; |
| 95 | + material.metalnessMapChannel ='r'; |
| 96 | + material.glossMap = otherMap; |
| 97 | + material.glossMapChannel ='g'; |
| 98 | + material.normalMap = normalMap; |
| 99 | + material.diffuse = new pc.Color(0.6, 0.6, 0.9); |
| 100 | + material.diffuseTint = true; |
| 101 | + material.metalness = 1.0; |
| 102 | + material.shininess = 90.0; |
| 103 | + material.bumpiness = 0.7; |
| 104 | + material.useMetalness = true; |
| 105 | + material.update(); |
| 106 | + |
| 107 | + createSphere(-0.5, 0, 0, material); |
| 108 | + |
| 109 | + var clearCoatMaterial = new pc.StandardMaterial(); |
| 110 | + clearCoatMaterial.diffuseMap = diffuseMap; |
| 111 | + clearCoatMaterial.metalnessMap = otherMap; |
| 112 | + clearCoatMaterial.metalnessMapChannel ='r'; |
| 113 | + clearCoatMaterial.glossMap = otherMap; |
| 114 | + clearCoatMaterial.glossMapChannel ='g'; |
| 115 | + clearCoatMaterial.normalMap = normalMap; |
| 116 | + clearCoatMaterial.diffuse = new pc.Color(0.6, 0.6, 0.9); |
| 117 | + clearCoatMaterial.diffuseTint = true; |
| 118 | + clearCoatMaterial.metalness = 1.0; |
| 119 | + clearCoatMaterial.shininess = 90; |
| 120 | + clearCoatMaterial.bumpiness = 0.7; |
| 121 | + clearCoatMaterial.useMetalness = true; |
| 122 | + clearCoatMaterial.clearCoat = 0.25; |
| 123 | + clearCoatMaterial.clearCoatGlossiness = 0.9; |
| 124 | + clearCoatMaterial.update(); |
| 125 | + |
| 126 | + createSphere(0.5, 0, 0, clearCoatMaterial); |
| 127 | + |
| 128 | + app.start(); |
| 129 | + }); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + // update things each frame |
| 134 | + var time = 0; |
| 135 | + app.on("update", function (dt) { |
| 136 | + // rotate camera around the objects |
| 137 | + time += dt; |
| 138 | + camera.setLocalPosition(3 * Math.sin(time*0.5), 0, 3 * Math.cos(time*0.5)); |
| 139 | + camera.lookAt(pc.Vec3.ZERO); |
| 140 | + }); |
| 141 | + |
| 142 | + </script> |
| 143 | +</body> |
| 144 | +</html> |
0 commit comments