Skip to content

Commit 1523820

Browse files
committed
Export sun direction.
1 parent 4f2a19e commit 1523820

File tree

13 files changed

+177
-57
lines changed

13 files changed

+177
-57
lines changed

source/.rubocop.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ Naming/RescuedExceptionsVariableName:
1717
Metrics/AbcSize:
1818
Enabled: false
1919

20+
Metrics/CyclomaticComplexity:
21+
Enabled: false
22+
23+
Metrics/PerceivedComplexity:
24+
Enabled: false
25+
2026
Metrics/MethodLength:
2127
Max: 25
2228

source/pbr.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# PBR plugin namespace.
2727
module PBR
2828

29-
VERSION = '1.5.6'.freeze
29+
VERSION = '1.5.7'.freeze
3030

3131
# Load translation if it's available for current locale.
3232
TRANSLATE = LanguageHandler.new('pbr.strings')

source/pbr/Viewport App/viewport.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<script src="lib/localforage.min.js"></script>
1313
<script src="lib/claygl.modded.js"></script>
1414
<script src="lib/claygl-advanced-renderer.min.js"></script>
15+
16+
<script src="assets/sketchup-sun-dir.json"></script>
1517
<script src="assets/sketchup-locale.json"></script>
1618

1719
<link rel="stylesheet" href="viewport.css"></link>

source/pbr/Viewport App/viewport.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ PBR.Viewport.naturalLights = {};
101101
PBR.Viewport.artificialLights = [];
102102

103103
/**
104-
* Viewport glTF model version.
104+
* Viewport data version timestamp.
105105
*
106106
* @type {number}
107107
*/
108-
PBR.Viewport.modelVersion = 0;
108+
PBR.Viewport.dataVersion = 0;
109109

