Skip to content

Commit

Permalink
Export sun direction.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelTallet committed Dec 1, 2019
1 parent 4f2a19e commit 1523820
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 57 deletions.
6 changes: 6 additions & 0 deletions source/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ Naming/RescuedExceptionsVariableName:
Metrics/AbcSize:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false

Metrics/MethodLength:
Max: 25

Expand Down
2 changes: 1 addition & 1 deletion source/pbr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# PBR plugin namespace.
module PBR

VERSION = '1.5.6'.freeze
VERSION = '1.5.7'.freeze

# Load translation if it's available for current locale.
TRANSLATE = LanguageHandler.new('pbr.strings')
Expand Down
2 changes: 2 additions & 0 deletions source/pbr/Viewport App/viewport.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
<script src="lib/localforage.min.js"></script>
<script src="lib/claygl.modded.js"></script>
<script src="lib/claygl-advanced-renderer.min.js"></script>

<script src="assets/sketchup-sun-dir.json"></script>
<script src="assets/sketchup-locale.json"></script>

<link rel="stylesheet" href="viewport.css"></link>
Expand Down
50 changes: 30 additions & 20 deletions source/pbr/Viewport App/viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ PBR.Viewport.naturalLights = {};
PBR.Viewport.artificialLights = [];

/**
* Viewport glTF model version.
* Viewport data version timestamp.
*
* @type {number}
*/
PBR.Viewport.modelVersion = 0;
PBR.Viewport.dataVersion = 0;

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

// Create a directional light.
PBR.Viewport.naturalLights.direct = app.createDirectionalLight([-1, -1, -1], '#fff', 0.8);
PBR.Viewport.naturalLights.direct = app.createDirectionalLight(
[
// XXX XYZ to -X-ZY
sketchUpSunDir.x * -1,
sketchUpSunDir.z * -1,
sketchUpSunDir.y,
],
'#fff',
0.8
);

PBR.Viewport.naturalLights.direct.shadowResolution = 4096;

// Set HDR background image.
Expand Down Expand Up @@ -383,55 +393,55 @@ PBR.Viewport.listenToCameraReset = function() {
};

