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