<commit_before>import defined from "../../Core/defined.js";
import PrimitiveType from "../../Core/PrimitiveType.js";
import AttributeType from "../AttributeType.js";
import VertexAttributeSemantic from "../VertexAttributeSemantic.js";
import GeometryStageVS from "../../Shaders/ModelExperimental/GeometryStageVS.js";
import ShaderDestination from "../../Renderer/ShaderDestination.js";

/**
 * The geometry pipeline stage processes the vertex attributes of a primitive.
 *
 * @namespace GeometryPipelineStage
 *
 * @private
 */
var GeometryPipelineStage = {};

/**
 * This pipeline stage processes the vertex attributes of a mesh primitive, adding the attribute declarations to the shaders,
 * the attribute objects to the render resources and setting the flags as needed.
 *
 * Processes a mesh primitive. This stage modifies the following parts of the render resources:
 * <ul>
 *  <li> adds attribute and varying declarations for the vertex attributes in the vertex and fragment shaders
 *  <li> creates the objects required to create VertexArrays
 *  <li> sets the flag for point primitive types
 * </ul>
 *
 * @param {MeshPrimitiveRenderResources} renderResources The render resources for this mesh primitive.
 * @param {ModelComponents.Primitive} primitive The mesh primitive.
 *
 * @private
 */
GeometryPipelineStage.process = function (renderResources, primitive) {
  // The attribute index is taken from the node render resources, which may have added some attributes of its own.
  var attributeIndex = renderResources.attributeIndex;
  var index;
  var customAttributeInitializationLines = [];
  for (var i = 0; i < primitive.attributes.length; i++) {
    var attribute = primitive.attributes[i];
    if (attribute.semantic !== VertexAttributeSemantic.POSITION) {
      index = attributeIndex++;
    } else {
      index = 0;
    }
    processAttribute(
      renderResources,
      attribute,
      index,
      customAttributeInitializationLines
    );
  }

  var shaderBuilder = renderResources.shaderBuilder;

  // add a function to initialize varyings for custom attributes.
  // for example "v_custom_attribute = a_custom_attribute;""
  if (customAttributeInitializationLines.length > 0) {
    shaderBuilder.addDefine(
      "HAS_CUSTOM_ATTRIBUTES",
      undefined,
      ShaderDestination.VERTEX
    );
    var varyingFunctionLines = [].concat(
      "void initializeCustomAttributes()",
      "{",
      customAttributeInitializationLines,
      "}"
    );
    shaderBuilder.addVertexLines(varyingFunctionLines);
  }

  if (primitive.primitiveType === PrimitiveType.POINTS) {
    shaderBuilder.addDefine("PRIMITIVE_TYPE_POINTS");
  }

  shaderBuilder.addVertexLines([GeometryStageVS]);
  shaderBuilder.addVarying("vec3", "v_positionEC");
};

function processAttribute(
  renderResources,
  attribute,
  attributeIndex,
  customAttributeInitializationLines
) {
  var semantic = attribute.semantic;
  var setIndex = attribute.setIndex;
  var attributeType = attribute.type;

  var shaderBuilder = renderResources.shaderBuilder;

  var variableName;
  var varyingName;
  var glslType = AttributeType.getGlslType(attributeType);

  if (defined(semantic)) {
    variableName = VertexAttributeSemantic.getVariableName(semantic, setIndex);
    varyingName = "v_" + variableName;

    switch (semantic) {
      case VertexAttributeSemantic.NORMAL:
        shaderBuilder.addDefine("HAS_NORMALS");
        break;
      case VertexAttributeSemantic.TANGENT:
        shaderBuilder.addDefine("HAS_TANGENTS");
        break;
      case VertexAttributeSemantic.TEXCOORD:
        shaderBuilder.addDefine("HAS_TEXCOORD_" + setIndex);
        break;
      case VertexAttributeSemantic.COLOR:
        shaderBuilder.addDefine("HAS_VERTEX_COLORS");
        break;
      case VertexAttributeSemantic.FEATURE_ID:
        shaderBuilder.addDefine("HAS_FEATURE_ID");
        break;
    }
  }

  var vertexAttribute = {
    index: attributeIndex,
    vertexBuffer: attribute.buffer,
    componentsPerAttribute: AttributeType.getComponentsPerAttribute(
      attributeType
    ),
    componentDatatype: attribute.componentDatatype,
    offsetInBytes: attribute.byteOffset,
    strideInBytes: attribute.byteStride,
  };

  // Handle custom vertex attributes.
  // For example, "_TEMPERATURE" will be converted to "a_temperature".
  if (!defined(variableName)) {
    variableName = attribute.name;

    // Per the glTF 2.0 spec, custom vertex attributes must be prepended with an underscore.
    variableName = variableName.substring(1);
    variableName = variableName.toLowerCase();
    varyingName = "v_" + variableName;

    var initializationLine =
      "    " + varyingName + " = a_" + variableName + ";";
    customAttributeInitializationLines.push(initializationLine);
  }

  variableName = "a_" + variableName;
  shaderBuilder.addVarying(glslType, varyingName);

  if (semantic === VertexAttributeSemantic.POSITION) {
    shaderBuilder.setPositionAttribute(glslType, variableName);
  } else {
    shaderBuilder.addAttribute(glslType, variableName);
  }

  renderResources.attributes.push(vertexAttribute);
}

