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 = material is MeshStandardMaterial?cubeuvmaps.get(material.envMap ?? environment):cubemaps.get(material.envMap ?? environment);
final envMapCubeUVHeight = (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(
shaderID: shaderID,
shaderType: material.type,
shaderName: "${material.type} - ${material.name}",
vertexShader: vertexShader ?? '',
fragmentShader: fragmentShader ?? '',
defines: material.defines,
customVertexShaderID: customVertexShaderID,
customFragmentShaderID: customFragmentShaderID,
isRawShaderMaterial: material is RawShaderMaterial,
glslVersion: material.glslVersion,
precision: precision,
batching: object is BatchedMesh,
instancing: object is InstancedMesh,
instancingColor: object is InstancedMesh && object.instanceColor != null,
instancingMorph: object is InstancedMesh && object.morphTexture != null,
supportsVertexTextures: vertexTextures,
outputColorSpace: ( currentRenderTarget == null ) ? renderer.outputColorSpace : ( currentRenderTarget.isXRRenderTarget? currentRenderTarget.texture.colorSpace : LinearSRGBColorSpace ),
alphaToCoverage: !! material.alphaToCoverage,
map: material.map != null,
matcap: material.matcap != null,
envMap: envMap != null,
envMapMode: envMap?.mapping,
envMapCubeUVHeight: envMapCubeUVHeight,
aoMap: material.aoMap != null,
lightMap: material.lightMap != null,
bumpMap: material.bumpMap != null,
normalMap: material.normalMap != null,
displacementMap: capabilities.vertexTextures && material.displacementMap != null,
emissiveMap: material.emissiveMap != null,
normalMapObjectSpace: material.normalMap != null && material.normalMapType == ObjectSpaceNormalMap,
normalMapTangentSpace: material.normalMap != null && material.normalMapType == TangentSpaceNormalMap,
metalnessMap: material.metalnessMap != null,
roughnessMap: material.roughnessMap != null,
anisotropy: material is MeshPhysicalMaterial && material.anisotropy > 0,
anisotropyMap: material is MeshPhysicalMaterial && material.anisotropy > 0 && material.anisotropyMap != null,
clearcoat: useClearcoat,
clearcoatMap: useClearcoat && material.clearcoatMap != null,
clearcoatNormalMap: useClearcoat && material.clearcoatRoughnessMap != null,
clearcoatRoughnessMap: useClearcoat && material.clearcoatNormalMap != null,
dispersion: material is MeshPhysicalMaterial && material.dispersion > 0,
iridescence: material is MeshPhysicalMaterial && material.iridescence > 0,
iridescenceMap: material is MeshPhysicalMaterial && material.iridescence > 0 && material.iridescenceMap != null,
iridescenceThicknessMap: material is MeshPhysicalMaterial && material.iridescence > 0 && material.iridescenceThicknessMap != null,
sheen: material.sheen > 0,
sheenColorMap: material.sheenColorMap != null,
sheenRoughnessMap: material.sheenRoughnessMap != null,
specularMap: material.specularMap != null,
specularColorMap: material.specularColorMap != null,
specularIntensityMap: material.specularIntensityMap != null,
transmission: material.transmission > 0,
transmissionMap: material.transmissionMap != null,
thicknessMap: material.thicknessMap != null,
gradientMap: material.gradientMap != null,
opaque: !material.transparent && material.blending == NormalBlending && !material.alphaToCoverage,
alphaMap: material.alphaMap != null,
alphaTest: useAlphaTest,
alphaHash: material.alphaHash,
combine: material.combine,
//
mapUv: material.map ==null?null: getChannel( material.map!.channel ),
aoMapUv: material.aoMap ==null?null:getChannel( material.aoMap!.channel ),
lightMapUv: material.lightMap ==null?null:getChannel( material.lightMap!.channel ),
bumpMapUv: material.bumpMap ==null?null:getChannel( material.bumpMap!.channel ),
normalMapUv:material.normalMap ==null?null:getChannel( material.normalMap!.channel ),
displacementMapUv: material.displacementMap ==null?null:getChannel( material.displacementMap!.channel ),
emissiveMapUv: material.emissiveMap ==null?null:getChannel( material.emissiveMap!.channel ),
metalnessMapUv: material.metalnessMap ==null?null:getChannel( material.metalnessMap!.channel ),
roughnessMapUv: material.roughnessMap ==null?null:getChannel( material.roughnessMap!.channel ),
anisotropyMapUv: material.anisotropyMap==null?null:getChannel( material.anisotropyMap!.channel ),
clearcoatMapUv: material.clearcoatMap ==null?null:getChannel( material.clearcoatMap!.channel ),
clearcoatNormalMapUv: material.clearcoatNormalMap ==null?null:getChannel( material.clearcoatNormalMap!.channel ),
clearcoatRoughnessMapUv: material.clearcoatRoughnessMap ==null?null:getChannel( material.clearcoatRoughnessMap!.channel ),
iridescenceMapUv: material.iridescenceMap ==null?null:getChannel( material.iridescenceMap!.channel ),
iridescenceThicknessMapUv: material.iridescenceThicknessMap ==null?null:getChannel( material.iridescenceThicknessMap!.channel ),
sheenColorMapUv: material.sheenColorMap ==null?null:getChannel( material.sheenColorMap!.channel ),
sheenRoughnessMapUv: material.sheenRoughnessMap ==null?null:getChannel( material.sheenRoughnessMap!.channel ),
specularMapUv: material.specularMap ==null?null:getChannel( material.specularMap!.channel ),
specularColorMapUv: material.specularColorMap ==null?null:getChannel( material.specularColorMap!.channel ),
specularIntensityMapUv: material.specularIntensityMap ==null?null:getChannel( material.specularIntensityMap!.channel ),
transmissionMapUv: material.transmissionMap == null?null:getChannel( material.transmissionMap!.channel ),
thicknessMapUv: material.thicknessMap ==null?null:getChannel( material.thicknessMap!.channel ),
alphaMapUv: material.alphaMap ==null?null:getChannel( material.alphaMap!.channel ),
//
vertexTangents: (material.normalMap != null && geometry != null && geometry.attributes["tangent"] != null),
vertexColors: material.vertexColors,
vertexAlphas: material.vertexColors == true &&
geometry != null &&
geometry.attributes["color"] != null &&
geometry.attributes["color"].itemSize == 4,
pointsUvs: object is Points && geometry?.attributes['uv'] != null && ( material.map != null || material.alphaMap != null ),
fog: fog != null,
useFog: material.fog,
fogExp2: (fog != null && fog.isFogExp2),
flatShading: material.flatShading,
sizeAttenuation: material.sizeAttenuation,
logarithmicDepthBuffer: logarithmicDepthBuffer,
skinning: object is SkinnedMesh,
morphTargets: geometry?.morphAttributes['position'] != null,
morphNormals: geometry?.morphAttributes['normal'] != null,
morphColors: geometry?.morphAttributes['color'] != null,
morphTargetsCount: morphTargetsCount,
morphTextureStride: morphTextureStride,
numDirLights: lights.directional.length,
numPointLights: lights.point.length,
numSpotLights: lights.spot.length,
numSpotLightMaps: lights.spotLightMap.length,
numRectAreaLights: lights.rectArea.length,
numHemiLights: lights.hemi.length,
numDirLightShadows: lights.directionalShadowMap.length,
numPointLightShadows: lights.pointShadowMap.length,
numSpotLightShadows: lights.spotShadowMap.length,
numSpotLightShadowsWithMaps: lights.numSpotLightShadowsWithMaps,
numLightProbes: lights.numLightProbes,
numClippingPlanes: clipping.numPlanes,
numClipIntersection: clipping.numIntersection,
dithering: material.dithering,
shadowMapEnabled: renderer.shadowMap.enabled && shadows.length > 0,
shadowMapType: renderer.shadowMap.type,
toneMapping: material.toneMapped ? renderer.toneMapping : NoToneMapping,
useLegacyLights: renderer.useLegacyLights,
decodeVideoTexture: material.map != null &&
( material.map is VideoTexture) &&
( ColorManagement.getTransfer( ColorSpace.fromString(material.map!.colorSpace) ) == SRGBTransfer ),
premultipliedAlpha: material.premultipliedAlpha,
doubleSided: material.side == DoubleSide,
flipSided: material.side == BackSide,
useDepthPacking: (material.depthPacking ?? 0) >= 0,
depthPacking: material.depthPacking ?? 0,
index0AttributeName: material.index0AttributeName,
extensionClipCullDistance: material.extensions != null && material.extensions?['clipCullDistance'] == true && extensions.has( 'WEBGL_clip_cull_distance' ),
extensionMultiDraw: material.extensions != null && material.extensions?['multiDraw'] == true && extensions.has( 'WEBGL_multi_draw' ),
rendererExtensionParallelShaderCompile: extensions.has( 'KHR_parallel_shader_compile' ),
customProgramCacheKey: material.customProgramCacheKey()
);
parameters.vertexUv1s = _activeChannels.contains( 1 );
parameters.vertexUv2s = _activeChannels.contains( 2 );
parameters.vertexUv3s = _activeChannels.contains( 3 );
_activeChannels.clear();
return parameters;
}