12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694 |
- import Cartesian2 from "../Core/Cartesian2.js";
- import clone from "../Core/clone.js";
- import Color from "../Core/Color.js";
- import combine from "../Core/combine.js";
- import createGuid from "../Core/createGuid.js";
- import defaultValue from "../Core/defaultValue.js";
- import defined from "../Core/defined.js";
- import destroyObject from "../Core/destroyObject.js";
- import DeveloperError from "../Core/DeveloperError.js";
- import loadCRN from "../Core/loadCRN.js";
- import loadKTX from "../Core/loadKTX.js";
- import Matrix2 from "../Core/Matrix2.js";
- import Matrix3 from "../Core/Matrix3.js";
- import Matrix4 from "../Core/Matrix4.js";
- import Resource from "../Core/Resource.js";
- import CubeMap from "../Renderer/CubeMap.js";
- import Texture from "../Renderer/Texture.js";
- import AspectRampMaterial from "../Shaders/Materials/AspectRampMaterial.js";
- import BumpMapMaterial from "../Shaders/Materials/BumpMapMaterial.js";
- import CheckerboardMaterial from "../Shaders/Materials/CheckerboardMaterial.js";
- import DotMaterial from "../Shaders/Materials/DotMaterial.js";
- import ElevationContourMaterial from "../Shaders/Materials/ElevationContourMaterial.js";
- import ElevationRampMaterial from "../Shaders/Materials/ElevationRampMaterial.js";
- import FadeMaterial from "../Shaders/Materials/FadeMaterial.js";
- import GridMaterial from "../Shaders/Materials/GridMaterial.js";
- import NormalMapMaterial from "../Shaders/Materials/NormalMapMaterial.js";
- import PolylineArrowMaterial from "../Shaders/Materials/PolylineArrowMaterial.js";
- import PolylineDashMaterial from "../Shaders/Materials/PolylineDashMaterial.js";
- import PolylineGlowMaterial from "../Shaders/Materials/PolylineGlowMaterial.js";
- import PolylineOutlineMaterial from "../Shaders/Materials/PolylineOutlineMaterial.js";
- import RimLightingMaterial from "../Shaders/Materials/RimLightingMaterial.js";
- import Sampler from "../Renderer/Sampler.js";
- import SlopeRampMaterial from "../Shaders/Materials/SlopeRampMaterial.js";
- import StripeMaterial from "../Shaders/Materials/StripeMaterial.js";
- import TextureMagnificationFilter from "../Renderer/TextureMagnificationFilter.js";
- import TextureMinificationFilter from "../Renderer/TextureMinificationFilter.js";
- import WaterMaterial from "../Shaders/Materials/Water.js";
- import when from "../ThirdParty/when.js";
- function Material(options) {
-
- this.type = undefined;
-
- this.shaderSource = undefined;
-
- this.materials = undefined;
-
- this.uniforms = undefined;
- this._uniforms = undefined;
-
- this.translucent = undefined;
- this._minificationFilter = defaultValue(
- options.minificationFilter,
- TextureMinificationFilter.LINEAR
- );
- this._magnificationFilter = defaultValue(
- options.magnificationFilter,
- TextureMagnificationFilter.LINEAR
- );
- this._strict = undefined;
- this._template = undefined;
- this._count = undefined;
- this._texturePaths = {};
- this._loadedImages = [];
- this._loadedCubeMaps = [];
- this._textures = {};
- this._updateFunctions = [];
- this._defaultTexture = undefined;
- initializeMaterial(options, this);
- Object.defineProperties(this, {
- type: {
- value: this.type,
- writable: false,
- },
- });
- if (!defined(Material._uniformList[this.type])) {
- Material._uniformList[this.type] = Object.keys(this._uniforms);
- }
- }
- Material._uniformList = {};
- Material.fromType = function (type, uniforms) {
-
- if (!defined(Material._materialCache.getMaterial(type))) {
- throw new DeveloperError(
- "material with type '" + type + "' does not exist."
- );
- }
-
- var material = new Material({
- fabric: {
- type: type,
- },
- });
- if (defined(uniforms)) {
- for (var name in uniforms) {
- if (uniforms.hasOwnProperty(name)) {
- material.uniforms[name] = uniforms[name];
- }
- }
- }
- return material;
- };
- Material.prototype.isTranslucent = function () {
- if (defined(this.translucent)) {
- if (typeof this.translucent === "function") {
- return this.translucent();
- }
- return this.translucent;
- }
- var translucent = true;
- var funcs = this._translucentFunctions;
- var length = funcs.length;
- for (var i = 0; i < length; ++i) {
- var func = funcs[i];
- if (typeof func === "function") {
- translucent = translucent && func();
- } else {
- translucent = translucent && func;
- }
- if (!translucent) {
- break;
- }
- }
- return translucent;
- };
- Material.prototype.update = function (context) {
- var i;
- var uniformId;
- var loadedImages = this._loadedImages;
- var length = loadedImages.length;
- for (i = 0; i < length; ++i) {
- var loadedImage = loadedImages[i];
- uniformId = loadedImage.id;
- var image = loadedImage.image;
- var sampler = new Sampler({
- minificationFilter: this._minificationFilter,
- magnificationFilter: this._magnificationFilter,
- });
- var texture;
- if (defined(image.internalFormat)) {
- texture = new Texture({
- context: context,
- pixelFormat: image.internalFormat,
- width: image.width,
- height: image.height,
- source: {
- arrayBufferView: image.bufferView,
- },
- sampler: sampler,
- });
- } else {
- texture = new Texture({
- context: context,
- source: image,
- sampler: sampler,
- });
- }
- this._textures[uniformId] = texture;
- var uniformDimensionsName = uniformId + "Dimensions";
- if (this.uniforms.hasOwnProperty(uniformDimensionsName)) {
- var uniformDimensions = this.uniforms[uniformDimensionsName];
- uniformDimensions.x = texture._width;
- uniformDimensions.y = texture._height;
- }
- }
- loadedImages.length = 0;
- var loadedCubeMaps = this._loadedCubeMaps;
- length = loadedCubeMaps.length;
- for (i = 0; i < length; ++i) {
- var loadedCubeMap = loadedCubeMaps[i];
- uniformId = loadedCubeMap.id;
- var images = loadedCubeMap.images;
- var cubeMap = new CubeMap({
- context: context,
- source: {
- positiveX: images[0],
- negativeX: images[1],
- positiveY: images[2],
- negativeY: images[3],
- positiveZ: images[4],
- negativeZ: images[5],
- },
- sampler: new Sampler({
- minificationFilter: this._minificationFilter,
- magnificationFilter: this._magnificationFilter,
- }),
- });
- this._textures[uniformId] = cubeMap;
- }
- loadedCubeMaps.length = 0;
- var updateFunctions = this._updateFunctions;
- length = updateFunctions.length;
- for (i = 0; i < length; ++i) {
- updateFunctions[i](this, context);
- }
- var subMaterials = this.materials;
- for (var name in subMaterials) {
- if (subMaterials.hasOwnProperty(name)) {
- subMaterials[name].update(context);
- }
- }
- };
- Material.prototype.isDestroyed = function () {
- return false;
- };
- Material.prototype.destroy = function () {
- var textures = this._textures;
- for (var texture in textures) {
- if (textures.hasOwnProperty(texture)) {
- var instance = textures[texture];
- if (instance !== this._defaultTexture) {
- instance.destroy();
- }
- }
- }
- var materials = this.materials;
- for (var material in materials) {
- if (materials.hasOwnProperty(material)) {
- materials[material].destroy();
- }
- }
- return destroyObject(this);
- };
- function initializeMaterial(options, result) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
- result._strict = defaultValue(options.strict, false);
- result._count = defaultValue(options.count, 0);
- result._template = clone(
- defaultValue(options.fabric, defaultValue.EMPTY_OBJECT)
- );
- result._template.uniforms = clone(
- defaultValue(result._template.uniforms, defaultValue.EMPTY_OBJECT)
- );
- result._template.materials = clone(
- defaultValue(result._template.materials, defaultValue.EMPTY_OBJECT)
- );
- result.type = defined(result._template.type)
- ? result._template.type
- : createGuid();
- result.shaderSource = "";
- result.materials = {};
- result.uniforms = {};
- result._uniforms = {};
- result._translucentFunctions = [];
- var translucent;
-
- var cachedMaterial = Material._materialCache.getMaterial(result.type);
- if (defined(cachedMaterial)) {
- var template = clone(cachedMaterial.fabric, true);
- result._template = combine(result._template, template, true);
- translucent = cachedMaterial.translucent;
- }
-
- checkForTemplateErrors(result);
-
- if (!defined(cachedMaterial)) {
- Material._materialCache.addMaterial(result.type, result);
- }
- createMethodDefinition(result);
- createUniforms(result);
- createSubMaterials(result);
- var defaultTranslucent =
- result._translucentFunctions.length === 0 ? true : undefined;
- translucent = defaultValue(translucent, defaultTranslucent);
- translucent = defaultValue(options.translucent, translucent);
- if (defined(translucent)) {
- if (typeof translucent === "function") {
- var wrappedTranslucent = function () {
- return translucent(result);
- };
- result._translucentFunctions.push(wrappedTranslucent);
- } else {
- result._translucentFunctions.push(translucent);
- }
- }
- }
- function checkForValidProperties(object, properties, result, throwNotFound) {
- if (defined(object)) {
- for (var property in object) {
- if (object.hasOwnProperty(property)) {
- var hasProperty = properties.indexOf(property) !== -1;
- if (
- (throwNotFound && !hasProperty) ||
- (!throwNotFound && hasProperty)
- ) {
- result(property, properties);
- }
- }
- }
- }
- }
- function invalidNameError(property, properties) {
-
- var errorString =
- "fabric: property name '" + property + "' is not valid. It should be ";
- for (var i = 0; i < properties.length; i++) {
- var propertyName = "'" + properties[i] + "'";
- errorString +=
- i === properties.length - 1
- ? "or " + propertyName + "."
- : propertyName + ", ";
- }
- throw new DeveloperError(errorString);
-
- }
- function duplicateNameError(property, properties) {
-
- var errorString =
- "fabric: uniforms and materials cannot share the same property '" +
- property +
- "'";
- throw new DeveloperError(errorString);
-
- }
- var templateProperties = [
- "type",
- "materials",
- "uniforms",
- "components",
- "source",
- ];
- var componentProperties = [
- "diffuse",
- "specular",
- "shininess",
- "normal",
- "emission",
- "alpha",
- ];
- function checkForTemplateErrors(material) {
- var template = material._template;
- var uniforms = template.uniforms;
- var materials = template.materials;
- var components = template.components;
-
-
- if (defined(components) && defined(template.source)) {
- throw new DeveloperError(
- "fabric: cannot have source and components in the same template."
- );
- }
-
-
- checkForValidProperties(template, templateProperties, invalidNameError, true);
- checkForValidProperties(
- components,
- componentProperties,
- invalidNameError,
- true
- );
-
- var materialNames = [];
- for (var property in materials) {
- if (materials.hasOwnProperty(property)) {
- materialNames.push(property);
- }
- }
- checkForValidProperties(uniforms, materialNames, duplicateNameError, false);
- }
- function isMaterialFused(shaderComponent, material) {
- var materials = material._template.materials;
- for (var subMaterialId in materials) {
- if (materials.hasOwnProperty(subMaterialId)) {
- if (shaderComponent.indexOf(subMaterialId) > -1) {
- return true;
- }
- }
- }
- return false;
- }
- function createMethodDefinition(material) {
- var components = material._template.components;
- var source = material._template.source;
- if (defined(source)) {
- material.shaderSource += source + "\n";
- } else {
- material.shaderSource +=
- "czm_material czm_getMaterial(czm_materialInput materialInput)\n{\n";
- material.shaderSource +=
- "czm_material material = czm_getDefaultMaterial(materialInput);\n";
- if (defined(components)) {
- var isMultiMaterial =
- Object.keys(material._template.materials).length > 0;
- for (var component in components) {
- if (components.hasOwnProperty(component)) {
- if (component === "diffuse" || component === "emission") {
- var isFusion =
- isMultiMaterial &&
- isMaterialFused(components[component], material);
- var componentSource = isFusion
- ? components[component]
- : "czm_gammaCorrect(" + components[component] + ")";
- material.shaderSource +=
- "material." + component + " = " + componentSource + "; \n";
- } else if (component === "alpha") {
- material.shaderSource +=
- "material.alpha = " + components.alpha + "; \n";
- } else {
- material.shaderSource +=
- "material." + component + " = " + components[component] + ";\n";
- }
- }
- }
- }
- material.shaderSource += "return material;\n}\n";
- }
- }
- var matrixMap = {
- mat2: Matrix2,
- mat3: Matrix3,
- mat4: Matrix4,
- };
- var ktxRegex = /\.ktx$/i;
- var crnRegex = /\.crn$/i;
- function createTexture2DUpdateFunction(uniformId) {
- var oldUniformValue;
- return function (material, context) {
- var uniforms = material.uniforms;
- var uniformValue = uniforms[uniformId];
- var uniformChanged = oldUniformValue !== uniformValue;
- oldUniformValue = uniformValue;
- var texture = material._textures[uniformId];
- var uniformDimensionsName;
- var uniformDimensions;
- if (uniformValue instanceof HTMLVideoElement) {
-
-
- if (uniformValue.readyState >= 2) {
- if (uniformChanged && defined(texture)) {
- if (texture !== context.defaultTexture) {
- texture.destroy();
- }
- texture = undefined;
- }
- if (!defined(texture) || texture === context.defaultTexture) {
- var sampler = new Sampler({
- minificationFilter: material._minificationFilter,
- magnificationFilter: material._magnificationFilter,
- });
- texture = new Texture({
- context: context,
- source: uniformValue,
- sampler: sampler,
- });
- material._textures[uniformId] = texture;
- return;
- }
- texture.copyFrom(uniformValue);
- } else if (!defined(texture)) {
- material._textures[uniformId] = context.defaultTexture;
- }
- return;
- }
- if (uniformValue instanceof Texture && uniformValue !== texture) {
- material._texturePaths[uniformId] = undefined;
- var tmp = material._textures[uniformId];
- if (tmp !== material._defaultTexture) {
- tmp.destroy();
- }
- material._textures[uniformId] = uniformValue;
- uniformDimensionsName = uniformId + "Dimensions";
- if (uniforms.hasOwnProperty(uniformDimensionsName)) {
- uniformDimensions = uniforms[uniformDimensionsName];
- uniformDimensions.x = uniformValue._width;
- uniformDimensions.y = uniformValue._height;
- }
- return;
- }
- if (!defined(texture)) {
- material._texturePaths[uniformId] = undefined;
- if (!defined(material._defaultTexture)) {
- material._defaultTexture = context.defaultTexture;
- }
- texture = material._textures[uniformId] = material._defaultTexture;
- uniformDimensionsName = uniformId + "Dimensions";
- if (uniforms.hasOwnProperty(uniformDimensionsName)) {
- uniformDimensions = uniforms[uniformDimensionsName];
- uniformDimensions.x = texture._width;
- uniformDimensions.y = texture._height;
- }
- }
- if (uniformValue === Material.DefaultImageId) {
- return;
- }
-
-
-
-
- var isResource = uniformValue instanceof Resource;
- if (
- !defined(material._texturePaths[uniformId]) ||
- (isResource &&
- uniformValue.url !== material._texturePaths[uniformId].url) ||
- (!isResource && uniformValue !== material._texturePaths[uniformId])
- ) {
- if (typeof uniformValue === "string" || isResource) {
- var resource = isResource
- ? uniformValue
- : Resource.createIfNeeded(uniformValue);
- var promise;
- if (ktxRegex.test(resource.url)) {
- promise = loadKTX(resource);
- } else if (crnRegex.test(resource.url)) {
- promise = loadCRN(resource);
- } else {
- promise = resource.fetchImage();
- }
- when(promise, function (image) {
- material._loadedImages.push({
- id: uniformId,
- image: image,
- });
- });
- } else if (
- uniformValue instanceof HTMLCanvasElement ||
- uniformValue instanceof HTMLImageElement
- ) {
- material._loadedImages.push({
- id: uniformId,
- image: uniformValue,
- });
- }
- material._texturePaths[uniformId] = uniformValue;
- }
- };
- }
- function createCubeMapUpdateFunction(uniformId) {
- return function (material, context) {
- var uniformValue = material.uniforms[uniformId];
- if (uniformValue instanceof CubeMap) {
- var tmp = material._textures[uniformId];
- if (tmp !== material._defaultTexture) {
- tmp.destroy();
- }
- material._texturePaths[uniformId] = undefined;
- material._textures[uniformId] = uniformValue;
- return;
- }
- if (!defined(material._textures[uniformId])) {
- material._texturePaths[uniformId] = undefined;
- material._textures[uniformId] = context.defaultCubeMap;
- }
- if (uniformValue === Material.DefaultCubeMapId) {
- return;
- }
- var path =
- uniformValue.positiveX +
- uniformValue.negativeX +
- uniformValue.positiveY +
- uniformValue.negativeY +
- uniformValue.positiveZ +
- uniformValue.negativeZ;
- if (path !== material._texturePaths[uniformId]) {
- var promises = [
- Resource.createIfNeeded(uniformValue.positiveX).fetchImage(),
- Resource.createIfNeeded(uniformValue.negativeX).fetchImage(),
- Resource.createIfNeeded(uniformValue.positiveY).fetchImage(),
- Resource.createIfNeeded(uniformValue.negativeY).fetchImage(),
- Resource.createIfNeeded(uniformValue.positiveZ).fetchImage(),
- Resource.createIfNeeded(uniformValue.negativeZ).fetchImage(),
- ];
- when.all(promises).then(function (images) {
- material._loadedCubeMaps.push({
- id: uniformId,
- images: images,
- });
- });
- material._texturePaths[uniformId] = path;
- }
- };
- }
- function createUniforms(material) {
- var uniforms = material._template.uniforms;
- for (var uniformId in uniforms) {
- if (uniforms.hasOwnProperty(uniformId)) {
- createUniform(material, uniformId);
- }
- }
- }
- function createUniform(material, uniformId) {
- var strict = material._strict;
- var materialUniforms = material._template.uniforms;
- var uniformValue = materialUniforms[uniformId];
- var uniformType = getUniformType(uniformValue);
-
- if (!defined(uniformType)) {
- throw new DeveloperError(
- "fabric: uniform '" + uniformId + "' has invalid type."
- );
- }
-
- var replacedTokenCount;
- if (uniformType === "channels") {
- replacedTokenCount = replaceToken(material, uniformId, uniformValue, false);
-
- if (replacedTokenCount === 0 && strict) {
- throw new DeveloperError(
- "strict: shader source does not use channels '" + uniformId + "'."
- );
- }
-
- } else {
-
-
- if (uniformType === "sampler2D") {
- var imageDimensionsUniformName = uniformId + "Dimensions";
- if (getNumberOfTokens(material, imageDimensionsUniformName) > 0) {
- materialUniforms[imageDimensionsUniformName] = {
- type: "ivec3",
- x: 1,
- y: 1,
- };
- createUniform(material, imageDimensionsUniformName);
- }
- }
-
- var uniformDeclarationRegex = new RegExp(
- "uniform\\s+" + uniformType + "\\s+" + uniformId + "\\s*;"
- );
- if (!uniformDeclarationRegex.test(material.shaderSource)) {
- var uniformDeclaration = "uniform " + uniformType + " " + uniformId + ";";
- material.shaderSource = uniformDeclaration + material.shaderSource;
- }
- var newUniformId = uniformId + "_" + material._count++;
- replacedTokenCount = replaceToken(material, uniformId, newUniformId);
-
- if (replacedTokenCount === 1 && strict) {
- throw new DeveloperError(
- "strict: shader source does not use uniform '" + uniformId + "'."
- );
- }
-
-
- material.uniforms[uniformId] = uniformValue;
- if (uniformType === "sampler2D") {
- material._uniforms[newUniformId] = function () {
- return material._textures[uniformId];
- };
- material._updateFunctions.push(createTexture2DUpdateFunction(uniformId));
- } else if (uniformType === "samplerCube") {
- material._uniforms[newUniformId] = function () {
- return material._textures[uniformId];
- };
- material._updateFunctions.push(createCubeMapUpdateFunction(uniformId));
- } else if (uniformType.indexOf("mat") !== -1) {
- var scratchMatrix = new matrixMap[uniformType]();
- material._uniforms[newUniformId] = function () {
- return matrixMap[uniformType].fromColumnMajorArray(
- material.uniforms[uniformId],
- scratchMatrix
- );
- };
- } else {
- material._uniforms[newUniformId] = function () {
- return material.uniforms[uniformId];
- };
- }
- }
- }
- function getUniformType(uniformValue) {
- var uniformType = uniformValue.type;
- if (!defined(uniformType)) {
- var type = typeof uniformValue;
- if (type === "number") {
- uniformType = "float";
- } else if (type === "boolean") {
- uniformType = "bool";
- } else if (
- type === "string" ||
- uniformValue instanceof Resource ||
- uniformValue instanceof HTMLCanvasElement ||
- uniformValue instanceof HTMLImageElement
- ) {
- if (/^([rgba]){1,4}$/i.test(uniformValue)) {
- uniformType = "channels";
- } else if (uniformValue === Material.DefaultCubeMapId) {
- uniformType = "samplerCube";
- } else {
- uniformType = "sampler2D";
- }
- } else if (type === "object") {
- if (Array.isArray(uniformValue)) {
- if (
- uniformValue.length === 4 ||
- uniformValue.length === 9 ||
- uniformValue.length === 16
- ) {
- uniformType = "mat" + Math.sqrt(uniformValue.length);
- }
- } else {
- var numAttributes = 0;
- for (var attribute in uniformValue) {
- if (uniformValue.hasOwnProperty(attribute)) {
- numAttributes += 1;
- }
- }
- if (numAttributes >= 2 && numAttributes <= 4) {
- uniformType = "vec" + numAttributes;
- } else if (numAttributes === 6) {
- uniformType = "samplerCube";
- }
- }
- }
- }
- return uniformType;
- }
- function createSubMaterials(material) {
- var strict = material._strict;
- var subMaterialTemplates = material._template.materials;
- for (var subMaterialId in subMaterialTemplates) {
- if (subMaterialTemplates.hasOwnProperty(subMaterialId)) {
-
- var subMaterial = new Material({
- strict: strict,
- fabric: subMaterialTemplates[subMaterialId],
- count: material._count,
- });
- material._count = subMaterial._count;
- material._uniforms = combine(
- material._uniforms,
- subMaterial._uniforms,
- true
- );
- material.materials[subMaterialId] = subMaterial;
- material._translucentFunctions = material._translucentFunctions.concat(
- subMaterial._translucentFunctions
- );
-
- var originalMethodName = "czm_getMaterial";
- var newMethodName = originalMethodName + "_" + material._count++;
- replaceToken(subMaterial, originalMethodName, newMethodName);
- material.shaderSource = subMaterial.shaderSource + material.shaderSource;
-
- var materialMethodCall = newMethodName + "(materialInput)";
- var tokensReplacedCount = replaceToken(
- material,
- subMaterialId,
- materialMethodCall
- );
-
- if (tokensReplacedCount === 0 && strict) {
- throw new DeveloperError(
- "strict: shader source does not use material '" + subMaterialId + "'."
- );
- }
-
- }
- }
- }
- function replaceToken(material, token, newToken, excludePeriod) {
- excludePeriod = defaultValue(excludePeriod, true);
- var count = 0;
- var suffixChars = "([\\w])?";
- var prefixChars = "([\\w" + (excludePeriod ? "." : "") + "])?";
- var regExp = new RegExp(prefixChars + token + suffixChars, "g");
- material.shaderSource = material.shaderSource.replace(regExp, function (
- $0,
- $1,
- $2
- ) {
- if ($1 || $2) {
- return $0;
- }
- count += 1;
- return newToken;
- });
- return count;
- }
- function getNumberOfTokens(material, token, excludePeriod) {
- return replaceToken(material, token, token, excludePeriod);
- }
- Material._materialCache = {
- _materials: {},
- addMaterial: function (type, materialTemplate) {
- this._materials[type] = materialTemplate;
- },
- getMaterial: function (type) {
- return this._materials[type];
- },
- };
- Material.DefaultImageId = "czm_defaultImage";
- Material.DefaultCubeMapId = "czm_defaultCubeMap";
- Material.ColorType = "Color";
- Material._materialCache.addMaterial(Material.ColorType, {
- fabric: {
- type: Material.ColorType,
- uniforms: {
- color: new Color(1.0, 0.0, 0.0, 0.5),
- },
- components: {
- diffuse: "color.rgb",
- alpha: "color.a",
- },
- },
- translucent: function (material) {
- return material.uniforms.color.alpha < 1.0;
- },
- });
- Material.ImageType = "Image";
- Material._materialCache.addMaterial(Material.ImageType, {
- fabric: {
- type: Material.ImageType,
- uniforms: {
- image: Material.DefaultImageId,
- repeat: new Cartesian2(1.0, 1.0),
- color: new Color(1.0, 1.0, 1.0, 1.0),
- },
- components: {
- diffuse:
- "texture2D(image, fract(repeat * materialInput.st)).rgb * color.rgb",
- alpha: "texture2D(image, fract(repeat * materialInput.st)).a * color.a",
- },
- },
- translucent: function (material) {
- return material.uniforms.color.alpha < 1.0;
- },
- });
- Material.DiffuseMapType = "DiffuseMap";
- Material._materialCache.addMaterial(Material.DiffuseMapType, {
- fabric: {
- type: Material.DiffuseMapType,
- uniforms: {
- image: Material.DefaultImageId,
- channels: "rgb",
- repeat: new Cartesian2(1.0, 1.0),
- },
- components: {
- diffuse: "texture2D(image, fract(repeat * materialInput.st)).channels",
- },
- },
- translucent: false,
- });
- Material.AlphaMapType = "AlphaMap";
- Material._materialCache.addMaterial(Material.AlphaMapType, {
- fabric: {
- type: Material.AlphaMapType,
- uniforms: {
- image: Material.DefaultImageId,
- channel: "a",
- repeat: new Cartesian2(1.0, 1.0),
- },
- components: {
- alpha: "texture2D(image, fract(repeat * materialInput.st)).channel",
- },
- },
- translucent: true,
- });
- Material.SpecularMapType = "SpecularMap";
- Material._materialCache.addMaterial(Material.SpecularMapType, {
- fabric: {
- type: Material.SpecularMapType,
- uniforms: {
- image: Material.DefaultImageId,
- channel: "r",
- repeat: new Cartesian2(1.0, 1.0),
- },
- components: {
- specular: "texture2D(image, fract(repeat * materialInput.st)).channel",
- },
- },
- translucent: false,
- });
- Material.EmissionMapType = "EmissionMap";
- Material._materialCache.addMaterial(Material.EmissionMapType, {
- fabric: {
- type: Material.EmissionMapType,
- uniforms: {
- image: Material.DefaultImageId,
- channels: "rgb",
- repeat: new Cartesian2(1.0, 1.0),
- },
- components: {
- emission: "texture2D(image, fract(repeat * materialInput.st)).channels",
- },
- },
- translucent: false,
- });
- Material.BumpMapType = "BumpMap";
- Material._materialCache.addMaterial(Material.BumpMapType, {
- fabric: {
- type: Material.BumpMapType,
- uniforms: {
- image: Material.DefaultImageId,
- channel: "r",
- strength: 0.8,
- repeat: new Cartesian2(1.0, 1.0),
- },
- source: BumpMapMaterial,
- },
- translucent: false,
- });
- Material.NormalMapType = "NormalMap";
- Material._materialCache.addMaterial(Material.NormalMapType, {
- fabric: {
- type: Material.NormalMapType,
- uniforms: {
- image: Material.DefaultImageId,
- channels: "rgb",
- strength: 0.8,
- repeat: new Cartesian2(1.0, 1.0),
- },
- source: NormalMapMaterial,
- },
- translucent: false,
- });
- Material.GridType = "Grid";
- Material._materialCache.addMaterial(Material.GridType, {
- fabric: {
- type: Material.GridType,
- uniforms: {
- color: new Color(0.0, 1.0, 0.0, 1.0),
- cellAlpha: 0.1,
- lineCount: new Cartesian2(8.0, 8.0),
- lineThickness: new Cartesian2(1.0, 1.0),
- lineOffset: new Cartesian2(0.0, 0.0),
- },
- source: GridMaterial,
- },
- translucent: function (material) {
- var uniforms = material.uniforms;
- return uniforms.color.alpha < 1.0 || uniforms.cellAlpha < 1.0;
- },
- });
- Material.StripeType = "Stripe";
- Material._materialCache.addMaterial(Material.StripeType, {
- fabric: {
- type: Material.StripeType,
- uniforms: {
- horizontal: true,
- evenColor: new Color(1.0, 1.0, 1.0, 0.5),
- oddColor: new Color(0.0, 0.0, 1.0, 0.5),
- offset: 0.0,
- repeat: 5.0,
- },
- source: StripeMaterial,
- },
- translucent: function (material) {
- var uniforms = material.uniforms;
- return uniforms.evenColor.alpha < 1.0 || uniforms.oddColor.alpha < 1.0;
- },
- });
- Material.CheckerboardType = "Checkerboard";
- Material._materialCache.addMaterial(Material.CheckerboardType, {
- fabric: {
- type: Material.CheckerboardType,
- uniforms: {
- lightColor: new Color(1.0, 1.0, 1.0, 0.5),
- darkColor: new Color(0.0, 0.0, 0.0, 0.5),
- repeat: new Cartesian2(5.0, 5.0),
- },
- source: CheckerboardMaterial,
- },
- translucent: function (material) {
- var uniforms = material.uniforms;
- return uniforms.lightColor.alpha < 1.0 || uniforms.darkColor.alpha < 1.0;
- },
- });
- Material.DotType = "Dot";
- Material._materialCache.addMaterial(Material.DotType, {
- fabric: {
- type: Material.DotType,
- uniforms: {
- lightColor: new Color(1.0, 1.0, 0.0, 0.75),
- darkColor: new Color(0.0, 1.0, 1.0, 0.75),
- repeat: new Cartesian2(5.0, 5.0),
- },
- source: DotMaterial,
- },
- translucent: function (material) {
- var uniforms = material.uniforms;
- return uniforms.lightColor.alpha < 1.0 || uniforms.darkColor.alpha < 1.0;
- },
- });
- Material.WaterType = "Water";
- Material._materialCache.addMaterial(Material.WaterType, {
- fabric: {
- type: Material.WaterType,
- uniforms: {
- baseWaterColor: new Color(0.2, 0.3, 0.6, 1.0),
- blendColor: new Color(0.0, 1.0, 0.699, 1.0),
- specularMap: Material.DefaultImageId,
- normalMap: Material.DefaultImageId,
- frequency: 10.0,
- animationSpeed: 0.01,
- amplitude: 1.0,
- specularIntensity: 0.5,
- fadeFactor: 1.0,
- },
- source: WaterMaterial,
- },
- translucent: function (material) {
- var uniforms = material.uniforms;
- return (
- uniforms.baseWaterColor.alpha < 1.0 || uniforms.blendColor.alpha < 1.0
- );
- },
- });
- Material.RimLightingType = "RimLighting";
- Material._materialCache.addMaterial(Material.RimLightingType, {
- fabric: {
- type: Material.RimLightingType,
- uniforms: {
- color: new Color(1.0, 0.0, 0.0, 0.7),
- rimColor: new Color(1.0, 1.0, 1.0, 0.4),
- width: 0.3,
- },
- source: RimLightingMaterial,
- },
- translucent: function (material) {
- var uniforms = material.uniforms;
- return uniforms.color.alpha < 1.0 || uniforms.rimColor.alpha < 1.0;
- },
- });
- Material.FadeType = "Fade";
- Material._materialCache.addMaterial(Material.FadeType, {
- fabric: {
- type: Material.FadeType,
- uniforms: {
- fadeInColor: new Color(1.0, 0.0, 0.0, 1.0),
- fadeOutColor: new Color(0.0, 0.0, 0.0, 0.0),
- maximumDistance: 0.5,
- repeat: true,
- fadeDirection: {
- x: true,
- y: true,
- },
- time: new Cartesian2(0.5, 0.5),
- },
- source: FadeMaterial,
- },
- translucent: function (material) {
- var uniforms = material.uniforms;
- return (
- uniforms.fadeInColor.alpha < 1.0 || uniforms.fadeOutColor.alpha < 1.0
- );
- },
- });
- Material.PolylineArrowType = "PolylineArrow";
- Material._materialCache.addMaterial(Material.PolylineArrowType, {
- fabric: {
- type: Material.PolylineArrowType,
- uniforms: {
- color: new Color(1.0, 1.0, 1.0, 1.0),
- },
- source: PolylineArrowMaterial,
- },
- translucent: true,
- });
- Material.PolylineDashType = "PolylineDash";
- Material._materialCache.addMaterial(Material.PolylineDashType, {
- fabric: {
- type: Material.PolylineDashType,
- uniforms: {
- color: new Color(1.0, 0.0, 1.0, 1.0),
- gapColor: new Color(0.0, 0.0, 0.0, 0.0),
- dashLength: 16.0,
- dashPattern: 255.0,
- },
- source: PolylineDashMaterial,
- },
- translucent: true,
- });
- Material.PolylineGlowType = "PolylineGlow";
- Material._materialCache.addMaterial(Material.PolylineGlowType, {
- fabric: {
- type: Material.PolylineGlowType,
- uniforms: {
- color: new Color(0.0, 0.5, 1.0, 1.0),
- glowPower: 0.25,
- taperPower: 1.0,
- },
- source: PolylineGlowMaterial,
- },
- translucent: true,
- });
- Material.PolylineOutlineType = "PolylineOutline";
- Material._materialCache.addMaterial(Material.PolylineOutlineType, {
- fabric: {
- type: Material.PolylineOutlineType,
- uniforms: {
- color: new Color(1.0, 1.0, 1.0, 1.0),
- outlineColor: new Color(1.0, 0.0, 0.0, 1.0),
- outlineWidth: 1.0,
- },
- source: PolylineOutlineMaterial,
- },
- translucent: function (material) {
- var uniforms = material.uniforms;
- return uniforms.color.alpha < 1.0 || uniforms.outlineColor.alpha < 1.0;
- },
- });
- Material.ElevationContourType = "ElevationContour";
- Material._materialCache.addMaterial(Material.ElevationContourType, {
- fabric: {
- type: Material.ElevationContourType,
- uniforms: {
- spacing: 100.0,
- color: new Color(1.0, 0.0, 0.0, 1.0),
- width: 1.0,
- },
- source: ElevationContourMaterial,
- },
- translucent: false,
- });
- Material.ElevationRampType = "ElevationRamp";
- Material._materialCache.addMaterial(Material.ElevationRampType, {
- fabric: {
- type: Material.ElevationRampType,
- uniforms: {
- image: Material.DefaultImageId,
- minimumHeight: 0.0,
- maximumHeight: 10000.0,
- },
- source: ElevationRampMaterial,
- },
- translucent: false,
- });
- Material.SlopeRampMaterialType = "SlopeRamp";
- Material._materialCache.addMaterial(Material.SlopeRampMaterialType, {
- fabric: {
- type: Material.SlopeRampMaterialType,
- uniforms: {
- image: Material.DefaultImageId,
- },
- source: SlopeRampMaterial,
- },
- translucent: false,
- });
- Material.AspectRampMaterialType = "AspectRamp";
- Material._materialCache.addMaterial(Material.AspectRampMaterialType, {
- fabric: {
- type: Material.AspectRampMaterialType,
- uniforms: {
- image: Material.DefaultImageId,
- },
- source: AspectRampMaterial,
- },
- translucent: false,
- });
- export default Material;
|