export default GeometryPipelineStage;<commit_msg>Adds normalized param to GeometryPipelineStage<commit_after>import defined from "../../Core/defined.js";
import PrimitiveType from "../../Core/PrimitiveType.js";
import AttributeType from "../AttributeType.js";
import VertexAttributeSemantic from "../VertexAttributeSemantic.js";
import GeometryStageVS from "../../Shaders/ModelExperimental/GeometryStageVS.js";
import ShaderDestination from "../../Renderer/ShaderDestination.js";

/**
 * The geometry pipeline stage processes the vertex attributes of a primitive.
 *
 * @namespace GeometryPipelineStage
 *
 * @private
 */
var GeometryPipelineStage = {};

/**
 * This pipeline stage processes the vertex attributes of a mesh primitive, adding the attribute declarations to the shaders,
 * the attribute objects to the render resources and setting the flags as needed.
 *
 * Processes a mesh primitive. This stage modifies the following parts of the render resources:
 * <ul>
 *  <li> adds attribute and varying declarations for the vertex attributes in the vertex and fragment shaders
 *  <li> creates the objects required to create VertexArrays
 *  <li> sets the flag for point primitive types
 * </ul>
 *
 * @param {MeshPrimitiveRenderResources} renderResources The render resources for this mesh primitive.
 * @param {ModelComponents.Primitive} primitive The mesh primitive.
 *
 * @private
 */
GeometryPipelineStage.process = function (renderResources, primitive) {
  // The attribute index is taken from the node render resources, which may have added some attributes of its own.
  var attributeIndex = renderResources.attributeIndex;
  var index;
  var customAttributeInitializationLines = [];
  for (var i = 0; i < primitive.attributes.length; i++) {
    var attribute = primitive.attributes[i];
    if (attribute.semantic !== VertexAttributeSemantic.POSITION) {
      index = attributeIndex++;
    } else {
      index = 0;
    }
    processAttribute(
      renderResources,
      attribute,
      index,
      customAttributeInitializationLines
    );
  }

  var shaderBuilder = renderResources.shaderBuilder;

  // add a function to initialize varyings for custom attributes.
  // for example "v_custom_attribute = a_custom_attribute;""
  if (customAttributeInitializationLines.length > 0) {
    shaderBuilder.addDefine(
      "HAS_CUSTOM_ATTRIBUTES",
      undefined,
      ShaderDestination.VERTEX
    );
    var varyingFunctionLines = [].concat(
      "void initializeCustomAttributes()",
      "{",
      customAttributeInitializationLines,
      "}"
    );
    shaderBuilder.addVertexLines(varyingFunctionLines);
  }

  if (primitive.primitiveType === PrimitiveType.POINTS) {
    shaderBuilder.addDefine("PRIMITIVE_TYPE_POINTS");
  }

  shaderBuilder.addVertexLines([GeometryStageVS]);
  shaderBuilder.addVarying("vec3", "v_positionEC");
};

function processAttribute(
  renderResources,
  attribute,
  attributeIndex,
  customAttributeInitializationLines
) {
  var semantic = attribute.semantic;
  var setIndex = attribute.setIndex;
  var attributeType = attribute.type;

  var shaderBuilder = renderResources.shaderBuilder;

  var variableName;
  var varyingName;
  var glslType = AttributeType.getGlslType(attributeType);

  if (defined(semantic)) {
    variableName = VertexAttributeSemantic.getVariableName(semantic, setIndex);
    varyingName = "v_" + variableName;

    switch (semantic) {
      case VertexAttributeSemantic.NORMAL:
        shaderBuilder.addDefine("HAS_NORMALS");
        break;
      case VertexAttributeSemantic.TANGENT:
        shaderBuilder.addDefine("HAS_TANGENTS");
        break;
      case VertexAttributeSemantic.TEXCOORD:
        shaderBuilder.addDefine("HAS_TEXCOORD_" + setIndex);
        break;
      case VertexAttributeSemantic.COLOR:
        shaderBuilder.addDefine("HAS_VERTEX_COLORS");
        break;
      case VertexAttributeSemantic.FEATURE_ID:
        shaderBuilder.addDefine("HAS_FEATURE_ID");
        break;
    }
  }

  var vertexAttribute = {
    index: attributeIndex,
    vertexBuffer: attribute.buffer,
    componentsPerAttribute: AttributeType.getComponentsPerAttribute(
      attributeType
    ),
    componentDatatype: attribute.componentDatatype,
    offsetInBytes: attribute.byteOffset,
    strideInBytes: attribute.byteStride,
    normalize: attribute.normalized,
  };

  // Handle custom vertex attributes.
  // For example, "_TEMPERATURE" will be converted to "a_temperature".
  if (!defined(variableName)) {
    variableName = attribute.name;

    // Per the glTF 2.0 spec, custom vertex attributes must be prepended with an underscore.
    variableName = variableName.substring(1);
    variableName = variableName.toLowerCase();
    varyingName = "v_" + variableName;

    var initializationLine =
      "    " + varyingName + " = a_" + variableName + ";";
    customAttributeInitializationLines.push(initializationLine);
  }

  variableName = "a_" + variableName;
  shaderBuilder.addVarying(glslType, varyingName);

  if (semantic === VertexAttributeSemantic.POSITION) {
    shaderBuilder.setPositionAttribute(glslType, variableName);
  } else {
    shaderBuilder.addAttribute(glslType, variableName);
  }

  renderResources.attributes.push(vertexAttribute);
}

export default GeometryPipelineStage;
{'fraction_non_alphanumeric': 0.09216101694915255, 'fraction_numerical': 0.0013482280431432975, 'mean_word_length': 3.9832053742802302, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '1787319', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2786, 'n_words': 719}