Line data Source code
1 0 : /* 2 : 3 : OOMaterialSpecifier.m 4 : 5 : 6 : Copyright (C) 2010-2013 Jens Ayton 7 : 8 : Permission is hereby granted, free of charge, to any person obtaining a copy 9 : of this software and associated documentation files (the "Software"), to deal 10 : in the Software without restriction, including without limitation the rights 11 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 : copies of the Software, and to permit persons to whom the Software is 13 : furnished to do so, subject to the following conditions: 14 : 15 : The above copyright notice and this permission notice shall be included in all 16 : copies or substantial portions of the Software. 17 : 18 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 : SOFTWARE. 25 : 26 : */ 27 : 28 : #import "OOMaterialSpecifier.h" 29 : #import "OOColor.h" 30 : #import "OOCollectionExtractors.h" 31 : #import "OOTexture.h" 32 : #import "Universe.h" 33 : #import "NSDictionaryOOExtensions.h" 34 : 35 : 36 0 : NSString * const kOOMaterialDiffuseColorName = @"diffuse_color"; 37 0 : NSString * const kOOMaterialDiffuseColorLegacyName = @"diffuse"; 38 0 : NSString * const kOOMaterialAmbientColorName = @"ambient_color"; 39 0 : NSString * const kOOMaterialAmbientColorLegacyName = @"ambient"; 40 0 : NSString * const kOOMaterialSpecularColorName = @"specular_color"; 41 0 : NSString * const kOOMaterialSpecularColorLegacyName = @"specular"; 42 0 : NSString * const kOOMaterialSpecularModulateColorName = @"specular_modulate_color"; 43 0 : NSString * const kOOMaterialEmissionColorName = @"emission_color"; 44 0 : NSString * const kOOMaterialEmissionColorLegacyName = @"emission"; 45 0 : NSString * const kOOMaterialEmissionModulateColorName = @"emission_modulate_color"; 46 0 : NSString * const kOOMaterialIlluminationModulateColorName = @"illumination_modulate_color"; 47 : 48 0 : NSString * const kOOMaterialDiffuseMapName = @"diffuse_map"; 49 0 : NSString * const kOOMaterialSpecularColorMapName = @"specular_color_map"; 50 0 : NSString * const kOOMaterialSpecularExponentMapName = @"specular_exponent_map"; 51 0 : NSString * const kOOMaterialCombinedSpecularMapName = @"specular_map"; // Combined specular_color_map and specular_exponent_map (unfortunate name required for backwards-compatibility). 52 0 : NSString * const kOOMaterialNormalMapName = @"normal_map"; 53 0 : NSString * const kOOMaterialParallaxMapName = @"parallax_map"; 54 0 : NSString * const kOOMaterialNormalAndParallaxMapName = @"normal_and_parallax_map"; 55 0 : NSString * const kOOMaterialEmissionMapName = @"emission_map"; 56 0 : NSString * const kOOMaterialIlluminationMapName = @"illumination_map"; 57 0 : NSString * const kOOMaterialEmissionAndIlluminationMapName = @"emission_and_illumination_map"; 58 : 59 0 : NSString * const kOOMaterialParallaxScaleName = @"parallax_scale"; 60 0 : NSString * const kOOMaterialParallaxBiasName = @"parallax_bias"; 61 : 62 0 : NSString * const kOOMaterialGammaCorrectName = @"gamma_correct"; 63 : 64 0 : NSString * const kOOMaterialGlossName = @"gloss"; 65 : 66 0 : NSString * const kOOMaterialSpecularExponentName = @"specular_exponent"; 67 0 : NSString * const kOOMaterialSpecularExponentLegacyName = @"shininess"; 68 : 69 0 : NSString * const kOOMaterialLightMapsName = @"light_map"; 70 : 71 : 72 : @implementation NSDictionary (OOMateralProperties) 73 : 74 : // Internal. Used to avoid mutual recusion between -oo_specularExponentMapSpecifier and -oo_specularExponent. 75 0 : - (int) oo_rawSpecularExponentValue 76 : { 77 : NSObject *value = [self objectForKey:kOOMaterialSpecularExponentName]; 78 : if (value == nil) value = [self objectForKey:kOOMaterialSpecularExponentLegacyName]; 79 : return OOIntFromObject(value, -1); 80 : } 81 : 82 : 83 : - (OOColor *) oo_diffuseColor 84 : { 85 : OOColor *result = [OOColor colorWithDescription:[self objectForKey:kOOMaterialDiffuseColorName]]; 86 : if (result == nil) result = [OOColor colorWithDescription:[self objectForKey:kOOMaterialDiffuseColorLegacyName]]; 87 : 88 : if ([result isWhite]) result = nil; 89 : return result; 90 : } 91 : 92 : 93 : - (OOColor *) oo_ambientColor 94 : { 95 : OOColor *result = [OOColor colorWithDescription:[self objectForKey:kOOMaterialAmbientColorName]]; 96 : if (result == nil) result = [OOColor colorWithDescription:[self objectForKey:kOOMaterialAmbientColorLegacyName]]; 97 : return result; 98 : } 99 : 100 : 101 : - (OOColor *) oo_specularColor 102 : { 103 : OOColor *result = [OOColor colorWithDescription:[self objectForKey:kOOMaterialSpecularColorName]]; 104 : if (result == nil) result = [OOColor colorWithDescription:[self objectForKey:kOOMaterialSpecularColorLegacyName]]; 105 : if (result == nil) 106 : { 107 : result = [OOColor colorWithWhite:0.2f alpha:1.0f]; 108 : } 109 : return result; 110 : } 111 : 112 : 113 : - (OOColor *) oo_specularModulateColor 114 : { 115 : OOColor *result = [OOColor colorWithDescription:[self objectForKey:kOOMaterialSpecularModulateColorName]]; 116 : if (result == nil) result = [OOColor whiteColor]; 117 : 118 : return result; 119 : } 120 : 121 : 122 : - (OOColor *) oo_emissionColor 123 : { 124 : OOColor *result = [OOColor colorWithDescription:[self objectForKey:kOOMaterialEmissionColorName]]; 125 : if (result == nil) result = [OOColor colorWithDescription:[self objectForKey:kOOMaterialEmissionColorLegacyName]]; 126 : 127 : if ([result isBlack]) result = nil; 128 : return result; 129 : } 130 : 131 : 132 : - (OOColor *) oo_emissionModulateColor 133 : { 134 : OOColor *result = [OOColor colorWithDescription:[self objectForKey:kOOMaterialEmissionModulateColorName]]; 135 : 136 : if ([result isWhite]) result = nil; 137 : return result; 138 : } 139 : 140 : 141 : - (OOColor *) oo_illuminationModulateColor 142 : { 143 : OOColor *result = [OOColor colorWithDescription:[self objectForKey:kOOMaterialIlluminationModulateColorName]]; 144 : 145 : if ([result isWhite]) result = nil; 146 : return result; 147 : } 148 : 149 : 150 : - (NSDictionary *) oo_diffuseMapSpecifierWithDefaultName:(NSString *)name 151 : { 152 : return [self oo_textureSpecifierForKey:kOOMaterialDiffuseMapName defaultName:name]; 153 : } 154 : 155 : 156 : - (NSDictionary *) oo_combinedSpecularMapSpecifier 157 : { 158 : if ([self oo_rawSpecularExponentValue] == 0) return nil; 159 : return [self oo_textureSpecifierForKey:kOOMaterialCombinedSpecularMapName defaultName:nil]; 160 : } 161 : 162 : 163 : - (NSDictionary *) oo_specularColorMapSpecifier 164 : { 165 : if ([self oo_rawSpecularExponentValue] == 0) return nil; 166 : NSDictionary *result = [self oo_textureSpecifierForKey:kOOMaterialSpecularColorMapName defaultName:nil]; 167 : if (result == nil) result = [self oo_combinedSpecularMapSpecifier]; 168 : return result; 169 : } 170 : 171 : 172 : - (NSDictionary *) oo_specularExponentMapSpecifier 173 : { 174 : if ([self oo_rawSpecularExponentValue] == 0) return nil; 175 : NSDictionary *result = [self oo_textureSpecifierForKey:kOOMaterialSpecularExponentMapName defaultName:nil]; 176 : if (result == nil) result = [[self oo_combinedSpecularMapSpecifier] dictionaryByAddingObject:@"a" forKey:@"extract_channel"]; 177 : return result; 178 : } 179 : 180 : 181 : - (NSDictionary *) oo_normalMapSpecifier 182 : { 183 : if ([self oo_normalAndParallaxMapSpecifier] != nil) return nil; 184 : return [self oo_textureSpecifierForKey:kOOMaterialNormalMapName defaultName:nil]; 185 : } 186 : 187 : 188 : - (NSDictionary *) oo_parallaxMapSpecifier 189 : { 190 : id spec = [self oo_textureSpecifierForKey:kOOMaterialParallaxMapName defaultName:nil]; 191 : if (spec == nil) 192 : { 193 : // Default is alpha channel of normal_and_parallax_map. 194 : spec = [[self oo_normalAndParallaxMapSpecifier] dictionaryByAddingObject:@"a" 195 : forKey:@"extract_channel"]; 196 : } 197 : 198 : return spec; 199 : } 200 : 201 : 202 : - (NSDictionary *) oo_normalAndParallaxMapSpecifier 203 : { 204 : return [self oo_textureSpecifierForKey:kOOMaterialNormalAndParallaxMapName defaultName:nil]; 205 : } 206 : 207 : 208 : - (NSDictionary *) oo_emissionMapSpecifier 209 : { 210 : return [self oo_textureSpecifierForKey:kOOMaterialEmissionMapName defaultName:nil]; 211 : } 212 : 213 : 214 : - (NSDictionary *) oo_illuminationMapSpecifier 215 : { 216 : return [self oo_textureSpecifierForKey:kOOMaterialIlluminationMapName defaultName:nil]; 217 : } 218 : 219 : 220 : - (NSDictionary *) oo_emissionAndIlluminationMapSpecifier 221 : { 222 : if ([self oo_emissionMapSpecifier] != nil || [self oo_illuminationMapSpecifier] != nil) return nil; 223 : return [self oo_textureSpecifierForKey:kOOMaterialEmissionAndIlluminationMapName defaultName:nil]; 224 : } 225 : 226 : 227 : - (float) oo_parallaxScale 228 : { 229 : return [self oo_floatForKey:kOOMaterialParallaxScaleName defaultValue:kOOMaterialDefaultParallaxScale]; 230 : } 231 : 232 : 233 : - (float) oo_parallaxBias 234 : { 235 : return [self oo_floatForKey:kOOMaterialParallaxBiasName]; 236 : } 237 : 238 : 239 : - (BOOL) oo_gammaCorrect 240 : { 241 : return [self oo_boolForKey:kOOMaterialGammaCorrectName defaultValue:![[NSUserDefaults standardUserDefaults] boolForKey:@"no-gamma-correct"]]; 242 : } 243 : 244 : 245 : - (float) oo_gloss 246 : { 247 : return OOClamp_0_1_f([self oo_floatForKey:kOOMaterialGlossName defaultValue:0.375f]); 248 : } 249 : 250 : 251 : - (int) oo_specularExponent 252 : { 253 : int result = [self oo_rawSpecularExponentValue]; 254 : if (result < 0) 255 : { 256 : if ([UNIVERSE useShaders] && [self oo_specularExponentMapSpecifier] != nil) 257 : { 258 : result = 128; 259 : } 260 : else 261 : { 262 : result = 10; 263 : } 264 : } 265 : 266 : return result; 267 : } 268 : 269 : @end