forked from mcneel/RhinoCycles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CyclesShader.cs
659 lines (571 loc) · 19.2 KB
/
CyclesShader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/**
Copyright 2014-2017 Robert McNeel and Associates
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**/
using System;
using ccl;
using Rhino.DocObjects;
using Rhino.Render;
using RhinoCyclesCore.Materials;
using RhinoCyclesCore.Converters;
using System.Collections.Generic;
namespace RhinoCyclesCore
{
/// <summary>
/// Intermediate class to convert various Rhino shader types
/// to Cycles shaders
///
/// @todo better organise shader intermediary code instead of overloading heavily
/// </summary>
public class CyclesShader
{
private ShaderBody _front;
private ShaderBody _back;
public CyclesShader(uint id)
{
Id = id;
_front = null;
_back = null;
}
/// <summary>
/// RenderHash of the RenderMaterial for which this intermediary is created.
/// </summary>
public uint Id { get; }
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as CyclesShader;
return other != null && Id.Equals(other.Id);
}
public bool CreateFrontShader(RenderMaterial rm, float gamma)
{
_front = new ShaderBody(Id);
return CreateShaderPart(_front, rm, gamma);
}
/// <summary>
/// Sets up a shader with Front set to a default ShaderBody. Used for CodeShader
/// </summary>
public void SetupShaderShim()
{
_front = new ShaderBody(Id);
}
public void FrontXmlShader(string name, ICyclesMaterial crm)
{
_front = new ShaderBody(Id)
{
Name = name,
Crm = crm,
CyclesMaterialType = ShaderBody.CyclesMaterial.Xml
};
}
public bool CreateBackShader(RenderMaterial rm, float gamma)
{
_back = new ShaderBody(Id);
return CreateShaderPart(_back, rm, gamma);
}
public ShaderBody Front => _front;
public ShaderBody Back => _back;
public bool DisplayMaterial => _front != null && _back != null;
public bool ValidDisplayMaterial =>
_front?.CyclesMaterialType != ShaderBody.CyclesMaterial.Xml
&&
_back?.CyclesMaterialType != ShaderBody.CyclesMaterial.Xml;
public float Gamma { get; set; }
private enum ProbableMaterial
{
Plaster,
Picture,
Paint,
Glass,
Gem,
Plastic,
Metal,
Custom
}
private static ProbableMaterial WhatMaterial(RenderMaterial rm, Rhino.DocObjects.Material m)
{
if (rm.TypeId.Equals(RenderMaterial.PictureMaterialGuid))
{
return ProbableMaterial.Picture;
}
if (rm.TypeId.Equals(RenderMaterial.PlasterMaterialGuid))
{
return ProbableMaterial.Plaster;
}
if (rm.TypeId.Equals(RenderMaterial.GlassMaterialGuid))
{
return ProbableMaterial.Glass;
}
if (rm.TypeId.Equals(RenderMaterial.GemMaterialGuid))
{
return ProbableMaterial.Gem;
}
if (rm.TypeId.Equals(RenderMaterial.PaintMaterialGuid))
{
return ProbableMaterial.Paint;
}
if (rm.TypeId.Equals(RenderMaterial.PlasticMaterialGuid))
{
return ProbableMaterial.Plastic;
}
if (rm.TypeId.Equals(RenderMaterial.MetalMaterialGuid))
{
return ProbableMaterial.Metal;
}
if (rm.SmellsLikePlaster || rm.SmellsLikeTexturedPlaster)
{
return ProbableMaterial.Plaster;
}
if (rm.SmellsLikeGlass || rm.SmellsLikeTexturedGlass)
{
return ProbableMaterial.Glass;
}
if (rm.SmellsLikeGem || rm.SmellsLikeTexturedGem)
{
return ProbableMaterial.Gem;
}
if (rm.SmellsLikePaint || rm.SmellsLikeTexturedPaint)
{
return ProbableMaterial.Paint;
}
if (rm.SmellsLikePlastic || rm.SmellsLikeTexturedPlastic)
{
return ProbableMaterial.Plastic;
}
if (rm.SmellsLikeMetal || rm.SmellsLikeTexturedMetal)
{
return ProbableMaterial.Metal;
}
return ProbableMaterial.Custom;
}
private bool CreateShaderPart(ShaderBody shb, RenderMaterial rm, float gamma)
{
var crm = rm as ICyclesMaterial;
ShaderBody.CyclesMaterial mattype = ShaderBody.CyclesMaterial.No;
if (crm == null)
{
// always simulate material, need to know now myself
// what to read out from the simulated material to
// populate my own material descriptions.
var m = rm.SimulateMaterial(true);
// figure out what type of material we are.
//var probemat = GuessMaterialFromSmell(rm);
var probemat = WhatMaterial(rm, m);
rm.BeginChange(RenderContent.ChangeContexts.Ignore);
var dcl = m.DiffuseColor;
var scl = m.SpecularColor;
var rcl = m.ReflectionColor;
var rfcl = m.TransparentColor;
var emcl = m.EmissionColor;
var reflectivity = (float)m.Reflectivity;
var metalic = 0f;
var shine = (float)(m.Shine / Material.MaxShine);
switch (probemat)
{
case ProbableMaterial.Plaster:
mattype = ShaderBody.CyclesMaterial.Diffuse;
break;
case ProbableMaterial.Glass:
case ProbableMaterial.Gem:
metalic = 0f;
mattype = ShaderBody.CyclesMaterial.Glass;
break;
case ProbableMaterial.Metal:
metalic = 1.0f;
mattype = ShaderBody.CyclesMaterial.SimpleMetal;
break;
case ProbableMaterial.Plastic:
//polish = reflectivity;
//shine = polish;
//reflectivity = 0f;
metalic = 0f;
mattype = ShaderBody.CyclesMaterial.SimplePlastic;
break;
case ProbableMaterial.Paint:
mattype = ShaderBody.CyclesMaterial.Paint;
break;
case ProbableMaterial.Custom:
mattype = ShaderBody.CyclesMaterial.No;
break;
}
var difftexAlpha = m.AlphaTransparency;
var col = RenderEngine.CreateFloat4(dcl.R, dcl.G, dcl.B, 255);
var spec = RenderEngine.CreateFloat4(scl.R, scl.G, scl.B, 255);
var refl = RenderEngine.CreateFloat4(rcl.R, rcl.G, rcl.B, 255);
var transp = RenderEngine.CreateFloat4(rfcl.R, rfcl.G, rfcl.B, 255);
var refr = RenderEngine.CreateFloat4(rfcl.R, rfcl.G, rfcl.B, 255);
var emis = RenderEngine.CreateFloat4(emcl.R, emcl.G, emcl.B, 255);
//shb.Type = CyclesShader.Shader.Diffuse,
shb.CyclesMaterialType = mattype;
shb.Shadeless = m.DisableLighting;
shb.DiffuseColor = col;
shb.SpecularColor = spec;
shb.ReflectionColor = refl;
shb.ReflectionRoughness = (float)m.ReflectionGlossiness;
shb.RefractionColor = refr;
shb.RefractionRoughness = (float)m.RefractionGlossiness;
shb.TransparencyColor = transp;
shb.EmissionColor = emis;
shb.FresnelIOR = (float)m.FresnelIndexOfRefraction;
shb.IOR = (float)m.IndexOfRefraction;
shb.Roughness = (float)m.ReflectionGlossiness;
shb.Reflectivity = reflectivity;
shb.Metallic = metalic;
shb.Transparency = (float)m.Transparency;
shb.Shine = shine;
shb.Gloss = (float)m.ReflectionGlossiness;
shb.FresnelReflections = m.FresnelReflections;
shb.Gamma = gamma;
shb.Name = m.Name ?? "";
shb.DiffuseTexture.Amount = 0.0f;
shb.BumpTexture.Amount = 0.0f;
shb.TransparencyTexture.Amount = 0.0f;
shb.EnvironmentTexture.Amount = 0.0f;
if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Diffuse))
{
var difftex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Diffuse);
BitmapConverter.MaterialBitmapFromEvaluator(ref shb, difftex, RenderMaterial.StandardChildSlots.Diffuse);
if (shb.HasDiffuseTexture)
{
shb.DiffuseTexture.UseAlpha = difftexAlpha;
shb.DiffuseTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Diffuse) / 100.0f, 1.0f);
}
}
if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Bump))
{
var bumptex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Bump);
BitmapConverter.MaterialBitmapFromEvaluator(ref shb, bumptex, RenderMaterial.StandardChildSlots.Bump);
if (shb.HasBumpTexture)
{
shb.BumpTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Bump) / 100.0f, 1.0f);
}
}
if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Transparency))
{
var transtex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Transparency);
BitmapConverter.MaterialBitmapFromEvaluator(ref shb, transtex,
RenderMaterial.StandardChildSlots.Transparency);
if (shb.HasTransparencyTexture)
{
shb.TransparencyTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Transparency) / 100.0f, 1.0f);
}
}
if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Environment))
{
var envtex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Environment);
BitmapConverter.MaterialBitmapFromEvaluator(ref shb, envtex,
RenderMaterial.StandardChildSlots.Environment);
if (shb.HasEnvironmentTexture)
{
shb.EnvironmentTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Environment) / 100.0f, 1.0f);
}
}
rm.EndChange();
}
else
{
if (crm.MaterialType == ShaderBody.CyclesMaterial.CustomRenderMaterial)
{
crm.Gamma = gamma;
crm.BakeParameters();
shb.Crm = crm;
shb.CyclesMaterialType = ShaderBody.CyclesMaterial.CustomRenderMaterial;
shb.Gamma = gamma;
shb.Name = rm.Name ?? "Cycles custom material";
}
else
{
crm.Gamma = gamma;
crm.BakeParameters();
shb.Crm = crm;
shb.CyclesMaterialType = ShaderBody.CyclesMaterial.Xml;
shb.Gamma = gamma;
shb.Name = rm.Name ?? "some cycles material";
}
}
return true;
}
/// <summary>
/// Type of shader this represents.
/// </summary>
public enum Shader
{
Background,
Diffuse
}
public Shader Type { get; set; }
/// <summary>
/// A shader should override this function if it needs to reload textures.
///
/// Textures change after i.e. gamma changes.
/// </summary>
/// <param name="bytes"></param>
/// <param name="floats"></param>
public void ReloadTextures(Dictionary<uint, ByteBitmap> bytes, Dictionary<uint, FloatBitmap> floats)
{
_front?.ReloadTextures(bytes, floats);
_back?.ReloadTextures(bytes, floats);
}
}
public class ShaderBody
{
public uint Id { get; }
public ShaderBody(uint id)
{
Id = id;
DiffuseTexture = new CyclesTextureImage();
BumpTexture = new CyclesTextureImage();
TransparencyTexture = new CyclesTextureImage();
EnvironmentTexture = new CyclesTextureImage();
GiEnvTexture = new CyclesTextureImage();
BgEnvTexture = new CyclesTextureImage();
ReflRefrEnvTexture = new CyclesTextureImage();
CyclesMaterialType = CyclesMaterial.No;
}
public ICyclesMaterial Crm { get; set; }
/// <summary>
/// Set the CyclesMaterial type
/// </summary>
public CyclesMaterial CyclesMaterialType { get; set; }
/// <summary>
/// Enumeration of Cycles custom materials.
///
/// Note: don't forget to update this enumeration for each
/// custom material that is added.
///
/// Enumeration for both material and background (world)
/// shaders.
/// </summary>
public enum CyclesMaterial
{
/// <summary>
/// No is used when the material isn't a Cycles material
/// </summary>
No,
Xml,
CustomRenderMaterial,
Brick,
Test,
FlakedCarPaint,
BrickCheckeredMortar,
Translucent,
PhongTest,
Glass,
Diffuse,
Paint,
SimplePlastic,
SimpleMetal,
Emissive,
VertexColor,
SimpleNoiseEnvironment,
XmlEnvironment,
}
/// <summary>
/// A shader should override this function if it needs to reload textures.
///
/// Textures change after i.e. gamma changes.
/// </summary>
/// <param name="bytes"></param>
/// <param name="floats"></param>
public void ReloadTextures(Dictionary<uint, ByteBitmap> bytes, Dictionary<uint, FloatBitmap> floats)
{
if (HasDiffuseTexture)
{
LoadOneTexture(DiffuseTexture, bytes, floats);
}
if (HasBumpTexture)
{
LoadOneTexture(BumpTexture, bytes, floats);
}
if (HasTransparencyTexture)
{
LoadOneTexture(TransparencyTexture, bytes, floats);
}
if (HasEnvironmentTexture)
{
LoadOneTexture(EnvironmentTexture, bytes, floats);
}
if (HasGiEnvTexture)
{
LoadOneTexture(GiEnvTexture, bytes, floats);
}
if (HasBgEnvTexture)
{
LoadOneTexture(BgEnvTexture, bytes, floats);
}
if (HasReflRefrEnvTexture)
{
LoadOneTexture(ReflRefrEnvTexture, bytes, floats);
}
}
static private void LoadOneTexture(CyclesTextureImage tex, Dictionary<uint, ByteBitmap> bytes, Dictionary<uint, FloatBitmap> floats)
{
uint rid;
if (uint.TryParse(tex.Name, out rid))
{
if (tex.HasByteImage)
{
if (bytes.ContainsKey(rid))
{
tex.TexByte = bytes[rid].Data;
}
}
else if (tex.HasFloatImage)
{
if (floats.ContainsKey(rid))
{
tex.TexFloat = floats[rid].Data;
}
}
}
}
/// <summary>
/// Set to true if a shadeless effect is wanted (self-illuminating).
/// </summary>
public bool Shadeless { get; set; }
/// <summary>
/// Get <c>Shadeless</c> as a float value
/// </summary>
public float ShadelessAsFloat => Shadeless ? 1.0f : 0.0f;
/// <summary>
/// Gamma corrected base color
/// </summary>
public float4 BaseColor
{
get
{
float4 c = DiffuseColor;
switch (CyclesMaterialType)
{
case CyclesMaterial.SimpleMetal:
c = ReflectionColor;
break;
case CyclesMaterial.Glass:
c = TransparencyColor;
break;
default:
c = DiffuseColor;
break;
}
var gcc = c ^ Gamma;
return gcc;
}
}
public float4 DiffuseColor { get; set; } = new float4();
public bool HasOnlyDiffuseColor => !HasDiffuseTexture
&& !HasBumpTexture
&& !HasTransparencyTexture
&& !HasEmission
&& !Shadeless
&& NoTransparency
&& NoReflectivity;
public bool HasOnlyDiffuseTexture => HasDiffuseTexture
&& !HasBumpTexture
&& !HasTransparencyTexture
&& !HasEmission
&& !Shadeless
&& NoTransparency
&& NoReflectivity;
public bool DiffuseAndBumpTexture => HasDiffuseTexture
&& HasBumpTexture
&& !HasTransparencyTexture
&& !HasEmission
&& !Shadeless
&& NoTransparency
&& NoReflectivity;
public bool HasOnlyReflectionColor => HasReflectivity
&& !HasDiffuseTexture
&& !HasEmission
&& !Shadeless
&& NoTransparency
&& !HasTransparency
&& !HasBumpTexture;
public float4 SpecularColor { get; set; } = new float4();
public float4 SpecularColorGamma => SpecularColor ^ Gamma;
public float4 ReflectionColor { get; set; } = new float4();
public float4 ReflectionColorGamma => ReflectionColor ^ Gamma;
public float ReflectionRoughness { get; set; }
public float ReflectionRoughnessPow2 => ReflectionRoughness * ReflectionRoughness;
public float4 RefractionColor { get; set; } = new float4();
public float4 RefractionColorGamma => RefractionColor ^ Gamma;
public float RefractionRoughness { get; set; }
public float RefractionRoughnessPow2 => RefractionRoughness * RefractionRoughness;
public float4 TransparencyColor { get; set; } = new float4();
public float4 TransparencyColorGamma => TransparencyColor ^ Gamma;
public float4 EmissionColor { get; set; } = new float4();
public float4 EmissionColorGamma => EmissionColor ^ Gamma;
public bool HasEmission => !EmissionColor.IsZero(false);
public CyclesTextureImage DiffuseTexture { get; set; }
public bool HasDiffuseTexture => DiffuseTexture.HasTextureImage;
public float HasDiffuseTextureAsFloat => HasDiffuseTexture ? 1.0f : 0.0f;
public CyclesTextureImage BumpTexture { get; set; }
public bool HasBumpTexture => BumpTexture.HasTextureImage;
public float HasBumpTextureAsFloat => HasBumpTexture ? 1.0f : 0.0f;
public CyclesTextureImage TransparencyTexture { get; set; }
public bool HasTransparencyTexture => TransparencyTexture.HasTextureImage;
public float HasTransparencyTextureAsFloat => HasTransparencyTexture ? 1.0f : 0.0f;
public CyclesTextureImage EnvironmentTexture { get; set; }
public bool HasEnvironmentTexture => EnvironmentTexture.HasTextureImage;
public float HasEnvironmentTextureAsFloat => HasEnvironmentTexture ? 1.0f : 0.0f;
public CyclesTextureImage GiEnvTexture { get; set; }
public bool HasGiEnvTexture => GiEnvTexture.HasTextureImage;
public float4 GiEnvColor { get; set; } = new float4();
public bool HasGiEnv => HasGiEnvTexture || GiEnvColor != null;
public CyclesTextureImage BgEnvTexture { get; set; }
public bool HasBgEnvTexture => BgEnvTexture.HasTextureImage;
public float4 BgEnvColor { get; set; } = new float4();
public bool HasBgEnv => HasBgEnvTexture || BgEnvColor != null;
public CyclesTextureImage ReflRefrEnvTexture { get; set; }
public bool HasReflRefrEnvTexture => ReflRefrEnvTexture.HasTextureImage;
public float4 ReflRefrEnvColor { get; set; } = new float4();
public bool HasReflRefrEnv => HasReflRefrEnvTexture || ReflRefrEnvColor != null;
public bool HasUV { get; set; }
public float FresnelIOR { get; set; }
public float IOR { get; set; }
public float Roughness { get; set; }
public float Reflectivity { get; set; }
public float Specular => Reflectivity;
public float SpecularTint => ReflectivityInverse;
public float ReflectivityInverse => 1.0f - Reflectivity;
public float Sheen => Reflectivity;
public float SheenTint => ReflectivityInverse;
public float ClearCoat => NoMetalic ? Reflectivity : 0.0f;
public float ClearCoatGloss => ClearCoat;
public float Metallic { get; set; }
public bool NoMetalic => Math.Abs(Metallic) < 0.00001f;
public float Shine { get; set; }
public float Gloss { get; set; }
public float Transparency { get; set; }
public bool NoTransparency => Math.Abs(Transparency) < 0.00001f;
public bool HasTransparency => !NoTransparency;
public bool NoReflectivity => Math.Abs(Reflectivity) < 0.00001f;
public bool HasReflectivity => !NoReflectivity;
private float m_gamma;
public float Gamma
{
get { return m_gamma; }
set
{
m_gamma = value;
if (Crm != null)
{
Crm.Gamma = m_gamma;
}
}
}
public bool FresnelReflections { get; set; }
public float FresnelReflectionsAsFloat => FresnelReflections ? 1.0f : 0.0f;
public string Name { get; set; }
}
}