110110
/**
111111
* Helper function to convert HTML colors.
@@ -198,7 +198,17 @@ localforage.getItem('cameraPosition').then(function(cameraPosition) {
198198
PBR.Viewport.naturalLights.specular = ambientLight.specular;
199199

200200
// Create a directional light.
201-
PBR.Viewport.naturalLights.direct = app.createDirectionalLight([-1, -1, -1], '#fff', 0.8);
201+
PBR.Viewport.naturalLights.direct = app.createDirectionalLight(
202+
[
203+
// XXX XYZ to -X-ZY
204+
sketchUpSunDir.x * -1,
205+
sketchUpSunDir.z * -1,
206+
sketchUpSunDir.y,
207+
],
208+
'#fff',
209+
0.8
210+
);
211+
202212
PBR.Viewport.naturalLights.direct.shadowResolution = 4096;
203213

204214
// Set HDR background image.
@@ -383,55 +393,55 @@ PBR.Viewport.listenToCameraReset = function() {
383393
};
384394

385395
/**
386-
* Set model version from URL parameter?
396+
* Set data version from URL parameter?
387397
*/
388-
PBR.Viewport.setModelVersion = function() {
398+
PBR.Viewport.setDataVersion = function() {
389399

390-
PBR.Viewport.modelVersion = parseInt(document.location.search.replace(/\D/g, ''));
400+
PBR.Viewport.dataVersion = parseInt(document.location.search.replace(/\D/g, ''));
391401

392-
if ( isNaN(PBR.Viewport.modelVersion) ) {
402+
if ( isNaN(PBR.Viewport.dataVersion) ) {
393403

394-
PBR.Viewport.modelVersion = parseInt(Date.now() / 1000);
404+
PBR.Viewport.dataVersion = parseInt(Date.now() / 1000);
395405

396406
}
397407

398408
};
399409

400410
/**
401-
* Refresh Viewport if a newer version of model is available.
411+
* Refresh Viewport if a newer data version is available.
402412
*/
403-
PBR.Viewport.checkForModelUpdates = function() {
413+
PBR.Viewport.checkForDataUpdates = function() {
404414

405415
var request = new XMLHttpRequest();
406416

407-
var lastModelVersion = 0;
417+
var lastDataVersion = 0;
408418

409419
request.addEventListener('load', function(event) {
410420

411-
lastModelVersion = parseInt(event.target.response);
421+
lastDataVersion = parseInt(event.target.response);
412422

413-
if ( PBR.Viewport.modelVersion < lastModelVersion ) {
423+
if ( PBR.Viewport.dataVersion < lastDataVersion ) {
414424

415425
// Refresh Viewport.
416-
document.location.search = 'model_ver=' + lastModelVersion;
426+
document.location.search = 'last_data_ver=' + lastDataVersion;
417427

418428
}
419429

420430
});
421431

422-
request.open('GET', 'assets/sketchup-model.ver');
432+
request.open('GET', 'assets/sketchup-data.version');
423433

424434
request.send();
425435

426436
};
427437

428438
/**
429-
* Set Viewport "Check Model Updates" interval.
439+
* Set Viewport "Check Data Updates" interval.
430440
*/
431-
PBR.Viewport.setCmuInterval = function() {
441+
PBR.Viewport.setCduInterval = function() {
432442

433443
window.setInterval(
434-
PBR.Viewport.checkForModelUpdates,
444+
PBR.Viewport.checkForDataUpdates,
435445
1000
436446
);
437447

@@ -440,7 +450,7 @@ PBR.Viewport.setCmuInterval = function() {
440450
// When document is ready:
441451
document.addEventListener('DOMContentLoaded', function() {
442452

443-
PBR.Viewport.setModelVersion();
453+
PBR.Viewport.setDataVersion();
444454

445455
PBR.Viewport.translateStrings();
446456

@@ -450,7 +460,7 @@ document.addEventListener('DOMContentLoaded', function() {
450460

451461
PBR.Viewport.listenToCameraReset();
452462

453-
PBR.Viewport.setCmuInterval();
463+
PBR.Viewport.setCduInterval();
454464

455465
});
456466

source/pbr/app_observer.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
require 'sketchup'
2424
require 'pbr/viewport'
2525
require 'pbr/model_observer'
26+
require 'pbr/sun_observer'
2627

2728
# PBR plugin namespace.
2829
module PBR
@@ -41,6 +42,8 @@ def onNewModel(model)
4142

4243
model.add_observer(ModelObserver.new)
4344

45+
model.shadow_info.add_observer(SunObserver.new)
46+
4447
end
4548

4649
# When SketchUp user opens an existing model:
@@ -52,6 +55,8 @@ def onOpenModel(model)
5255

5356
model.add_observer(ModelObserver.new)
5457

58+
model.shadow_info.add_observer(SunObserver.new)
59+
5560
end
5661

5762
# When SketchUp user turns off an extension:

source/pbr/gltf.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def initialize
114114

115115
Sketchup.status_text = nil
116116

117-
rescue StandardError => _exception
117+
rescue StandardError => exception
118118

119119
@valid = false
120120

@@ -124,7 +124,7 @@ def initialize
124124

125125
Sketchup.status_text = nil
126126

127-
puts 'glTF export failed because: ' + _exception.to_s
127+
puts 'glTF export failed because: ' + exception.to_s
128128

129129
end
130130

source/pbr/load.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
require 'sketchup'
2525
require 'pbr/app_observer'
2626
require 'pbr/model_observer'
27+
require 'pbr/sun_observer'
2728
require 'pbr/menu'
2829
require 'pbr/toolbar'
2930
require 'pbr/viewport'
@@ -35,6 +36,7 @@ module PBR
3536

3637
Sketchup.add_observer(AppObserver.new)
3738
Sketchup.active_model.add_observer(ModelObserver.new)
39+
Sketchup.active_model.shadow_info.add_observer(SunObserver.new)
3840

3941
# Material Editor is not open yet.
4042
SESSION[:material_editor_open?] = false

source/pbr/material_editor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def show
162162

163163
@dialog.close
164164

165-
Viewport.update_model
165+
Viewport.update
166166

167167
end
168168

source/pbr/menu.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def initialize(parent_menu)
128128

129129
return PBR.open_required_plugin_page unless PBR.required_plugin_exist?
130130

131-
Viewport.update_model_and_reopen
131+
Viewport.update_and_reopen
132132

133133
end
134134

source/pbr/model_observer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def onTransactionCommit(_model)
4646

4747
SESSION[:last_viewport_update] = Time.now.to_i
4848

49-
Viewport.update_model
49+
Viewport.update
5050

5151
end
5252

source/pbr/sun_observer.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Physically-Based Rendering extension for SketchUp 2017 or newer.
2+
# Copyright: © 2019 Samuel Tallet <samuel.tallet arobase gmail.com>
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU General Public License as published by
6+
# the Free Software Foundation, either version 3.0 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# If you release a modified version of this program TO THE PUBLIC,
10+
# the GPL requires you to MAKE THE MODIFIED SOURCE CODE AVAILABLE
11+
# to the program's users, UNDER THE GPL.
12+
#
13+
# This program is distributed in the hope that it will be useful,
14+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
# General Public License for more details.
17+
#
18+
# Get a copy of the GPL here: https://www.gnu.org/licenses/gpl.html
19+
20+
raise 'The PBR plugin requires at least Ruby 2.2.0 or SketchUp 2017.'\
21+
unless RUBY_VERSION.to_f >= 2.2 # SketchUp 2017 includes Ruby 2.2.4.
22+
23+
require 'sketchup'
24+
require 'pbr/viewport'
25+
26+
# PBR plugin namespace.
27+
module PBR
28+
29+
# Observes SketchUp sun events and reacts.
30+
class SunObserver < Sketchup::ShadowInfoObserver
31+
32+
# rubocop: disable Naming/MethodName
33+
34+
# When user changes shadow settings:
35+
def onShadowInfoChanged(_shadow_info, type)
36+
37+
# Escape if it's not about "Time/Date sliders".
38+
return unless type.zero?
39+
40+
if SESSION[:track_all_changes?]
41+
42+
Viewport.update_sun_direction
43+
44+
Viewport.update_data_version
45+
46+
end
47+
48+
end
49+
50+
# rubocop: enable Naming/MethodName
51+
52+
end
53+
54+
end

source/pbr/toolbar.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def initialize
105105

106106
return PBR.open_required_plugin_page unless PBR.required_plugin_exist?
107107

108-
Viewport.update_model_and_reopen
108+
Viewport.update_and_reopen
109109

110110
end
111111

0 commit comments

Comments
 (0)