<commit_before><!DOCTYPE html>
<html>
<head>
    <title>PlayCanvas Hardware Instancing</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/output/playcanvas.js"></script>
    <style>
        body { 
            margin: 0;
            overflow-y: hidden;
        }
    </style>
</head>

<body>
    <!-- The canvas element -->
    <canvas id="application-canvas"></canvas>

    <!-- The script -->
    <script>
        var canvas = document.getElementById("application-canvas");

        // Create the application and start the update loop
        var app = new pc.Application(canvas);
        app.start();

        // 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);
        });

        app.scene.ambientLight = new pc.Color(0.2, 0.2, 0.2);

        // Create an Entity with a point light component
        var light = new pc.Entity();
        light.addComponent("light", {
            type: "point",
            color: new pc.Color(1, 0, 1),
            radius: 10
        });

        // Create an Entity with a camera component
        var camera = new pc.Entity();
        camera.addComponent("camera", {
            clearColor: new pc.Color(0.4, 0.45, 0.5)
        });

        // Add the new Entities to the hierarchy
        app.root.addChild(light);
        app.root.addChild(camera);

        // Move the camera back to see the cubes
        camera.translate(0, 0, 10);

        // create standard material and enable instancing on it        
        var material = new pc.StandardMaterial();
        material.onUpdateShader = function(options) {
            options.useInstancing = true;
            return options;
        };
        material.update();

        // Create a Entity with a Box model component and the instancing material
        var box = new pc.Entity();
        box.addComponent("model", {
            material: material,
            type: "box"
        });

        // add the box entity to the hierarchy
        app.root.addChild(box);

        // number of instances to render
        var instanceCount = 15;

        // store matrices for individual instances into array
        var matrices = new Float32Array(instanceCount * 16);
        var matrixIndex = 0;
    
        var pos = new pc.Vec3();
        var rot = new pc.Quat();
        var scl = new pc.Vec3(0.1, 0.1, 0.1);
        var matrix = new pc.Mat4();

        for (var i = 0; i < instanceCount; i++) {
            pos.x = i * 0.2 - instanceCount * 0.5 * 0.2;
            rot.setFromEulerAngles(i * 30, i * 50, i * 70);
            matrix.setTRS(pos, rot, scl);

            // copy matrix elements into array of floats
            for (var m = 0; m < 16; m++)
                matrices[matrixIndex++] = matrix.data[m];
        }

        // create static vertex buffer containing the matrices
        var vertexBuffer = new pc.VertexBuffer(this.app.graphicsDevice, pc.VertexFormat.defaultInstancingFormat, instanceCount, pc.BUFFER_STATIC, matrices);

        // initialise instancing using the vertex buffer on meshInstance of the created box
        var boxMeshInst = box.model.meshInstances[0];    
        boxMeshInst.setInstancing(vertexBuffer);
    </script>
</body>
</html><commit_msg>Improved hardware instancing demo (#1885)<commit_after><!DOCTYPE html>
<html>
<head>
    <title>PlayCanvas Hardware Instancing</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/output/playcanvas.js"></script>
    <style>
        body { 
            margin: 0;
            overflow-y: hidden;
        }
    </style>
</head>

<body>
    <!-- The canvas element -->
    <canvas id="application-canvas"></canvas>

    <!-- The script -->
    <script>
        var canvas = document.getElementById("application-canvas");

        // Create the application and start the update loop
        var app = new pc.Application(canvas);
        app.start();

        // 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);
        });

        app.scene.ambientLight = new pc.Color(0.1, 0.1, 0.1);

        // Create an Entity with a point light component, which is casting shadows (using rendering to cubemap)
        var light = new pc.Entity();
        light.addComponent("light", {
            type: "point",
            color: new pc.Color(2, 3, 1),
            radius: 10,
            castShadows : true
        });

        // add sphere at the position of light
        light.addComponent("model", {
            type: "sphere"
        });
        
        // Scale the sphere down to 0.1m
        light.setLocalScale(0.1, 0.1, 0.1);

        // Create an Entity with a camera component
        var camera = new pc.Entity();
        camera.addComponent("camera", {
            clearColor: new pc.Color(0.4, 0.45, 0.5)
        });

        // Add the new Entities to the hierarchy
        app.root.addChild(light);
        app.root.addChild(camera);

        // Move the camera back to see the cubes
        camera.translate(0, 0, 10);

        // create standard material and enable instancing on it        
        var material = new pc.StandardMaterial();
        material.onUpdateShader = function(options) {
            options.useInstancing = true;
            return options;
        };
        material.update();

        // Create a Entity with a Box model component and the instancing material
        var box = new pc.Entity();
        box.addComponent("model", {
            material: material,
            type: "box"
        });

        // add the box entity to the hierarchy
        app.root.addChild(box);

        // number of instances to render
        var instanceCount = 1000;

        // store matrices for individual instances into array
        var matrices = new Float32Array(instanceCount * 16);
        var matrixIndex = 0;
    
        var radius = 5;
        var pos = new pc.Vec3();
        var rot = new pc.Quat();
        var scl = new pc.Vec3();
        var matrix = new pc.Mat4();

        for (var i = 0; i < instanceCount; i++) {
            // generate random positions / scales and rotations
            pos.set(Math.random() * radius - radius * 0.5, Math.random() * radius - radius * 0.5, Math.random() * radius - radius * 0.5);
            scl.set(0.1 + Math.random() * 0.1, 0.1 + Math.random() * 0.3, 0.1 + Math.random() * 0.1);
            rot.setFromEulerAngles(i * 30, i * 50, i * 70);
            matrix.setTRS(pos, rot, scl);

            // copy matrix elements into array of floats
            for (var m = 0; m < 16; m++)
                matrices[matrixIndex++] = matrix.data[m];
        }

        // create static vertex buffer containing the matrices
        var vertexBuffer = new pc.VertexBuffer(this.app.graphicsDevice, pc.VertexFormat.defaultInstancingFormat, instanceCount, pc.BUFFER_STATIC, matrices);

        // initialise instancing using the vertex buffer on meshInstance of the created box
        var boxMeshInst = box.model.meshInstances[0];    
        boxMeshInst.setInstancing(vertexBuffer);

        // Set an update function on the app's update event
        var angle = 0;
        app.on("update", function (dt) {
            angle += dt * 0.2;

            // Move the light in a circle
            light.setLocalPosition(7 * Math.sin(angle), 7 * Math.cos(angle), 0);

            // orbit camera around
            camera.setLocalPosition(10 * Math.sin(angle), 0, 10 * Math.cos(angle));
            camera.lookAt(pc.Vec3.ZERO);
        });
    </script>
</body>
</html>
{'fraction_non_alphanumeric': 0.11955093753732235, 'fraction_numerical': 0.018511883434850114, 'mean_word_length': 1.9896465548018565, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5904007', 'n_tokens_mistral': 2447, 'n_tokens_neox': 2297, 'n_words': 700}