package X3dForAdvancedModeling.TextureMapping;
/*
Copyright (c) 1995-2022 held by the author(s). All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of the Web3D Consortium (https://www.web3d.org)
nor the names of its contributors may be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
import org.web3d.x3d.jsail.Core.*;
import org.web3d.x3d.jsail.EnvironmentalEffects.*;
import org.web3d.x3d.jsail.fields.*;
import org.web3d.x3d.jsail.Geometry3D.*;
import org.web3d.x3d.jsail.Grouping.*;
import org.web3d.x3d.jsail.Navigation.*;
import org.web3d.x3d.jsail.Networking.*;
import org.web3d.x3d.jsail.PointingDeviceSensor.*;
import org.web3d.x3d.jsail.Rendering.*;
import org.web3d.x3d.jsail.Scripting.*;
import org.web3d.x3d.jsail.Shape.*;
import org.web3d.x3d.jsail.Text.*;
import org.web3d.x3d.jsail.Texturing.*;
// Javadoc annotations follow, see below for Java source code.
/**
*
View a given texture from different angles to judge readability, example use is a QR code.
Related links: TextureAngleViewer.java source, TextureAngleViewer catalog page, X3D Resources, X3D Scene Authoring Hints, and X3D Tooltips.
This program uses the
X3D Java Scene Access Interface Library (X3DJSAIL).
It has been produced using the
X3dToJava.xslt
stylesheet to create Java source code from an .x3d
model.
* @author Don Brutzman
*/
public class TextureAngleViewer
{
/** Default constructor to create this object. */
public TextureAngleViewer ()
{
initialize();
}
/** Create and initialize the X3D model for this object. */
public final void initialize()
{
x3dModel = new X3D().setProfile(X3D.PROFILE_IMMERSIVE).setVersion(X3D.VERSION_3_3)
.setHead(new head()
.addMeta(new meta().setName(meta.NAME_TITLE ).setContent("TextureAngleViewer.x3d"))
.addMeta(new meta().setName(meta.NAME_DESCRIPTION).setContent("View a given texture from different angles to judge readability, example use is a QR code."))
.addMeta(new meta().setName(meta.NAME_CREATOR ).setContent("Don Brutzman"))
.addMeta(new meta().setName(meta.NAME_CREATED ).setContent("23 April 2013"))
.addMeta(new meta().setName(meta.NAME_MODIFIED ).setContent("20 April 2020"))
.addMeta(new meta().setName(meta.NAME_TODO ).setContent("color mapper in scene for varying Background contrast"))
.addMeta(new meta().setName(meta.NAME_TODO ).setContent("second X-axis angle slider"))
.addMeta(new meta().setName(meta.NAME_IMAGE ).setContent("TextureAngleViewer36degrees.png"))
.addMeta(new meta().setName(meta.NAME_IMAGE ).setContent("images/QrNpsEduCode.png"))
.addMeta(new meta().setName(meta.NAME_REFERENCE ).setContent("http://qr.nps.edu"))
.addMeta(new meta().setName(meta.NAME_REFERENCE ).setContent("https://zxing.appspot.com/generator"))
.addMeta(new meta().setName(meta.NAME_IDENTIFIER ).setContent("https://x3dgraphics.com/examples/X3dForAdvancedModeling/TextureMapping/TextureAngleViewer.x3d"))
.addMeta(new meta().setName(meta.NAME_GENERATOR ).setContent("X3D-Edit 3.3, https://savage.nps.edu/X3D-Edit"))
.addMeta(new meta().setName(meta.NAME_LICENSE ).setContent("../license.html")))
.setScene(new Scene()
.addComments(" ================================ ")
.addChild(new WorldInfo().setTitle("Texture Angle Viewer"))
.addChild(new Background().setSkyColor(new MFColor(new double[] {0.72549,1.0,0.721569})))
.addChild(new Viewpoint().setDescription("Texture angle viewer").setPosition(0.0,0.0,11.0))
.addChild(new NavigationInfo("NavigationInfoFinal").setType("\"NONE\""))
.addComments(" ")
.addComments(" ================================ ")
.addChild(new Transform().setTranslation(0.0,3.6,0.0)
.addChild(new Shape()
.setGeometry(new Text("AngleOutputText").setString(new String[] {"Texture angle viewer"})
.setFontStyle(new FontStyle().setJustify(FontStyle.JUSTIFY_MIDDLE_MIDDLE).setSize(0.7)))
.setAppearance(new Appearance()
.setMaterial(new Material().setDiffuseColor(0.2,0.4,0.8)))))
.addComments(" ================================ ")
.addChild(new Anchor().setDescription("this QR code links to http://qr.nps.edu").setUrl(new String[] {"http://qr.nps.edu"})
.addChild(new Transform("ImageTransform")
.addChild(new Shape()
.addComments(" TODO add a single geometry node here ")
.setGeometry(new IndexedFaceSet().setCoordIndex(new int[] {1,0,2,3,0,-1})
.setCoord(new Coordinate().setPoint(new MFVec3f(new double[] {-3.0,-3.0,0.0,3.0,-3.0,0.0,3.0,3.0,0.0,-3.0,3.0,0.0,-3.0,-3.0,0.0}))))
.setAppearance(new Appearance()
.setMaterial(new Material())
.setTexture(new ImageTexture().setUrl(new String[] {"images/QrNpsEduCode.png","https://x3dgraphics.com/examples/X3dForAdvancedModeling/TextureMapping/images/QrNpsEduCode.png"}))))))
.addComments(" ================================ ")
.addChild(new Transform("SliderSituated").setTranslation(0.0,-3.6,0.0)
.addChild(new Transform("SliderKnobTransform")
.addChild(new PlaneSensor("SliderPlaneSensor").setDescription("select and drag to change angle values").setMaxPosition(3.0,0.0).setMinPosition(-3.0,0.0))
.addChild(new Transform().setRotation(0.0,0.0,1.0,1.570796)
.addChild(new Shape()
.setGeometry(new Cylinder("SliderKnob").setHeight(0.2).setRadius(0.2))
.setAppearance(new Appearance("Metals13Appearance")
.setMaterial(new Material().setAmbientIntensity(0.25641).setDiffuseColor(0.222308,0.15428,0.0).setSpecularColor(0.882653,0.860832,0.687861)
.addComments(" Universal Media Library: Metals 13 "))))))
.addComments(" SliderBar does not rotate or drag with SliderKnob ")
.addChild(new Transform("SliderBar").setRotation(0.0,0.0,1.0,1.570796)
.addChild(new Shape()
.setGeometry(new Cylinder().setHeight(6).setRadius(0.05))
.setAppearance(new Appearance().setUSE("Metals13Appearance")))))
.addComments(" ================================ ")
.addChild(new Script("SliderScript").setSourceCode("""
ecmascript:
function set_translation (eventValue)
{
x = eventValue.x;
if (tracePrint)
{
Browser.println ("========================");
Browser.println (' x=' + x);
}
// clamp values to prevent overrun/underrun, in case minPosition/maxPosition fail
if (x > 3.0) x = 3.0;
if (x < -3.0) x = -3.0;
translation_changed = eventValue;
angle = Math.round(x * 90.0 / 3.0);
rotation_changed = new SFRotation (0, 1, 0, angle * Math.PI / 180.0);
var angleString = angle.toString() + ' degrees'; // JavaScript string
// angleMFString_changed = new MFString (new SFString(angleString)); // only works in InstantReality
angleMFString_changed = new MFString (angleString); // works in several players but not all
// angleMFString_changed = [ angleString ]; // Use JavaScript string array instead of MFString doesn't work
if (tracePrint)
{
Browser.println (' clamped x=' + x);
Browser.println (" set_translation=" + eventValue);
Browser.println ("translation_changed=" + translation_changed.toString() + ", rotation_changed=" + rotation_changed.toString());
Browser.println ("angle=" + angle + ", angleString=" + angleString + ", angleMFString_changed=" + angleMFString_changed.toString());
}
/*
example console excerpt:
========================
x=-0.05248255282640457
clamped x=-0.05248255282640457
set_translation=-0.05248255282640457 0 0
translation_changed=-0.05248255282640457 0 0, rotation_changed=0 1 0 -0.03490658503988659
angle=-2, angleString=-2 degrees, angleMFString_changed="-2 degrees"
========================
*/
}
""")
.addField(new field().setName("set_translation").setType(field.TYPE_SFVEC3F).setAccessType(field.ACCESSTYPE_INPUTONLY).setAppinfo("input provided by SliderKnobTransform PlaneSensor output"))
.addField(new field().setName("angle").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY).setValue(0.0))
.addField(new field().setName("y").setType(field.TYPE_SFFLOAT).setAccessType(field.ACCESSTYPE_INITIALIZEONLY).setValue(0.0))
.addField(new field().setName("rotation_changed").setType(field.TYPE_SFROTATION).setAccessType(field.ACCESSTYPE_OUTPUTONLY).setAppinfo("output provided for ImageTransform rotation"))
.addComments(" Note that trackPoint_changed events represent unclamped intersection points on plane surface. Browsers can interpret drags off of the surface in various ways. Note that translation_changed events are clamped by minPosition/maxPosition and thus may be preferable. ")
.addField(new field().setName("translation_changed").setType(field.TYPE_SFVEC3F).setAccessType(field.ACCESSTYPE_OUTPUTONLY).setAppinfo("output provided for SliderKnobTransform translation"))
.addField(new field().setName("angleMFString_changed").setType(field.TYPE_MFSTRING).setAccessType(field.ACCESSTYPE_OUTPUTONLY).setAppinfo("output provided for degrees of rotation in Text node"))
.addField(new field().setName("tracePrint").setType(field.TYPE_SFBOOL).setAccessType(field.ACCESSTYPE_INITIALIZEONLY).setValue(true).setAppinfo("console output for debugging")))
.addChild(new ROUTE().setFromNode("SliderPlaneSensor").setFromField("translation_changed").setToNode("SliderScript").setToField("set_translation"))
.addChild(new ROUTE().setFromNode("SliderScript").setFromField("translation_changed").setToNode("SliderKnobTransform").setToField("translation"))
.addChild(new ROUTE().setFromNode("SliderScript").setFromField("rotation_changed").setToNode("ImageTransform").setToField("rotation"))
.addChild(new ROUTE().setFromNode("SliderScript").setFromField("angleMFString_changed").setToNode("AngleOutputText").setToField("string")));
}
// end of initialize() method
/** The initialized model object, created within initialize() method. */
private X3D x3dModel;
/**
* Provide a
* shallow copy
* of the X3D model.
* @see X3D
* @return TextureAngleViewer model
*/
public X3D getX3dModel()
{
return x3dModel;
}
/**
* Default main() method provided for test purposes, uses CommandLine to set global ConfigurationProperties for this object.
* @param args array of input parameters, provided as arguments
* @see X3D.handleArguments(args)
* @see X3D.validationReport()
* @see CommandLine
* @see CommandLine.USAGE
* @see ConfigurationProperties
*/
public static void main(String args[])
{
X3D thisExampleX3dModel = new TextureAngleViewer().getX3dModel();
boolean hasArguments = (args != null) && (args.length > 0);
boolean validate = true; // default
boolean argumentsLoadNewModel = false;
String fileName = new String();
if (args != null)
{
for (String arg : args)
{
if (arg.toLowerCase().startsWith("-v") || arg.toLowerCase().contains("validate"))
{
validate = true; // making sure
}
if (arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_X3D) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_CLASSICVRML) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_X3DB) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_VRML97) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_EXI) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_GZIP) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_ZIP) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_HTML) ||
arg.toLowerCase().endsWith(X3D.FILE_EXTENSION_XHTML))
{
argumentsLoadNewModel = true;
fileName = arg;
}
}
}
if (argumentsLoadNewModel)
System.out.println("WARNING: \"X3dForAdvancedModeling.TextureMapping.TextureAngleViewer\" model invocation is attempting to load file \"" + fileName + "\" instead of simply validating itself... file loading ignored.");
else if (hasArguments) // if no arguments provided, this method produces usage warning
thisExampleX3dModel.handleArguments(args);
if (validate)
{
System.out.print("Java program \"X3dForAdvancedModeling.TextureMapping.TextureAngleViewer\" self-validation test results: ");
String validationResults = thisExampleX3dModel.validationReport();
if (validationResults.startsWith("\n"))
System.out.println();
System.out.println(validationResults.trim());
}
}
}