Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Jogos em Javascript com WebGL | Games in Javasc...

Jogos em Javascript com WebGL | Games in Javascript with WebGL

- JSExperience 2018 (05/07/2018)

Juliana Negreiros

July 05, 2018
Tweet

More Decks by Juliana Negreiros

Other Decks in Technology

Transcript

  1. <canvas id="c"></canvas> <script id="2d-vertex-shader" type="notjs"> attribute vec4 a_position; void main()

    { gl_Position = a_position; } </script> <script id="2d-fragment-shader" type="notjs"> precision mediump float; void main() { gl_FragColor = vec4(1, 0, 0.5, 1); // return redish-purple } </script> <script src="https://webglfundamentals.org/webgl/resources/webgl-utils.js"></script> <script src="https://webglfundamentals.org/webgl/resources/webgl-lessons-helper.js"></script>
  2. function createShader(gl, type, source) { var shader = gl.createShader(type); gl.shaderSource(shader,

    source); gl.compileShader(shader); var success = gl.getShaderParameter(shader, gl.COMPILE_STATUS); if (success) { return shader; } console.log(gl.getShaderInfoLog(shader)); gl.deleteShader(shader); } function createProgram(gl, vertexShader, fragmentShader) { var program = gl.createProgram(); gl.attachShader(program, vertexShader); gl.attachShader(program, fragmentShader); gl.linkProgram(program); var success = gl.getProgramParameter(program, gl.LINK_STATUS); if (success) { return program; } console.log(gl.getProgramInfoLog(program)); gl.deleteProgram(program); } function main() { var canvas = document.getElementById("c"); var gl = canvas.getContext("webgl"); if (!gl) { return; } var vertexShaderSource = document.getElementById("2d-vertex-shader").text; var fragmentShaderSource = document.getElementById("2d-fragment-shader").text; var vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource); var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource); var program = createProgram(gl, vertexShader, fragmentShader); var positionAttributeLocation = gl.getAttribLocation(program, "a_position"); var positionBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); var positions = [ 0, 0, 0, 0.5, 0.7, 0, ]; gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW); webglUtils.resizeCanvasToDisplaySize(gl.canvas); gl.viewport(0, 0, gl.canvas.width, gl.canvas.height); gl.clearColor(0, 0, 0, 0); gl.clear(gl.COLOR_BUFFER_BIT); gl.useProgram(program); gl.enableVertexAttribArray(positionAttributeLocation); gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); var size = 2; var type = gl.FLOAT; var normalize = false; var stride = 0; var offset = 0; gl.vertexAttribPointer( positionAttributeLocation, size, type, normalize, stride, offset) var primitiveType = gl.TRIANGLES; var offset = 0; var count = 3; gl.drawArrays(primitiveType, offset, count); } main();
  3. Luzes const pointLight = new THREE.PointLight (0xFFFFFF); pointLight.position.x = 10;

    pointLight.position.y = 50; pointLight.position.z = 130; scene.add(pointLight);
  4. const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(

    60, sceneWidth / sceneHeight, 0.1, 1000 ); const renderer = new THREE.WebGLRenderer(); Cena, Camera e Renderizador
  5. const sphereGeometry = new THREE.DodecahedronGeometry(heroRadius, 1); const sphereMaterial = new

    THREE.MeshStandardMaterial({ color: 0xe5f2f2, shading: THREE.FlatShading, }); addHero();