|
87 | 87 | // add the box entity to the hierarchy
|
88 | 88 | app.root.addChild(box);
|
89 | 89 |
|
90 |
| - // number of instances to render |
91 |
| - var instanceCount = 1000; |
92 |
| - |
93 |
| - // store matrices for individual instances into array |
94 |
| - var matrices = new Float32Array(instanceCount * 16); |
95 |
| - var matrixIndex = 0; |
96 |
| - |
97 |
| - var radius = 5; |
98 |
| - var pos = new pc.Vec3(); |
99 |
| - var rot = new pc.Quat(); |
100 |
| - var scl = new pc.Vec3(); |
101 |
| - var matrix = new pc.Mat4(); |
102 |
| - |
103 |
| - for (var i = 0; i < instanceCount; i++) { |
104 |
| - // generate random positions / scales and rotations |
105 |
| - pos.set(Math.random() * radius - radius * 0.5, Math.random() * radius - radius * 0.5, Math.random() * radius - radius * 0.5); |
106 |
| - scl.set(0.1 + Math.random() * 0.1, 0.1 + Math.random() * 0.3, 0.1 + Math.random() * 0.1); |
107 |
| - rot.setFromEulerAngles(i * 30, i * 50, i * 70); |
108 |
| - matrix.setTRS(pos, rot, scl); |
109 |
| - |
110 |
| - // copy matrix elements into array of floats |
111 |
| - for (var m = 0; m < 16; m++) |
112 |
| - matrices[matrixIndex++] = matrix.data[m]; |
| 90 | + if (this.app.graphicsDevice.supportsInstancing) { |
| 91 | + // number of instances to render |
| 92 | + var instanceCount = 1000; |
| 93 | + |
| 94 | + // store matrices for individual instances into array |
| 95 | + var matrices = new Float32Array(instanceCount * 16); |
| 96 | + var matrixIndex = 0; |
| 97 | + |
| 98 | + var radius = 5; |
| 99 | + var pos = new pc.Vec3(); |
| 100 | + var rot = new pc.Quat(); |
| 101 | + var scl = new pc.Vec3(); |
| 102 | + var matrix = new pc.Mat4(); |
| 103 | + |
| 104 | + for (var i = 0; i < instanceCount; i++) { |
| 105 | + // generate random positions / scales and rotations |
| 106 | + pos.set(Math.random() * radius - radius * 0.5, Math.random() * radius - radius * 0.5, Math.random() * radius - radius * 0.5); |
| 107 | + scl.set(0.1 + Math.random() * 0.1, 0.1 + Math.random() * 0.3, 0.1 + Math.random() * 0.1); |
| 108 | + rot.setFromEulerAngles(i * 30, i * 50, i * 70); |
| 109 | + matrix.setTRS(pos, rot, scl); |
| 110 | + |
| 111 | + // copy matrix elements into array of floats |
| 112 | + for (var m = 0; m < 16; m++) |
| 113 | + matrices[matrixIndex++] = matrix.data[m]; |
| 114 | + } |
| 115 | + |
| 116 | + // create static vertex buffer containing the matrices |
| 117 | + var vertexBuffer = new pc.VertexBuffer(this.app.graphicsDevice, pc.VertexFormat.defaultInstancingFormat, instanceCount, pc.BUFFER_STATIC, matrices); |
| 118 | + |
| 119 | + // initialise instancing using the vertex buffer on meshInstance of the created box |
| 120 | + var boxMeshInst = box.model.meshInstances[0]; |
| 121 | + boxMeshInst.setInstancing(vertexBuffer); |
113 | 122 | }
|
114 | 123 |
|
115 |
| - // create static vertex buffer containing the matrices |
116 |
| - var vertexBuffer = new pc.VertexBuffer(this.app.graphicsDevice, pc.VertexFormat.defaultInstancingFormat, instanceCount, pc.BUFFER_STATIC, matrices); |
117 |
| - |
118 |
| - // initialise instancing using the vertex buffer on meshInstance of the created box |
119 |
| - var boxMeshInst = box.model.meshInstances[0]; |
120 |
| - boxMeshInst.setInstancing(vertexBuffer); |
121 |
| - |
122 | 124 | // Set an update function on the app's update event
|
123 | 125 | var angle = 0;
|
124 | 126 | app.on("update", function (dt) {
|
|
0 commit comments