/**
* Set model version from URL parameter?
* Set data version from URL parameter?
*/
PBR.Viewport.setModelVersion = function() {
PBR.Viewport.setDataVersion = function() {

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

if ( isNaN(PBR.Viewport.modelVersion) ) {
if ( isNaN(PBR.Viewport.dataVersion) ) {

PBR.Viewport.modelVersion = parseInt(Date.now() / 1000);
PBR.Viewport.dataVersion = parseInt(Date.now() / 1000);

}

};

/**
* Refresh Viewport if a newer version of model is available.
* Refresh Viewport if a newer data version is available.
*/
PBR.Viewport.checkForModelUpdates = function() {
PBR.Viewport.checkForDataUpdates = function() {

var request = new XMLHttpRequest();

var lastModelVersion = 0;
var lastDataVersion = 0;

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

lastModelVersion = parseInt(event.target.response);
lastDataVersion = parseInt(event.target.response);

if ( PBR.Viewport.modelVersion < lastModelVersion ) {
if ( PBR.Viewport.dataVersion < lastDataVersion ) {

// Refresh Viewport.
document.location.search = 'model_ver=' + lastModelVersion;
document.location.search = 'last_data_ver=' + lastDataVersion;

}

});

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

request.send();

};

/**
* Set Viewport "Check Model Updates" interval.
* Set Viewport "Check Data Updates" interval.
*/
PBR.Viewport.setCmuInterval = function() {
PBR.Viewport.setCduInterval = function() {

window.setInterval(
PBR.Viewport.checkForModelUpdates,
PBR.Viewport.checkForDataUpdates,
1000
);

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

PBR.Viewport.setModelVersion();
PBR.Viewport.setDataVersion();

PBR.Viewport.translateStrings();

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

PBR.Viewport.listenToCameraReset();

PBR.Viewport.setCmuInterval();
PBR.Viewport.setCduInterval();

});

Expand Down
5 changes: 5 additions & 0 deletions source/pbr/app_observer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require 'sketchup'
require 'pbr/viewport'
require 'pbr/model_observer'
require 'pbr/sun_observer'

# PBR plugin namespace.
module PBR
Expand All @@ -41,6 +42,8 @@ def onNewModel(model)

model.add_observer(ModelObserver.new)

model.shadow_info.add_observer(SunObserver.new)

end

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

model.add_observer(ModelObserver.new)

model.shadow_info.add_observer(SunObserver.new)

end

# When SketchUp user turns off an extension:
Expand Down
4 changes: 2 additions & 2 deletions source/pbr/gltf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def initialize

Sketchup.status_text = nil

rescue StandardError => _exception
rescue StandardError => exception

@valid = false

Expand All @@ -124,7 +124,7 @@ def initialize

Sketchup.status_text = nil

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

end

Expand Down
2 changes: 2 additions & 0 deletions source/pbr/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require 'sketchup'
require 'pbr/app_observer'
require 'pbr/model_observer'
require 'pbr/sun_observer'
require 'pbr/menu'
require 'pbr/toolbar'
require 'pbr/viewport'
Expand All @@ -35,6 +36,7 @@ module PBR

Sketchup.add_observer(AppObserver.new)
Sketchup.active_model.add_observer(ModelObserver.new)
Sketchup.active_model.shadow_info.add_observer(SunObserver.new)

# Material Editor is not open yet.
SESSION[:material_editor_open?] = false
Expand Down
2 changes: 1 addition & 1 deletion source/pbr/material_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def show

@dialog.close

Viewport.update_model
Viewport.update

end

Expand Down
2 changes: 1 addition & 1 deletion source/pbr/menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def initialize(parent_menu)

return PBR.open_required_plugin_page unless PBR.required_plugin_exist?

Viewport.update_model_and_reopen
Viewport.update_and_reopen

end

Expand Down
2 changes: 1 addition & 1 deletion source/pbr/model_observer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def onTransactionCommit(_model)

SESSION[:last_viewport_update] = Time.now.to_i

Viewport.update_model
Viewport.update

end

Expand Down
54 changes: 54 additions & 0 deletions source/pbr/sun_observer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Physically-Based Rendering extension for SketchUp 2017 or newer.
# Copyright: © 2019 Samuel Tallet <samuel.tallet arobase gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3.0 of the License, or
# (at your option) any later version.
#
# If you release a modified version of this program TO THE PUBLIC,
# the GPL requires you to MAKE THE MODIFIED SOURCE CODE AVAILABLE
# to the program's users, UNDER THE GPL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# Get a copy of the GPL here: https://www.gnu.org/licenses/gpl.html

raise 'The PBR plugin requires at least Ruby 2.2.0 or SketchUp 2017.'\
unless RUBY_VERSION.to_f >= 2.2 # SketchUp 2017 includes Ruby 2.2.4.

require 'sketchup'
require 'pbr/viewport'

# PBR plugin namespace.
module PBR

# Observes SketchUp sun events and reacts.
class SunObserver < Sketchup::ShadowInfoObserver

# rubocop: disable Naming/MethodName

# When user changes shadow settings:
def onShadowInfoChanged(_shadow_info, type)

# Escape if it's not about "Time/Date sliders".
return unless type.zero?

if SESSION[:track_all_changes?]

Viewport.update_sun_direction

Viewport.update_data_version

end

end

# rubocop: enable Naming/MethodName

end

end
2 changes: 1 addition & 1 deletion source/pbr/toolbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def initialize

return PBR.open_required_plugin_page unless PBR.required_plugin_exist?

Viewport.update_model_and_reopen
Viewport.update_and_reopen

end

Expand Down
Loading

0 comments on commit 1523820

Please sign in to comment.