getParameters method

WebGLParameters getParameters(
  1. Material material,
  2. LightState lights,
  3. List<Light> shadows,
  4. Scene scene,
  5. Object3D object,
)

Implementation

WebGLParameters getParameters(Material material, LightState lights, List<Light> shadows, Scene scene, Object3D object) {
  final fog = scene.fog;
  final geometry = object.geometry;
  final environment = material is MeshStandardMaterial ? scene.environment : null;

  Texture? envMap;
  if (material is MeshStandardMaterial) {
    envMap = cubeuvmaps.get(material.envMap ?? environment);
  } else {
    envMap = cubemaps.get(material.envMap ?? environment);
  }

  final cubeUVHeight = (envMap != null) && (envMap.mapping == CubeUVReflectionMapping) ? envMap.image?.height : null;

  final shaderID = shaderIDs[material.shaderID];

  // heuristics to create shader parameters according to lights in the scene
  // (not to blow over maxLights budget)

  if (material.precision != null) {
    precision = capabilities.getMaxPrecision(material.precision);

    if (precision != material.precision) {
      console.warning('WebGLProgram.getParameters: ${material.precision} not supported, using $precision instead.');
    }
  }

  final morphAttribute = geometry?.morphAttributes["position"] ?? geometry?.morphAttributes["normal"] ?? geometry?.morphAttributes["color"];
  final morphTargetsCount = (morphAttribute != null) ? morphAttribute.length : 0;

  int morphTextureStride = 0;

  if (geometry?.morphAttributes["position"] != null) morphTextureStride = 1;
  if (geometry?.morphAttributes["normal"] != null) morphTextureStride = 2;
  if (geometry?.morphAttributes["color"] != null) morphTextureStride = 3;

  //

  String? vertexShader, fragmentShader;
  dynamic customVertexShaderID;
  dynamic customFragmentShaderID;

  if (shaderID != null) {
    final shader = shaderLib[shaderID];
    vertexShader = shader["vertexShader"];
    fragmentShader = shader["fragmentShader"];
  } else {
    vertexShader = material.vertexShader;
    fragmentShader = material.fragmentShader;

    _customShaders.update(material);

    customVertexShaderID = _customShaders.getVertexShaderID(material);
    customFragmentShaderID = _customShaders.getFragmentShaderID(material);
  }

  // print(" WebGLPrograms material : ${material.type} ${material.shaderID} ${material.id} object: ${object.type} ${object.id} shaderID: ${shaderID} vertexColors: ${material.vertexColors} ");

  final currentRenderTarget = renderer.getRenderTarget();

  final useAlphaTest = material.alphaTest > 0;
  final useClearcoat = material.clearcoat > 0;

  final parameters = WebGLParameters.create();

  parameters.isWebGL2 = isWebGL2;
  parameters.shaderID = shaderID;
  parameters.shaderName = "${material.type} - ${material.name}";
  parameters.vertexShader = vertexShader!;
  parameters.fragmentShader = fragmentShader!;
  parameters.defines = material.defines;
  parameters.customVertexShaderID = customVertexShaderID;
  parameters.customFragmentShaderID = customFragmentShaderID;
  parameters.isRawShaderMaterial = material is RawShaderMaterial;
  parameters.glslVersion = material.glslVersion;
  parameters.precision = precision;
  parameters.instancing = object is InstancedMesh;
  parameters.instancingColor = object is InstancedMesh && object.instanceColor != null;
  parameters.supportsVertexTextures = vertexTextures;
  parameters.outputEncoding = (currentRenderTarget == null)
      ? renderer.outputEncoding
      : (currentRenderTarget.isXRRenderTarget == true ? currentRenderTarget.texture.encoding : LinearEncoding);
  parameters.map = material.map != null;
  parameters.matcap = material.matcap != null;
  parameters.envMap = envMap != null;
  parameters.envMapMode = envMap?.mapping;
  parameters.cubeUVHeight = cubeUVHeight;
  parameters.lightMap = material.lightMap != null;
  parameters.aoMap = material.aoMap != null;
  parameters.emissiveMap = material.emissiveMap != null;
  parameters.bumpMap = material.bumpMap != null;
  parameters.normalMap = material.normalMap != null;
  parameters.objectSpaceNormalMap = material.normalMapType == ObjectSpaceNormalMap;
  parameters.tangentSpaceNormalMap = material.normalMapType == TangentSpaceNormalMap;
  parameters.decodeVideoTexture =
      material.map != null && (material.map is VideoTexture) && (material.map!.encoding == sRGBEncoding);
  parameters.clearcoat = useClearcoat;
  parameters.clearcoatMap = useClearcoat && material.clearcoatMap != null;
  parameters.clearcoatRoughnessMap = useClearcoat && material.clearcoatRoughnessMap != null;
  parameters.clearcoatNormalMap = useClearcoat && material.clearcoatNormalMap != null;
  parameters.displacementMap = material.displacementMap != null;
  parameters.roughnessMap = material.roughnessMap != null;
  parameters.metalnessMap = material.metalnessMap != null;
  parameters.specularMap = material.specularMap != null;
  parameters.specularIntensityMap = material.specularIntensityMap != null;
  parameters.specularColorMap = material.specularColorMap != null;
  parameters.opaque = material.transparent == false && material.blending == NormalBlending;
  parameters.alphaMap = material.alphaMap != null;
  parameters.alphaTest = useAlphaTest;
  parameters.gradientMap = material.gradientMap != null;
  parameters.sheen = material.sheen > 0;
  parameters.sheenColorMap = material.sheenColorMap != null;
  parameters.sheenRoughnessMap = material.sheenRoughnessMap != null;
  parameters.transmission = material.transmission > 0;
  parameters.transmissionMap = material.transmissionMap != null;
  parameters.thicknessMap = material.thicknessMap != null;
  parameters.combine = material.combine;
  parameters.vertexTangents =
      (material.normalMap != null && geometry != null && geometry.attributes["tangent"] != null);
  parameters.vertexColors = material.vertexColors;
  parameters.vertexAlphas = material.vertexColors == true &&
      geometry != null &&
      geometry.attributes["color"] != null &&
      geometry.attributes["color"].itemSize == 4;
  parameters.vertexUvs = material.map != null ||
      material.bumpMap != null ||
      material.normalMap != null ||
      material.specularMap != null ||
      material.alphaMap != null ||
      material.emissiveMap != null ||
      material.roughnessMap != null ||
      material.metalnessMap != null ||
      material.clearcoatMap != null ||
      material.clearcoatRoughnessMap != null ||
      material.clearcoatNormalMap != null ||
      material.displacementMap != null ||
      material.transmissionMap != null ||
      material.thicknessMap != null ||
      material.specularIntensityMap != null ||
      material.specularColorMap != null ||
      material.sheenColorMap != null ||
      material.sheenRoughnessMap != null;
  parameters.uvsVertexOnly = !(material.map != null ||
          material.bumpMap != null ||
          material.normalMap != null ||
          material.specularMap != null ||
          material.alphaMap != null ||
          material.emissiveMap != null ||
          material.roughnessMap != null ||
          material.metalnessMap != null ||
          material.clearcoatNormalMap != null ||
          material.transmissionMap != null ||
          material.thicknessMap != null ||
          material.sheen > 0 ||
          material.sheenColorMap != null ||
          material.sheenRoughnessMap != null) &&
      material.displacementMap != null;
  parameters.fog = fog != null;
  parameters.useFog = material.fog;
  parameters.fogExp2 = (fog != null && fog.isFogExp2);
  parameters.flatShading = material.flatShading;
  parameters.sizeAttenuation = material.sizeAttenuation;
  parameters.logarithmicDepthBuffer = logarithmicDepthBuffer;
  parameters.skinning = object is SkinnedMesh;
  parameters.morphTargets = geometry != null && geometry.morphAttributes["position"] != null;
  parameters.morphNormals = geometry != null && geometry.morphAttributes["normal"] != null;
  parameters.morphColors = geometry != null && geometry.morphAttributes["color"] != null;
  parameters.morphTargetsCount = morphTargetsCount;
  parameters.morphTextureStride = morphTextureStride;
  parameters.numDirLights = lights.directional.length;
  parameters.numPointLights = lights.point.length;
  parameters.numSpotLights = lights.spot.length;
  parameters.numRectAreaLights = lights.rectArea.length;
  parameters.numHemiLights = lights.hemi.length;
  parameters.numDirLightShadows = lights.directionalShadowMap.length;
  parameters.numPointLightShadows = lights.pointShadowMap.length;
  parameters.numSpotLightShadows = lights.spotShadowMap.length;
  parameters.numClippingPlanes = clipping.numPlanes;
  parameters.numClipIntersection = clipping.numIntersection;
  parameters.dithering = material.dithering;
  parameters.shadowMapEnabled = renderer.shadowMap.enabled && shadows.isNotEmpty;
  parameters.shadowMapType = renderer.shadowMap.type;
  parameters.toneMapping = material.toneMapped ? renderer.toneMapping : NoToneMapping;
  parameters.physicallyCorrectLights = renderer.physicallyCorrectLights;
  parameters.premultipliedAlpha = material.premultipliedAlpha;
  parameters.doubleSided = material.side == DoubleSide;
  parameters.flipSided = material.side == BackSide;
  parameters.useDepthPacking = material.depthPacking != null;
  parameters.depthPacking = material.depthPacking ?? 0;
  parameters.index0AttributeName = material.index0AttributeName;
  parameters.extensionDerivatives = material.extensions != null && material.extensions!["derivatives"] != null;
  parameters.extensionFragDepth = material.extensions != null && material.extensions!["fragDepth"] != null;
  parameters.extensionDrawBuffers = material.extensions != null && material.extensions!["drawBuffers"] != null;
  parameters.extensionShaderTextureLOD =
      material.extensions != null && material.extensions!["shaderTextureLOD"] != null;
  parameters.rendererExtensionFragDepth = isWebGL2 ? isWebGL2 : extensions.has('EXT_frag_depth');
  parameters.rendererExtensionDrawBuffers = isWebGL2 ? isWebGL2 : extensions.has('WEBGL_draw_buffers');
  parameters.rendererExtensionShaderTextureLod = isWebGL2 ? isWebGL2 : extensions.has('EXT_shader_texture_lod');
  parameters.customProgramCacheKey = material.customProgramCacheKey() ?? "";

  return parameters;
}